原文地址
http://smartinvoke.cn/pages/disArticel.jsp?id=13
大家都知道flex的web application不能操作和访问本地文件,我们今天就通过
smartInvoke让flex可以轻松的访问到本地文件。
一:在java应用程序中创建一操作本地文件的test.CFile类,创建几个对文件进行
访问的方法;具体如下:
- package test;
- import java.io.File;
- import cn.smartinvoke.javaflex.gui.IServerObject;
- //IServerObject表示此类型是一个服务类型专门接受flex代理类型对象的访问
- public class CFile implements IServerObject{
- public File file= null ;
- public CFile(String fileName) {
- file=new File(fileName);
- }
- public long getSize(){
- return this .file.length();
- }
- public String getName(){
- return this .file.getName();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- }
- }
二:运用CodeTransform工具生成test.CFile类的代理类test.CFile.as,具体内容如下:
- package test
- import cn.smartinvoke.RemoteObject;
- /**
- 此类继承于cn.smartinvoke.RemoteObject是java中的test.CFile类的代理类型
- 专门用于调用java中的对应类型。
- */
- public class CFile extends RemoteObject {
- public function CFile(){
- super ();
- }
- //调用此方法可以创建java中的test.CFile类型对象
- public static function create_CFile(fileName:String):CFile{
- var file:CFile=new CFile();
- file.createRemoteObject(null ,arguments);
- return file;
- }
- //此方法会调用java中的test.CFile.getName()方法,并将该方法的返回值返回
- public function getName():String{
- var retObj:Object=this .call( "getName" ,arguments);
- return retObj as String;
- }
- public function getSize():Number{
- var retObj:Object=this .call( "getSize" ,arguments);
- return Number(retObj);
- }
- }
三:创建flex程序smartinvoke.mxml并调用CFile代理类
- <? xml version = "1.0" encoding = "utf-8" ?>
- < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "absolute" creationComplete = "onLoad()" >
- < mx:Script >
- <![CDATA[
- import mx.controls.Alert;
- import test.CFile;
- import cn.smartinvoke.executor.Executor;
- /**
- *此swf加载完毕后调用此方法
- */
- function onLoad():void{
- //让smartInvoke为java与flex相互调用做好准备
- Executor.init();
- }
- /**
- *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来
- */
- function getFileName():void{
- //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile
- //代理对象并返回
- var file:CFile=CFile.create_CFile("C:/win_yy.png");
- //获得C:/win_yy.png文件的大小
- var fileSize:Number=file.getSize();
- Alert.show("文件大小为"+fileSize);
- }
- ]]>
- </ mx:Script >
- < mx:Button label = "getSize" click = "getFileName()" />
- </ mx:Application >
四:利用swt 将flex 整合到java程序当中并运行整个程序。
- package test;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import cn.smartinvoke.javaflex.gui.FlashContainer;
- public class DemoFirst extends Shell {
- /**
- * Launch the application
- * @param args
- */
- public static void main(String args[]) {
- try {
- Display display = Display.getDefault();
- DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);
- shell.open();
- shell.layout();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Create the shell
- * @param display
- * @param style
- */
- public DemoFirst(Display display, int style) {
- super (display, style);
- createContents();
- setLayout(new FillLayout());
- }
- /**
- * Create contents of the window
- */
- protected void createContents() {
- setText("smartinvoke测试程序" );
- //将flash控件添加到窗体
- FlashContainer flashContainer=new FlashContainer( this );
- //加载smartinvoke.mxml编译生成的smartinvoke.swf
- flashContainer.loadMovie(0 , "E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf" );
- setSize(500 , 375 );
- //
- }
- @Override
- protected void checkSubclass() {
- // Disable the check that prevents subclassing of SWT components
- }
- }
运行DemoFirst程序就可以看到结果了^_^,是不是觉得一切都很简单呀
java调用flex也是类似
接下来我们模拟一执行后台业务的java工作线程,当此线程执行完毕后调用test.FlexShell类的
setStatus(String info)方法将info字符串显示在flex窗体中,以表示后台业务执行完成。
首先:在flex中创建test.FlexShell.as类,具体内容如下:
- package test
- {
- import cn.smartinvoke.IServerObject;
- import mx.controls.Alert;
- public class FlexShell implements IServerObject
- {
- public function FlexShell()
- {
- }
- /**
- *将info代表的消息打印出来
- */
- public function setStatus(info:String): void {
- Alert.show(info);
- }
- }
- }
其次:使用CodeTransform工具生成test.FlexShell.as类的代理类test.FlexShell.java,具体内容如下:
- package test;
- import cn.smartinvoke.javaflex.gui.RemoteObject;
- import cn.smartinvoke.javaflex.gui.FlashContainer;
- public class FlexShell extends RemoteObject {
- public FlexShell(FlashContainer container){
- super (container);
- this .createRemoteObject( null ); //调用flex创建该代理类的服务类
- }
- public void setStatus(String info){ //调用flex对应服务类型的setStatus方法
- this .call( "setStatus" , new Object[]{info});
- }
- }
最后:在java的DemoFirst类型中加入后台业务处理线程的逻辑如下:
- package test;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import cn.smartinvoke.javaflex.gui.FlashContainer;
- import cn.smartinvoke.javaflex.gui.ILoadCompleteListener;
- import cn.smartinvoke.javaflex.util.Log;
- public class DemoFirst extends Shell {
- /**
- * Launch the application
- * @param args
- */
- public static void main(String args[]) {
- try {
- Log.open=true ;
- Display display = Display.getDefault();
- DemoFirst shell = new DemoFirst(display, SWT.SHELL_TRIM);
- shell.open();
- shell.layout();
- while (!shell.isDisposed()) {
- if (!display.readAndDispatch())
- display.sleep();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * Create the shell
- * @param display
- * @param style
- */
- public DemoFirst(Display display, int style) {
- super (display, style);
- createContents();
- setLayout(new FillLayout());
- }
- /**
- * Create contents of the window
- */
- protected void createContents() {
- setText("smartinvoke测试程序" );
- //将flash控件添加到窗体
- final FlashContainer flashContainer= new FlashContainer( this );
- /**
- *当flash加载完毕后,FlashContainer会调用ILoadCompleteListener
- *的run方法,你可以在这里做一些初始化
- */
- flashContainer.completeListener=new ILoadCompleteListener(){
- public void run() {
- //启动一后台执行线程
- Thread task=new Thread(){
- public void run(){
- //模拟后台任务
- try {
- Thread.sleep(2000 ); //等待10秒
- FlexShell flexShell=new FlexShell(flashContainer);
- flexShell.setStatus("后退任务执行完毕..." );
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- };
- task.setDaemon(true ); //将后台线程设置为守护线程,保证主线程退出时此线程也会退出
- task.start();
- }
- };
- //加载smartinvoke.mxml编译生成的smartinvoke.swf
- flashContainer.loadMovie(0 , "E:/flexWork/smartinvoke/bin-debug/smartinvoke.swf" );
- setSize(500 , 375 );
- //
- }
- @Override
- protected void checkSubclass() {
- // Disable the check that prevents subclassing of SWT components
- }
- }
将smartinvoke.mxml该为如下:
- <?xml version= "1.0" encoding= "utf-8" ?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="vertical" creationComplete= "onLoad()" >
- <mx:Script>
- <![CDATA[
- import test.FlexShell;
- import mx.controls.Alert;
- import test.CFile;
- import cn.smartinvoke.executor.Executor;
- /**
- *此swf加载完毕后调用此方法
- */
- var file:CFile=null ;
- //将FlexShell引用到当前程序当中,不然flex无法用反射方式创建FlexShell类型对象
- var flexShell:FlexShell;
- function onLoad():void {
- //让smartInvoke为java与flex相互调用做好准备
- Executor.init();
- this .file=CFile.create_CFile( "C:/win_yy.png" );
- }
- /**
- *此方法调用java的test.CFile.getSize方法获得C:/win_yy.png文件的大小,并显示出来
- */
- function getFileName():void {
- //调用java创建test.CFile类对象,并将此对象包装成flex的test.CFile
- //代理对象并返回
- //获得C:/win_yy.png文件的大小
- var fileSize:Number=file.getSize();
- Alert.show("文件大小为" +fileSize);
- //使用完毕后调用dispose方法释放java的对应类型
- file.dispose();
- }
- ]]>
- </mx:Script>
- <mx:Label id="testLabel" />
- <mx:Button label="getSize" click= "getFileName()" />
- </mx:Application>
重新编译mxml,并运行DemoFirst 程序就可以看到效果了。
到现在我们已经轻松的实现java与flex之间互相调用了,是不是有种幸福来得太突然的感觉^_^
下载地址 http://smartinvoke.cn/pages/download.jsp