有两种设置编辑器Dirty状态的策略:
1. 只要有修改,就通知Editor Dirty,这样,即便是把 0改为1,又改回为0,编辑器的状态也是Dirty
2. 修改后对比初始数据是否与修改后的不同,只有不同,才通知编辑器 Dirty,相同时,通知编辑器 没有Dirty
我们用第二种方式:
1. 我们在setInput的时候,在各个需要有可能改变EditorDirty状态的控件上保存初始数据,添加一个对象保存修改的数据dirtyFields,以后可以用来判断是否dirty
代码
public void setInput(LazyDTO project) {
txNumber.setText(project.getText("md$number"));
addtoDirtyControl(txNumber,"md$number",project.getText("md$number"));
...
private void addtoDirtyControl(Widget widget, String fieldname, Object initValue) {
widget.setData(INITDATA,initValue);
if(widget instanceof Text){
((Text)widget).addModifyListener(new ModifyListener(){
@Override
public void modifyText(ModifyEvent e) {
Text text = (Text)e.getSource();
Object initData = text.getData(INITDATA);
String fieldname = (String) text.getData(FIELDNAME);
String newData = text.getText();
if(newData.equals(initData)){
dirtyFields.remove(fieldname);
}else{
dirtyFields.put(fieldname, new Object[]{initData,newData});
}
mform.dirtyStateChanged();
}
});
}
}
txNumber.setText(project.getText("md$number"));
addtoDirtyControl(txNumber,"md$number",project.getText("md$number"));
...
private void addtoDirtyControl(Widget widget, String fieldname, Object initValue) {
widget.setData(INITDATA,initValue);
if(widget instanceof Text){
((Text)widget).addModifyListener(new ModifyListener(){
@Override
public void modifyText(ModifyEvent e) {
Text text = (Text)e.getSource();
Object initData = text.getData(INITDATA);
String fieldname = (String) text.getData(FIELDNAME);
String newData = text.getText();
if(newData.equals(initData)){
dirtyFields.remove(fieldname);
}else{
dirtyFields.put(fieldname, new Object[]{initData,newData});
}
mform.dirtyStateChanged();
}
});
}
}
2. FormPage类上覆盖isDirty()
代码
public class PlanPage extends FormPage {
private LazyDTO inputData;
private PlanContent pc;
public PlanPage(FormEditor editor) {
super(editor, "com.sg.intellifmea.editor.PlanPage", "概览");
inputData = ((ProjectInput) getEditor().getEditorInput()).getData();
}
@Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm form = managedForm.getForm();
toolkit.decorateFormHeading(form.getForm());
form.setText("项目基本信息以及计划");
form.setImage(inputData.getImageDescriptor().createImage());
pc = new PlanContent(managedForm);
pc.setInput(inputData);
}
@Override
public boolean isDirty() {
return pc.isDirty();
}
}
private LazyDTO inputData;
private PlanContent pc;
public PlanPage(FormEditor editor) {
super(editor, "com.sg.intellifmea.editor.PlanPage", "概览");
inputData = ((ProjectInput) getEditor().getEditorInput()).getData();
}
@Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm form = managedForm.getForm();
toolkit.decorateFormHeading(form.getForm());
form.setText("项目基本信息以及计划");
form.setImage(inputData.getImageDescriptor().createImage());
pc = new PlanContent(managedForm);
pc.setInput(inputData);
}
@Override
public boolean isDirty() {
return pc.isDirty();
}
}
3. isDirty()
public boolean isDirty() {
return !dirtyFields.isEmpty();
}
return !dirtyFields.isEmpty();
}
要注意这里实现的方法可以非常灵活。
1. 可以直接为FormEditor设置 dirty,激活dirty的方法是editorDirtyStateChanged()
2. 可以在FormPage上设置dirty, 激活dirty的方法是mform.dirtyStateChanged();
任何一种方法,client都会调用各自的isDirty(),我们在isDirty中判断,是否是dirty了
最后的工作就是保存了
这很简单,在Editor的doSave 调用 Page的doSave, 还可以加上 Monitor和进度条。
只需注意2点:
1.保存后,要激活editorDirtyStateChanged()或者mform.dirtyStateChanged();
2.保存失败怎么办