zoukankan
html css js c++ java
给自定义Dialog加入保留对话框值的功能
给自定义Dialog加入保留对话框值的功能
有的时候我们需要这样的功能,记下Dialog中的值,当再次打开这个Dialog的时候,还原这些值。这就需要把这些Dialog的值保存起来。Dialog的IDialogSettings类提供了这个功能。下边是使用方法:
在上例"自定义Dialog"的代码上加入三个函数:
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;
}
然后在覆写的buttonPressed函数中调用save
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文件。当然你也可以利用程序来实现。
运行一下看看吧,是不是记住了上次填入的内容.
SourceCODE
查看全文
相关阅读:
iOS设计模式:观察者
Java面向接口编程小例子
《The DeadLine》(《最后期限》) 读后感
Codeforces Round #395 Div1的A题Timofey and a tree
重写和强制转换再调用能编译但不能运行
Java继承和静态-加载顺序
C++之pair
用Java面向对象思想实现一个微博的功能(未完)
Java对象在内存图示
Java中OOP对象和引用
原文地址:https://www.cnblogs.com/kentyshang/p/858307.html
最新文章
136. Single Number
leetcode水题题解
leetcode水题题解
338. Counting Bits
338. Counting Bits
如何使用C#操作快捷方式(获取快捷方式属性、创建快捷方式)
使用c#检测文件正在被那个进程占用
解决EnableVisualStyles Bug
JS自定义属性的设置与获取
C# 多线程基础
热门文章
C#方法解析
06-IOSCore
05-IOSCore
04-IOSCore
03-IOSCore
02-IOSCore
01-IOSCore
IOS Notification 通知中心
一些Xcode 5的使用提示和技巧
iOS KVC & KVO
Copyright © 2011-2022 走看看