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();