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

📄 sendimwindow.java

📁 lumaQQ的源文件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * 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.jface.window.Window;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
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.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
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.ShellAdapter;
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.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.FillLayout;
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.Control;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.events.IFaceSelectionListener;
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.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.qstyle.Slat;
import edu.tsinghua.lumaqq.widgets.record.FriendRecordProvider;
import edu.tsinghua.lumaqq.widgets.record.RecordViewer;
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 SendIMWindow extends Window implements ShellListener, IFaceSelectionListener {
	/**
	 * <pre>
	 * 闪烁图标
	 * </pre>
	 * 
	 * @author luma
	 */
	private class Blink implements Runnable {
		private boolean flag;
		private volatile boolean stop;
		private Image blinkImage;


		public Blink() {
			stop = true;
		}

		public void setBlinkImage(Image image) {
			blinkImage = image;
			stop = false;
			flag = true;
		}

		public void run() {
			try {
				if (flag) {
					getShell().setImage(blinkImage);
					if (LumaQQ.IS_GTK)
						((BorderStyler) getShell().getData(BorderStyler.STYLER)).repaintTitleBar();
				} else {
					getShell().setImage(res.getImage(Resources.icoBlank));
					if (LumaQQ.IS_GTK)
						((BorderStyler) getShell().getData(BorderStyler.STYLER)).repaintTitleBar();
				}
				flag = !flag;
				if (!stop)
					main.getDisplay().timerExec(500, this);
				else {
					getShell().setImage(res.getImage(Resources.icoMessage));
					if (LumaQQ.IS_GTK)
						((BorderStyler) getShell().getData(BorderStyler.STYLER)).repaintTitleBar();
				}
			} catch (SWTException e) {
				// 这个操作可能会抛出SWTException,如果组件已经dispose的话,
				// 所以我们需要捕捉这个异常,不然程序可能崩溃
			}
		}

		public void setStop(boolean stop) {
			this.stop = stop;
		}

		public boolean isStop() {
			return stop;
		}
	}


	private User model;
	private MainShell main;
	private Resources res;

	// 表示当前窗口是否是active的
	private boolean active;

	private CLabel lblName, lblIp;
	private RichBox outputBox, inputBox;
	private RecordViewer viewer;
	private Menu enterMenu, fileMenu;
	private Cursor handCursor;
	private Slat btnMode;
	private Sash sash;

	private Blink blinkRunnable;

	private ViewForm inputForm, outputForm;

	private String ip, place;

	// style样式表缓冲区
	private List<LineStyle> styleCache;

	// 发送消息的参数
	private String message;

	// 缺省的用户名称提示样式
	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);

	// 关闭窗口action
	private Runnable closeAction = new Runnable() {
		public void run() {
			getShell().close();
		}
	};
	// 查看记录的action
	private Runnable showRecordAction = new Runnable() {
		public void run() {
			showRecord();
		}
	};
	// 发送消息的action
	private Runnable sendAction = new Runnable() {
		public void run() {
			if (!inputBox.isReadonly())
				sendMessage(inputBox.getText());
		}
	};
	// 切换模式
	private Runnable modeAction = new Runnable() {
		public void run() {
			setTalkMode(!model.talkMode);
		}
	};

	// 系统平台
	private static boolean IS_GTK;
	private static boolean IS_MOTIF;
	private ImageSelector fss;
	static {
		String platform = SWT.getPlatform();
		IS_GTK = "gtk".equals(platform);
		IS_MOTIF = "motif".equals(platform);
	}


	/**
	 * @param parentShell
	 */
	public SendIMWindow(MainShell main, User f) {
		super(main.getShell());
		this.main = main;
		this.model = f;
		this.res = Resources.getInstance();
		blinkRunnable = new Blink();
		blinkRunnable.setBlinkImage(HeadFactory.getSmallHeadByStatus(f));
		styleCache = new ArrayList<LineStyle>();
		handCursor = main.getDisplay().getSystemCursor(SWT.CURSOR_HAND);

		resolveIPLocation(f);

		setBlockOnOpen(false);
	}

	/**
	 * 解析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;
		}
	}

	public String getIPText() {
		if (ip.equals(""))
			return place;
		else
			return ip + " - " + place;
	}

	/**
	 * 刷新
	 * 
	 * @param f
	 */
	public void refresh(User f) {
		model = f;
		setFriendNameLabel();
	}

	/**
	 * 得到行样式
	 * 
	 * @param fs
	 * @return
	 */
	private LineStyle getLineStyle(FontStyle fs) {
		int fontStyle = 0;
		if (fs.isBold())
			fontStyle |= SWT.BOLD;
		if (fs.isItalic())
			fontStyle |= SWT.ITALIC;
		if (fontStyle == 0)
			fontStyle = SWT.NORMAL;

		return getLineStyle(fs.getFontName(), fontStyle, fs.getFontSize(), fs.getRed(), fs.getGreen(), fs.getBlue());
	}

	/**
	 * 根据一个现有的style,在cache里面查找一个相同的style,没有则新建一个
	 * 
	 * @param style
	 * @return
	 */
	private LineStyle getLineStyle(LineStyle ls) {
		int size = styleCache.size();
		for (int i = 0; i < size; i++) {
			LineStyle style = styleCache.get(i);
			if (style.equals(ls))
				return style;
		}

		LineStyle style = (LineStyle) ls.clone();
		styleCache.add(style);
		return style;
	}

	/**
	 * 根据具体的样式信息查找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;
	}

	/**
	 * 初始化使用Enter键的菜单
	 */
	private void initEnterMenu() {
		enterMenu = new Menu(getShell(), SWT.POP_UP);
		// 使用Enter键菜单
		MenuItem mi = new MenuItem(enterMenu, SWT.RADIO);
		mi.setText(im_menu_use_enter);
		mi.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				inputBox.removeUserKeyAction(SWT.MOD1 | SWT.CR);
				inputBox.setUserKeyAction(SWT.CR, sendAction);
				inputBox.setKeyBinding(SWT.MOD1 | SWT.CR, RichBox.NEW_LINE);
				if (model.talkMode)
					main.getOptionHelper().setUseEnterInTalkMode(true);
				else
					main.getOptionHelper().setUseEnterInMessageMode(true);
			}
		});
		// 使用Ctrl + Enter键菜单
		mi = new MenuItem(enterMenu, SWT.RADIO);
		mi.setText(im_menu_use_ctrl_enter);
		mi.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				inputBox.removeUserKeyAction(SWT.CR);
				inputBox.setUserKeyAction(SWT.MOD1 | SWT.CR, sendAction);
				inputBox.setKeyBinding(SWT.CR, RichBox.NEW_LINE);
				if (model.talkMode)

⌨️ 快捷键说明

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