??xml version="1.0" encoding="utf-8" standalone="yes"?>
本篇译自?/span>
Eclipse.Building.Commercial.Quality.Plug.ins.2nd.Edition
》第四章W二节,希望对入门者有所帮助?/span>
lg的生命周?/span>
设计
SWT
的一个目标就是小而灵巧。ؓ了达到这个目标,一个基本的设计{略Q就是尽可能的ɾlg的状态存储在q_lg中而不是存储在
SWT
lg?/span>
(
q句话,我不理解Q也译的不明白Q附原文Q?/span>
To achieve this, a basic design decision was made that as much widget state as possible would be stored in the platform widget rather than in the SWT widget.)
。这?/span>
Swing
形成显著的对比,
Swing
?/span>
Swing
lg中维护了所有的lg状态?/span>
(
可能是这P
SWT
把各个组件的状态交l系l^台来理Q?/span>
Swing
自己理各个
Swing
lg的状态,q样
Swing
复杂,耗费的资源,内存的需求也多,不知道是不是
J
)
。通过不把pȝq_U别的组件信息复制过来自q护,
SWT
非常的y而且对内存的需求也相应?/span>
辑ֈq个目的所付出的代价就?/span>
SWT
的组件不能够自己独立存在。当一?/span>
SWT
lg被创建时Q同时立卛_pȝq_下也创徏了该lg的对应体。接下来Q几乎所有对该组件的信息状态请求都会由q_来处理?/span>
大部分的pȝq_在创Z个组件时Q需要该lg明确的父lg的上下文Q所?/span>
SWT
需要一个父lg来最为它的构造声明。许多^台在创徏lg时还需要该lg的特定风格参数的讑֮。例如:按钮有复选框Q单选框Q简单按钮和文本域有单行和多行之分?/span>
?/span>
SWT
cMQ风gD는整数cd来定义且不会更改。风格参数被l织在一P其它各种构造器传递着q些参数来创Z个组件的初始风格。注意到Q所有的q_q不支持所有的风格Q所以在很多情况下,被请求的风格被认为是一U提C,它或怼或许不会对一个特D的q_产生影响?/span>
?/span>
SWT
lg不在需要时Q一些^台要求作出明的释放。对于组件本w和一些资?/span>
(
例如Q图形,字体Q颜?/span>
)
而言Q他们已l具备了pȝ的这个要求。基本的规则是:如果你创Z一个组Ӟp使用
dispose()
Ҏ来撤销q个lg。如果你使用了一些系l资源,例如pȝ颜色Q你׃需要释放他们?/span>
q运的,当一个组件被释放的,它的所有子lg会自动地被释?/b>。这意味着Q如果你释放了一?/span>
shell
Q那么所?/span>
shell
包含的组仉会被自动释放?/span>
]]>
让我们从单的 HelloWorld 应用E序开始?/span>
一?/span>?/span>SWTd到工E的c\径中
首先建立一?span lang="EN-US">java工程。在开始?span lang="EN-US">SWT之前Q需要将SWT库文件添加到工程的类路径中。步骤如下:
1. 下蝲SWT。在Eclipse SDK的下载页面中Q提供了独立版本?span lang="EN-US">SWT下蝲。找到标?span class="docemphstrong">SWT Binary and Source的栏目。下载适合你操作系l的版本Q不用解压,直接保存到硬盘中?/span>
2. ?span lang="EN-US">Eclipse菜单栏,选择File-?span lang="EN-US">Import...打开导入向导
3. 选择Existing Projects into WorkspaceQ点?b>Next按钮
4. 选择 Select archive fileq?Browse...按钮来找C刚才下蝲?span lang="EN-US">SWT文g?
5. 点击Finish按钮Q完?span lang="EN-US">SWT导入?
6. 右键点击你徏立的工程Q选择Properties 来打开Properties对话框?/span>
7. 选择 Java Build Path -> Projects tabq点?span class="docemphstrong">Add按钮.
8. 选择 org.eclipse.swt工程Q?点击 OKQ完成添?span lang="EN-US">SWT库到你的工程?span lang="EN-US">(见图1).
?/span> 1
现在在你建立?span lang="EN-US">java工程中,新徏一?span lang="EN-US">java文gQ取名ؓ
HelloWorld。在HelloWorld.java文g中把main()Ҏ覆盖Z下代?span lang="EN-US">:
注:
在覆盖了上述代码?span lang="EN-US">, 选择菜单栏中?
Source
-> Organize Imports命o (或者按Ctrl+Shift+O) 来把需要引用的SWT包导入到HelloWorld.java文g?
?span lang="EN-US">
W?span lang="EN-US">2行:每个ZSWT的应用程序都有一?span lang="EN-US">Displaycȝ实例。用来将低层q_?span lang="EN-US">SWTq行链接。除了管?span lang="EN-US">SWT的事件@环,q能讉KSWT需要的q_资源。在16行,display实例会提交l垃圾收集器?span lang="EN-US">
W?span lang="EN-US">3行:每一个窗口都有一?span lang="EN-US">ShellH口框架Q来与用戯行交互?span lang="EN-US">Shell像所有的windowspȝ一h处理动作行ؓQƈ作ؓH口控g的放|场所?span lang="EN-US">
W?span lang="EN-US">4行: setText()Ҏ讄H口的标?span lang="EN-US">.
W?span lang="EN-US">5行: setBounds() Ҏ讄H口的大和攄的位|?span lang="EN-US">. 在这个例子中, 讄H口?span lang="EN-US">200个像素宽Q?span lang="EN-US">50个像素高Qƈ攑֜dq的左上?span lang="EN-US">100x100像素的位|上?span lang="EN-US">
W?span lang="EN-US">6行: setLayout()Ҏ 讄H口框架的布局. FillLayoutQ充满式布局理器,使得lg大小会尽量的充满整个容器.SWT的布局理器会在以后详l介l?
W?span lang="EN-US">7行:?span lang="EN-US">shell上新Z个简单的labellgq居中显C?span lang="EN-US">label的文本内宏V?span lang="EN-US">
W?span lang="EN-US">8行: setText() Ҏ 讄label的文本内宏V?span lang="EN-US">
W?span lang="EN-US">9行:创徏一个红色的颜色cȝ实例。你也可以用以下语句获得系l红色的实例Q?span lang="EN-US">
Color red = display.getSystemColor(SWT.COLOR_RED);
W?span lang="EN-US">10行: setForeground() Ҏ 讄label的前景色
W?span lang="EN-US">11行:到目前ؓ止,H口框架q是不可见的。通过open()Ҏ使得H口可见?span lang="EN-US">
W?span lang="EN-US">12行: while语句循环窗口有没有关闭?span lang="EN-US">
W?span lang="EN-US">13行:display 控制事g的@?span lang="EN-US">. readAndDispatch() Ҏ从^台的事g队列中读取事Ӟq分配他们到合适的处理E序(接收?span lang="EN-US">)。只要队列中一直有事g可以处理Q这个方法一直返?span lang="EN-US">trueQ当事g队列为空Ӟ则返?span lang="EN-US">false(因此允许用户界面UIU程Zsleep状态直C仉列不为空)?
W?span lang="EN-US">15,16行:当@环检到H口被关闭时Q需要将colorQ?span lang="EN-US">display和一些相兌的^台资源释放。注意到pȝ颜色实例(colors)会被提交释放?span lang="EN-US">
二?span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">
q行q个例子
通常情况下,Z启动一?span lang="EN-US">java应用E序Q会使用Run As -?/b>
Java Application
命o。在q里Q如此运行将会抛?/span>
UnsatisfiedLinkError
异常Q说明没有找?/span>
SWT
的本地库。如果运?/span>
Run As > SWT Application
命oQ将会弹?/span>
SWT
启动配置H口Q见?/span>
2
Q在?/span>
2
中可以点?/span>
run
按钮?
?/span> 2
点击 run Q运行结果图 3 ?/span>
?/span> 3
(以后l箋)
A workbench window is a top level window in a workbench. Visually, a workbench window has a menubar, a toolbar, a status bar, and a main area for displaying a single page consisting of a collection of views and editors.
Each workbench window has a collection of 0 or more pages; the active page is the one that is being presented to the end user; at most one page is active in a window at a time.
This interface is not intended to be implemented by clients.
一个工作台H口是一个工作台的顶层窗?从视觉上?一个工作台H口拥有一个菜单栏,工具?状态栏和一个主区域,用来昄包括昄和编辑的一个页?
每个工作台窗口有0个或者更多的面.zd的页面是一U显C给当前使用者的面.大多数情况下,在一个时d有一个页面处于活动状?
q个接口不打由用户来实?
(to more)
Rich Client Tutorial Part 1
The Rich Client Platform (RCP) is an exciting new way to build Java applications that can compete with native applications on any platform. This tutorial is designed to get you started building RCP applications quickly. It has been updated for Eclipse 3.1.2
Try this experiment: Show Eclipse to some friends or co-workers who haven't seen it before and ask them to guess what language it is written in. Chances are, they'll guess VB, C++, or C#, because those languages are used most often for high quality client side applications. Then watch the look on their faces when you tell them it was created in Java, especially if they are Java programmers.
Because of its unique open source license, you can use the technologies that went into Eclipse to create your own commercial quality programs. Before version 3.0, this was possible but difficult, especially when you wanted to heavily customize the menus, layouts, and other user interface elements. That was because the "IDE-ness" of Eclipse was hard-wired into it. Version 3.0 introduced the Rich Client Platform (RCP), which is basically a refactoring of the fundamental parts of Eclipse's UI, allowing it to be used for non-IDE applications. Version 3.1 updated RCP with new capabilities, and, most importantly, new tooling support to make it easier to create than before.
If you want to cut to the chase and look at the code for this part you can find it in the accompanying zip file . Otherwise, let's take a look at how to construct an RCP application.
引言
试做这个实验:?/span> Eclipse 展示l你的那些以前没见过 Eclipse 的朋友或合作伙伴Q让他们猜猜q是用何U语a~写的。很有可能,他们会说?/span> VB Q?/span> C++ 或?/span> C# Q因些语al常被用来作为高质量客户端方面的应用E序。当你告诉他们是?/span> java ~写Ӟ察看他们怸的表情,其当他们是 java E序员的话?/span>
因ؓ Eclipse 是唯一 开放源代码许可的,你可以用 Eclipse q个技术来创徏你自己商业质量的E序。在 3.0 版本以前Q这是可能的Q但也是困难的,其当你惌订制菜单Q多层和其他用户界面元素时。那是因为很难接触到 Eclipse ?/span> IDE 核心?/span> 3.0 版本?/span> Eclipse 引入了富客户端编E( RCP Q,q基本上?/span> Eclipse UI 的重新构建,也就允许 Eclipse UI 被应用到?/span> Eclipse IDE 的应用程序中?/span> 3.1 版本?/span> Eclipse 用新Ҏ升U了 RCP Q最重要的是Q新的工具的创?/span> RCP 应用E序比以前更加容易?/span>
如果你想要研I?/span> Rich Client Tutorial Part 1 的代码,你可以到 accompanying zip file 下蝲。另外,让我们一步步来看看如何创Z?/span> RCP 应用E序?/span>
开?/span>
RCP applications are based on the familiar Eclipse plug-in architecture, (if it's not familiar to you, see the references section). Therefore, you'll need to create a plug-in to be your main program. Eclipse's Plug-in Development Environment (PDE) provides a number of wizards and editors that take some of the drudgery out of the process. PDE is included with the Eclipse SDK download so that is the package you should be using. Here are the steps you should follow to get started.
RCP 应用E序Z熟悉?/span> Eclipse 插g机制Q(如果你不熟悉Q请查看参考书目部分)。因此,你要创徏一个插件来作ؓ你的ȝ序?/span> Eclipse 的插件开发环境( PDE Q提供了大量的控件和~辑器来使得开发过E脱苦?/span> PDE 包括在了 Eclipse SDK Q所?/span> PDE 开发包你可以直接用。以下是开始开发的步骤Q?/span>
First, bring up Eclipse and select File > New > Project, then expand Plug-in Development and double-click Plug-in Project to bring up the Plug-in Project wizard. On the subsequent pages, enter a Project name such as org.eclipse.ui.tutorials.rcp.part1, indicate you want a Java project, select the version of Eclipse you're targeting (at least 3.1), and enable the option to Create an OSGi bundle manifest. Then click Next >.
首先Q启?/span> Eclipse Q选择File > New > Project?/strong>接下来选中 Plug-in DevelopmentQ?/strong>双击来打开插g工程向导。在弹出的页面中Q键入工E名Q例如: org.eclipse.ui.tutorials.rcp.part1Q,指明你要一?/span> java工程Q选择你想创徏适用?/span> Eclise的版本,q中使用 OSGI包。接下来点击 Next> ?/span>
noteQ?/b>Beginning in Eclipse 3.1 you will get best results by using the OSGi bundle manifest. In contrast to previous versions, this is now the default.
In the next page of the Wizard you can change the Plug-in ID and other parameters. Of particular importance is the question, "Would you like to create a rich client application?". Select Yes. The generated plug-in class is optional but for this example just leave all the other options at their default values. Click Next > to continue.
注: 通过使用 Eclipse 3.1 ?/span> OSGI 包,你会获得很好的结果。与以前的版本相比, OSGi 现在是默认的了?/span>
在下一向gQ你可以更改插g?/span> ID 号和其他参数。尤其要x一个问题:“你惌创徏一个富客户端的应用E序吗?”,选择“是”。生成的插gcL可选的Q但对于q个例子Q一切其他的可选项都设为默认倹{单?strong>Next >C一?strong>?/strong>
noteQ?/b> If you get a dialog asking if Eclipse can switch to the Plug-in Development Perspective click Remember my decision and select Yes (this is optional).
注:如果你得C个对话框Q询?/span> Eclipse 是否切换到插件开发透视图。这时单击记住我的选择q择“是”(q是可选的Q?/span>
Starting with Eclipse 3.1, several templates have been provided to make creating an RCP application a breeze. We'll use the simplest one available and see how it works. Make sure the option to Create a plug-in using one of the templates is enabled, then select the Hello RCP template. This is RCP's equivalent of "Hello, world". Click Finish to accept all the defaults and generate the project (see Figure 1). Eclipse will open the Plug-in Manifest Editor. The Plug-in Manifest editor puts a friendly face on the various configuration files that control your RCP application.
?/span> Eclipse 3.1 开始,提供了几个模板得创?/span> RCP 应用E序相当Ҏ。我们用最单的一个模板来观察q是如何工作的。确保创Z个插件用模板之一是允许的Q接着选择?/span> Hello RCP ”模ѝ这?/span> RCP 应用E序中的?/span> Hello, world ”。选择“完成”来创徏工程Q见图一Q?/span> Eclipse 会打开插g~辑器。插件编辑器提供了友好的界面Q拥有多L配置文g来控制你的RQP应用E序?/span>
Figure 1. The Hello World RCP project was created by a PDE wizard.
图一QPQI向导创徏的ᅢello QPrld工E?/span>
(未完Q待l?