⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 onlinetipshell.java

📁 类似于MSN
💻 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.shells;

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.MouseTrackListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
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.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.QQShowManager;
import edu.tsinghua.lumaqq.models.FriendModel;

/**
 * 好友上线提示窗口
 * 
 * @author 王华刚 马若劼 
 */
public class OnlineTipShell  { //extends MouseTrackAdapter {
	private Shell shell;
	private Display display;
	private Label lblFace;
	private Label lblSetup, lblUserInfo, lblClose;
	private Color background;
	private Cursor handCursor;
	//需要显示的好友队列
	private LinkedList friendLink;
	// MainShell对象
	private MainShell main;
	// 鼠标按下时,设置此变量为true,表示处于移动窗口状态
	private boolean isMove;
	// 鼠标按下时的位置
	private int downX, downY;
	// 好友是否有QQShow保存在本地
	private boolean hasQQShow;
	// 头像或者QQ秀
	private Image faceOrQQShow;

	// 执行检查需要显示的队列并显示的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);
			}		        
		}
	};

	// 鼠标移动到标签上时的事件处理器
	private MouseTrackListener mtl = new MouseTrackAdapter() {
		public void mouseEnter(MouseEvent e) {
		    Control c = (Control)e.getSource();
			c.setForeground(main.display.getSystemColor(SWT.COLOR_YELLOW));
		}

		public void mouseExit(MouseEvent e) {
		    Control c = (Control)e.getSource();
			c.setForeground(main.display.getSystemColor(SWT.COLOR_WHITE));
		}    
	};

	/**
	 * 添加要显示的好友到队列
	 * @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.display;
		background = new Color(display, 0x73, 0x8A, 0xEF);
		handCursor = new Cursor(display, SWT.CURSOR_HAND);
		isMove = false;
		hasQQShow = false;
		friendLink = new LinkedList();
		// 初始化窗口布局
		initLayout();
	}

	/**
	 * 立刻关闭提示窗口
	 */
	private void closeNow() {
        // 清空可能遗留下来的model
        friendLink.clear();
        // 设置窗口为不可见 
		shell.setVisible(false);
	}
	
	/**
	 * 打开提示窗口
	 */
	private void setFriendModel(FriendModel f) {
		shell.setData(f);
		// 设置用户信息
		Integer qqNum = (Integer) f.getProperty("qq");
		lblUserInfo.setText(LumaQQ.getResourceString("onlinetip.shell.label.qq.onlinetip", new Object[] { f.getProperty("name"), qqNum.toString() } ));
		// 加载小头像或者QQshow
		QQShowManager manager = QQShowManager.getInstance();
		if(manager.isCached(qqNum)) {
		    hasQQShow = true;
		    faceOrQQShow = manager.getQQShowImage(qqNum);
		} else {
		    hasQQShow = false;
		    faceOrQQShow = main.getFriendFace(f);
		}
		lblFace.redraw();
	}
	
	/**
	 * 打开窗口
	 */
	public void show() {
	    if(!shell.isVisible()) {
	        // 得到上站通知的显示位置,-1表示缺省
	        int x = main.options.getOnlineTipLocationX();
	        int y = main.options.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();	        
	    }
	}

	/**
	 * 初始化窗口布局
	 */
	private void initLayout() {
		// 创建tip窗口
		shell = new Shell(display, SWT.ON_TOP | SWT.NO_TRIM);
		shell.setSize(167, 87);
		shell.setLayout(new FormLayout());
		shell.addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				background.dispose();
				handCursor.dispose();
			}
		});
		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);
					main.options.setOnlineTipLocationX(x);
					main.options.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(main.icons.getResource(IconHolder.bmpOnlineTipBackground), 0, 0);
            }		    
		});
		// 头像显示,显示小头像
		lblFace = new Label(shell, SWT.NONE);
		lblFace.setBackground(background);
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 3);
		fd.right = new FormAttachment(0, 58);
		fd.top = new FormAttachment(0, 13);
		fd.bottom = new FormAttachment(100, -3);
		lblFace.setLayoutData(fd);
		lblFace.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				Rectangle ca = lblFace.getBounds();
				if (faceOrQQShow != null) {
				    // 得到图像大小
					Rectangle bound = faceOrQQShow.getBounds();
				    // 如果是QQ秀需要画边框,没有是画头像,不要边框
				    if(hasQQShow) {
				        int x = (bound.width - ca.width) / 2;
				        e.gc.drawImage(faceOrQQShow, x, 38, ca.width, ca.height, 0, 0, ca.width, ca.height);
						e.gc.drawRectangle(0, 0, ca.width - 1, ca.height - 1);
				    } else {
						int x = (bound.width - ca.width) / 2;
						int y = (bound.height - ca.height) / 2;
						if (x >= 0 && y >= 0)
							e.gc.drawImage(faceOrQQShow, x, y, ca.width, ca.height, 0, 0,
									ca.width, ca.height);
						else if (x >= 0 && y < 0)
							e.gc.drawImage(faceOrQQShow, x, 0, ca.width, bound.height, 0,
									-y, ca.width, bound.height);
						else if (x < 0 && y >= 0)
							e.gc.drawImage(faceOrQQShow, 0, y, bound.width, ca.height, -x,
									0, bound.width, ca.height);
						else
							e.gc.drawImage(faceOrQQShow, 0, 0, bound.width, bound.height,
									-x, -y, bound.width, bound.height);				        
				    }
				}
			}
		});
		// QQ号标签
		lblUserInfo = new Label(shell, SWT.WRAP);
		lblUserInfo.setBackground(background);
		lblUserInfo.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		lblUserInfo.setCursor(handCursor);
		fd = new FormData();
		fd.left = new FormAttachment(lblFace, 5, SWT.RIGHT);
		fd.right = new FormAttachment(100, -5);
		fd.top = new FormAttachment(0, 30);
		fd.bottom = new FormAttachment(100, -20);
		lblUserInfo.setLayoutData(fd);
		lblUserInfo.addMouseTrackListener(mtl);
		lblUserInfo.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				closeNow();
				main.openNormalMessageShell((FriendModel)shell.getData());
			}
		});
		// 关闭标签
		lblClose = new Label(shell, SWT.NONE);
		lblClose.setBackground(background);
		lblClose.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.right = new FormAttachment(100, -5);
		fd.left = new FormAttachment(100, -12);
		fd.top = new FormAttachment(0, 13);
		fd.bottom = new FormAttachment(0, 20);
		lblClose.setLayoutData(fd);
		lblClose.setCursor(handCursor);
		lblClose.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(MouseEvent e) {
			    Control c = (Control)e.getSource();
				c.setForeground(main.display.getSystemColor(SWT.COLOR_YELLOW));
				c.redraw();
			}

			public void mouseExit(MouseEvent e) {
			    Control c = (Control)e.getSource();
				c.setForeground(main.display.getSystemColor(SWT.COLOR_WHITE));
				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);
            }		    
		});
		// 设置标签
		lblSetup = new Label(shell, SWT.NONE);
		lblSetup.setText(LumaQQ.getResourceString("onlinetip.shell.label.qq.setup"));
		lblSetup.setBackground(background);
		lblSetup.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		lblSetup.setCursor(handCursor);
		lblSetup.addMouseTrackListener(mtl);
		lblSetup.addMouseListener(new MouseAdapter() {
		    public void mouseDown(MouseEvent e) {
		        closeNow();
		        main.openSystemOptionShell().showPanel(SystemOptionShell.GUI);
		    }
		});
		fd = new FormData();
		fd.right = new FormAttachment(100, -5);
		fd.bottom = new FormAttachment(100, -5);
		lblSetup.setLayoutData(fd);
		// 专用伺候linux的代码
		shell.layout();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -