当前页面: 开发资料首页 → JSP 专题 → 在定制Eclipse SWT组件中实现MVC
摘要: Eclipse SWT(标准部件工具包)提供了丰富的 API 集来实现定制部件(widget)。在这篇文章中,作者简要介绍了 MVC(模型-视图-控制器)架构,以结构化查看器的形式解释了 MVC 的当前实现,并介绍了一种使用定制 SWT 部件的实现。
public class ExampleView extends ViewPart
{ ... public void createPartControl(Composite parent)
{ // define a grid layout
GridLayout layout = new GridLayout();
layout.numColumns = 1;
layout.marginHeight = 0;
layout.marginWidth = 0; l
ayout.horizontalSpacing = 0;
layout.verticalSpacing = 1;
parent.setLayout(layout);
// create widgets createActionBar(parent);
createTree(parent);
// add context menu and listeners
viewer.addDoubleClickListener(this); viewer.addSelectionChangedListener(openAction);
// register viewer so actions respond to selection getSite().setSelectionProvider(viewer);
hookContextMenu();
}
private void createTree(Composite parent)
{
viewer = new TreeViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
viewer.setContentProvider(new ExampleViewContentProvider()); viewer.setLabelProvider
(new ExampleViewLabelProvider());
viewer.setSorter(new ViewerSorter());
viewer.setInput(ModelManager.getExampleModel());
viewer.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));
} ... }
public class ExampleViewContentprovide implements ITreeContentProvide {
/** * Register content provider with model. */
public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
{
if (newInput != null)
{
this.viewer = viewer;
this.model = (ExampleDelegate)newInput; this.model.addModelListener(this);
}
}
public void createPartControl(Composite parent) {
ExampleEditLayout _layout = new
ExampleEditLayout(parent,SWT.NONE,FieldMode.Read,new ExampleViewContentProvider());
}
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));
...}
public class TextField extends Field {
/**
* @param control
* @param id
*/
public TextField(Control control, int id) {
super(control, id);
}
/* Based on the ID of the widget, values retrieved from
* the content provider are set.
*/
public void readFromContent(IExampleContentProvider content) {
String newText = (String )content.getElement(getId());
if (newText != null)
((Text )_control).setText(newText);
}
/* Based on the ID of the widget, values retrieved from widget are
* sent back to the content provider.
*/
public void writeToContent(IExampleContentProvider content) {
String newText = ((Text )_control).getText();
content.setElement(getId(), newText);
}
}
public Object getElement(int iIndex) {
switch (iIndex) {
case FIRST_INDEX: return "developer@ibm.com";
case SECOND_INDEX : return new Integer(1);
case FOURTH_INDEX : return new Boolean(true);
case THIRD_INDEX: return new Boolean(false);
case FIFTH_INDEX: return new Boolean(false);
}
return null;
}
public Form (Composite parent, int style, FieldMode mode, ExampleViewContentProvider content) {
super(parent, style);
_content = content;
_style = style;
setMode(mode);
init(style);
}
private void init(int style) {
createControls(style);
controlsCreated();
}
protected void controlsCreated() {
readFromContent();
}