前回はMaster
パッケージ・クラスの構造の整理
今までは
パッケージの分割
まず,
パッケージ名 | 説明 | 所属するファイル |
---|---|---|
plugin | プラグイン全体で使用するクラスを配置する | Activator. |
model | モデルクラスを配置する | Field. |
ui | ユーザーインタフェースに関係するクラスを配置する | FormDesignerEditor. |
インターナルパッケージ
Eclipseのプラグイン開発では慣習として外部に公開しないパッケージはパッケージ名に
本来,
Master(一覧)のクラス化
現在のフォームデザイナーのソースコードの中で一番複雑なのは,
マニフェストエディターの
FieldsMasterSectionPartクラスでウィジェットの生成・
FieldsMasterSectionPartクラス
public class FieldsMasterSectionPart extends SectionPart {
public FieldsMasterSectionPart(Composite parent, IManagedForm managedForm) {
super(parent,
managedForm.getToolkit(),
Section.TITLE_BAR | Section.DESCRIPTION);
initialize(managedForm);
createContents(getSection(), managedForm.getToolkit());
}
private void createContents(Section section, FormToolkit toolkit) {
section.setText("フィールド一覧");
section.setDescription("編集するフィールドを選択してください。");
Composite composite = toolkit.createComposite(section);
composite.setLayout(new GridLayout(2, false));
Table table = toolkit.createTable(
composite,
SWT.SINGLE | SWT.FULL_SELECTION);
table.setHeaderVisible(true);
table.setLinesVisible(true);
GridData layoutData = new GridData();
layoutData.horizontalAlignment = GridData.FILL;
layoutData.verticalAlignment = GridData.FILL;
layoutData.grabExcessHorizontalSpace = true;
layoutData.grabExcessVerticalSpace = true;
table.setLayoutData(layoutData);
TableColumn column1 = new TableColumn(table, SWT.NULL);
column1.setText("フィールド名");
column1.setWidth(100);
TableColumn column2 = new TableColumn(table, SWT.NULL);
column2.setText("説明");
column2.setWidth(100);
TableViewer viewer = new TableViewer(table);
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new ITableLabelProvider() {
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public String getColumnText(Object element, int columnIndex) {
if (element == null) {
return "";
}
if (!(element instanceof Field)) {
return "";
}
Field field = (Field) element;
if (columnIndex == 0) {
return field.getName();
} else if (columnIndex == 1) {
return field.getDescription();
}
return "";
}
public void addListener(ILabelProviderListener listener) {
}
public void dispose() {
}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {
}
});
final SectionPart part = new SectionPart(section);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
FieldsMasterSectionPart.this.getManagedForm().fireSelectionChanged(
part,
event.getSelection());
}
});
Composite buttons = toolkit.createComposite(composite);
layoutData = new GridData();
layoutData.verticalAlignment = GridData.FILL;
buttons.setLayoutData(layoutData);
buttons.setLayout(new GridLayout());
Button addButton = toolkit.createButton(buttons, "追加(&A)...", SWT.PUSH);
layoutData = new GridData();
layoutData.horizontalAlignment = GridData.FILL;
addButton.setLayoutData(layoutData);
Button delButton = toolkit.createButton(buttons, "削除(&D)...", SWT.PUSH);
layoutData = new GridData();
layoutData.horizontalAlignment = GridData.FILL;
delButton.setLayoutData(layoutData);
Button upButton = toolkit.createButton(buttons, "上へ", SWT.PUSH);
layoutData = new GridData();
layoutData.horizontalAlignment = GridData.FILL;
upButton.setLayoutData(layoutData);
Button downButton = toolkit.createButton(buttons, "下へ", SWT.PUSH);
layoutData = new GridData();
layoutData.horizontalAlignment = GridData.FILL;
downButton.setLayoutData(layoutData);
section.setClient(composite);
viewer.setInput(createSample());
}
private List createSample() {
List fields = new ArrayList();
Field field1 = new Field("name");
field1.setDescription("名前");
field1.setRequired(true);
field1.setMessage("名前は必須です。");
fields.add(field1);
Field field2 = new Field("phone");
field2.setDescription("電話番号");
field2.setRequired(true);
field2.setMessage("電話番号は必須です。");
fields.add(field2);
Field field3 = new Field("address");
field3.setDescription("住所");
field3.setRequired(false);
fields.add(field3);
return fields;
}
}
FieldsBlockクラス
private class FieldsBlock extends MasterDetailsBlock {
@Override
protected void createMasterPart(
final IManagedForm managedForm,
final Composite parent) {
new FieldsMasterSectionPart(parent, managedForm);
}
@Override
protected void createToolBarActions(IManagedForm managedForm) {
}
@Override
protected void registerPages(DetailsPart detailsPart) {
...
}
}
ここまで実装できたら,