- 浏览: 1126717 次
- 性别:
- 来自: 火星郊区
博客专栏
-
OSGi
浏览量:0
文章分类
- 全部博客 (695)
- 项目管理 (48)
- OSGi (122)
- java (79)
- Vaadin (5)
- RAP (47)
- mysql (40)
- Maven (22)
- SVN (8)
- 孔雀鱼 (10)
- hibernate (9)
- spring (10)
- css (3)
- 年审 (6)
- ant (1)
- jdbc (3)
- FusionCharts (2)
- struts (4)
- 决策分析 (2)
- 生活 (10)
- 架构设计 (5)
- 破解 (2)
- 狼文化 (4)
- JVM (14)
- J2EE (1)
- 应用服务器 (1)
- 我的链接 (5)
- 数学 (2)
- 报表 (1)
- 百科 (6)
- Flex (7)
- log4j (2)
- PHP (1)
- 系统 (2)
- Web前端 (7)
- linux (6)
- Office (1)
- 安全管理 (5)
- python (2)
- dom4j (1)
- 工作流 (3)
- 养生保健 (4)
- Eclipse (8)
- 监控开发 (1)
- 设计 (3)
- CAS (1)
- ZK (41)
- BluePrint (3)
- 工具 (1)
- SWT (7)
- google (2)
- NIO (1)
- 企业文化 (2)
- Windoes (0)
- RCP (7)
- JavaScript (10)
- UML (1)
- 产品经理 (2)
- Velocity (10)
- C (1)
- 单元测试 (1)
- 设计模式 (2)
- 系统分析师 (2)
- 架构 (4)
- 面试 (2)
- 代码走查 (1)
- MongoDB (1)
- 企业流程优化 (1)
- 模式 (1)
- EJB (1)
- Jetty (1)
- Git (13)
- IPV6 (1)
- JQuery (8)
- SSH (1)
- mybatis (10)
- SiteMesh (2)
- JSTL (1)
- veloctiy (1)
- Spring MVC (1)
- struts2 (3)
- Servlet (1)
- 权限管理 (1)
- Java Mina (1)
- java 系统信息 (6)
- OSGi 基础 (3)
- html (1)
- spring--security (6)
- HTML5 (1)
- java爬虫搜索 (1)
- mvc (3)
最新评论
-
Tom.X:
http://osgia.com/
将web容器置于OSGi框架下进行web应用的开发 -
chenyuguxing:
你好, 为什么我的bundle export到felix工程中 ...
在Apache Felix中运行bundle -
string2020:
<niceManifest>true</ni ...
Bundle Plugin for Maven -
jsonmong:
OSGI,是未来的主流,目前已相当成熟。应用OSGI比较好的, ...
基于OSGi的声明式服务 -
zyhui98:
貌似是翻译过来的,有很少人在linux上做开发吧
如何成为“10倍效率”开发者
是不是觉得Windows的倒计时关机对话框很酷?
其实你也可以通过SWT的对话框来实现这个功能,如果你真的够无聊的话,可以用来吓唬一下你的用户,当然你的确定你的人缘很好才行.
好了,开场白到此为止,下面进入正题:
<!--endfragment--> 我设计这个倒计时对话框主要是为了让我们系统在自动更新完成后自动重启,我们的系统提供了自动更新的功能,而且有个数据传输模块,数据传输模块是使用Wrapper这个工具封装Java程序成服务,数据传输模块会共享RCP客户端的插件Jar包,由于Eclipse插件的特殊性,自动更新完成后可能会在安装目录中存在同名称但不同版本号的插件,具体细节省去7443个字节。
想实现倒计时对话框,我们可以使用Timer这个定时工具类,这主要考虑到倒计时对话框要和RCP项目的主线程区分开来,对话框弹出不能够影响用户的操作,至少得让他们的有时间保存信息不至于丢失,我们把这个类命名为 TimerMessageDialog .java, 根据MessageDialog中提供的方法,我们无法动态显示提示信息,比如还剩下多少时间.你可以重载MessageDialog的 createMessageArea(Composite composite) 方法,可以再TimerMessageDialog中新增一个字段引用 messageLabel ,比如 localMessageLabel ,然后对 localMessageLabel 的值进行修改,下面是我的实现,可以参考一下:
因为我是在RCP的非UI现在调用这个对话框,所以需要在Display的asyncExec方法启用一个新的异步线程.这样就不会抛出非法的线程访问异常了。
看下效果:
下面是完整的类
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Timer;
import java.util.TimerTask;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
/**
* 定时执行对话框 。
* @author 刘尧兴
* <p>2009-12-7</p>
*/
public class TimerMessageDialog extends MessageDialog2 {
/** 标签文本 */
protected static Label localMessageLabel;
/** 属性修改支持实现类 */
private PropertyChangeSupportImpl instance = new PropertyChangeSupportImpl();
/** 定时器 */
private Timer timer;
/**
* 构造函数。
* @param parentShell
* @param dialogTitle
* @param dialogMessage
*/
public TimerMessageDialog(Shell parentShell, String dialogTitle, String dialogMessage) {
super (parentShell, dialogTitle, null , dialogMessage, INFORMATION, new String[] { " 立即重启 " , " 隐藏 " }, 0 );
setShellStyle(SWT.BORDER | SWT.MIN);
}
protected Control createMessageArea(Composite composite) {
// create composite
// create image
Image image = getImage();
if (image != null ) {
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
// addAccessibleListeners(imageLabel, image);
GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING)
.applyTo(imageLabel);
}
// create message
if (message != null ) {
messageLabel = new Label(composite, getMessageLabelStyle());
TimerMessageDialog.localMessageLabel = messageLabel;
messageLabel.setText(message);
GridDataFactory
.fillDefaults()
.align(SWT.FILL, SWT.BEGINNING)
.grab( true , false )
.hint(
convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
SWT.DEFAULT).applyTo(messageLabel);
}
return composite;
}
/**
* 打开定时关闭对话框,顾名思义,就是在指定的时间自动关闭 。
* @author 刘尧兴
* @param second
* @param message
*/
public void open( final int second, final String message) {
setBlockOnOpen( false );
open();
// final MessageFormat format = new MessageFormat("系统检查到重要插件的更新,系统将会在 {0} 秒后自动重启,请注意保存文件");
timer = new Timer( " 检查更新 " );
final TimerTask task = new TimerTask() {
int count = second;
public void run() {
Display display = Display.getDefault();
if (display == null ) {
return ;
}
display.asyncExec( new Runnable() {
public void run() {
try {
if (count == 0 ) {
cancel();
buttonPressed(Window.OK);
} else {
count -= 1 ;
if ( ! localMessageLabel.isDisposed() && localMessageLabel != null ) {
localMessageLabel.setText(message + " 系统将在: " + count + " 秒后自动重启 " );
// localMessageLabel.setText(format.format(new Object[]{new Integer(count)}));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(task, 1000 , 1000 );
}
protected void buttonPressed( int buttonId) {
if (buttonId == Window.OK) {
timer.cancel();
firePropertyChange(IPropery.RESTART_OK_PRESSED, false , true );
} else if (buttonId == Window.CANCEL) {
close();
}
}
/**
* 添加属性事件 。
* @author 刘尧兴
* @param l
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
instance.addPropertyChangeListener(l);
}
/**
* 删除属性修改事件 。
* @author 刘尧兴
* @param l
*/
public void removePropertyChangeListener(PropertyChangeListener l) {
instance.removePropertyChangeListener(l);
}
/**
* 通知属性修改事件(通知前台代码修改) 。
* @author 刘尧兴
* @param prop
* @param old
* @param newValue
*/
public void firePropertyChange(String prop, Object old, Object newValue) {
instance.firePropertyChange(prop, old, newValue);
}
/**
* 属性修改支持实现类 。
* @author 刘尧兴
* <p>2009-8-18</p>
*/
public class PropertyChangeSupportImpl {
/** 属性修改支持 */
PropertyChangeSupport listeners = new PropertyChangeSupport( this );
/**
* 添加属性修改事件 。
* @author 刘尧兴
* @param l
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
listeners.addPropertyChangeListener(l);
}
/**
* 通知修改事件发生 。
* @author 刘尧兴
* @param prop
* @param old
* @param newValue
*/
public void firePropertyChange(String prop, Object old, Object newValue) {
listeners.firePropertyChange(prop, old, newValue);
}
/**
* 通知事件修改 。
* @author 刘尧兴
* @param prop
* @param child
*/
protected void fireStructureChange(String prop, Object child) {
listeners.firePropertyChange(prop, null , child);
}
/**
* 删除属性修改事件 。
* @author 刘尧兴
* @param l
*/
public void removePropertyChangeListener(PropertyChangeListener l) {
listeners.removePropertyChangeListener(l);
}
}
}
// create composite
// create image
Image image = getImage();
if (image != null ) {
imageLabel = new Label(composite, SWT.NULL);
image.setBackground(imageLabel.getBackground());
imageLabel.setImage(image);
// addAccessibleListeners(imageLabel, image);
GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING)
.applyTo(imageLabel);
}
// create message
if (message != null ) {
messageLabel = new Label(composite, getMessageLabelStyle());
TimerMessageDialog.localMessageLabel = messageLabel;
messageLabel.setText(message);
GridDataFactory
.fillDefaults()
.align(SWT.FILL, SWT.BEGINNING)
.grab( true , false )
.hint(
convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
SWT.DEFAULT).applyTo(messageLabel);
}
return composite;
}
然后我们添加一个打开对话框的方法: open(final int second,final String message) ,second表示倒计时时间,message表示提示信息,在open方法中新建一个 Timer 对象。对了,为了让MessageDialog弹出后不阻塞线程的执行,需要在调用对话框的open方法之前调用 setBlockOnOpen(false); 方法,下面是我的实现,大家可以参考一下:
* 打开定时关闭对话框,顾名思义,就是在指定的时间自动关闭 。
* @author 刘尧兴
* @param second
* @param message
*/
public void open( final int second, final String message) {
setBlockOnOpen( false );
open();
// final MessageFormat format = new MessageFormat("系统检查到重要插件的更新,系统将会在 {0} 秒后自动重启,请注意保存文件");
timer = new Timer( " 检查更新 " );
final TimerTask task = new TimerTask() {
int count = second;
public void run() {
Display display = Display.getDefault();
if (display == null ) {
return ;
}
display.asyncExec( new Runnable() {
public void run() {
try {
if (count == 0 ) {
cancel();
buttonPressed(Window.OK);
} else {
count -= 1 ;
if ( ! localMessageLabel.isDisposed() && localMessageLabel != null ) {
localMessageLabel.setText(message + " 系统将在: " + count + " 秒后自动重启 " );
// localMessageLabel.setText(format.format(new Object[]{new Integer(count)}));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
};
timer.schedule(task, 1000 , 1000 );
}
这里的Timer是每一秒钟执行一次,在定时器执行完成后应该让对话框自动关闭,然后可以通知事件发起人来执行对应的操作,从这样设计的角度上看,我们的需要在用户点击确认或者取消或者关闭对话框时响应用户的操作,这样我们的重载父类的 buttonPressed(int buttonId) 方法,在这个方法中执行对应的操作.比如这样:
if (buttonId == Window.OK) {
timer.cancel();
firePropertyChange(IPropery.RESTART_OK_PRESSED, false , true );
} else if (buttonId == Window.CANCEL) {
close();
}
}
这里我使用了java.util包中的
PropertyChangeListener
来监听属性值的修改。这里的对话框应该算是事件源,由这个发起属性事件的修改通知,其他监听类根据接收到的信息作出相应的响应。我在TimerMessageDialog实现了一个内部类:
PropertyChangeSupportImpl
,这个类管理通知的对象:
<!--endfragment-->
* 属性修改支持实现类 。
* @author 刘尧兴
* <p>2009-8-18</p>
*/
public class PropertyChangeSupportImpl {
/** 属性修改支持 */
PropertyChangeSupport listeners = new PropertyChangeSupport( this );
/**
* 添加属性修改事件 。
* @author 刘尧兴
* @param l
*/
public void addPropertyChangeListener(PropertyChangeListener l) {
listeners.addPropertyChangeListener(l);
}
/**
* 通知修改事件发生 。
* @author 刘尧兴
* @param prop
* @param old
* @param newValue
*/
public void firePropertyChange(String prop, Object old, Object newValue) {
listeners.firePropertyChange(prop, old, newValue);
}
/**
* 通知事件修改 。
* @author 刘尧兴
* @param prop
* @param child
*/
protected void fireStructureChange(String prop, Object child) {
listeners.firePropertyChange(prop, null , child);
}
/**
* 删除属性修改事件 。
* @author 刘尧兴
* @param l
*/
public void removePropertyChangeListener(PropertyChangeListener l) {
listeners.removePropertyChangeListener(l);
}
}
在完成这些之后,在监听类中注册一个属性修改的事件,比如在UpdatePlan这个类中执行TimerMessageDialog的打开操作,那就的让UpdatePlan这个类实现 PropertyChangeListener 这个接口并实现 propertyChange(final PropertyChangeEvent evt) 这个方法,这个方法可以根据传过来的属性名称和属性值执行相应的操作。UpdatePlan属性注册方式是:
@Override
public void run() {
try {
TimerMessageDialog dialog = new TimerMessageDialog(Display.getDefault().getActiveShell(), " 系统信息 " , " 系统将要自动重启 " );
dialog.addPropertyChangeListener( this );
dialog.open( 30 , "" );
}
catch (Exception e) {
e.printStackTrace();
}
}
});
发表评论
-
Eclicpse3.1.1下配置SWT,打包发布SWT程序,转EXE一条龙过程记录
2012-04-01 09:29 14841.到www.eclipse.org上下载SWT. 我 ... -
通过TreeColumn实现“表格树”TableTree
2012-04-01 09:28 2163Eclipse 3.1里deprecate了TableT ... -
SWT---资源---第一!
2012-03-31 10:16 885使用SWT做开发的朋友,一定要注意~资源是一个杀手!对于我们这 ... -
所支持拖拽功能的Swing-JTree代码
2012-03-31 10:01 1009备份原型 -
SWT对话框系列:让对话框支撑方向键选择
2012-02-09 08:04 1146在SWT 3.3中弹出的对话框比如确认对话框,可以通过Tab键 ... -
使用SWT处理图片的大小和分辨率
2012-02-09 08:04 1306以前研究过AWT的图像处理,知道AWT在图像处理方面的功力 ...
相关推荐
SWT的MessageBox对话框 SWT有不同类型的对话框。有些对话框具有特殊的属性。 MessageBox messageBox = new MessageBox(shell, SWT.OK|SWT.CANCEL); if (messageBox.open() == SWT.OK) { System.out.println(...
java中使用SWT折叠菜单的做法,十分好的学习资料
此 repo 包含用于 Python 3 的 libccv 的 SWT 算法的包装器。它与 Linux、macOS 和 Docker 兼容。 有关更多信息,请参阅我的博文:http: 用法 from swt_python3 import swt buffer = open ( "input.jpg" , "rb" )...
java的swing实现计时与倒计时,可输入起始时间与终止时间(包括时、分、秒),多线程进行计时和倒计时
SWT-AirlineReservationSystem:软件测试项目
主要讲述了Jface向导式对话框的编写及代码。能够正确的运行在Eclipse中,希望能帮有所帮助
用Eclipse SWT 实现的动态背景对话框的源码,动态背景为黑客帝国字符雨效果,极为炫酷。高端大气且上档次。
SWT examples,SWT官方例子:http://www.eclipse.org/swt/examples.php 该页面所要下载的例子 其中,swt的例子在\plugins\org.eclipse.swt.examples.source_3.7.0.v3740f.jar中
开发实例Designer SWT JFace for Eclipse
android-swt-演示演示如何使用桌面 Java 和 SWT 显示 Android 自定义绘制视图的原型这个原型包括两个项目: DrawIt - 一个非常简单的 Android 应用程序,其中包含一个自定义绘制的视图 (TestView) AndroidPaint - 一...
SWT 的 PlotControl PlotControl 是一个简单的 SWT 小部件,用于绘制折线图。 它包括填充和描边合成曲线、处理正负 x/y 值、缩放以适应小部件空间以及标准或 MRTG 样式图的功能。 PlotControl 在 MIT 许可下可用。...
swt、jface对话框
java SWT 对话框 table 添加 删除 修改 查看信息 menu栏操作 右键点击查看
第4章 使用基本控件与对话框.pdf 第4章 使用基本控件与对话框.pdf eclipse插件开发笔记,使用基本控件与对话框
支持功能有: 1. 跨行和跨列功能 2. 页码和页数统计 3. 边距和间距调整 4. 各边框颜色设置 5. 前景和背景颜色 6. 自适应页面大小 .........一个是SWT Report的项目包,另一个是CustomReportTest 类生成的报表
Scala SWT 构建器 DSL 该项目试图为 SWT GUI 创建 DSL。 版本 该项目正在开发中(尚未准备好使用)。 它目前支持创建大多数 SWT 小部件。 我也想加入事件处理(目前只实现了onSelect )。 安装(SBT 0.13.6+) 在 ...
SWT 选择对话框 该项目为 SWT 实现了一个对话框,用户可以在其中选择多个选项之一。 它的灵感来自于提供的 DialogBox。 与其实现相反,该项目提供的对话框使用各自平台的本机小部件,并且支持键盘交互。 例子 此基本...
JAVA.SWT/JFace: 按键与其对应的常量表、KeyEvent事件比较、VerifyEvent事件比较
swt&jface英文教程 书名:The Definitive Guide to SWT and JFace by:by Robert Harris and Rob Warner
在swt开发中实现的一个小小的日历对话框,与文本框共同使用的,可以供大家参考,