你link的常見問題回答說:
另一種機制,堅持視圖狀態是JFace的IDialogSettings設施。對話框設置優於視圖保存/初始化機制的優點是,您可以控制何時保持設置。
這是一個關於如何使用IDialogSettings堅持一個IntroPart的狀態,當它被關閉,以及如何創建時,以恢復它的一個示例。 MyIntroPart是一個具有Text
小部件的IntroPart。顯示的文本在IntroPart關閉時保存,並在創建時恢復。
要獲取partClosed
事件,MyIntroPart會實現IPartListener2並向PartService註冊自己。
private static final String MY_INTRO_SETTINGS = "my_intro_settings";
private static final String MY_INTRO_TEXT_KEY = "my_intro_text";
@Override
public void createPartControl(Composite parent) {
this.text = new Text(parent, SWT.BORDER);
// try to load the settings
IDialogSettings settings = Activator.getDefault().getDialogSettings();
IDialogSettings section = settings.getSection(MyIntroPart.MY_INTRO_SETTINGS);
if (section != null) {
// set the restored text string in the text widget
this.text.setText(section.get(MyIntroPart.MY_INTRO_TEXT_KEY));
}
// register the part listener
getIntroSite().getWorkbenchWindow().getPartService().addPartListener(this);
}
這會將文本恢復到文本小部件。
MyIntroPart實現IPartListener2的partClosed
當視圖被關閉,以節省對話框設置:
@Override
public void partClosed(IWorkbenchPartReference partRef) {
// remove part listener from part service
getIntroSite().getWorkbenchWindow().getPartService().removePartListener(this);
IDialogSettings settings = Activator.getDefault().getDialogSettings();
// get the section of the text
IDialogSettings section = settings.getSection(MyIntroPart.MY_INTRO_SETTINGS);
// if it doesn't exist, create it
if (section == null) {
section = settings.addNewSection(MyIntroPart.MY_INTRO_SETTINGS);
}
// put text from text field in section
section.put(MyIntroPart.MY_INTRO_TEXT_KEY, this.text.getText());
}
感謝弗雷德裏克用於指出沒有加載從或保存到一個文件,這裏需要。
調用'saveState'方法? – tkotisis
調用'saveState'would是一個偉大的想法,如果你弄清楚如何獲得用於保留在工作臺 – benez