plugin.xml:
<extension point="org.eclipse.ui.editors"> <editor class="com.xt.editors.TestEditor" default="true" extensions="TestEditor" filenames="TestEditor" id="com.xt.editors.TestEditor" name="测试编辑器"> </editor> <extension>
Editor页面:
package com.xt.editors; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.swt.SWT; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.ISaveablePart2; import org.eclipse.ui.PartInitException; import org.eclipse.ui.forms.editor.FormEditor; import com.xt.dialog.Message; /** * * 类说明: Form编辑器. * @author 小天__ 2013-7-16 上午10:44:07 */ public class TestEditor extends FormEditor implements ISaveablePart2{ public static final String ID = "com.xt.editors.TestEditor"; /**页面引用**/ private CreateUnitEditorPage page; @Override protected void addPages() { try { page = new CreateUnitEditorPage(this, "TestEditor", "属性"); //此名字为左下的名字. addPage(page); } catch (PartInitException e) { e.printStackTrace(); } updateTitle(); } private void updateTitle() { IEditorInput input = getEditorInput(); setPartName(input.getName()); setTitleToolTip(input.getToolTipText()); } @Override public void doSave(IProgressMonitor monitor) { System.out.println("保存成功"); //保存成功.修改属性. page.setDirty(false); page.getManagedForm().staleStateChanged(); editorDirtyStateChanged(); } @Override public void doSaveAs() { // TODO Auto-generated method stub } @Override public boolean isSaveAsAllowed() { // TODO Auto-generated method stub return false; } @Override public int promptToSaveOnClose() { //退出时会调用. 返回0会调用上面的保存方法.返回1则不会调用.真接退出. if(page.isDirty()) { //修改过. int result = Message.showConfirmMessage("是否保存后再退出?", page.getManagedForm().getForm().getShell()); if(result == SWT.YES) { //保存后退出. return 0; } else { return 1; } } else { return 1; } } @Override protected void pageChange(int newPageIndex) { super.pageChange(newPageIndex); } }
package com.xt.editors; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ModifyEvent; import org.eclipse.swt.events.ModifyListener; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.forms.IManagedForm; import org.eclipse.ui.forms.editor.FormEditor; import org.eclipse.ui.forms.editor.FormPage; import org.eclipse.ui.forms.widgets.FormToolkit; import org.eclipse.ui.forms.widgets.ScrolledForm; public class CreateUnitEditorPage extends FormPage { private Text unitName; /**是否保存**/ private boolean dirty = false; /**窗口引用**/ private IManagedForm managedForm; /**input对象, 可以获取Input里的属性**/ private UserOrganEditorInput input; public CreateUnitEditorPage(FormEditor editor, String id, String title) { super(editor, id, title); } @Override protected void createFormContent(final IManagedForm managedForm) { this.managedForm = managedForm; this.input = (UserOrganEditorInput)this.getEditorInput(); this.userID = input.getId(); managedForm.getForm().getBody().setLayout(new FillLayout(SWT.HORIZONTAL)); FormToolkit toolkit = managedForm.getToolkit(); ScrolledForm form = managedForm.getForm(); toolkit.decorateFormHeading(form.getForm()); toolkit.paintBordersFor(managedForm.getForm().getBody()); managedForm.getForm().setText("单位信息"); Composite composite = new Composite(managedForm.getForm().getBody(), SWT.NONE); managedForm.getToolkit().adapt(composite); managedForm.getToolkit().paintBordersFor(composite); composite.setLayout(new FormLayout()); /**添加两个控件**/ Label label = new Label(composite, SWT.NONE); FormData fd_label = new FormData(); fd_label.top = new FormAttachment(0, 15); fd_label.left = new FormAttachment(0, 45); label.setLayoutData(fd_label); managedForm.getToolkit().adapt(label, true, true); label.setText("测试:"); FormData fd_text_1 = new FormData(); fd_text_1.left = new FormAttachment(label, 6); fd_text_1.top = new FormAttachment(label, -3, SWT.TOP); fd_text_1.right = new FormAttachment(60, 0); unitName.setLayoutData(fd_text_1); managedForm.getToolkit().adapt(unitName, true, true); ////////////////////////为控件添加修改事件///////////////////////////// unitName.addModifyListener(new ModifyListener(){ @Override public void modifyText(ModifyEvent e) { dirty = true; //设置为脏状态.好用于保存. 编辑器标签上出现的星号. managedForm.dirtyStateChanged(); } }); } @Override public boolean isDirty() { return dirty; } public void setDirty(boolean dirty) { this.dirty = dirty; } }
EditorInput:
package com.xt.editors; import org.eclipse.core.resources.IFile; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.ui.IEditorInput; import org.eclipse.ui.IPersistableElement; /** * * 类说明: 文本编辑器类. * @author LYQ 2013-7-16 上午10:21:25 */ public class UserOrganEditorInput implements IEditorInput { /**编辑器名称**/ private String name; /**编辑器ID**/ private String id; /**构造方法可自行修改**/ public UserOrganEditorInput(String name, String id) { this.name = name; this.id = id; } @Override public Object getAdapter(Class adapter) { if(adapter.equals(IFile.class)){ return this; }else{ return null; } } @Override public boolean exists() { // TODO Auto-generated method stub return true; } @Override public ImageDescriptor getImageDescriptor() { // TODO Auto-generated method stub return null; } @Override public String getName() { //获取编辑器上方面字 return name; } @Override public IPersistableElement getPersistable() { return null; } @Override public String getToolTipText() { return name; } @Override public boolean equals(Object obj) { //重写equals方法. 返回 true:激活当前已打开的页面. false:则重新打开一个页面. if(obj instanceof UserOrganEditorInput){ UserOrganEditorInput input = (UserOrganEditorInput)obj; return ((input).getName()).equals(this.getName()) && input.getId().equals(this.getId()); } return false; } public String getId() { return id; } }
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); //打开一个空白页. TestEditorInput input = new TestEditorInput("测试编辑器", "1"); //后面的为ID. try { page.openEditor(input, TestEditor.ID); } catch (PartInitException e1) { e1.printStackTrace(); }