与 Eclipse SWT 结合使用 MVC
在多数常见 GUI 应用程序中,创建布局来显示请求的数据,或完成表单(例如用户界面)来添加或修改数据。图 1 的示例应用程序演示了如何在定制表单中,用只读和可编写模式显示来自 XML 存储的数据。它还解释了每个组件相对于 MVC 架构的角色。
图 1. 示例应用程序
图 2 显示了应用程序的类图,有助于更好地理解整体架构。
图 2. 示例应用程序的类图
创建控件
ExampleView 充当整个应用程序的容器。它将在 createPartControl 方法中初始化应用程序。
清单 4. CreatePartControl 方法初始化布局
public void createPartControl(Composite parent) {
ExampleEditLayout _layout = new
ExampleEditLayout(parent,SWT.NONE,FieldMode.Read,new ExampleViewContentProvider());
}
创建表单和布局
基本布局类定义了不同的表单应用程序使用的全局方法和声明。有些充当回调机制的容器事件,也注册到了这里。
清单 5. 布局的 CreateControl 方法
public void createControls(int style) {
GridData gridData;
Text textFld, subjectFld;
Control toLabel, ccLabel, bccLabel;
Control fromDateTime;
Control control;
Button durationText;
Button submit;
GridLayout layout = new GridLayout(2, false);
layout.marginWidth = 0;
layout.marginHeight = 4;
setLayout(layout);
//Label
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 10;
LabelFactory.create(this,
Messages.getString("ExampleEditLayout.Title"), gridData); //$NON-NLS-1$
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 40;
LabelFactory.create(this, "", gridData);
//Text
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 10;
control = LabelFactory.create(this,
Messages.getString("ExampleEditLayout.Email"), gridData); //$NON-NLS-1$
gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 10;
control = TextFactory.create(this,
SWT.BORDER | SWT.V_SCROLL | SWT.WRAP, gridData, FieldMode.Edit); //$NON-NLS-1$
addField(new TextField(control, ExampleViewContentProvider.FIRST_INDEX));
//Combo
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 10;
LabelFactory.create(this,
Messages.getString("ExampleEditLayout.Group"), gridData); //$NON-NLS-1$
gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
| GridData.VERTICAL_ALIGN_CENTER);
gridData.horizontalIndent = 40;
control = ComboFactory.create(this,
FieldMode.Edit, false, gridData); //$NON-NLS-1$
addField(new ComboField(control,
ExampleViewContentProvider.SECOND_INDEX));
...}
