前言
前一篇文章介绍了如何自定义编辑器,这一篇介绍如何使得自定义编辑器向文本编辑器那样,如果修改了,文章标题处出现一个 * ,表示该文本已被编辑过还未保存。当保存后 * 消失。 编辑时效果如下: 关闭时提示如下: 视频效果
思路
public void changeDirtyState(boolean isDirty) {
this.isDirty = isDirty;
firePropertyChange(PROP_DIRTY);
}
public void toDirty() {
changeDirtyState(true);
}
public void clearDirty() {
changeDirtyState(false);
}
Widget 的子类可以添加addModifyListener 监听器,也可以写一个工具方法
private void addDirtyListener(Widget com) {
com.addListener(SWT.Modify, e->{
editor.toDirty();
});
}
- 对非
Widget 子类可在修改完成时改变dirty 状态,如我们可以对delete Button做如下修改:
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (dogImage != null) {
dogImage.dispose();
dogImage = null;
dogPhoto.redraw();
dogImagePath = "";
editor.toDirty();
}
}
});
最终代码
FormEditor
package com.xzbd.editors;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import com.xzbd.views.FormEditorViewer;
public class FormEditor extends EditorPart {
public static String ID = "com.xzbd.editors.FormEditor";
public boolean isDirty = false;
private FormEditorViewer viewer;
@Override
public void doSave(IProgressMonitor monitor) {
FormEditorData data = viewer.getData();
System.out.println(data);
clearDirty();
}
@Override
public void doSaveAs() {
}
@Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException {
this.setSite(site);
this.setInput(input);
}
@Override
public boolean isDirty() {
return isDirty;
}
@Override
public boolean isSaveAsAllowed() {
return false;
}
@Override
public void createPartControl(Composite parent) {
viewer = new FormEditorViewer(parent,this);
}
@Override
public void setFocus() {
}
public void changeDirtyState(boolean isDirty) {
this.isDirty = isDirty;
firePropertyChange(PROP_DIRTY);
}
public void toDirty() {
changeDirtyState(true);
}
public void clearDirty() {
changeDirtyState(false);
}
}
FormEditorViewer
package com.xzbd.views;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Widget;
import com.xzbd.editors.FormEditor;
import com.xzbd.editors.FormEditorData;
public class FormEditorViewer {
private FormEditor editor;
private Composite parent;
private Text dogName;
private Combo dogBreed;
private Canvas dogPhoto;
private Image dogImage;
private String dogImagePath;
private List categories;
private Text ownerName;
private Text ownerPhone;
public FormEditorViewer(Composite parent, FormEditor editor) {
this.parent = parent;
setEditor(editor);
buildPageUI();
}
private void buildPageUI() {
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
parent.setLayout(gridLayout);
new Label(parent, SWT.NONE).setText("Dog's Name:");
dogName = new Text(parent, SWT.SINGLE | SWT.BORDER);
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
dogName.setLayoutData(gridData);
addDirtyListener(dogName);
new Label(parent, SWT.NONE).setText("Breed:");
dogBreed = new Combo(parent, SWT.NONE);
dogBreed.setItems(new String[] { "Collie", "Pitbull", "Poodle", "Scottie" });
dogBreed.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
addDirtyListener(dogBreed);
Label label = new Label(parent, SWT.NONE);
label.setText("Categories");
label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
new Label(parent, SWT.NONE).setText("Photo:");
dogPhoto = new Canvas(parent, SWT.BORDER);
gridData = new GridData(GridData.FILL_BOTH);
gridData.widthHint = 80;
gridData.heightHint = 80;
gridData.verticalSpan = 3;
dogPhoto.setLayoutData(gridData);
dogPhoto.addPaintListener(new PaintListener() {
public void paintControl(final PaintEvent event) {
if (dogImage != null) {
event.gc.drawImage(dogImage, 0, 0);
editor.toDirty();
}
}
});
categories = new List(parent, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
categories.setItems(new String[] { "Best of Breed", "Prettiest Female", "Handsomest Male", "Best Dressed",
"Fluffiest Ears", "Most Colors", "Best Performer", "Loudest Bark", "Best Behaved", "Prettiest Eyes",
"Most Hair", "Longest Tail", "Cutest Trick" });
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.verticalSpan = 4;
int listHeight = categories.getItemHeight() * 12;
Rectangle trim = categories.computeTrim(0, 0, 0, listHeight);
gridData.heightHint = trim.height;
categories.setLayoutData(gridData);
categories.addTouchListener(e->{
editor.toDirty();
});
Button browse = new Button(parent, SWT.PUSH);
browse.setText("Browse...");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalIndent = 5;
browse.setLayoutData(gridData);
browse.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
String fileName = new FileDialog(parent.getShell()).open();
if (fileName != null) {
dogImage = new Image(parent.getDisplay(), fileName);
dogImagePath = fileName;
editor.toDirty();
}
}
});
Button delete = new Button(parent, SWT.PUSH);
delete.setText("Delete");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_BEGINNING);
gridData.horizontalIndent = 5;
delete.setLayoutData(gridData);
delete.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
if (dogImage != null) {
dogImage.dispose();
dogImage = null;
dogPhoto.redraw();
dogImagePath = "";
editor.toDirty();
}
}
});
Group ownerInfo = new Group(parent, SWT.NONE);
ownerInfo.setText("Owner Info");
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
ownerInfo.setLayout(gridLayout);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
gridData.horizontalSpan = 2;
ownerInfo.setLayoutData(gridData);
new Label(ownerInfo, SWT.NONE).setText("Name:");
ownerName = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
addDirtyListener(ownerName);
new Label(ownerInfo, SWT.NONE).setText("Phone:");
ownerPhone = new Text(ownerInfo, SWT.SINGLE | SWT.BORDER);
ownerPhone.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
addDirtyListener(ownerPhone);
Button enter = new Button(parent, SWT.PUSH);
enter.setText("Enter");
gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.horizontalSpan = 3;
enter.setLayoutData(gridData);
enter.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
System.out.println("/nDog Name: " + dogName.getText());
System.out.println("Dog Breed: " + dogBreed.getText());
System.out.println("Owner Name: " + ownerName.getText());
System.out.println("Owner Phone: " + ownerPhone.getText());
System.out.println("Categories:");
String cats[] = categories.getSelection();
for (int i = 0; i < cats.length; i++) {
System.out.println("/t" + cats[i]);
}
}
});
if (dogImage != null) {
dogImage.dispose();
}
}
public FormEditor getEditor() {
return editor;
}
public void setEditor(FormEditor editor) {
this.editor = editor;
}
private void addDirtyListener(Widget com) {
com.addListener(SWT.Modify, e->{
editor.toDirty();
});
}
public FormEditorData getData() {
String dogNameText = dogName.getText();
String dogBreedText = dogBreed.getText();
String ownerNameText = ownerName.getText();
String ownerPhoneText = ownerPhone.getText();
String[] categoriesArr = categories.getSelection();
FormEditorData data = new FormEditorData(
dogNameText,
dogBreedText,
dogImagePath,
ownerNameText,
ownerPhoneText,
categoriesArr
);
return data;
}
}
FormEditorData
package com.xzbd.editors;
import java.util.Arrays;
public class FormEditorData {
private String dogName;
private String dogBreed;
private String dogImagePath;
private String ownerName;
private String ownerPhone;
private String[] categories;
public FormEditorData(String dogName, String dogBreed, String dogImagePath,
String ownerName, String ownerPhone,
String[] categories) {
super();
this.dogName = dogName;
this.dogBreed = dogBreed;
this.dogImagePath = dogImagePath;
this.ownerName = ownerName;
this.ownerPhone = ownerPhone;
this.categories = categories;
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public String getDogBreed() {
return dogBreed;
}
public void setDogBreed(String dogBreed) {
this.dogBreed = dogBreed;
}
public String getDogImagePath() {
return dogImagePath;
}
public void setDogImagePath(String dogImagePath) {
this.dogImagePath = dogImagePath;
}
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getOwnerPhone() {
return ownerPhone;
}
public void setOwnerPhone(String ownerPhone) {
this.ownerPhone = ownerPhone;
}
public String[] getCategories() {
return categories;
}
public void setCategories(String[] categories) {
this.categories = categories;
}
@Override
public String toString() {
return "FormEditorData [dogName=" + dogName + ",\r\n dogBreed=" + dogBreed + ",\r\n dogImagePath=" + dogImagePath
+ ",\r\n ownerName=" + ownerName + ",、\r\n ownerPhone=" + ownerPhone + ",\r\n categories="
+ Arrays.toString(categories) + "]";
}
}
FormEditorData 是表单数据对象,当按下Ctrl+S 后,FormEditor 的doSave 方法会从viewer 中获取最新的值,并打印,其相关代码如下:
@Override
public void doSave(IProgressMonitor monitor) {
FormEditorData data = viewer.getData();
System.out.println(data);
clearDirty();
}
打印内容示例:
FormEditorData [dogName=中华田园犬2,
dogBreed=Pitbull,
dogImagePath=C:\Users\sun\Pictures\9958c8db-0a11-4bab-8d12-7782ed892fa1.jpg,
ownerName=张三,、
ownerPhone=1899999999,
categories=[Fluffiest Ears, Best Performer, Best Behaved]]
总结
文章讲述了如何对自定义编辑器添加 dirty 效果,以使得编辑器与文本编辑器具有相同的保存、修改行为,方便客户使用。
|