今天开始做些小的总结,因为Eclipse插件方面做了不少东西了,也该总结总结学习到的东西了!
为了增加普遍性,例子使用的Editor继承自最简单的EditorPart来制作。
程序里的说明,作为帮助吧!
package ztestpluginproject.editors.zeditors;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.widgets.ColumnLayout;
import org.eclipse.ui.forms.widgets.ColumnLayoutData;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.part.EditorPart;
public class ZTestEditor extends EditorPart {
// 简单情况下可以自己制作一个标志来记录当前编辑器状况
public boolean isDirty = false;
@Override
public void doSave(IProgressMonitor monitor) {
this.isDirty = false; // 先将标志设置为false
this.firePropertyChange(PROP_DIRTY); // 通知编辑器要修改状态了
}
@Override
public void doSaveAs() {
}
@Override
public void init(IEditorSite site, IEditorInput input)
throws PartInitException {
this.setInput(input);
this.setSite(site);
this.setPartName(input.getName());//设置标签为 文件名
}
@Override
public boolean isDirty() {
//firePropertyChange(PROP_DIRTY) 将根据这里返回的值,来修改编辑器状态(保存或者未保存<*>)
return this.isDirty;
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void createPartControl(Composite parent) {
// 这里新建了一个表单 放了三个文本框
final FormToolkit toolkit = new FormToolkit(parent.getDisplay());
ColumnLayout layout = new ColumnLayout();
layout.horizontalSpacing = 5;
layout.verticalSpacing = 5;
parent.setLayout(layout);
Section section = toolkit.createSection(parent, Section.TWISTIE|Section.TITLE_BAR);
Composite compo = toolkit.createComposite(section, SWT.NONE);
ColumnLayout layoutSection = new ColumnLayout();
compo.setLayout(layoutSection);
section.setClient(compo);
section.setExpanded(true);
// 文本框内容变动监听器
TxtChangeListener listener = new TxtChangeListener();
Text txtName = toolkit.createText(compo, "Mike", SWT.NONE);
ColumnLayoutData td = new ColumnLayoutData();
txtName.setLayoutData(td);
txtName.addModifyListener(listener);
Text txtAge = toolkit.createText(compo, "22", SWT.NONE);
td = new ColumnLayoutData();
txtAge.setLayoutData(td);
txtAge.addModifyListener(listener);
Text txtSex = toolkit.createText(compo, "m", SWT.NONE);
td = new ColumnLayoutData();
txtSex.setLayoutData(td);
txtSex.addModifyListener(listener);
}
// 制作监听器 ,这里是对Text进行监听使用ModifyListener
//如果是TextField(awt包下)之类的有DocumentListener,此处不再赘述
class TxtChangeListener implements ModifyListener {
@Override
public void modifyText(ModifyEvent e) {
System.out.println("ok");
isDirty = true;
ZTestEditor.this.firePropertyChange(PROP_DIRTY);
}
}
@Override
public void setFocus() {
}
}