`
smartinvoke
  • 浏览: 104229 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类

smartrcp开源平台使用Java与Flex构建桌面程序 三(使用eclipse插件扩展smartrcp平台)

阅读更多

1:背景:
  Adobe AIR平台最大的劣势就是很难在该平台的基础上进行扩展,程序的功能受限于
该平台的标准API,而smartrcp使用eclipse插件体系很好的解决了平台的扩展问题。
 
  从此可以使用Java解决复杂的业务逻辑,Flex负责界面展示,Java与Flex互相配合,
充分发挥各自的优势。

2:基本开发步骤如下:
  
   2.1 保证您的eclipse 具有插件开发的功能,File -> New -> Other 看看有没
 Plug-in Development 节点如果有则具有插件开发功能。转到smartrcp的解压目录,
 将plugins目录下的cn.smartinvoke.smartrcp.core_1.0.0.jar文件拷贝到您eclipse
 开发工具的根目录下的plugins目录下。
  
   2.2  创建一名为org.smartrcp.test的plug-in Project,将Target Platform
 选中为Equinox。一路next直到完成。打开项目下的MANIFEST.MF文件,打开Dependencies
 选项,将cn.smartinvoke.smartrcp.core添加进Required Plug-ins集合,这样该插件才
 可以引用到smartrcp的基础类。在org.smartrcp.test包下创建CFileWriter类,内容如下:

 

 package org.smartrcp.test;

import java.io.*;

import cn.smartinvoke.IServerObject;

public class CFileWriter implements IServerObject {

	public CFileWriter() {
		
	}
	/**
	 * 将字符串content保存到path文件中
	 * @param path
	 * @param content
	 * @throws IOException
	 */
	public void saveFile(String path,String content) throws IOException{
		Writer writer=new FileWriter(path);
		writer.write(content);
		writer.flush();
		writer.close();
	}
	
	public void dispose() {
		
	}

}

 2.3 右击org.smartrcp.test项目选择Export -> Plug-in Development ->
    Deployable plug-ins and fragments  将该项目导出为一个eclipse 插件。
    最后将导出的插件jar文件拷贝到smartrcp安装目录的ext目录中,如果没有该目录则手工
    创建之。

2.4 到现在Java部分开发并配置好了,接下来就是Flex部分了,打开先前创建的smartrcpDemo
    项目,修改Module_Content.mxml文件内容如下:

 

 

<?xml version="1.0" encoding="utf-8"?>
<rcp:RCPModule xmlns:mx="http://www.adobe.com/2006/mxml"
	xmlns:rcp="http://www.smartrcp.org"
	 layout="vertical" verticalAlign="middle" horizontalAlign="center"
	  creationComplete="init()"
	 >
	 <mx:Script>
	 	<![CDATA[
	 		import cn.smartinvoke.smartrcp.gui.module.CommonDialogs;
	 		import mx.controls.Alert;
	 		import cn.smartinvoke.smartrcp.gui.control.CActionManager;
	 		import cn.smartinvoke.smartrcp.gui.module.CActionEvent;
	 		import mx.collections.ArrayCollection;
	 		import cn.smartinvoke.rcp.CToolBar;
	 		import cn.smartinvoke.smartrcp.gui.FlashViewPart;
	 		import cn.smartinvoke.RemoteObject;
	 		import cn.smartinvoke.smartrcp.gui.ViewPartActionBar;
	 	    var actionBar:ViewPartActionBar=null;
	 	    //初始化方法,在该方法中为当前视图添加一刷新按钮
	 	    function init():void{
	 	    	/**在容器上添加刷新按钮*/
	 	    	var parent:RemoteObject=this.flashViewer.getParent();
				//如果存载当前flash的容器时viewPart,才具有此功能
				if(parent is FlashViewPart){
				   var viewPart:FlashViewPart=parent as FlashViewPart;
				   //获得当前视图容器的工具栏容器对象
				   this.actionBar=viewPart.getPartActionBar();
				   //注册freshAction到工具栏容器对象actionBar
				   this.actionBar.addAction(this.freshAction);
				   //定义工具栏对象
				   var toolBar:CToolBar=new CToolBar();
				   //将freshAction 添加到该工具栏对象
				   toolBar.actionIds=new ArrayCollection([this.freshAction.actionId]);
				   //将工具栏对象添加到工具栏容器
				   this.actionBar.fillToolBar(toolBar);
				   //给freshAction 添加事件监听器
				   this.actionBar.addListener(this.freshAction.actionId,function (ret:CActionEvent):void{
				   	   this.setInfo("刷新当前的所有操作..............");
				   },this);
				}
				/**添加对Splash.mxml启动文件中定义的id为test的全局action的事件监听*/
				CActionManager.Instance.addListener("test",function (evt:CActionEvent):void{
					Alert.show("id为test的全局action被用户点击触发了");
				},this);
	 	    }
	 	    //公共方法,该方法可以被其他模块调用
	 		public function setInfo(info:String):void{
	 			this.labelInfo.text=info;
	 		}
	 		function onSaveClick():void{
	 			/**打开文件保存对话框,并将TextArea中的内容保存到用户选择的路径下*/
	 		    
	 			var fileWriter:RemoteObject=new RemoteObject();
	 			fileWriter.create("org.smartrcp.test.CFileWriter");
	 			fileWriter.call("saveFile",["D:/testSave.text",this.saveText.text]);
	 		}
	 	]]>
	 </mx:Script>
	 <!--定义一action -->
	 <rcp:CAction id="freshAction" actionId="actionfresh" 
	 	 imageUrl="icons/update.gif"
	 	text="刷新" toolTip="刷新当前视图"/>
	 <mx:Label id="labelInfo" text="欢迎使用......"/>
	 <mx:TextArea  id="saveText" width="292" height="144"/>
	 <mx:Button label="保存" click="onSaveClick()"/>
</rcp:RCPModule>

 

保存并编译之。

2.5 接下来就是运行啦^_^
   
    打开控制台,切换到smartrcp安装目录,运行
    smartrcp.exe -app E:\flexWork\SmartRCPDemo\bin-debug\Splash.swf 命令
   
    -app 参数定义运行时加载的smartrcp程序的启动文件的路径,这里为smartrcpDemo
    程序中的Splash.mxml文件编译后得到的Splash.swf文件路径。
    当然为了偷懒你也可也在smartrcp安装目录下创建一run.bat批处理文件,将该命令
    拷贝到其中,这样只需双击运行该批处理文件就可以了。

    运行效果如下图:
            

 
    在文本框中输入内容,点击保存后你就会发现D:/testSave.text文件中保存了该字符串,
    证明我们调用Java是成功了的。

 

  • 大小: 36.9 KB
分享到:
评论
1 楼 timshaw9791 2010-11-05  
看起来相当不错!

相关推荐

Global site tag (gtag.js) - Google Analytics