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

📄 normalimcontainer.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.util.ArrayList;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
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.graphics.Color;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import edu.tsinghua.lumaqq.events.IFaceSelectionListener;
import edu.tsinghua.lumaqq.models.Model;
import edu.tsinghua.lumaqq.models.User;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.beans.FontStyle;
import edu.tsinghua.lumaqq.qq.beans.NormalIM;
import edu.tsinghua.lumaqq.qq.beans.NormalIMHeader;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.record.IKeyConstants;
import edu.tsinghua.lumaqq.record.RecordEntry;
import edu.tsinghua.lumaqq.resource.Colors;
import edu.tsinghua.lumaqq.resource.DefaultFace;
import edu.tsinghua.lumaqq.resource.Resources;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.ui.helper.DateTool;
import edu.tsinghua.lumaqq.ui.helper.FaceRegistry;
import edu.tsinghua.lumaqq.ui.helper.HeadFactory;
import edu.tsinghua.lumaqq.ui.jobs.SendNormalIMJob;
import edu.tsinghua.lumaqq.widgets.FaceImageAdvisor;
import edu.tsinghua.lumaqq.widgets.IImageSelectorAdvisor;
import edu.tsinghua.lumaqq.widgets.ImageSelector;
import edu.tsinghua.lumaqq.widgets.rich.IRichContent;
import edu.tsinghua.lumaqq.widgets.rich.LineStyle;
import edu.tsinghua.lumaqq.widgets.rich.RichBox;

/**
 * 普通聊天容器
 * 
 * @author luma
 */
public class NormalIMContainer extends Composite implements IIMContainer {
	private Composite parent;
	private Resources res;
	private IContainerKeyHandler keyHandler;
	
	private CLabel lblName, lblIp;
	private RichBox outputBox, inputBox;
	private ViewForm inputForm, outputForm;
	
	private MainShell main;
	private String ip, place;
	private User user;
	
	private int unread;
	private boolean active;
	
	// 发送消息的参数
	private String message;
	
	// style样式表缓冲区
	private List<LineStyle> styleCache;	
	// 缺省的用户名称提示样式
	private static final LineStyle myStyle = new LineStyle(Colors.MY_HINT_COLOR, null, "宋体", SWT.NORMAL, 9);
	private static final LineStyle otherStyle = new LineStyle(Colors.BLUE, null, "宋体", SWT.NORMAL, 9);
	
	// 系统平台
    private static boolean IS_GTK;
    private static boolean IS_MOTIF;
    static {
        String platform = SWT.getPlatform();
        IS_GTK = "gtk".equals(platform);
        IS_MOTIF = "motif".equals(platform);
    }
    
	// 关闭窗口action
	private Runnable closeAction = new Runnable() {
        public void run() {
        	if(keyHandler != null)
        		keyHandler.onKeyDown(SWT.MOD3, 'C');
        }
	};
	// 查看记录的action
	private Runnable showRecordAction = new Runnable() {
        public void run() {
        	if(keyHandler != null)
        		keyHandler.onKeyDown(SWT.MOD3, 'H');
        }
	};
	// 发送消息的action
	private Runnable sendAction = new Runnable() {
        public void run() {
            if(!inputBox.isReadonly())
                send();
        }
	};
	// 切换下一个未读tab
	private Runnable nextUnreadTabAction = new Runnable() {
        public void run() {
        	if(keyHandler != null)
        		keyHandler.onKeyDown(SWT.MOD3, 'Q');
        }
	};
	// 切换下一个tab
	private Runnable nextTabAction = new Runnable() {
        public void run() {
        	if(keyHandler != null)
        		keyHandler.onKeyDown(SWT.MOD3, SWT.ARROW_RIGHT);
        }
	};
	// 切换上一个tab
	private Runnable prevTabAction = new Runnable() {
        public void run() {
        	if(keyHandler != null)
        		keyHandler.onKeyDown(SWT.MOD3, SWT.ARROW_LEFT);
        }
	};
	
	public NormalIMContainer(Composite parent) {
		super(parent, SWT.NONE);
		this.parent = parent;
		res = Resources.getInstance();
		unread = 0;
		active = false;
		styleCache = new ArrayList<LineStyle>();
		initLayout();
	}
	
	/**
	 * 解析IP地址的地点
	 * 
	 * @param f
	 * 		User对象
	 */
	private void resolveIPLocation(User f) {
		// 设置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 = "";
			if(country != bad_ip_file)
				place = country + area;
			else
				place = bad_ip_file;
			ip = ipStr + ":" + port;
		}
	}
	
    /**
     * 初始化快捷键设置
     */
    private void initKeyAction() {
        // mod3 -> alt
        inputBox.setUserKeyAction('C' | SWT.MOD3, closeAction);
        inputBox.setUserKeyAction('S' | SWT.MOD3, sendAction);
        inputBox.setUserKeyAction('H' | SWT.MOD3, showRecordAction);
        inputBox.setUserKeyAction('Q' | SWT.MOD3, nextUnreadTabAction);
        inputBox.setUserKeyAction(SWT.MOD3 | SWT.ARROW_RIGHT, nextTabAction);
        inputBox.setUserKeyAction(SWT.MOD3 | SWT.ARROW_LEFT, prevTabAction);
    }
	
	/**
	 * 初始化布局
	 */
	private void initLayout() {
        GridLayout layout = new GridLayout();
        layout.horizontalSpacing = layout.verticalSpacing = layout.marginHeight = layout.marginWidth = 0;
        setLayout(layout);
        setBackground(Colors.MAINSHELL_BACKGROUND);
        
        // 聊天区
        Composite chatContainer = new Composite(this, SWT.NONE);
        chatContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
        layout = new GridLayout();        
        layout.marginHeight = layout.marginWidth = 1;
        layout.horizontalSpacing = layout.verticalSpacing = 0;
        chatContainer.setLayout(layout);
        chatContainer.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent e) {
                Composite c = (Composite)e.getSource();
                Rectangle rect = c.getClientArea();
                rect.width--;
                rect.height--;
                e.gc.setForeground(Colors.WIDGET_BORDER);
                e.gc.drawRectangle(rect);
            }
        });
        
        // output form
        outputForm = new ViewForm(chatContainer, SWT.FLAT);
        outputForm.setLayoutData(new GridData(GridData.FILL_BOTH));
        outputForm.setBackground(Colors.VIEWFORM_BANNER_BACKGROUND);
        // output form left control
        Composite left = new Composite(outputForm, SWT.NONE);
        layout = new GridLayout(2, false);
        layout.marginHeight = layout.marginWidth = 0;
        left.setLayout(layout);
        left.setBackground(Colors.VIEWFORM_BANNER_BACKGROUND);
        // 设置名称标签
        lblName = new CLabel(left, SWT.LEFT);
        lblName.setLayoutData(new GridData(GridData.FILL_VERTICAL));
        lblName.setBackground(Colors.VIEWFORM_BANNER_BACKGROUND);
        lblName.setCursor(getDisplay().getSystemCursor(SWT.CURSOR_HAND));
        lblName.setForeground(Colors.VIEWFORM_BANNER_TEXT);
        lblName.addMouseListener(new MouseAdapter() {
            public void mouseUp(MouseEvent e) {
                main.getShellLauncher().openUserInfoWindow(user, UserInfoWindow.READ_ONLY);
            }
        });
        lblName.addMouseTrackListener(new MouseTrackAdapter() {
            public void mouseEnter(MouseEvent e) {
                lblName.setForeground(Colors.VIEWFORM_BANNER_TEXT_HOVER);
            }
            
            public void mouseExit(MouseEvent e) {
                lblName.setForeground(Colors.VIEWFORM_BANNER_TEXT);
            }
        }); 
        // 设置ip标签
        lblIp = new CLabel(left, SWT.RIGHT);
        lblIp.setLayoutData(new GridData(GridData.FILL_BOTH));
        lblIp.setBackground(Colors.VIEWFORM_BANNER_BACKGROUND);
        // set left control
        outputForm.setTopLeft(left);
        // set content
        outputBox = new RichBox(outputForm);
        outputBox.setReadonly(true);
        outputBox.setBackground(Colors.WHITE);
        outputForm.setContent(outputBox);     
        
        Sash sash = new Sash(chatContainer, SWT.HORIZONTAL);
        sash.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        sash.setBackground(Colors.READONLY_BACKGROUND);
        sash.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                GridData data = (GridData)inputForm.getLayoutData();
                data.heightHint = inputForm.getParent().getClientArea().height - e.y;
                inputForm.getParent().layout();
            }
        });
        
        inputForm = new ViewForm(chatContainer, SWT.FLAT);
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.heightHint = 100;
        inputForm.setLayoutData(gd);
        ToolBar tb = new ToolBar(inputForm, SWT.FLAT);
        tb.setBackground(Colors.VIEWFORM_BANNER_BACKGROUND);
        // 字体
        ToolItem ti = new ToolItem(tb, SWT.NONE);        
        ti.setImage(res.getImage(Resources.icoFont));
        ti.setToolTipText(tooltip_button_font);
        ti.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				FontDialog dialog = new FontDialog(parent.getShell());
				if(main.getDefaultStyle() != null) 
					dialog.setFontList(main.getDefaultStyle().createFontData());
				if(main.getFontColor() != null)
					dialog.setRGB(main.getFontColor().getRGB());
				dialog.open();
				FontData[] fontList = dialog.getFontList();
				if(fontList == null) return;
				RGB rgb = dialog.getRGB();
				if(main.getFontColor() != null) main.getFontColor().dispose();
				main.getDefaultStyle().fontName = fontList[0].getName();
				main.getDefaultStyle().fontSize = fontList[0].getHeight();
				main.getDefaultStyle().fontStyle = fontList[0].getStyle();
				if(rgb != null) {
					main.setFontColor(new Color(main.getDisplay(), rgb));
					main.getDefaultStyle().foreground = main.getFontColor();
				}
				inputBox.setDefaultStyle(main.getDefaultStyle());
			}
		});
        // 颜色,在有些平台下需要
        if(IS_GTK || IS_MOTIF) {
	        ToolItem tiColor = new ToolItem(tb, SWT.NONE);
	        tiColor.setImage(res.getImage(Resources.icoColor));
	        tiColor.setToolTipText(tooltip_button_color);
	        tiColor.addSelectionListener(new SelectionAdapter() {
	            public void widgetSelected(SelectionEvent e) {
	                ColorDialog dialog = new ColorDialog(parent.getShell());
					if(main.getFontColor() != null)
						dialog.setRGB(main.getFontColor().getRGB());
					dialog.open();
					RGB rgb = dialog.getRGB();
					if(rgb != null) {
						if(main.getFontColor() != null) 
						    main.getFontColor().dispose();
						if(rgb != null) {
							main.setFontColor(new Color(main.getDisplay(), rgb));
							main.getDefaultStyle().foreground = main.getFontColor();
						}
					}
					inputBox.setDefaultStyle(main.getDefaultStyle());
	            }

⌨️ 快捷键说明

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