Friday, October 31, 2008
Removing and Adding Options Dynamically
Wednesday, September 24, 2008
Flex Data Services(FDS): FMS
- Flex Message Service (FMS)
- Flex Data Management Service (FDMS)
- Flex Proxy Service (FPS)
- Remoting Service
consumer.subscribe();
consumer.unsubscribe();
5. Whenever there is a message generated at the backend, now your flex application will hear it. A very simple sample java file that generates message will look like this:
Thursday, July 24, 2008
Large Numbers in JavaScript
Hazards of large numbers in JavaScript
Big Integers in JavaScript
Hope it helps you too :).
Monday, July 14, 2008
Using Webservices
For <mx:WebService> tag, specify id, url of the wsdl file and optionally fault handler function. You can trap an event called load that fires when WSDL loads successfully
You can use methods on a Web Service in three ways:
1.Undeclared Method: Use WS with id. Flex looks at WSDL for the method based on how it is called
2.Declared Method: Operations defined as children of WS tag using <mx:operation> tag. Args validated using WSDL
3.Fully Declared Method: Both operations and arguments declared using <mx:operation> and <mx:request> tag.
Declaring operations enables specifying event handlers for each operation
<mx:WebService id="myWebservice"
wsdl="http://myWsdlUrl/myWsdlFile.wsdl"
fault= handleFaultFunc(event)>
<mx:operation name="funcNameInWsdl" result="resultHandlingFunc(event)">
<mx:request>
<paramName1InWsdl> {asVar1ToPickInputFrom} </paramName1InWsdl>
<paramName2InWsdl> {asVar2ToPickInputFrom} </paramName2InWsdl>
</mx:request>
</mx:operation>
</mx:WebService>
Then to call the operation
myWebservice.funcNameInWsdl.send();
To do in AS:
var myWebservice:WebService = new WebService();
myWebservice.wsdl =
"http://myWsdlUrl/myWsdlFile.wsdl";
ws.loadWSDL(); /*Before calling any method we need to load wsdl. Use canLoadWSDL() to check if WSDL was loaded or not*/
ws.addEventListener("result", resultHandlingFunc);
ws.addEventListener("fault", handleFaultFunc);
ws.funcNameInWsdl(asVar1ToPickInputFrom, asVar2ToPickInputFrom);
Monday, June 2, 2008
Flex-AJAX Bridge
Thursday, April 10, 2008
Object Introspection in Flex
import flash.utils.*; and use the method describeType() , passing it target object to be inspected. The result may be parsed using E4X API
If you call describeType(this) , non-static members returned
If you call describeType(getDefinitionByName("MyClass")) , static members returned
An example from Adobe Flex 3 reference to use it for a Button. Assume a Button with id button1:
import flash.utils.*;
pubic function inspector():void{
//get E4X description
var classInfo:XML = describeType(button1);
//class name
var className:String = classInfo.@name.toString();
//variables and their types
var classVarStr:String="";
for each(var v:XML in classInfo..variable){
classVarStr+= "Variable "+ v.@name + "=" + button1[v.@name] + "("+v.@type+")\n";
}
//properties, type and value
var classPropStr:String="";
for each(var a:XML in classInfo..accessor){
//no need to get value if write only
if(a.@access == 'writeOnly'){
classPropStr+="Property "+ a.@name + "(" + a.@type + ")\n";
}else{
classPropStr+="Property "+ a.@name + "=" +button1[a.@name] + "(" + a.@type + ")\n";
}
}
//methods
var classMethodStr:String="";
for each(var m:XML in classInfo..method){
classMethodStr += "Method " + m.@name + "():" + m.@returnType + "\n";
}
}
Thursday, March 6, 2008
Percentage Width For Datagrid Columns
On creationComplete event of your DataGrid call a function that has a logic similar to this:
var idx:Number=0;
//myTable is the id for the dataGrid you want to manipulate
for each(var col:DataGridColumn in myTable.columns{
switch(idx){
case 0:
col.width = summaryTable.width*0.23;
break;
case 1:
col.width = summaryTable.width*0.13;
break;
case 2:
col.width = summaryTable.width*0.10;
break;
case 3:
col.width = summaryTable.width*0.11;
break;
case 4:
col.width = summaryTable.width*0.11;
break;
case 5:
col.width = summaryTable.width*0.11;
break;
case 6:
col.width = summaryTable.width*0.17;
break;
case 7:
col.width = summaryTable.width*0.04;
break;
}
if(idx < summaryTable.columnCount){
idx++;
}
}
Yes, this is a bit ugly. Not only you need to know the exact number of columns, you need to also make sure that the sum of parts is 100. For slightly better maintainability at most you can use declared constants in the switch statement.
Tuesday, February 26, 2008
Drag and Drop Grid of Panels
The grids would look something like this:
You can check out the code at: http://docs.google.com/Doc?id=ddhf5h22_69w36cmdh
Any queries/problems /suggestions are welcome :)
Cheers!
Tuesday, February 19, 2008
Datagrid Fix
Datagrid Bugs Hotfix at Adobe
Thursday, February 7, 2008
Mapping AS objects to Server-Side Java Objects
AS class:
/*we mark a class we want to map with RemoteClass tag. alias is the exact location of the class*/
package valueObjects
{
[RemoteClass(alias="valueObjects.SampleVo")]
public class SampleVo{
public var val1:String = "val1";
public var num1:int = 1;
public function getServerVal(obj:SampleVo):SampleVo{
return new SampleVo();
}
}
}
Java class (you will have to configure it as a remote object):
package valueObjects;
public class SampleVo {
public String val1 = "val2";
public int num1 = 2;
public SampleVo getServerVal(SampleVo obj){
return new SampleVo();
}
}
If you have this, you can do this in flex while handling result on calling getServerVal function:
var vo:SampleVo = event.result as SampleVo;
resultTxt.text = vo.val1+vo.num1;
Wednesday, February 6, 2008
Implementing Singleton in Flex
package managers {
/*A singleton class which enforces only a single object is created for each key. To access the specific instance, use getInstance(key:String) */
public class MySingleton{
private static var instanceMap:Object = new Object();
public function MySingleton(pri:PrivateClass){}
public static function getInstance(key:String):MySingleton{
if(MySingleton.instanceMap[key] == null){
MySingleton.instanceMap[key] = new MySingleton(new PrivateClass());
}
return MySingleton.instanceMap[key];
}
}
}
/* PrivateClass is used to make constructor private (to implement Singleton)*/
class PrivateClass{ public function PrivateClass(){}}
Tuesday, February 5, 2008
Uploading File From Flex to Tomcat
In Flex use FileReference object to get the file to upload:
private var fileRef:FileReference;
private function fileBrowse():void{
this.fileRef = new FileReference();
fileRef.addEventListener(Event.SELECT,selectHandler);
fileRef.browse();
}
private function selectHandler(event:Event):void{
var request:URLRequest = new URLRequest("http://localhost:8080/FlexJava1/fileLoader.jsp");
/*Map this to the servlet or jsp you wish to use to handle your file writing request*/
fileRef.upload(request);
}
The jsp will be like this (O yes this should be done in a servlet but lazy me doing it in a jsp. But you be good and make a nice servlet :p):
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileOutputStream"%>
<%@page import="java.io.InputStream"%>
<%
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
//handling a normal form-field
if (item.isFormField()) {
System.out.println("Got a form field");
String name = item.getFieldName();
String value = item.getString();
System.out.println("Name:"+name+",Value:"+value);
} else {//handling file loads
System.out.println("Not form field");
String fieldName = item.getFieldName();
String fileName = item.getName();
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
System.out.println("Field Name:"+fieldName+",File Name:"+fileName);
System.out.println("Content Type:"+contentType+",Is In Memory:"+isInMemory+",Size:"+sizeInBytes);
byte[] data = item.get();
FileOutputStream fileOutSt = new FileOutputStream("outagain.jpg");
fileOutSt.write(data);
fileOutSt.close();
}
}
%>
You will find your file written under the Tomcat root installation folder
Monday, February 4, 2008
Using Remote Objects
Download Adobe LiveCycle Data Services ES from:
http://www.adobe.com/products/livecycle/dataservices/Install it for J2EE and not for integrated JRun. We will be using the express version. Alternatively you could use BlazeDS. To see the differences between the two visit this link:
Differences between Flex Data Services Express and BlazeDS
Please remember that using either means that you will have to buy it at some stage for commercial applications. See the respective licence for details. That being said these services are extremely useful and without them Flex looses a lot of utility.
You have set up the stage to use Remote Object.
Now, copy the java class you intend to use in the appropriate directory under WEB-INF/classes. Lets say we have a class file from your compiled java file called MyRemoteObj.class made from this file:
package app;
public class MyRemoteObj{
public String getMessage(){ return "hello"; }
}
Browse to WEB-INF/flex and open remoting-config.xml. Make sure it looks something like this:
<?xml version="1.0" encoding="UTF-8"?>
<service id="remoting-service" class="flex.messaging.services.RemotingService">
<adapters> <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
</adapters>
<default-channels>
<channel ref="my-amf"/>
</default-channels>
<destination id="responder">
<properties>
<source>app.MyRemoteObj</source>
<scope>application</scope>
</properties>
</destination>
</service>
Remember this xml file is just one of the files uploaded by services-config.xml. It just helps in breaking our config xml into usable parts. <adapters> define the backend object. <channels> are defined in service-config. Here we just specify the channel we intend to use. destination is the part that helps flex use the remote object. id is ised to refer to it. source is its location under the WEB-INF/classes directory.
Declare your remoter object in the MXML as
<mx:RemoteObject id="remoteObjRef" destination="responder" fault="handleFault(event)" result="handelResult(event)"/>
Use the id in AS to call the methods directly on the object: remoteObjRef.getMessage();
Monday, January 28, 2008
Pondering on AJAX and Flex
I think AJAX, more specifically yui, can be used for most of the web-applications. It is simpler to use and written pretty well.
However if we need controls with interactive graphics/charting data or we have a very big and complex application, flex would be a better option.
Lets face it, flex is more powerful and easy to develop (and maintain) of the two. However it has its learning curve and people with good flex skills & strong programming basics are not that easy to find. If your application is indeed that complex and demands a high degree of interaction and/or analytics, flex is a wise decision. Else it is like using a hammer to kill a fly. It is good but you dont have to use it if you dont need it :).
DataGrid Rendering
Button, Label, CheckBox, NumericStepper, ComboBox, Text, DateField, TextArea, Image, TextInput
You can also specify your own control as long as it implements IDropInListItemRenderer interface in its class definition
Item renderers can be used to customize looks of individual columns.
One way is to make a custom renderer (say using Canvas) as an MXML component and giving the appropriate DataGridColumn the full-qualified path to itemRenderer property. Remember that the data property will help you in the renderer to access row data.
Another way is using
labelFunction signature for DataGrids is: