📄 onlinetipshell.java
字号:
/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 whg2001 <whg_2001@sohu.com>
* 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.util.LinkedList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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;
import edu.tsinghua.lumaqq.Colors;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.ui.config.sys.SystemOptionWindow;
import edu.tsinghua.lumaqq.ui.tool.HeadFactory;
import edu.tsinghua.lumaqq.ui.tool.UITool;
import edu.tsinghua.lumaqq.utils.OptionUtil;
/**
* 好友上线提示窗口
*
* @author 王华刚 马若劼
*/
public class OnlineTipShell {
private Shell shell;
private Display display;
private Label lblFace;
private Label lblSetup, lblUserInfo, lblClose;
private IconHolder icons;
//需要显示的好友队列
private LinkedList friendLink;
// MainShell对象
private MainShell main;
// 鼠标按下时,设置此变量为true,表示处于移动窗口状态
private boolean isMove;
// 鼠标按下时的位置
private int downX, downY;
// hand cursor
private Cursor handCursor;
// 执行检查需要显示的队列并显示的Runnable对象
private Runnable showRunnable = new Runnable() {
public void run() {
// 得到下一个要显示的好友model
FriendModel f = getNextFriendModel();
// model为null,说明没有更多的好友要显示,关闭窗口
if(f == null) {
Point p = display.getCursorLocation();
// 但是如果这个时候鼠标在窗口之上,不关闭
// 不在,则关闭
if(!shell.getBounds().contains(p))
closeNow();
else
display.timerExec(5000, this);
} else if(shell.isVisible()) {
// 显示
setFriendModel(f);
// 等五秒再调用自己一次
display.timerExec(5000, this);
}
}
};
/**
* 添加要显示的好友到队列
* @param f
*/
public void addFriendModel(FriendModel f) {
synchronized(friendLink) {
friendLink.addLast(f);
if(!shell.isVisible()) {
show();
display.asyncExec(showRunnable);
}
}
}
/**
* @return 下一个要显示的好友model,没有则返回null
*/
private FriendModel getNextFriendModel() {
synchronized(friendLink) {
if(friendLink.isEmpty())
return null;
else
return (FriendModel)friendLink.removeFirst();
}
}
/**
* 构造函数
* @param main
*/
public OnlineTipShell(MainShell main) {
// 初始化变量
this.main = main;
this.display = main.getDisplay();
isMove = false;
friendLink = new LinkedList();
icons = IconHolder.getInstance();
handCursor = display.getSystemCursor(SWT.CURSOR_HAND);
// 初始化窗口布局
initLayout();
}
/**
* 立刻关闭提示窗口
*/
private void closeNow() {
synchronized(friendLink) {
// 设置窗口为不可见
shell.setVisible(false);
// 清空可能遗留下来的model
friendLink.clear();
}
}
/**
* 打开提示窗口
*/
private void setFriendModel(FriendModel f) {
shell.setData(f);
// 设置用户信息
lblUserInfo.setText(LumaQQ.getString("onlinetip.user", new Object[] { f.getName(), String.valueOf(f.getQQ()) } ));
// 加载头像
lblFace.setImage(HeadFactory.getOnlineHead(f));
}
/**
* 打开窗口
*/
public void show() {
if(!shell.isVisible()) {
// 得到上站通知的显示位置,-1表示缺省
int x = OptionUtil.getInstance().getOnlineTipLocationX();
int y = OptionUtil.getInstance().getOnlineTipLocationY();
// 得到屏幕客户区的大小,之所以是客户区是因为任务条之类的东西不计算在内
// 不过这个在Linux下面好像没有效果
Rectangle displayRect = display.getClientArea();
Point size = shell.getSize();
// 如果有变量等于-1,采用缺省值
if(x == -1)
x = displayRect.width - size.x - 2;
if(y == -1)
y = displayRect.height - size.y - 2;
// 设置shell位置
shell.setLocation(x, y);
// 设置shell为可见
shell.setVisible(true);
// 这也是为了满足Linux,必须layout
shell.layout();
}
}
/**
* @return
* true表示窗口已经disposed
*/
public boolean isDisposed() {
return shell.isDisposed();
}
/**
* 初始化窗口布局
*/
private void initLayout() {
// 创建tip窗口
shell = new Shell(display, SWT.ON_TOP | SWT.NO_TRIM);
shell.setSize(167, 74);
shell.setLayout(new FormLayout());
shell.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(MouseEvent e) {
if (isMove) {
Point loc = shell.getLocation();
int x = loc.x + e.x - downX;
int y = loc.y + e.y - downY;
shell.setLocation(x, y);
OptionUtil.getInstance().setOnlineTipLocationX(x);
OptionUtil.getInstance().setOnlineTipLocationY(y);
}
}
});
shell.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
downX = e.x;
downY = e.y;
isMove = true;
}
public void mouseUp(MouseEvent e) {
isMove = false;
}
});
shell.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawImage(icons.getImage(IconHolder.bmpOnlineTipBackground), 0, 0);
}
});
UITool.setDefaultBackground(Colors.ONLINE_TIP_BACKGROUND);
FormData fd = new FormData();
fd.left = new FormAttachment(0, 8);
fd.right = new FormAttachment(100, -8);
fd.top = new FormAttachment(0, 18);
fd.bottom = new FormAttachment(100, -8);
GridLayout layout = new GridLayout(3, false);
layout.marginHeight = layout.marginWidth = 0;
layout.horizontalSpacing = 10;
Composite container = UITool.createContainer(shell, fd, layout);
// 头像
GridData gd = new GridData(GridData.FILL_VERTICAL);
gd.verticalSpan = 3;
gd.widthHint = 32;
lblFace = UITool.createLabel(container, "", gd, SWT.CENTER);
// 关闭标签
gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
gd.grabExcessHorizontalSpace = true;
gd.widthHint = gd.heightHint = 8;
gd.horizontalSpan = 2;
lblClose = UITool.createLabel(container, "", gd);
lblClose.setForeground(Colors.LIGHT_BLUE);
lblClose.setCursor(handCursor);
lblClose.addMouseTrackListener(new MouseTrackAdapter() {
public void mouseEnter(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.YELLOW);
c.redraw();
}
public void mouseExit(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.LIGHT_BLUE);
c.redraw();
}
});
lblClose.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
closeNow();
}
});
lblClose.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Rectangle rect = lblClose.getBounds();
e.gc.drawLine(0, 0, rect.width - 1, rect.height - 1);
e.gc.drawLine(1, 0, rect.width - 1, rect.height - 2);
e.gc.drawLine(0, 1, rect.width - 2, rect.height - 1);
e.gc.drawLine(rect.width - 1, 0, 0, rect.height - 1);
e.gc.drawLine(rect.width - 2, 0, 0, rect.height - 2);
e.gc.drawLine(rect.width - 1, 1, 1, rect.height - 1);
}
});
// QQ号标签
gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
gd.horizontalSpan = 2;
lblUserInfo = UITool.createLabel(container, "", gd);
lblUserInfo.setCursor(handCursor);
lblUserInfo.addMouseTrackListener(new MouseTrackAdapter() {
public void mouseEnter(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.LIGHT_BLUE);
}
public void mouseExit(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.BLACK);
}
});
lblUserInfo.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
closeNow();
main.getShellLauncher().openNormalIMWindow((FriendModel)shell.getData());
}
});
// 上线了标签
UITool.createLabel(container, LumaQQ.getString("onlinetip.online"));
// 设置标签
gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
gd.grabExcessHorizontalSpace = true;
lblSetup = UITool.createLabel(container, LumaQQ.getString("onlinetip.setting"), gd);
lblSetup.setForeground(Colors.LIGHT_BLUE);
lblSetup.setCursor(handCursor);
lblSetup.addMouseTrackListener(new MouseTrackAdapter() {
public void mouseEnter(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.YELLOW);
}
public void mouseExit(MouseEvent e) {
Control c = (Control)e.getSource();
c.setForeground(Colors.LIGHT_BLUE);
}
});
lblSetup.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent e) {
closeNow();
main.getShellLauncher().openSystemOptionWindow().setCurrentPage(SystemOptionWindow.GUI);
}
});
// 专用伺候linux的代码
shell.layout();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -