给自定义Dialog加入保留对话框值的功能

给自定义Dialog加入保留对话框值的功能

有的时候我们需要这样的功能,记下Dialog中的值,当再次打开这个Dialog的时候,还原这些值。这就需要把这些Dialog的值保存起来。Dialog的IDialogSettings类提供了这个功能。下边是使用方法:
在上例"自定义Dialog"的代码上加入三个函数:
ExpandedBlockStart.gif ContractedBlock.gif public   void  saveState() dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (text.getText() == null || text.getText().equals(""))dot.gif{
InBlock.gif            
return ;
ExpandedSubBlockEnd.gif        }

InBlock.gif        IDialogSettings topSettings 
= getTopSettings();
InBlock.gif        IDialogSettings settings 
=  topSettings.getSection("TestDialog");
InBlock.gif        
if(settings == null)settings = topSettings.addNewSection("TestDialog");
InBlock.gif        settings.put(
"value", text.getText());
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            topSettings.save(
"content/system.xml");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(IOException e)dot.gif{
InBlock.gif            System.out.println(e.getMessage());
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif    
public   void  restoreState() dot.gif {
InBlock.gif        IDialogSettings topSettings 
= getTopSettings();
InBlock.gif        IDialogSettings settings 
=  topSettings.getSection("TestDialog");
InBlock.gif        
if(settings == nullreturn;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (text.getText() == null || text.getText().equals(""))dot.gif{
InBlock.gif            text.setText(settings.get(
"value"));
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

ExpandedBlockStart.gifContractedBlock.gif    
public  IDialogSettings getTopSettings() dot.gif {
InBlock.gif        IDialogSettings topSettings 
= new DialogSettings("system");
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            topSettings.load(
"content/system.xml");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(IOException e)dot.gif{
InBlock.gif            System.out.println(e.getMessage());
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return topSettings;
ExpandedBlockEnd.gif    }

然后在覆写的buttonPressed函数中调用save
ExpandedBlockStart.gif ContractedBlock.gif protected   void  buttonPressed( int  button) dot.gif {
InBlock.gif        saveState();
ExpandedBlockEnd.gif    }
在createDialogArea函数中加入取出数据
ExpandedBlockStart.gif ContractedBlock.gif protected  Control createDialogArea(Composite parent)  dot.gif {
InBlock.gif        Composite container 
= (Composite) super.createDialogArea(parent);
InBlock.gif        container.setLayout(
new RowLayout());
InBlock.gif        text 
= new Text(container, SWT.BORDER);
InBlock.gif        text.setLayoutData(
new RowData(100,-1));
InBlock.gif        
//加入这一句
ExpandedSubBlockStart.gifContractedSubBlock.gif
        if (text.getText() == null || text.getText().equals(""))dot.gif{
InBlock.gif            restoreState();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return container;
ExpandedBlockEnd.gif    }
最后,记得建立需要的文件,在当前workspace下建立文件夹content,然后在文件夹下建立system.xml文件。当然你也可以利用程序来实现。
运行一下看看吧,是不是记住了上次填入的内容.
SourceCODE

转载于:https://www.cnblogs.com/kentyshang/archive/2007/08/16/858307.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值