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

📄 tipshell.java

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
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.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.tsinghua.lumaqq.IPSeeker;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.QQShowManager;
import edu.tsinghua.lumaqq.models.ClusterModel;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.qq.Utils;

/**
 * <pre>
 * 好友提示窗口
 * </pre>
 * 
 * @author 马若劼
 */
public class TipShell {
	private Shell shell;
	private MainShell main;
	private Label qqShow;
	private Label lblQQ, lblNick, lblName, lblUserInfo;
	private Text textIPPlace;
	private Color background;
	private Cursor handCursor;
	private Image qqShowImage;

	// 是否应该关闭shell的标志,当鼠标离开好友后,应该关闭tip shell,但是不是立刻关闭,
	//     我们在这里等待1.5秒钟,如果没有什么进一步的动作,就关闭
	private boolean shouldClose;
	// 执行关闭任务的Runnable对象
	private Runnable closeRunnable = new Runnable() {
		public void run() {
			if(isShouldClose()) {
				Point p = main.display.getCursorLocation();
				if(!shell.getBounds().contains(p)) 
					shell.setVisible(false);				
				setShouldClose(false);
			}
		}		
	};
		
	/**
	 * 构造函数
	 * @param main
	 */
	public TipShell(MainShell main) {
	    this.main = main;
		// 初始化变量
		background = new Color(main.display, 0xDE, 0xE7, 0xF7);
		handCursor = new Cursor(main.display, SWT.CURSOR_HAND);
		shouldClose = false;
		// 初始化窗口布局
		initLayout();
	}
	
	/**
	 * 初始化窗口布局
	 */
	private void initLayout() {
		// 创建tip窗口
		shell = new Shell(main.getShell(), SWT.ON_TOP | SWT.NO_TRIM);
		shell.setLayout(new FormLayout());
		shell.addShellListener(
			new ShellAdapter() {
				public void shellClosed(ShellEvent e) {
					background.dispose();
					handCursor.dispose();
				}
			}
		);
		shell.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(main.icons.getResource(IconHolder.bmpTipBackground), 0, 0);
            }		    
		});
		shell.addMouseTrackListener(
			new MouseTrackAdapter() {
				public void mouseEnter(MouseEvent e) {
					setShouldClose(false);
				}
				
				public void mouseExit(MouseEvent e) {
					if(!isShouldClose()) {
						setShouldClose(true);
						main.display.timerExec(1500, closeRunnable);			
					}						
				}
			}
		);
		// QQ秀
		qqShow = new Label(shell, SWT.NONE);
		qqShow.setBackground(background);
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 3);
		fd.right = new FormAttachment(0, 103);
		fd.top = new FormAttachment(0, 13);
		fd.bottom = new FormAttachment(100, -3);
		qqShow.setLayoutData(fd);
		qqShow.addPaintListener(
			new PaintListener() {
				public void paintControl(PaintEvent e) {
					Rectangle ca = qqShow.getBounds();
					e.gc.fillRectangle(ca);
					if(qqShowImage != null) {
						Rectangle bound = qqShowImage.getBounds();
						int x = (bound.width - ca.width) / 2;
						int y = (bound.height - ca.height) / 2;
						if(x >= 0 && y >= 0)
							e.gc.drawImage(qqShowImage, x, y, ca.width, ca.height, 0, 0, ca.width, ca.height);
						else if(x >= 0 && y < 0)
							e.gc.drawImage(qqShowImage, x, 0, ca.width, bound.height, 0, -y, ca.width, bound.height);
						else if(x < 0 && y >= 0)
							e.gc.drawImage(qqShowImage, 0, y, bound.width, ca.height, -x, 0, bound.width, ca.height);
						else
							e.gc.drawImage(qqShowImage, 0, 0, bound.width, bound.height, -x, -y, bound.width, bound.height);							
					}
					e.gc.drawRectangle(0, 0, ca.width - 1, ca.height - 1);
				}				
			}
		);
		// QQ号标签
		lblQQ = new Label(shell, SWT.LEFT);
		lblQQ.setBackground(background);
		fd = new FormData();
		fd.left = new FormAttachment(qqShow, 10, SWT.RIGHT);
		fd.right = new FormAttachment(100, -3);
		fd.top = new FormAttachment(0, 13);
		lblQQ.setLayoutData(fd);
		// 昵称标签
		lblNick = new Label(shell, SWT.LEFT);
		lblNick.setBackground(background);
		fd = new FormData();
		fd.left = new FormAttachment(qqShow, 10, SWT.RIGHT);
		fd.right = new FormAttachment(100, -3);
		fd.top = new FormAttachment(lblQQ, 5, SWT.BOTTOM);
		lblNick.setLayoutData(fd);
		// 真实姓名标签
		lblName = new Label(shell, SWT.LEFT);
		lblName.setBackground(background);
		fd = new FormData();
		fd.left = new FormAttachment(qqShow, 10, SWT.RIGHT);
		fd.right = new FormAttachment(100, -3);
		fd.top = new FormAttachment(lblNick, 5, SWT.BOTTOM);
		lblName.setLayoutData(fd);
		// 用户资料标签
		lblUserInfo = new Label(shell, SWT.NONE);
		lblUserInfo.setText(LumaQQ.getResourceString("tip.shell.label.user.info"));
		lblUserInfo.setBackground(background);
		lblUserInfo.setForeground(main.display.getSystemColor(SWT.COLOR_BLUE));
		lblUserInfo.setCursor(handCursor);
		fd = new FormData();
		fd.right = new FormAttachment(100, -10);
		fd.bottom = new FormAttachment(100, -5);
		lblUserInfo.setLayoutData(fd);
		lblUserInfo.addMouseTrackListener(
			new MouseTrackAdapter() {
				public void mouseEnter(MouseEvent e) {
					lblUserInfo.setForeground(main.display.getSystemColor(SWT.COLOR_RED));
				}
				
				public void mouseExit(MouseEvent e) {
					lblUserInfo.setForeground(main.display.getSystemColor(SWT.COLOR_BLUE));
				}
			}
		);
		lblUserInfo.addMouseListener(
			new MouseAdapter() {
				public void mouseDown(MouseEvent e) {
					// 根据model的不同打开不同的窗口
					Object modelObj = shell.getData();
					if(modelObj instanceof FriendModel)
						main.openUserInfoShell((FriendModel)modelObj, false);
					else
						main.openClusterInfoShell((ClusterModel)modelObj);
					// 隐藏提示窗口
					shell.setVisible(false);
				}
			}
		);
		// IP和地点文本框
		textIPPlace = new Text(shell, SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
		textIPPlace.setBackground(background);
		fd = new FormData();
		fd.left = new FormAttachment(qqShow, 10, SWT.RIGHT);
		fd.right = new FormAttachment(100, -3);
		fd.top = new FormAttachment(lblName, 15, SWT.BOTTOM);
		fd.bottom = new FormAttachment(lblUserInfo, -5, SWT.TOP);
		textIPPlace.setLayoutData(fd);
		// 设置大小
		shell.setSize(250, 160);
	}
	
	/**
	 * 设置窗口位置
	 * @param p
	 */
	public void setLocation(Point p) {
	    shell.setLocation(p);
	}
	
	/**
	 * 设置可见状态
	 * @param b
	 */
	public void setVisible(boolean b) {
	    shell.setVisible(b);
	}
	
	/**
	 * @return true表示可见
	 */
	public boolean isVisible() {
	    return shell.isVisible();
	}
	
	/**
	 * 重新布局
	 */
	public void layout() {
	    shell.layout();
	}

	/**
	 * 设置Data
	 * @param obj
	 */
	public void setData(Object obj) {
	    shell.setData(obj);
	}
	
	/**
	 * 把群model中的信息填充到tip shell中
	 * @param c
	 */
	public void setClusterTip(ClusterModel c) {
		// 设置各标签信息
		// 群外部ID
		lblQQ.setText(LumaQQ.getResourceString("tip.shell.label.cluster.id") + c.getExternalId());
		// 名称
		lblNick.setText(LumaQQ.getResourceString("tip.shell.label.cluster.name") + c.getProperty("name"));
		// 清空IP标签
		textIPPlace.setText("");
		// 创建者的QQ秀
		int creator = c.getCreator();
		QQShowManager sm = QQShowManager.getInstance();
		if(sm.isCached(creator))
			qqShowImage = sm.getQQShowImage(creator);
		else
			qqShowImage = main.icons.getResource(IconHolder.bmpDefaultQQShow);
		qqShow.redraw();
		// 群资料标签
		lblUserInfo.setText(LumaQQ.getResourceString("tip.shell.label.cluster.info"));
	}

	/**
	 * 把好友model中的信息填充到tip shell中 
	 * @param f
	 */
	public void setFriendTip(FriendModel f) {
		// 设置各标签信息
		// QQ号
		Integer qqNum = (Integer)f.getProperty("qq");
		lblQQ.setText(LumaQQ.getResourceString("tip.shell.label.qq") + qqNum);
		// 昵称
		if(f.hasProperty("nick"))
			lblNick.setText(LumaQQ.getResourceString("tip.shell.label.nick") + f.getProperty("nick"));
		else
			lblNick.setText("");
		// 备注名称
		if(f.hasProperty("realName"))
			lblName.setText(LumaQQ.getResourceString("tip.shell.label.remark") + f.getProperty("realName"));
		else
			lblName.setText("");
		// IP和地点
		byte[] ip = (byte[])f.getProperty("ip");
		if(ip == null || main.isIpZero(ip)) {
			textIPPlace.setText(LumaQQ.getResourceString("tcp.login") + " " + LumaQQ.getResourceString("unknown.ip"));
		} else {
			IPSeeker seeker = IPSeeker.getInstance();
			String ipStr = Utils.getIpStringFromBytes(ip);
			String port = (String)f.getProperty("port");
			String country = seeker.getCountry(ip);
			String area = seeker.getArea(ip);
			if(area.endsWith("CZ88.NET"))
				area = "";
			textIPPlace.setText(ipStr + ":" + port + System.getProperty("line.separator") + country + area);
		}
		// QQ秀
		QQShowManager sm = QQShowManager.getInstance();
		if(sm.isCached(qqNum.intValue()))
			qqShowImage = sm.getQQShowImage(qqNum.intValue());
		else
			qqShowImage = main.icons.getResource(IconHolder.bmpDefaultQQShow);
		qqShow.redraw();
		// 用户资料标签
		lblUserInfo.setText(LumaQQ.getResourceString("tip.shell.label.user.info"));
	}
	
	/**
	 * 设置是否应该关闭tip shell
	 * @param b
	 */
	public synchronized void setShouldClose(boolean b) {
		shouldClose = b;
	}
	
	/**
	 * @return true如果应该关闭
	 */
	public synchronized boolean isShouldClose() {
		return shouldClose;
	}
	
	/**
	 * 立刻关闭提示窗口
	 */
	public void closeNow() {
		setShouldClose(false);
		if(shell.isVisible())
			shell.setVisible(false);
	}

    /**
     * 在一定延时后关闭窗口,设定是1.5秒
     */
    public void closeDelay() {
		setShouldClose(true);
		main.display.timerExec(1500, closeRunnable);			
    }
    
    /**
     * 设置窗口大小
     * @param width
     * @param height
     */
    public void setSize(int width, int height) {
        shell.setSize(width, height);
    }
}

⌨️ 快捷键说明

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