Apache Pivot初体验

  Apache已经将Pivot升级为顶级项目,虽然从前不知道这个项目,但是看来还是有发展前途的。我看好它,因为我对FLEX语法实在不感冒,还是Java写着舒服。似乎也很少有人用AWT/Swing做商业产品,但Applet的应用还是常见的。就研究研究Pivot吧。
    既然Apache将其定位为RIA in Java,做J2EE的还是关注下吧。这是纯Java血统的东西(相比于FLEX),下了开发包,看例子,语法类似Swing,还是比较容易接受的。但是官方例子中没有完整的目录结构,构建开发环境还是留下了一些障碍。可能是之前没有接触过。我自己摸索了一个适应web开发的目录结构,可能不是最好的,但是是可行的。如下:

    这里用了ANT进行jar打包,之前用Eclipse的导出打包不好用,就自己写了个ANT构建脚本运行,还真好用了。
    目录结构还是传统的web项目,不同的是webapp下也放置了lib,当然就是运行项目所需的lib库,如果放到WEB-INF/lib下,那页面是不能直接访问到的,而pivot程序是直接在页面显示调用类库的,就需要放到可见的位置。不过我是在WEB-INF/lib下也放置了一份,那里的在编译路径里的,这样不冲突,也为ANT构建省去来回设置类路径的麻烦。jar包也不大,基本的才1.29M,没有办法,谁让人家是RIA呢。
    先从Java Application开始吧,我是参照官方示例学习的。但是如果你刚下载开发包,估计得看上一阵子呢。其实很简单,就是一个类,一个XML配置文件(Pivot中是以wtkx作为扩展名的)。下面用代码来说明,更为直观。

Java代码 复制代码  收藏代码
  1. package pivot.tutorials.hellowtkx;   
  2.   
  3. import org.apache.pivot.collections.Map;   
  4. import org.apache.pivot.wtk.Application;   
  5. import org.apache.pivot.wtk.DesktopApplicationContext;   
  6. import org.apache.pivot.wtk.Display;   
  7. import org.apache.pivot.wtk.Window;   
  8. import org.apache.pivot.wtkx.WTKXSerializer;   
  9.   
  10. public class HelloWTKX implements Application {   
  11.     private Window window = null;   
  12.   
  13.     public void startup(Display display, Map<String, String> map)   
  14.             throws Exception {   
  15.         WTKXSerializer wtkxSerializer = new WTKXSerializer();   
  16.         window = (Window) wtkxSerializer.readObject(this"helloWTKX.wtkx.xml");   
  17.         window.setTitle("Hello JavaEye");   
  18.         window.open(display);   
  19.     }   
  20.   
  21.     public boolean shutdown(boolean optional) throws Exception {   
  22.         if (window != null) {   
  23.             window.close();   
  24.         }   
  25.         return false;   
  26.     }   
  27.   
  28.     public void resume() throws Exception {   
  29.     }   
  30.   
  31.     public void suspend() throws Exception {   
  32.     }   
  33.   
  34.     public static void main(String[] args) {   
  35.         DesktopApplicationContext.main(HelloWTKX.class, args);   
  36.     }   
  37.   
  38. }  
package pivot.tutorials.hellowtkx;

import org.apache.pivot.collections.Map;
import org.apache.pivot.wtk.Application;
import org.apache.pivot.wtk.DesktopApplicationContext;
import org.apache.pivot.wtk.Display;
import org.apache.pivot.wtk.Window;
import org.apache.pivot.wtkx.WTKXSerializer;

public class HelloWTKX implements Application {
	private Window window = null;

	public void startup(Display display, Map<String, String> map)
			throws Exception {
		WTKXSerializer wtkxSerializer = new WTKXSerializer();
		window = (Window) wtkxSerializer.readObject(this, "helloWTKX.wtkx.xml");
		window.setTitle("Hello JavaEye");
		window.open(display);
	}

	public boolean shutdown(boolean optional) throws Exception {
		if (window != null) {
			window.close();
		}
		return false;
	}

	public void resume() throws Exception {
	}

	public void suspend() throws Exception {
	}

	public static void main(String[] args) {
		DesktopApplicationContext.main(HelloWTKX.class, args);
	}

}


官方示例中配置文件都以XXX.wtkx形式存在,其实就是XML文档,我就直接以.xml来编辑,这样在Eclipse中可以高亮显示,否则就是普通文本编辑了。配置文件的编写参考官方示例就行了。我还加上了的XML声明。

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <Window title="Hello WTKX" maximized="true"  
  3. xmlns:wtkx="ttp://pivot.apache.org/wtkx"    
  4. xmlns="org.apache.pivot.wtk">  
  5.     <content>  
  6.         <Label text="Hello JavaEye! - The Pivot WTKX Application"  
  7.             styles="{font:'Arial bold 24',color:'#ff0000',   
  8. horizontalAlignment:'center',verticalAlignment:'center'}" />  
  9.     </content>  
  10. </Window>  
<?xml version="1.0" encoding="UTF-8" ?>
<Window title="Hello WTKX" maximized="true"
xmlns:wtkx="ttp://pivot.apache.org/wtkx" 
xmlns="org.apache.pivot.wtk">
	<content>
		<Label text="Hello JavaEye! - The Pivot WTKX Application"
			styles="{font:'Arial bold 24',color:'#ff0000',
horizontalAlignment:'center',verticalAlignment:'center'}" />
	</content>
</Window>


    基本的Pivot应用程序都是实现了Application接口,自然也要实现其中的抽象方法。从方法名启动,关闭,唤醒和挂起不难看出这是进程的状态转化方法。负责继承状态改变时的处理。主函数就是Pivot应用程序的执行方式,这都是固定的写法了,传入本类的class和主函数的参数。
    类中定义一个私有属性window,就是Pivot应用程序的窗口对象,在startup方法中进行进程加载启动时的事情。WTKXSerializer类在官方文档中解释为“从XML中加载对象层次结构”,可以看出类似于MXML,控件的定义都是写在配置文件中的。只不过这个XML编码时IDE还不能给出提示。我想是Schema还没有最后固定,编码时只能按照已有定义来编写。程序就可以解析XML了。wtkxSerializer对象读取XML配置文件,并从中获取window实例(window是根元素)。中间可以是业务流程的处理。这里是对window的title进行了设置,编码风格很像Swing。最后一步是window的open()方法,传入display参数。在shutdown()方法中别忘记关闭window就可以了。
    此时程序就可以以Application方式运行了。得到效果:

    第一部分介绍到此结束,第二部分中将介绍Web应用中的Pivot部署和用ANT构建自定义jar包。 
   我们探究了如何构建Pivot开发框架,编写Pivot桌面应用程序。这一篇我们来说说如何在web环境中部署Pivot程序。webapp发布目录 如下所示:

    css中的样式表和js下的JavaScript脚本都是根据官方示例中拷贝下来的,其中最重要的是deployJava.js,它是展示Java applet的工具包,Pivot在页面中就是以Applet的形式展现的,js文件在http://java.com/js/deployJava.js 下载。Lib下是Pivot应用所依赖的类库,可以看出pivot.jar是我自定义的jar,就是编好代码打的包,而WEB-INF/lib下的库和Pivot没有关系,那里的库正常是访问不到的。
    下面展示HTML页面

Html代码 复制代码  收藏代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />  
  5. <title>Push Buttons</title>  
  6. <script src="/pivot/js/deployJava.js" type="text/javascript"></script>  
  7. </head>  
  8. <body>  
  9. <script xmlns="" type="text/javascript">  
  10.             var attributes = {   
  11. code:'org.apache.pivot.wtk.BrowserApplicationContext$HostApplet',   
  12.                 width:'480',   
  13.                 height:'360'   
  14.             };   
  15.             var libraries = [];   
  16.             libraries.push("/pivot/lib/pivot-core-1.4.jar");   
  17.             libraries.push("/pivot/lib/pivot-wtk-1.4.jar");   
  18.             libraries.push("/pivot/lib/pivot-wtk-terra-1.4.jar");   
  19.             libraries.push("/pivot/lib/pivot.jar");   
  20.             attributes.archive = libraries.join(",");   
  21.             var parameters = {   
  22.                 codebase_lookup:false,   
  23.                 java_arguments:'-Dsun.awt.noerasebackground=true -Dsun.awt.erasebackgroundonresize=true',   
  24.      application_class_name:'pivot.tutorials.buttons.PushButtons'   
  25.             };   
  26.             deployJava.runApplet(attributes, parameters, "1.6");   
  27. </script>  
  28. </body>  
  29. </html>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" />
<title>Push Buttons</title>
<script src="/pivot/js/deployJava.js" type="text/javascript"></script>
</head>
<body>
<script xmlns="" type="text/javascript">
            var attributes = {
code:'org.apache.pivot.wtk.BrowserApplicationContext$HostApplet',
                width:'480',
                height:'360'
            };
            var libraries = [];
            libraries.push("/pivot/lib/pivot-core-1.4.jar");
            libraries.push("/pivot/lib/pivot-wtk-1.4.jar");
            libraries.push("/pivot/lib/pivot-wtk-terra-1.4.jar");
            libraries.push("/pivot/lib/pivot.jar");
            attributes.archive = libraries.join(",");
            var parameters = {
                codebase_lookup:false,
                java_arguments:'-Dsun.awt.noerasebackground=true -Dsun.awt.erasebackgroundοnresize=true',
     application_class_name:'pivot.tutorials.buttons.PushButtons'
            };
            deployJava.runApplet(attributes, parameters, "1.6");
</script>
</body>
</html>


    关键是JS段代码,其实都是固定套路,只需修改自定义的jar即可,其他段根据字母意思即可理解,关键是lib库的位置一定要对。下面说说ANT打包。

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <project name="pivot" default="all">  
  3.     <description>The Pivot Application</description>  
  4.     <!-- 定义文件夹 -->  
  5.     <property name="srcDir" location="." />  
  6.     <property name="classDir" location="../webapp/WEB-INF/classes" />  
  7.     <property name="libDir" location="../webapp/WEB-INF/lib" />  
  8.          <property name="webDir" location="../webapp" />  
  9.     <!--用于输出打包的文件夹-->  
  10.     <property name="tempDir" location="${java.io.tmpdir}/${ant.project.name}" />  
  11.     <property name="targetDir" location="../target" />  
  12.     <!-- 定义classpath -->  
  13.     <path id="master-classpath">  
  14.         <fileset file="${libDir}/*.jar" />  
  15.         <pathelement path="${classDir}" />  
  16.     </path>  
  17.     <!-- 执行清理 -->  
  18.     <target name="clean">  
  19.         <delete dir="${classDir}" />  
  20.         <delete dir="${tempDir}" />  
  21.         <delete file="${targetDir}/${ant.project.name}.jar" />  
  22.         <delete file="${targetDir}/${ant.project.name}.war" />  
  23.         <delete dir="${targetDir}" />  
  24.     </target>  
  25.     <!-- 初始化任务 -->  
  26.     <target name="init" depends="clean">  
  27.         <mkdir dir="${targetDir}" />  
  28.         <mkdir dir="${tempDir}" />  
  29.         <mkdir dir="${classDir}" />  
  30.     </target>  
  31.     <!-- 编译 -->  
  32.     <target name="compile" depends="init" description="compile the source files">  
  33.         <javac srcdir="${srcDir}" destdir="${classDir}" debug="true" encoding="UTF-8">  
  34.             <classpath refid="master-classpath" />  
  35.         </javac>  
  36.         <copy todir="${classDir}" overwrite="true">  
  37.             <fileset dir="${srcDir}">  
  38.                 <include name="**/*.xml" />  
  39.                 <include name="**/*.properties" />  
  40.             </fileset>  
  41.         </copy>  
  42.     </target>  
  43.     <!--打jar包-->  
  44.     <target name="jar" depends="compile">  
  45.         <jar jarfile="${targetDir}/${ant.project.name}.jar">  
  46.             <fileset dir="${classDir}">  
  47.                 <include name="**/*" />  
  48.             </fileset>  
  49.         </jar>  
  50.     </target>  
  51.     <!—准备war包 -->  
  52.        
  53.     <target name="preWar" depends="jar">  
  54.         <copy todir="${tempDir}/WEB-INF/lib" overwrite="true">  
  55.             <fileset dir="${libDir}">  
  56.                 <include name="*.jar" />  
  57.             </fileset>  
  58.         </copy>  
  59.         <copy todir="${tempDir}" overwrite="true">  
  60.             <fileset dir="${webDir}">  
  61.                 <include name="**/*" />  
  62.             </fileset>  
  63.         </copy>  
  64.     </target>  
  65.     <!--打war包-->  
  66.     <target name="war" depends="preWar">  
  67.         <jar jarfile="${targetDir}/${ant.project.name}.war">  
  68.             <fileset dir="${tempDir}">  
  69.                 <include name="**/*" />  
  70.             </fileset>  
  71.         </jar>  
  72.     </target>  
  73.        
  74.     <!-- 清理临时目录 -->  
  75.     <target name="all" depends="war">  
  76.         <delete dir="${tempDir}" />  
  77.     </target>  
  78. </project>  
<?xml version="1.0" encoding="UTF-8" ?>
<project name="pivot" default="all">
	<description>The Pivot Application</description>
	<!-- 定义文件夹 -->
	<property name="srcDir" location="." />
	<property name="classDir" location="../webapp/WEB-INF/classes" />
	<property name="libDir" location="../webapp/WEB-INF/lib" />
         <property name="webDir" location="../webapp" />
	<!--用于输出打包的文件夹-->
	<property name="tempDir" location="${java.io.tmpdir}/${ant.project.name}" />
	<property name="targetDir" location="../target" />
	<!-- 定义classpath -->
	<path id="master-classpath">
		<fileset file="${libDir}/*.jar" />
		<pathelement path="${classDir}" />
	</path>
	<!-- 执行清理 -->
	<target name="clean">
		<delete dir="${classDir}" />
		<delete dir="${tempDir}" />
		<delete file="${targetDir}/${ant.project.name}.jar" />
		<delete file="${targetDir}/${ant.project.name}.war" />
		<delete dir="${targetDir}" />
	</target>
	<!-- 初始化任务 -->
	<target name="init" depends="clean">
		<mkdir dir="${targetDir}" />
		<mkdir dir="${tempDir}" />
		<mkdir dir="${classDir}" />
	</target>
	<!-- 编译 -->
	<target name="compile" depends="init" description="compile the source files">
		<javac srcdir="${srcDir}" destdir="${classDir}" debug="true" encoding="UTF-8">
			<classpath refid="master-classpath" />
		</javac>
		<copy todir="${classDir}" overwrite="true">
			<fileset dir="${srcDir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
			</fileset>
		</copy>
	</target>
	<!--打jar包-->
	<target name="jar" depends="compile">
		<jar jarfile="${targetDir}/${ant.project.name}.jar">
			<fileset dir="${classDir}">
				<include name="**/*" />
			</fileset>
		</jar>
	</target>
	<!—准备war包 -->
	
	<target name="preWar" depends="jar">
		<copy todir="${tempDir}/WEB-INF/lib" overwrite="true">
			<fileset dir="${libDir}">
				<include name="*.jar" />
			</fileset>
		</copy>
		<copy todir="${tempDir}" overwrite="true">
			<fileset dir="${webDir}">
				<include name="**/*" />
			</fileset>
		</copy>
	</target>
	<!--打war包-->
	<target name="war" depends="preWar">
		<jar jarfile="${targetDir}/${ant.project.name}.war">
			<fileset dir="${tempDir}">
				<include name="**/*" />
			</fileset>
		</jar>
	</target>
	
	<!-- 清理临时目录 -->
	<target name="all" depends="war">
		<delete dir="${tempDir}" />
	</target>
</project>


    Eclipse中已经集成了ANT,只要新建build.xml的文件并且编写,右键就能直接运行,非常方便,ANT构建脚本的编写也很简单,关键是目录迭代不能遗漏,还有目录的定位,不过在IDE中都有提示,也很方便。
    将打包好的jar放到webapp/lib下并且修改页面的js段代码即可看到效果了。

    欢迎大家交流。
    (全篇完)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值