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

📄 friendtipshell.java

📁 java写的qq代码实现qq的部分功能
💻 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 org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
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.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.tsinghua.lumaqq.Colors;
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.models.IQQNode;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.ui.tool.UITool;
import edu.tsinghua.swt.models.INode;

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

	// 是否应该关闭shell的标志,当鼠标离开好友后,应该关闭tip shell,但是不是立刻关闭,
	// 我们在这里等待1.5秒钟,如果没有什么进一步的动作,就关闭
	private boolean shouldClose;
	// 执行关闭任务的Runnable对象
	private Runnable closeRunnable = new Runnable() {
		public void run() {
			if(isShouldClose()) {
				if(shell.isVisible()) {
					Point p = main.getDisplay().getCursorLocation();
					if(shell.getBounds().contains(p)) {
						setShouldClose(false);
						main.getDisplay().timerExec(1500, this);
					} else
						setVisible(false);									
				}
			} else
				main.getDisplay().timerExec(1500, this);
		}		
	};
		
	/**
	 * 构造函数
	 * @param main
	 */
	public FriendTipShell(MainShell main) {
	    this.main = main;
		// 初始化变量
		handCursor = main.getDisplay().getSystemCursor(SWT.CURSOR_HAND);
		shouldClose = false;
		icons = IconHolder.getInstance();
		// 初始化窗口布局
		initLayout();
	}
	
	/**
	 * 初始化窗口布局
	 */
	private void initLayout() {
		// 创建tip窗口
		shell = new Shell(main.getShell(), SWT.ON_TOP | SWT.NO_TRIM);
		shell.setLayout(new FormLayout());
		shell.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                e.gc.drawImage(icons.getImage(IconHolder.bmpTipBackground), 0, 0);
            }		    
		});
		
		UITool.setDefaultBackground(Colors.FRIEND_TIP_BACKGROUP);
		
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 3);
		fd.right = new FormAttachment(100, -3);
		fd.top = new FormAttachment(0, 13);
		fd.bottom = new FormAttachment(100, -3);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = layout.marginWidth = 0;
		layout.horizontalSpacing = 10;
		Composite container = UITool.createContainer(shell, fd, layout);
		
		// QQ秀
		GridData gd = new GridData(GridData.FILL_VERTICAL);
		gd.widthHint = 100;
		gd.verticalSpan = 5;
		lblQQShow = UITool.createLabel(container, "", gd);
		lblQQShow.setCursor(handCursor);
		lblQQShow.setToolTipText(LumaQQ.getString("friendtip.qqshow.mall"));
		lblQQShow.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				Rectangle ca = lblQQShow.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);
			}				
		});
		lblQQShow.addMouseListener(new MouseAdapter() {
            public void mouseDown(MouseEvent e) {
				// 隐藏提示窗口
				setVisible(false);
                // 打开QQ秀商城主页
				main.getShellLauncher().goQQShowMall();
            }
		});
		// QQ号标签
		lblQQ = UITool.createLabel(container, "", new GridData(GridData.FILL_HORIZONTAL));
		// 昵称标签
		lblNick = UITool.createLabel(container, "", new GridData(GridData.FILL_HORIZONTAL));
		// 真实姓名标签
		lblName = UITool.createLabel(container, "", new GridData(GridData.FILL_HORIZONTAL));
		// IP和地点文本框
		gd = new GridData(GridData.FILL_BOTH);
		gd.grabExcessVerticalSpace = true;
		textIPPlace = UITool.createMultiText(container, gd, SWT.MULTI | SWT.WRAP | SWT.READ_ONLY | SWT.CENTER);
		// 用户资料标签
		gd = new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.VERTICAL_ALIGN_END);
		gd.grabExcessHorizontalSpace = true;
		lblUserInfo = UITool.createLink(container, LumaQQ.getString("friendtip.user.info"), null, gd);
		lblUserInfo.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// 根据model的不同打开不同的窗口
				Object modelObj = shell.getData();
				if(modelObj instanceof FriendModel)
				    main.getShellLauncher().openUserInfoWindow((FriendModel)modelObj, UserInfoWindow.READ_ONLY);
				else
					main.getShellLauncher().openClusterInfoWindow((ClusterModel)modelObj);
				// 隐藏提示窗口
				setVisible(false);
			}
		});
		// 设置大小
		shell.setSize(250, 160);
	}
	
	/**
	 * 设置窗口位置
	 * @param p
	 */
	public void setLocation(Point p) {
	    shell.setLocation(p);
	}
	
	/**
	 * 设置可见状态
	 * @param b
	 */
	public void setVisible(boolean b) {
	    shell.setVisible(b);
	    if(b)
	    	main.getDisplay().timerExec(1500, closeRunnable);
	}
	
	/**
	 * @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.getString("friendtip.cluster.id") + c.getExternalId());
		// 名称
		lblNick.setText(LumaQQ.getString("friendtip.cluster.name") + c.getProperty(INode.NAME));
		// 备注,隐藏掉
		lblName.setText("");
		// 清空IP标签
		textIPPlace.setText("");
		// 创建者的QQ秀
		int creator = c.getCreator();
		QQShowManager sm = QQShowManager.getInstance();
		if(sm.isCached(creator))
			qqShowImage = sm.getQQShowImage(creator);
		else
			qqShowImage = icons.getImage(IconHolder.bmpDefaultQQShow);
		lblQQShow.redraw();
		// 群资料标签
		lblUserInfo.setText(LumaQQ.getString("friendtip.cluster.info"));
	}

	/**
	 * 把好友model中的信息填充到tip shell中 
	 * @param f
	 */
	public void setFriendTip(FriendModel f) {
		// 设置各标签信息
		// QQ号
	    int qqNum = f.getQQ();
		lblQQ.setText(LumaQQ.getString("friendtip.qq") + qqNum);
		// 昵称
		if(f.hasProperty(IQQNode.NICK))
			lblNick.setText(LumaQQ.getString("friendtip.nick") + f.getNick());
		else
			lblNick.setText("");
		// 备注名称
		if(f.hasProperty(IQQNode.REAL_NAME))
			lblName.setText(LumaQQ.getString("friendtip.remark") + f.getRealName());
		else
			lblName.setText("");
		// IP和地点
		byte[] ip = (byte[])f.getProperty(IQQNode.IP);
		if(ip == null || Util.isIpZero(ip)) {
			textIPPlace.setText(LumaQQ.getString("tcp.login") + " " + LumaQQ.getString("unknown.ip"));
		} else {
			IPSeeker seeker = IPSeeker.getInstance();
			String ipStr = Util.getIpStringFromBytes(ip);
			String port = (String)f.getProperty(IQQNode.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))
			qqShowImage = sm.getQQShowImage(qqNum);
		else
			qqShowImage = icons.getImage(IconHolder.bmpDefaultQQShow);
		lblQQShow.redraw();
		// 用户资料标签
		lblUserInfo.setText(LumaQQ.getString("friendtip.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.isDisposed())
			setVisible(false);
	}
    
    /**
     * @return
     * 		true如果shell资源已经释放
     */
    public boolean isDisposed() {
    	return shell.isDisposed();
    }
    
    /**
     * 设置窗口大小
     * @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 + -