📄 receiveimwindow.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 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.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.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import edu.tsinghua.lumaqq.Colors;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.IQQNode;
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.ui.config.sys.SystemOptionWindow;
import edu.tsinghua.lumaqq.ui.config.user.UserInfoWindow;
import edu.tsinghua.lumaqq.ui.tool.HeadFactory;
import edu.tsinghua.lumaqq.ui.tool.ShellFactory;
import edu.tsinghua.lumaqq.utils.ReplyUtil;
import edu.tsinghua.lumaqq.widgets.QButton;
import edu.tsinghua.lumaqq.widgets.rich.LineStyle;
import edu.tsinghua.lumaqq.widgets.rich.RichBox;
/**
* 接收消息窗口,用来查看收到的消息
*
* @author luma
*/
public class ReceiveIMWindow extends Window implements ShellListener {
private FriendModel model;
private MainShell main;
private IconHolder icons;
// 表示当前窗口是否是active的
private boolean active;
private CLabel lblName;
private CLabel lblList;
private RichBox outputBox;
private Cursor handCursor;
private QButton btnNext, btnQuickReply;
private Menu quickReplyMenu;
private Label lblPlace, lblIp;
private ViewForm inputForm;
private String ip, place;
// 用作临时用途
private Date date = new Date();
private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 回复信息工具类实例
private ReplyUtil replies = ReplyUtil.getInstance();
// style样式表缓冲区
private List 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);
// 关闭窗口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(main.getMVCHelper().getFriendViewPart(model.getQQ()));
if(!main.getMessageQueue().hasMessage(model.getQQ()))
setNextButtonEnabled(false);
}
}
};
/**
* @param parentShell
*/
public ReceiveIMWindow(MainShell main, FriendModel f) {
super(main.getShell());
this.main = main;
this.model = f;
this.icons = IconHolder.getInstance();
styleCache = new ArrayList();
handCursor = main.getDisplay().getSystemCursor(SWT.CURSOR_HAND);
setBlockOnOpen(false);
}
/**
* 初始化快捷回复菜单
*/
private void initQuickReplyMenu() {
quickReplyMenu = new Menu(getShell(), SWT.POP_UP);
// 循环初始化菜单项,有多少条快捷回复消息就创建多少菜单项,最后加上一个自定义项
for(int i = 0; i < replies.getQuickReplySize(); i++) {
final int j = i;
final MenuItem mi = new MenuItem(quickReplyMenu, SWT.RADIO);
mi.setText(replies.getQuickReply(i));
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
if(mi.getSelection()) {
replies.setCurrentQuickReply(j);
quickReply();
}
}
});
}
MenuItem separator = new MenuItem(quickReplyMenu, SWT.SEPARATOR);
// 自定义
MenuItem mi = new MenuItem(quickReplyMenu, SWT.PUSH);
mi.setImage(icons.getImage(IconHolder.icoReply));
mi.setText(LumaQQ.getString("menu.quick.reply.addnew"));
mi.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
main.getShellLauncher().openSystemOptionWindow().setCurrentPage(SystemOptionWindow.REPLY);
}
});
// 设置菜单项的选择状态
quickReplyMenu.getItem(replies.getCurrentQuickReply()).setSelection(true);
}
/**
* 快速回复
*/
protected void quickReply() {
SendIMWindow sms = openSendIMWindow();
getShell().close();
sms.sendMessage(replies.getCurrentQuickReplyString());
}
/**
* 打开回复消息窗口
*/
private SendIMWindow openSendIMWindow() {
SendIMWindow sms = main.getShellRegistry().getSendIMWindow(model);
if(sms == null) {
sms = ShellFactory.createSendIMWindow(main, model);
// 设置ip信息
sms.setIp(lblIp.getText());
sms.setPlace(lblPlace.getText());
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.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,没有则新建一个
*
* @param style
* @return
*/
private LineStyle getLineStyle(LineStyle ls) {
int size = styleCache.size();
for(int i = 0; i < size; i++) {
LineStyle style = (LineStyle)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 = (LineStyle)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()
*/
protected Shell getParentShell() {
return null;
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
*/
protected void configureShell(Shell newShell) {
newShell.setText(LumaQQ.getString("receive.im.title", new Object[] { model.getName() }));
newShell.setImage(icons.getImage(IconHolder.icoMessage));
newShell.addControlListener(new ControlAdapter() {
public void controlMoved(ControlEvent e) {
// 保存自己的位置,以便下次打开窗口时在同样的位置
Point loc = ((Shell)e.getSource()).getLocation();
model.addProperty(IQQNode.LOCATION_X, new Integer(loc.x));
model.addProperty(IQQNode.LOCATION_Y, new Integer(loc.y));
}
});
newShell.addDisposeListener(new DisposeListener() {
public void widgetDisposed(DisposeEvent e) {
int size = styleCache.size();
for(int i = 0; i < size; i++) {
LineStyle style = (LineStyle)styleCache.get(i);
if(style.foreground != null)
style.foreground.dispose();
if(style.background != null)
style.background.dispose();
}
}
});
}
/* (non-Javadoc)
* @see org.eclipse.jface.window.Window#getShellListener()
*/
protected ShellListener getShellListener() {
return this;
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.ShellListener#shellActivated(org.eclipse.swt.events.ShellEvent)
*/
public void shellActivated(ShellEvent e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -