资源里存储了用户需要的信息,而编辑器是用户创建和修改资源最主要的方式,它是eclipse插件开发中最复
杂也是最主要的部分。
编辑器必须实现org.eclipse.ui.IEditorPart接口,但通常继承子类org.eclipse.ui.part.EditorPart.
IEditorInput描述了编辑器的数据源。IPathEditorInput描述了数据源的格式,IFileEditorInput描述了基于
本地数据源。
当编辑器被创建后,输入源是通过编辑器的init方法来设定的。可以通过getEditorInput方法来重新获取。
MultiPageEditorPart,多页编辑器,每个页面都包含它自己的编辑器。FormEditor,它继承了MultiPageEditorPart类,用来实现表格编辑器。
创建编辑器,首先要在清单文件中声明扩展点,如下所示:
<extension
point="org.eclipse.ui.editors">
<editor
//与视图一样,一个编辑器对应一个class类,该类必须实现IEditorPart接口
class="com.plugindev.addressbook.editors.AddressFormEditor"
//实现了org.eclipse.ui.part.EditorActionBarContributor接口的全权限定名称
contributorClass="com.plugindev.addressbook.editors.AddressFormEditorContributor"
default="true"
//编辑器左上角显示的图像
icon="icons/editors/editor.gif"
id="com.plugindev.addressbook.tableEditor"
name="地址本编辑器"/>
</extension>
FormEditor抽象类,提供了抽象方法addPages(),子类必须实现,用与为编辑器添加页面,
在打开编辑器时,必须同时制定输入的数据源
doubleClickAction = new Action() {
public void run() {
ISelection selection = viewer.getSelection();
AddressItem obj = (AddressItem)((IStructuredSelection)
selection).getFirstElement();
IWorkbenchPage page = PlatformUI.getWorkbench().
getActiveWorkbenchWindow().getActivePage();
try {
SimpleFormEditorInput input = new SimpleFormEditorInput(obj.getName());
page.openEditor(input, "com.plugindev.addressbook.tableEditor");
} catch (PartInitException e) {
System.out.println(e);
}
//为Editor增加的操作
}
};
org.eclipse.ui.forms.editor.FormEditor
在创建表格编辑器时,必须继承FormEditor类,而加入到FormEditor编辑器中的每个页面都必须继承FormPage
类,例如:
public class PageWithSubPages extends FormPage {
private FormEditor editor;
private CTabFolder tabFolder;
public PageWithSubPages(FormEditor editor) {
super(editor, "pageWithSub", Messages.PAGE_NAME_PAGEWITHSUB);
this.editor = editor;
}
protected void createFormContent(IManagedForm managedForm) {
ScrolledForm form = managedForm.getForm();
FormToolkit toolkit = managedForm.getToolkit();
form.setText(Messages.PAGE_TITLE_PAGEWITHSUB);
form.setBackgroundImage(ImageCache.getInstance().getImage
(ImageKeys.getImageDescriptor(ImageKeys.IMG_FORM_BG)));
GridLayout layout = new GridLayout();
layout.marginWidth = 10;
form.getBody().setLayout(layout);
tabFolder = new CTabFolder(form.getBody(), SWT.FLAT|SWT.TOP);
toolkit.adapt(tabFolder, true, true);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.heightHint = 0;
tabFolder.setLayoutData(gd);
Color selectedColor = toolkit.getColors().getColor(FormColors.SEPARATOR);
tabFolder.setSelectionBackground(new Color[] {selectedColor, toolkit.getColors
().getBackground()}, new int[] {50});
}
}
主/从风格的页面
MasterDetailsBlock类采用注册页面的方式来构建主从块,registerPages()方法用来为具体的从属页面注册
所允许的数据模型,从而建立起从属页面和数据模型的一一对应关系。
public class MasterDetailsPage extends FormPage {
//该对象代表页面的MasterDetail部分
private ScrolledPropertiesBlock block;
public MasterDetailsPage(FormEditor editor) {
super(editor, "masterDetail", Messages.PAGE_NAME_MASTERDETAIL);
block = new ScrolledPropertiesBlock(this);
}
protected void createFormContent(final IManagedForm managedForm) {
final ScrolledForm form = managedForm.getForm();
form.setText(Messages.PAGE_TITLE_MASTERDETAIL);
form.setBackgroundImage(null);
//该方法非常重要,负责创建Master和Detail区域,尽量在最后调用
block.createContent(managedForm);
}
public IAction getTableAction(String workbenchActionId) {
return block.getTableAction(workbenchActionId);
}
}
具有MasterDetail模式的页面类都要继承自MasterDetailBlock类,并实现该类的3个方法:
1.createMasterPart
在该方法中创建master部分的控件
2.createToolBarActions
创建该页面的工具栏,本例有垂直和水平2个按钮
3.registerPages
注册master对应的detail部分,可以注册多个
public class ScrolledPropertiesBlock extends MasterDetailsBlock {
private FormPage page;
private TableViewer viewer;
private RemoveAddressListAction removeAction;
private Action vaction;
private Action haction;
public ScrolledPropertiesBlock(FormPage page) {
this.page = page;
}
protected void createMasterPart(final IManagedForm managedForm,
Composite parent) {
//final ScrolledForm form = managedForm.getForm();
FormToolkit toolkit = managedForm.getToolkit();
Section section = toolkit.createSection(parent,
Section.DESCRIPTION|Section.TITLE_BAR);
section.setText(Messages.SCROL_BLOC_NAME);
section.setDescription(Messages.SCROL_BLOC_DESCRIP);
section.marginWidth = 10;
section.marginHeight = 5;
Composite client = toolkit.createComposite(section, SWT.WRAP);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
layout.marginWidth = 2;
layout.marginHeight = 2;
client.setLayout(layout);
Table t = toolkit.createTable(client, SWT.NULL);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 20;
gd.widthHint = 100;
gd.verticalSpan = 2;
t.setLayoutData(gd);
toolkit.paintBordersFor(client);
section.setClient(client);
//代表master部分,且把该对象注册到managedForm中
final SectionPart spart = new SectionPart(section);
managedForm.addPart(spart);
viewer = new TableViewer(t);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
//当选择变化的时候,必须通过managedForm的该方法来发布事件,该事件接受的地方必须
//在Detail页面进行
managedForm.fireSelectionChanged(spart, event.getSelection());
}
});
viewer.setContentProvider(new MasterContentProvider());
viewer.setLabelProvider(new MasterLabelProvider());
viewer.setInput(page.getEditor().getEditorInput());
}
protected void registerPages(DetailsPart detailsPart) {
detailsPart.registerPage(BasicAddressList.class, new BasicAddressListProperties());
detailsPart.registerPage(PhoneAddressList.class, new PhoneAddressListItemProperties());
detailsPart.registerPage(AreaAddressList.class, new AreaAddressListProperties());
}
}
Detail部分必须实现IDetailPage接口,主要是在createContents方法中创建Detail所需要使用的控件,并且
在selectChanged方法中处理当master对象选中事件发生的代码。
public class AddressListProperties implements IDetailsPage {
public AddressListProperties() {
}
public void initialize(IManagedForm mform) {
}
public void createContents(Composite parent) {
}
protected void createSection(Composite parent,
ArrayList<String> stringKeys, Map<String, Object[]>choiceKeysMap)
{
}
//在此方法中处理mater选择事件
public void selectionChanged(IFormPart part, ISelection selection) {
IStructuredSelection ssel = (IStructuredSelection)selection;
if (ssel.size()==1) {
input = (AddressList)ssel.getFirstElement();
s1.setText(input.getName());
s1.setDescription(input.getDescription());
}
else
input = null;
update();
}
public void commit(boolean onSave) {
}
public void setFocus() {
choices[0][0].setFocus();
}
public void dispose() {
}
public boolean isDirty() {
return false;
}
public boolean isStale() {
return false;
}
public void refresh() {
update();
}
public boolean setFormInput(Object input) {
return false;
}
}