在多数常见 GUI 应用程序中,创建布局来显示请求的数据,或完成表单(例如用户界面)来添加或修改数据。图 1 的示例应用程序演示了如何在定制表单中,用只读和可编写模式显示来自 XML 存储的数据。它还解释了每个组件相对于 MVC 架构的角色。
图 1. 示例应用程序

图 2 显示了应用程序的类图,有助于更好地理解整体架构。
图 2. 示例应用程序的类图
创建控件
ExampleView 充当整个应用程序的容器。它将在 createPartControl 方法中初始化应用程序。
清单 4. CreatePartControl 方法初始化布局
1 public void createPartControl(Composite parent) {
2 ExampleEditLayout _layout = new
3 ExampleEditLayout(parent,SWT.NONE,FieldMode.Read,new ExampleViewContentProvider());
4 }
5
2 ExampleEditLayout _layout = new
3 ExampleEditLayout(parent,SWT.NONE,FieldMode.Read,new ExampleViewContentProvider());
4 }
5
创建表单和布局
基本布局类定义了不同的表单应用程序使用的全局方法和声明。有些充当回调机制的容器事件,也注册到了这里。
清单 5. 布局的 CreateControl 方法
1 public void createControls(int style) {
2 GridData gridData;
3 Text textFld, subjectFld;
4 Control toLabel, ccLabel, bccLabel;
5 Control fromDateTime;
6 Control control;
7 Button durationText;
8 Button submit;
9 GridLayout layout = new GridLayout(2, false);
10 layout.marginWidth = 0;
11 layout.marginHeight = 4;
12 setLayout(layout);
13 //Label
14 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
15 | GridData.VERTICAL_ALIGN_CENTER);
16 gridData.horizontalIndent = 10;
17 LabelFactory.create(this,
18 Messages.getString("ExampleEditLayout.Title"), gridData); //$NON-NLS-1$
19 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
20 | GridData.VERTICAL_ALIGN_CENTER);
21 gridData.horizontalIndent = 40;
22 LabelFactory.create(this, "", gridData);
23 //Text
24 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
25 | GridData.VERTICAL_ALIGN_CENTER);
26 gridData.horizontalIndent = 10;
27 control = LabelFactory.create(this,
28 Messages.getString("ExampleEditLayout.Email"), gridData); //$NON-NLS-1$
29 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
30 | GridData.VERTICAL_ALIGN_CENTER);
31 gridData.horizontalIndent = 10;
32 control = TextFactory.create(this,
33 SWT.BORDER | SWT.V_SCROLL | SWT.WRAP, gridData, FieldMode.Edit); //$NON-NLS-1$
34 addField(new TextField(control, ExampleViewContentProvider.FIRST_INDEX));
35 //Combo
36 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
37 | GridData.VERTICAL_ALIGN_CENTER);
38 gridData.horizontalIndent = 10;
39 LabelFactory.create(this,
40 Messages.getString("ExampleEditLayout.Group"), gridData); //$NON-NLS-1$
41 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
42 | GridData.VERTICAL_ALIGN_CENTER);
43 gridData.horizontalIndent = 40;
44 control = ComboFactory.create(this,
45 FieldMode.Edit, false, gridData); //$NON-NLS-1$
46 addField(new ComboField(control,
47 ExampleViewContentProvider.SECOND_INDEX));
48 ...}
49
2 GridData gridData;
3 Text textFld, subjectFld;
4 Control toLabel, ccLabel, bccLabel;
5 Control fromDateTime;
6 Control control;
7 Button durationText;
8 Button submit;
9 GridLayout layout = new GridLayout(2, false);
10 layout.marginWidth = 0;
11 layout.marginHeight = 4;
12 setLayout(layout);
13 //Label
14 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
15 | GridData.VERTICAL_ALIGN_CENTER);
16 gridData.horizontalIndent = 10;
17 LabelFactory.create(this,
18 Messages.getString("ExampleEditLayout.Title"), gridData); //$NON-NLS-1$
19 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
20 | GridData.VERTICAL_ALIGN_CENTER);
21 gridData.horizontalIndent = 40;
22 LabelFactory.create(this, "", gridData);
23 //Text
24 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
25 | GridData.VERTICAL_ALIGN_CENTER);
26 gridData.horizontalIndent = 10;
27 control = LabelFactory.create(this,
28 Messages.getString("ExampleEditLayout.Email"), gridData); //$NON-NLS-1$
29 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
30 | GridData.VERTICAL_ALIGN_CENTER);
31 gridData.horizontalIndent = 10;
32 control = TextFactory.create(this,
33 SWT.BORDER | SWT.V_SCROLL | SWT.WRAP, gridData, FieldMode.Edit); //$NON-NLS-1$
34 addField(new TextField(control, ExampleViewContentProvider.FIRST_INDEX));
35 //Combo
36 gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
37 | GridData.VERTICAL_ALIGN_CENTER);
38 gridData.horizontalIndent = 10;
39 LabelFactory.create(this,
40 Messages.getString("ExampleEditLayout.Group"), gridData); //$NON-NLS-1$
41 gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
42 | GridData.VERTICAL_ALIGN_CENTER);
43 gridData.horizontalIndent = 40;
44 control = ComboFactory.create(this,
45 FieldMode.Edit, false, gridData); //$NON-NLS-1$
46 addField(new ComboField(control,
47 ExampleViewContentProvider.SECOND_INDEX));
48 ...}
49
创建字段(视图)
Field 是一个抽象类,它定义了包含各种用户界面控件的方法,还有全局地识别这些控件的相关 ID。每个用户界面控件都是 Field 的子类,并向内容提供者提供了读写能力。清单 6 用工厂模式,在 Layout 类中创建了 Field。
清单 6. 用 Field 类创建文本对象
1 public class TextField extends Field {
2 /**
3 * @param control
4 * @param id
5 */
6 public TextField(Control control, int id) {
7 super(control, id);
8 }
9 /* Based on the ID of the widget, values retrieved from
10 * the content provider are set.
11 */
12 public void readFromContent(IExampleContentProvider content) {
13 String newText = (String )content.getElement(getId());
14 if (newText != null)
15 ((Text )_control).setText(newText);
16 }
17 /* Based on the ID of the widget, values retrieved from widget are
18 * sent back to the content provider.
19 */
20 public void writeToContent(IExampleContentProvider content) {
21 String newText = ((Text )_control).getText();
22 content.setElement(getId(), newText);
23 }
24 }
2 /**
3 * @param control
4 * @param id
5 */
6 public TextField(Control control, int id) {
7 super(control, id);
8 }
9 /* Based on the ID of the widget, values retrieved from
10 * the content provider are set.
11 */
12 public void readFromContent(IExampleContentProvider content) {
13 String newText = (String )content.getElement(getId());
14 if (newText != null)
15 ((Text )_control).setText(newText);
16 }
17 /* Based on the ID of the widget, values retrieved from widget are
18 * sent back to the content provider.
19 */
20 public void writeToContent(IExampleContentProvider content) {
21 String newText = ((Text )_control).getText();
22 content.setElement(getId(), newText);
23 }
24 }