IBM WebSphere 开发者技术期刊 : 使用 Eclipse 富客户端平台和 Rational Application Developer V6 创建 EJB 客户端
[@more@]
使用 SWT 布局小窗口构建视图
现在我们将要构建视图。download file
中提供了 View
类。我们将会导入该类(装载我们通过视图模板生成的类)并且接下来检验代码的相关部分。启动之前,我们需要确定我们的插件所依赖的插件。在视图中我们将会
使用标准窗口小部件工具箱(SWT)布局小窗口,因此我们需要设置包括布局小窗口的 Eclipse 插件的相关性。
- 选择 Dependencies 选项卡然后选择 Add (图 39)。定位
org.eclispe.ui.forms 插件。
- 导入 download file 中提供的 View 类,装载所生成的类。在
src 文件夹下,右键单击com.ibm.issw.temperature.form.views 包,然后选择 Import (图 40)。
- 选择 File System 然后接下来选择 Next。浏览目录
C:RCPEJBClientView ,并且将三个文件导入包中,如图 41 所示。
- 您应该获得将要装载现有类的警告。选择 Yes To All (图 42)。
- 打开导入的 TemperatureView.java (图 43)。
- 我们将会集中于两种方法(图 44):
- createPartControl(组合)—— 用于构建您的源自不同 SWT 小窗口的视图。
- handleEvent() —— 在 inner 类中,提供了当点击 SWT 小窗口命令按钮时调用的逻辑。
- createPartContol() 方法如清单 1 所示。代码使用了 FormToolkit 类帮助关于父组件的创建组件;在该情况下,创建所需的标签、按钮、以及文本框,并且将侦听器赋给按钮。侦听器是 View 的内部类。
清单 1
public void createPartControl(Composite parent) { toolkit = new FormToolkit(parent.getDisplay()); form = toolkit.createScrolledForm(parent); form.setText(RcpMessages.getString("TemperatureView.Title")); GridLayout layout = new GridLayout(2,false); form.getBody().setLayout(layout); Label label = toolkit.createLabel(form.getBody(), "Enter temperature in Celsius: "); Text textBox = toolkit.createText(form.getBody(), "", SWT.BORDER); GridData gdText = new GridData(8); textBox.setLayoutData(gdText); Button button = toolkit.createButton(form.getBody(), RcpMessages.getString("TemperatureView.Button"), SWT.PUSH); Label result = toolkit.createLabel(form.getBody(), ""); result.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); MyListener listener = new MyListener(); listener.setInput(textBox); listener.setResult(result); listener.setParent(parent); listener.setFormToolkit(toolkit); button.addListener(SWT.Selection,listener); } |
- 内
部类的事件处理方法如清单 2 所示。在这一类中,读取了文本框的值以及使用 EJB 客户端 API 调用 EJB。代码查找 EJB home
并且获得远程接口。接下来调用该方法并且用结果值填充结果标签。(要记住,最好将 EJB
调用隐藏在业务委派之后而不是在插件代码中直接调用。对本文来说,我们简化了代码。通常不推荐从 view 中直接调用 EJB。)
清单 2.
public void handleEvent(Event event) { try { InitialContext ctx = new InitialContext(); String jndi = RcpMessages.getString("TemperatureView.JNDIName"); Object obj = ctx.lookup(jndi); TempConverterHome homeRef = (TempConverterHome) PortableRemoteObject .narrow(obj, TempConverterHome.class); TempConverter serviceRef = homeRef.create(); retVal = serviceRef.celciusToFarenheight(Integer.parseInt(textBox.getText())); System.out.println("TemperatureAction.testEJB EJB Successful Return=" + + retVal); result.setText(textBox.getText() + " degrees Celsius is approximately " + Integer.toString(retVal) + " degrees Fahrenheit!"); } catch (Exception th) { System.out.println("TemperatureAction.testEJB EJB call ex=" + th); th.printStackTrace(); result.setText(th.getClass().getName() + ": " + th.getMessage()); } } |
- 也许您已经注意到了属性文件是用于避免 EJB JNDI 名称的硬编码以及应用程序服务器的定位。当部署 RCP 应用程序时,您可以将属性文件改变为针对实际的远程 EJB 定位。
清单 3
TemperatureView.JNDIName=ejb/TempConverter TemperatureView.Title=Celsius to Fahrenheit Temperature Converter TemperatureView.Button=Convert to Fahrenheit |
- 当构建插件时,您通常需要将插件添加到您的
plug-in.xml文件并且选择扩展点(在该情况下,您扩展了 View 插件)。对于我们来说这是额外的,因为我们在向导中使用 View 模板。您可以在 Extensions 选项卡中查看或者您可以浏览 plug-in.xml 资源。扩展配置如清单 4 所示。
清单 4
point="org.eclipse.ui.views"> name="Temperature Category" id="com.ibm.issw.temperature.form"> name="Temperature View" icon="icons/sample.gif" category="com.ibm.issw.temperature.form" class="com.ibm.issw.temperature.form.views.TemperatureView" id="com.ibm.issw.temperature.form.views.TemperatureView">
|
创建您自己的 RCP 工作台和透视图
因为我们正在开发 RCP 应用程序,使用缺省的 Eclipse 工作台,工作台是按照 IDE 定制的,不是很理想的。因此,我们将会构建我们自己的工作台以及透视图。我们将会导入类,下载文件提供了这些类,导入到我们的插件中,接下来向工作台以及透视图的 plug-in.xml 中添加条目。
- 右键单击 src 文件夹然后选择 Import (图 45)。
- 选择 File system 然后选择 Next。浏览
C:RCPEJBClientRCPCodesrc 文件夹并且导入三个类,如图 46 所示。
- 将类导入
(默认包),它是根(图 47)。
- 让我们检验这些类:
-
RcpWorkbenchAdvisor —— 用于创建我们的工作台。代码设置了某种配置,例如大小和标题,并且禁用某种缺省项。
清单 5
public class RcpWorkbenchAdvisor extends WorkbenchAdvisor { public String getInitialWindowPerspectiveId() { return "MyRCPPerspective"; } public void preWindowOpen(IWorkbenchWindowConfigurer configurer) { super.preWindowOpen(configurer); configurer.setInitialSize(new Point(750, 600)); configurer.setShowCoolBar(false); configurer.setShowStatusLine(false); configurer.setTitle("Temperature Converter"); } } |
-
MyRCPPerspective —— 向透视图中添加视图。这是透视图的初始布局。因为我们没有构建文件编辑的插件所以代码也禁用编辑器区域。
清单 6
public class MyRCPPerspective implements IPerspectiveFactory { public void createInitialLayout(IPageLayout layout) { layout.addView("com.ibm.issw.temperature.form.views.TemperatureView", IPageLayout.LEFT,0.66f,IPageLayout.ID_EDITOR_AREA);; layout.setEditorAreaVisible(false); } } |
-
platform —— RCP 应用程序的主类。创建了工作台并且启动了 RCP 应用程序。
清单 7
public class TempRCPApplication implements IPlatformRunnable { public Object run(Object args) throws Exception { WorkbenchAdvisor workbenchAdvisor = new RcpWorkbenchAdvisor(); Display display = PlatformUI.createDisplay(); try { int returnCode = PlatformUI.createAndRunWorkbench(display,workbenchAdvisor); if(returnCode == PlatformUI.RETURN_RESTART) { return IPlatformRunnable.EXIT_RESTART; } else { return IPlatformRunnable.EXIT_OK; } } finally { display.dispose(); } } } |
- 关于当前添加的代码,我们需要将它添加到我们的
plug-in.xml 中去。选择 Extension 选项卡,然后接下来选择 Add (图 48)。
- 因为我们在编写应用程序,所以我们需要扩展
org.eclipse.core.runtime.applications(图 49)。
- 选择 Finish。
- 在 Extension Details 对话框中,添加作为标识的
RcpApplication (图 50)。
- 右键单击扩展点并且选择 New => application (图 51)。
- 右键单击 (application) 然后选择 New => run (图 52)。
- 在 Extension Element Details 对话框中,浏览 TempRCPApplication 类(图 53)。.
- 添加透视图、添加其它扩展并且选择 org.eclipse.ui.perspectives 扩展点(图 54)。
- 右键单击该点并且选择 New => perspective (图 55)。
- 在 Extension Element Details 中,如图 56 所示浏览 perspective 类:
- 更新标识以符合类名(图 57):
- 现在扩展应该表现为图 58 所示。通过选择 File => Save 或 Ctrl-S 保存插件配置。
换句话说,您可以通过手动直接将它添加到 plug-in.xml 文件中,如清单 8 所示。附加的代码如黑体字所示:
清单 8
point="org.eclipse.ui.views"> name="Temperature Category" id="com.ibm.issw.temperature.form"> name="Temperature View" icon="icons/sample.gif" category="com.ibm.issw.temperature.form" class="com.ibm.issw.temperature.form.views.TemperatureView" id="com.ibm.issw.temperature.form.views.TemperatureView"> point="org.eclipse.core.runtime.applications"> point="org.eclipse.ui.perspectives"> class="MyRCPPerspective" name="com.ibm.issw.temperature.form.perspective1" id="MyRCPPerspective"/>
|
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/374079/viewspace-932012/
,如需转载,请注明出处,否则将追究法律责任。