浏览 1560 次
锁定老帖子 主题:给自定义Dialog加入保留对话框值的功能
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-02-23
有的时候我们需要这样的功能,记下Dialog中的值,当再次打开这个Dialog的时候,还原这些值。这就需要把这些Dialog的值保存起来。Dialog的IDialogSettings类提供了这个功能。下边是使用方法:
public void saveState(){
if (text.getText() == null || text.getText().equals("")){ return ; } IDialogSettings topSettings = getTopSettings(); IDialogSettings settings = topSettings.getSection("TestDialog"); if(settings == null)settings = topSettings.addNewSection("TestDialog"); settings.put("value", text.getText()); try{ topSettings.save("content/system.xml"); }catch(IOException e){ System.out.println(e.getMessage()); } } public void restoreState(){ IDialogSettings topSettings = getTopSettings(); IDialogSettings settings = topSettings.getSection("TestDialog"); if(settings == null) return; if (text.getText() == null || text.getText().equals("")){ text.setText(settings.get("value")); } } public IDialogSettings getTopSettings(){ IDialogSettings topSettings = new DialogSettings("system"); try{ topSettings.load("content/system.xml"); }catch(IOException e){ System.out.println(e.getMessage()); } return topSettings; }
protected void buttonPressed(int button){
saveState(); } 在createDialogArea函数中加入取出数据
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent); container.setLayout(new RowLayout()); text = new Text(container, SWT.BORDER); text.setLayoutData(new RowData(100,-1)); //加入这一句 if (text.getText() == null || text.getText().equals("")){ restoreState(); } return container; } 最后,记得建立需要的文件,在当前workspace下建立文件夹content,然后在文件夹下建立system.xml文件。当然你也可以利用程序来实现。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |