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

📄 sendimwindow.java

📁 java写的qq代码实现qq的部分功能
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
* 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 java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
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.FileDialog;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import edu.tsinghua.lumaqq.Colors;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.events.IFaceSelectionListener;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.IQQNode;
import edu.tsinghua.lumaqq.qq.QQ;
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.events.FileEvent;
import edu.tsinghua.lumaqq.qq.events.IFileListener;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.filetrans.FileReceiver;
import edu.tsinghua.lumaqq.qq.filetrans.FileSender;
import edu.tsinghua.lumaqq.qq.filetrans.FileWatcher;
import edu.tsinghua.lumaqq.qq.filetrans.SlideWindow;
import edu.tsinghua.lumaqq.qq.packets.in.ReceiveIMPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SendIMPacket;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.ui.tool.HeadFactory;
import edu.tsinghua.lumaqq.ui.tool.MessageIDGenerator;
import edu.tsinghua.lumaqq.utils.OptionUtil;
import edu.tsinghua.lumaqq.widgets.FaceImageProvider;
import edu.tsinghua.lumaqq.widgets.FriendMessageExporter;
import edu.tsinghua.lumaqq.widgets.FriendMessageFormatter;
import edu.tsinghua.lumaqq.widgets.IImageProvider;
import edu.tsinghua.lumaqq.widgets.ImageSelector;
import edu.tsinghua.lumaqq.widgets.QButton;
import edu.tsinghua.lumaqq.widgets.RecordViewer;
import edu.tsinghua.lumaqq.widgets.rich.IRichContent;
import edu.tsinghua.lumaqq.widgets.rich.LineStyle;
import edu.tsinghua.lumaqq.widgets.rich.RichBox;
import edu.tsinghua.swt.models.INode;

/**
 * 发送普通消息窗口
 * 
 * @author luma
 */
public class SendIMWindow extends Window implements IQQListener, ShellListener, IFileListener, IFaceSelectionListener {    
    /**
     * <pre>
     * 闪烁图标
     * </pre>
     * 
     * @author 马若劼
     */
    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);
	            else
	                getShell().setImage(icons.getImage(IconHolder.icoBlank));
	            flag = !flag;
	            if(!stop)
	                main.getDisplay().timerExec(500, this);
	            else
	                getShell().setImage(icons.getImage(IconHolder.icoMessage));
            } catch (SWTException e) {         
                // 这个操作可能会抛出SWTException,如果组件已经dispose的话,
                //     所以我们需要捕捉这个异常,不然程序可能崩溃
            }
        }

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

		public boolean isStop() {
			return stop;
		}
    }
    
    /**
     * 往输出框追加提示信息
     * 
     * @author luma
     */
    private class AppendRunnable implements Runnable {
        public String hint;
        
        /* (non-Javadoc)
         * @see java.lang.Runnable#run()
         */
        public void run() {
            appendHint('\n' + hint + "\n\n", hintStyle);
        }
    }
    
    /**
     * <pre>
     * 用来刷新文件传输的进度显示,由于这个会被频繁调用,因此建一个变量,减少对象创建数
     * 这个runnable只用来刷新当前文件会话
     * </pre>
     * 
     * @author 马若劼
     */
    private class RefreshProgressRunnable implements Runnable {
        public int selection;
        public int max;
        public String text;
        
        public void run() {
            bar.setMaximum(max);
            bar.setSelection(selection);
            lblPercent.setText(text);
        }
    }
    
    /**
     * <pre>
     * 文件任务列表菜单的事件监听器,在用户选择一项时,显示相应的文件的传输状态
     * </pre>
     * 
     * @author 马若劼
     */
    private class FileItemSelectionListener extends SelectionAdapter {
        public void widgetSelected(SelectionEvent e) {
            MenuItem mi = (MenuItem)e.widget;
            if(mi.getSelection())
                setCurrentFileSession((Integer)mi.getData());
        }
    }    
    
    private FriendModel model;
    private MainShell main;
    private IconHolder icons;
    
	// 表示当前窗口是否是active的
	private boolean active;
	
	private CLabel lblName;
	private CLabel lblList;
	private Label lblPercent, lblStatus, lblPlace, lblIp;
	private RichBox outputBox, inputBox;
	private RecordViewer viewer;
	private Menu enterMenu, fileMenu;
	private TableViewer listViewer;
	private Cursor handCursor;
	private ToolItem tiYes, tiNo;
	private ProgressBar bar;
	private QButton btnMode;
	private Sash sash;
	
	private Blink blinkRunnable;
	private AppendRunnable appendRunnable;
	
	private ViewForm inputForm, outputForm;
	
	private String ip, place;
	
	// true表示是聊天模式
	private boolean talkMode;
	
	// 用作临时用途
	private Date date = new Date();
	private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	// style样式表缓冲区
	private List styleCache;	
	// 提示信息的样式
	private LineStyle hintStyle;
	
	// 发送消息的参数
	private int totalFragments;
	private int currentFragment;
	private char messageId;
	private String fragment;
	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);
	
	// 存放文件守望者的哈希表
	private Hashtable watchers;
	// 当前显示的文件传输会话序号
	private Integer currentFileSessionId;
	// 文件任务列表事件监听器
	private FileItemSelectionListener fisl;
	// 文件传输进度刷新类
	private RefreshProgressRunnable refreshRunnable;
	
	// 关闭窗口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(!talkMode);
        }
	};
		
    //设置输入框为可输入
	private Runnable enableRunnable = new Runnable() {
        public void run() {
            inputBox.setReadonly(false);
            inputBox.setBackground(Colors.WHITE);
        }
    };
    
    // 系统平台
    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);
    }
    
    /**
     * @param parentShell
     */
    public SendIMWindow(MainShell main, FriendModel f) {
        super(main.getShell());
        this.main = main;
        this.model = f;
        this.icons = IconHolder.getInstance();
		blinkRunnable = new Blink();
		blinkRunnable.setBlinkImage(HeadFactory.getSmallHeadByStatus(f));
		appendRunnable = new AppendRunnable();
		refreshRunnable = new RefreshProgressRunnable();
		fisl = new FileItemSelectionListener();
		watchers = new Hashtable();
		hintStyle = new LineStyle(null, null, "宋体", SWT.NORMAL, 9);
		styleCache = new ArrayList();
		handCursor = main.getDisplay().getSystemCursor(SWT.CURSOR_HAND);
		talkMode = true;
        setBlockOnOpen(false);
    }
    
    /**
     * 得到行样式
     * 
     * @param im
     * @return
     */
    private LineStyle getLineStyle(NormalIM im) {
        int fontStyle = 0;
        if(im.bold)
            fontStyle |= SWT.BOLD;
        if(im.italic)
            fontStyle |= SWT.ITALIC;
        if(fontStyle == 0)
            fontStyle = SWT.NORMAL;
        
        return getLineStyle(im.fontName, fontStyle, im.fontSize, im.red, im.green, im.blue);
    }
    
    /**
     * 根据一个现有的style,在cache里面查找一个相同的style,没有则新建一个
     * 

⌨️ 快捷键说明

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