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

📄 receiveimwindow.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
* 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 static edu.tsinghua.lumaqq.resource.Messages.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
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.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
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.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.User;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.NormalIM;
import edu.tsinghua.lumaqq.qq.beans.NormalIMHeader;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.resource.Colors;
import edu.tsinghua.lumaqq.resource.DefaultFace;
import edu.tsinghua.lumaqq.resource.Resources;
import edu.tsinghua.lumaqq.ui.config.sys.SystemOptionWindow;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.ui.helper.HeadFactory;
import edu.tsinghua.lumaqq.ui.helper.ShellFactory;
import edu.tsinghua.lumaqq.widgets.qstyle.Slat;
import edu.tsinghua.lumaqq.widgets.rich.LineStyle;
import edu.tsinghua.lumaqq.widgets.rich.RichBox;

/**
 * 接收消息窗口,用来查看收到的消息
 * 
 * @author luma
 */
public class ReceiveIMWindow extends Window implements ShellListener {    
    private User model;
    private MainShell main;
    private Resources res;
    
	// 表示当前窗口是否是active的
	private boolean active;
	
	private CLabel lblName;
	private RichBox outputBox;
	private Cursor handCursor;
	private Slat btnNext, btnQuickReply;
	private Menu quickReplyMenu;
	private CLabel lblIp;
	
	private String ip, place;
	
	// 用作临时用途
	private Date date = new Date();
	private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	// style样式表缓冲区
	private List<LineStyle> styleCache;	
	
	// 缺省的用户名称提示样式
	private static final LineStyle myStyle = new LineStyle(Colors.MY_HINT_COLOR, null, "宋体", SWT.NORMAL, 9);
	
	// 关闭窗口action
	private Runnable closeAction = new Runnable() {
        public void run() {
            getShell().close();
        }
	};
	// 回复
	private Runnable replyAction = new Runnable() {
        public void run() {
            openSendIMWindow();
            getShell().close();
        }
	};
	// 快捷回复
	private Runnable quickReplyAction = new Runnable() {
        public void run() {
            quickReply();
        }
	};
	// 下一条
	private Runnable nextAction = new Runnable() {
        public void run() {
        	if(btnNext.isEnabled()) {
	            main.getShellLauncher().openNormalIMWindow(model);
	            if(!main.getMessageQueue().hasMessage(model.qq))
	            	setNextButtonEnabled(false);        		
        	}
        }
	};
    
    /**
     * @param parentShell
     */
    public ReceiveIMWindow(MainShell main, User f) {
        super(main.getShell());
        this.main = main;
        this.model = f;
        this.res = Resources.getInstance();
		styleCache = new ArrayList<LineStyle>();
		handCursor = main.getDisplay().getSystemCursor(SWT.CURSOR_HAND);
		
		// 设置ip地址信息
		if(f.ip == null || Util.isIpZero(f.ip)) {
			ip = "";
			place = unknown_ip;
		} else {
			String ipStr = Util.getIpStringFromBytes(f.ip);
			String port = String.valueOf(f.port);
			String country = main.getIPSeeker().getCountry(f.ip);
			String area = main.getIPSeeker().getArea(f.ip);
			if(area.endsWith("CZ88.NET"))
				area = "";
			place = country + area;
			ip = ipStr + ":" + port;
		}
		
        setBlockOnOpen(false);
    }    
    
	public String getIPText() {
		if(ip.equals(""))
			return place;
		else
			return ip + " - " + place;
	}

	/**
	 * 初始化快捷回复菜单
	 */
	@SuppressWarnings("unchecked")
	private void initQuickReplyMenu() {
		quickReplyMenu = new Menu(getShell(), SWT.POP_UP);
		// 循环初始化菜单项,有多少条快捷回复消息就创建多少菜单项,最后加上一个自定义项
		List<String> quickReplies = main.getConfigHelper().getReplies().getQuickReply();
		int size = quickReplies.size();
		for(int i = 0; i < size; i++) {
			final MenuItem mi = new MenuItem(quickReplyMenu, SWT.RADIO);
			mi.setData(i);
			mi.setText(quickReplies.get(i));
			mi.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {					
					int index = (Integer)((MenuItem)e.getSource()).getData();
					if(mi.getSelection()) {
						main.getConfigHelper().getReplies().setCurrentQuickReply(index);
						quickReply();							
					}
				}
			});			
		}
		new MenuItem(quickReplyMenu, SWT.SEPARATOR);
		// 自定义
		MenuItem mi = new MenuItem(quickReplyMenu, SWT.PUSH);
		mi.setImage(res.getImage(Resources.icoReply));
		mi.setText(menu_quick_reply_addnew);
		mi.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				main.getShellLauncher().openSystemOptionWindow().setCurrentPage(SystemOptionWindow.REPLY);
			}
		});
		// 设置菜单项的选择状态
		quickReplyMenu.getItem(main.getConfigHelper().getReplies().getCurrentQuickReply()).setSelection(true);
	}	
	
	/**
	 * 快速回复
	 */
	protected void quickReply() {
	    SendIMWindow sms = openSendIMWindow();
		getShell().close();
		sms.sendMessage(main.getConfigHelper().getCurrentQuickReplyString());
	}

	/**
	 * 打开回复消息窗口
	 */
	private SendIMWindow openSendIMWindow() {
		SendIMWindow sms = main.getShellRegistry().getSendIMWindow(model);
		if(sms == null) {
			sms = ShellFactory.createSendIMWindow(main, model);
			sms.open();
		} else {
			sms.setMinimized(false);
			sms.setActive();
			sms.setFocus();
		}
		return sms;
	}
	
    /**
     * 得到行样式
     * 
     * @param im
     * @return
     */
    private LineStyle getLineStyle(NormalIM im) {
        int fontStyle = 0;
        if(im.fontStyle.isBold())
            fontStyle |= SWT.BOLD;
        if(im.fontStyle.isItalic())
            fontStyle |= SWT.ITALIC;
        if(fontStyle == 0)
            fontStyle = SWT.NORMAL;
        
        return getLineStyle(im.fontStyle.getFontName(), 
        		fontStyle, 
        		im.fontStyle.getFontSize(), 
        		im.fontStyle.getRed(), 
        		im.fontStyle.getGreen(), 
        		im.fontStyle.getBlue());
    }
    
    /**
     * 根据具体的样式信息查找style
     * 
     * @param fontName
     * @param fontStyle
     * @param fontSize
     * @param red
     * @param green
     * @param blue
     * @return
     */
    private LineStyle getLineStyle(String fontName, int fontStyle, int fontSize, int red, int green, int blue) {        
        int size = styleCache.size();
        for(int i = 0; i < size; i++) {
            LineStyle style = styleCache.get(i);
            if(!style.fontName.equals(fontName))
                continue;
            if(style.fontSize != fontSize)
                continue;            
            if(style.fontStyle != fontStyle)
                continue;
            // TODO add underline here
            if(style.foreground.getRed() != red)
                continue;
            if(style.foreground.getGreen() != green)
                continue;
            if(style.foreground.getBlue() != blue)
                continue;
            return style;
        }
        
        LineStyle style = new LineStyle(new Color(main.getDisplay(), red, green, blue), null, fontName, fontStyle, fontSize);
        styleCache.add(style);
        return style;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.jface.window.Window#getParentShell()
     */
	@Override
    protected Shell getParentShell() {
        return null;
    }
    
	/* (non-Javadoc)
     * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
     */
	@Override
    protected void configureShell(Shell newShell) {
        newShell.setText(NLS.bind(receive_im_title, model.displayName)); 
		newShell.setImage(res.getImage(Resources.icoMessage));
        newShell.addControlListener(new ControlAdapter() {
            public void controlMoved(ControlEvent e) {
        		// 保存自己的位置,以便下次打开窗口时在同样的位置
        		Point loc = ((Shell)e.getSource()).getLocation();
        		model.windowX = loc.x;
        		model.windowY = loc.y;
            }
        });
        newShell.addDisposeListener(new DisposeListener() {
            public void widgetDisposed(DisposeEvent e) {
				for(LineStyle style : styleCache) {
                    if(style.foreground != null)
                        style.foreground.dispose();
                    if(style.background != null)
                        style.background.dispose();
                }
            }

⌨️ 快捷键说明

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