📄 checkupdateshell.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq.ui;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
/**
* <pre>
* 检查是否有更新的窗口
* 在资源文件中,保存着当前版本的最后修改时间。在服务器上,有一个update.txt文件,
* 保存着最新版本的修改时间,通过比较这两个时间,来判断当前版本是否最新。时间的格式
* 为yyyy-mm-dd hh-mm
* 后面的每一行用yyyy-mm-dd开头,表示一个修正
* 如果用*开头,表示说明文字
* </pre>
*
* @author 马若劼
*/
public class CheckUpdateShell extends ShellAdapter implements Runnable {
// Log对象
private static Log log = LogFactory.getLog(CheckUpdateShell.class);
private Shell shell;
private MainShell main;
private Display display;
private Text textInfo;
private Label lblHint;
private Button btnInfo, btnCancel, btnIKnow;
private Thread thread;
private IconHolder icons = IconHolder.getInstance();
// 修正信息buffer
private StringBuffer infoBuffer;
// shell缺省宽高
private static final int WIDTH = 328;
private static final int HEIGHT = 150;
/**
* 构造函数
* @param main
*/
public CheckUpdateShell(MainShell main) {
this.main = main;
this.display = main.getDisplay();
shell = new Shell(this.display, SWT.MIN | SWT.CLOSE | SWT.BORDER);
shell.setImage(icons.getImage(IconHolder.icoUpdate));
shell.setText(LumaQQ.getString("check.update.title"));
shell.setSize(WIDTH, HEIGHT);
// 添加listener
shell.addShellListener(this);
// 初始化变量
infoBuffer = new StringBuffer();
// 初始化界面布局
initLayout();
}
/**
* 初始化界面布局
*/
private void initLayout() {
// 设置shell的layout
GridLayout layout = new GridLayout();
layout.marginHeight = layout.marginWidth = 0;
layout.verticalSpacing = layout.horizontalSpacing = 5;
layout.numColumns = 3;
layout.makeColumnsEqualWidth = true;
shell.setLayout(layout);
// 图片标签
Label bar = new Label(shell, SWT.NONE);
bar.setImage(icons.getImage(IconHolder.bmpCheckUpdate));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.heightHint = bar.getImage().getBounds().height;
gd.horizontalSpan = layout.numColumns;
bar.setLayoutData(gd);
// 提示标签
lblHint = new Label(shell, SWT.LEFT | SWT.WRAP);
lblHint.setText(LumaQQ.getString("check.update.label.checking"));
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = layout.numColumns;
gd.horizontalIndent = 10;
lblHint.setLayoutData(gd);
// 查看信息按钮
btnInfo = new Button(shell, SWT.PUSH);
btnInfo.setText(LumaQQ.getString("check.update.button.show.info"));
btnInfo.setEnabled(false);
btnInfo.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
btnInfo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent r) {
if(textInfo.isVisible())
hideInfo();
else
showInfo();
}
});
// 取消按钮
btnCancel = new Button(shell, SWT.PUSH);
btnCancel.setText(LumaQQ.getString("check.update.button.cancel"));
btnCancel.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
btnCancel.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent r) {
close();
}
});
// 我知道了按钮
btnIKnow = new Button(shell, SWT.PUSH);
btnIKnow.setText(LumaQQ.getString("check.update.button.i.know"));
btnIKnow.setEnabled(false);
btnIKnow.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
btnIKnow.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent r) {
close();
}
});
// 修正信息文本框
textInfo = new Text(shell, SWT.MULTI | SWT.V_SCROLL | SWT.BORDER | SWT.READ_ONLY | SWT.WRAP);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.heightHint = 0;
gd.horizontalSpan = layout.numColumns;
textInfo.setLayoutData(gd);
textInfo.setVisible(false);
}
/**
* 显示修正信息
*/
private void showInfo() {
btnInfo.setText(LumaQQ.getString("check.update.button.hide.info"));
textInfo.setText(infoBuffer.toString());
GridData gd = (GridData)textInfo.getLayoutData();
gd.heightHint = 200;
textInfo.setVisible(true);
shell.setSize(WIDTH, HEIGHT + 200);
shell.layout();
}
/**
* 隐藏修正信息
*/
private void hideInfo() {
btnInfo.setText(LumaQQ.getString("check.update.button.show.info"));
GridData gd = (GridData)textInfo.getLayoutData();
gd.heightHint = 0;
textInfo.setVisible(false);
shell.setSize(WIDTH, HEIGHT);
shell.layout();
}
/**
* 关闭窗口
*/
private void close() {
if(thread != null) {
thread.interrupt();
thread = null;
}
shell.close();
}
/**
* 打开shell
*/
public void open() {
// 打开shell
shell.layout();
// set dialog to center of screen
Rectangle shellRect = shell.getBounds();
Rectangle displayRect = display.getBounds();
shell.setLocation((displayRect.width - shellRect.width) / 2, (displayRect.height - shellRect.height) / 2);
// set dialog to center of screen
shell.open();
// 开始检查
thread = new Thread(this);
thread.setName("Check Update");
thread.setDaemon(true);
thread.start();
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.ShellListener#shellClosed(org.eclipse.swt.events.ShellEvent)
*/
public void shellClosed(ShellEvent e) {
main.getShellRegistry().deregisterCheckUpdateShell();
}
/**
* 设置提示信息
* @param hint
*/
private void setHint(String hint) {
lblHint.setText(hint);
}
/**
* 操作失败,设置各控件状态
* @param msg
*/
private void fail(final String msg) {
display.syncExec(new Runnable() {
public void run() {
try {
setHint(LumaQQ.getString("check.update.label.error", new Object[] { msg }));
btnCancel.setText(LumaQQ.getString("check.update.button.close"));
btnCancel.setEnabled(true);
btnInfo.setEnabled(false);
btnIKnow.setEnabled(false);
} catch(SWTException e) {
// 没什么要做的
}
}
});
}
/**
* 操作成功,当前版本已经最新,设置各控件状态
*/
private void latest() {
display.syncExec(new Runnable() {
public void run() {
try {
setHint(LumaQQ.getString("check.update.label.latest"));
btnCancel.setEnabled(false);
btnIKnow.setEnabled(true);
btnInfo.setEnabled(false);
} catch(SWTException e) {
// 没什么要做的
}
}
});
}
/**
* 操作成功,当前版本不是最新,设置各控件状态
*/
private void old() {
display.syncExec(new Runnable() {
public void run() {
try {
setHint(LumaQQ.getString("check.update.lable.old"));
btnCancel.setEnabled(false);
btnIKnow.setEnabled(true);
btnInfo.setEnabled(true);
} catch(SWTException e) {
// 没什么要做的
}
}
});
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// 得到当前版本时间,包含了日月年小时分
String timeTag = LumaQQ.getString("update.time");
log.debug("当前版本修改时间 " + timeTag);
BufferedReader br = null;
try {
// 打开输入流,读取服务器上的文件
URL url = new URL("http://lumaqq.linuxsir.org/update/update.txt");
br = new BufferedReader(new InputStreamReader(url.openStream(), "GBK"));
// 读取最新版本修改时间
String latestTag = br.readLine();
log.debug("最新版本修改时间 " + latestTag);
// 比较,如果已经是最新,返回,如果不是,读取修正列表
if(timeTag.equals(latestTag)) {
latest();
} else {
// 得到不包含小时分的当前版本时间
timeTag = timeTag.substring(0, timeTag.indexOf(' '));
// 开始读取更新信息文件的其他部分
String s = null;
// 跳过两行,做为保留用途
br.readLine();
br.readLine();
// 开始读取数据
while((s = br.readLine()) != null) {
if(s.startsWith("*")) {
/* 星号开头的是说明文字,保留 */
infoBuffer.append(s);
infoBuffer.append(System.getProperty("line.separator"));
} else {
// 得到这条修正的更新日期
String modifyTag = s.substring(0, s.indexOf(' '));
// 如果当前版本时间小于等于这条修正的时间,添加。如果大于,跳出循环,因为
// 后面的修正都是当前版本已经有的了,不需要显示
if(timeTag.compareTo(modifyTag) <= 0) {
infoBuffer.append(s);
infoBuffer.append(System.getProperty("line.separator"));
} else
break;
}
}
old();
}
thread = null;
} catch (Exception e) {
fail(e.getMessage());
thread = null;
} finally {
try {
if(br != null)
br.close();
} catch (IOException e1) {
log.error(e1.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -