📄 sendsmsshell.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.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
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.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import edu.tsinghua.lumaqq.IconHolder;
import edu.tsinghua.lumaqq.LumaQQ;
import edu.tsinghua.lumaqq.events.FriendSelectionEvent;
import edu.tsinghua.lumaqq.events.IFriendSelectionListener;
import edu.tsinghua.lumaqq.models.FriendModel;
import edu.tsinghua.lumaqq.models.MobileModel;
import edu.tsinghua.lumaqq.qq.QQ;
import edu.tsinghua.lumaqq.qq.Util;
import edu.tsinghua.lumaqq.qq.events.QQEvent;
import edu.tsinghua.lumaqq.qq.events.IQQListener;
import edu.tsinghua.lumaqq.qq.packets.in.SendSMSReplyPacket;
import edu.tsinghua.lumaqq.qq.packets.out.SendSMSPacket;
import edu.tsinghua.lumaqq.ui.dialogs.SMSResultDialog;
import edu.tsinghua.lumaqq.ui.tool.AroundBorderPaintListener;
import edu.tsinghua.swt.widgets.CoolButton;
import edu.tsinghua.swt.widgets.MySWT;
/**
* <pre>
* 发送短消息的窗口
* </pre>
*
* @author 马若劼
*/
public class SendSMSShell extends ShellAdapter implements DisposeListener, IQQListener, IFriendSelectionListener {
// Log对象
protected static Log log = LogFactory.getLog(SendSMSShell.class);
// 主shell
private MainShell main;
// 本窗口shell
private Shell shell;
// display
private Display display;
// 资源管理类
private IconHolder icons = IconHolder.getInstance();
private Text textSenderName, textReceiver;
private StyledText textInput;
private Button chkSplit, chkHandfree;
private CoolButton btnBlink, btnSend;
private Label lblHint;
private Font font;
// 要发送的消息
private byte[][] message;
private char messageSequence;
private boolean sendToMobile;
private String receiver;
private Object model;
// 期待的回复包序号
private char nextAck;
// 还可以输入多少字符
private int available;
/** 发送内容类型 */
private byte contentType;
/** 内容类型编号 */
private int contentId;
// 存放每条短消息的回复
private List reply;
/** 单条和三条的最大字符数,如果选择了分条发送,则最多发3条 */
private static final int MAX_ONE = 116;
private static final int MAX_THREE = 324;
/** 闪烁的标识符 */
private static final byte BLINK = 0x01;
// 按钮的背景色
private Color buttonColor, toolbarBackground;
// 好友选择窗口
private FriendSelectShell fss;
public SendSMSShell(MainShell main) {
this.main = main;
this.display = main.getDisplay();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setSize(600, 250);
shell.setImage(icons.getImage(IconHolder.icoMobile));
shell.setText(LumaQQ.getString("send.sms.title"));
// 添加事件监听器
shell.addDisposeListener(this);
shell.addShellListener(this);
// 初始化变量
initVariable();
// 初始化布局
initLayout();
}
/**
* 初始化布局
*/
private void initLayout() {
GridLayout layout = new GridLayout();
layout.verticalSpacing = layout.horizontalSpacing = 0;
shell.setLayout(layout);
// 发送到指定手机
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
comp.setLayout(new GridLayout(3, false));
Label lblSendTo = new Label(comp, SWT.LEFT);
lblSendTo.setText(LumaQQ.getString("send.sms.lable.send.to"));
lblSendTo.setLayoutData(new GridData());
textReceiver = new Text(comp, SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY);
textReceiver.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
Button btnReceiver = new Button(comp, SWT.PUSH);
btnReceiver.setText(LumaQQ.getString("send.sms.button.receiver"));
btnReceiver.setLayoutData(new GridData());
btnReceiver.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
// 判断窗口是否dispose,因为linux下面这个窗口是有关闭按钮的
// 而windows下面没有,所以在linux下面这个窗口可能被dispose
if(fss.isDisposed())
createFriendSelectShell();
if(fss.isVisible())
fss.setVisible(false);
else {
// 把当前接收者置为选择状态
fss.select(model);
fss.setVisible(true);
}
}
});
/*
* 这些功能觉得没什么用处,暂时不要
*/
/*
TabFolder folder = new TabFolder(shell, SWT.TOP);
folder.setLayoutData(new GridData(GridData.FILL_BOTH));
// 言语传情
TabItem item = new TabItem(folder, SWT.NONE);
item.setText(LumaQQ.getString("send.sms.tab.love.word"));
Composite itemComp = new Composite(folder, SWT.NONE);
itemComp.setLayout(new GridLayout());
item.setControl(itemComp);
Label lblTemp = new Label(itemComp, SWT.NONE);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
lblTemp.setText("This function is under development");
// 精美图片
item = new TabItem(folder, SWT.NONE);
item.setText(LumaQQ.getString("send.sms.tab.picture"));
itemComp = new Composite(folder, SWT.NONE);
itemComp.setLayout(new GridLayout());
item.setControl(itemComp);
lblTemp = new Label(itemComp, SWT.NONE);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
lblTemp.setText("This function is under development");
// 悦耳铃声
item = new TabItem(folder, SWT.NONE);
item.setText(LumaQQ.getString("send.sms.tab.ring"));
itemComp = new Composite(folder, SWT.NONE);
itemComp.setLayout(new GridLayout());
item.setControl(itemComp);
lblTemp = new Label(itemComp, SWT.NONE);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
lblTemp.setText("This function is under development");
// 缤纷彩信
item = new TabItem(folder, SWT.NONE);
item.setText(LumaQQ.getString("send.sms.tab.color.sms"));
itemComp = new Composite(folder, SWT.NONE);
itemComp.setLayout(new GridLayout());
item.setControl(itemComp);
lblTemp = new Label(itemComp, SWT.NONE);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
lblTemp.setText("This function is under development");
// 靓照集
item = new TabItem(folder, SWT.NONE);
item.setText(LumaQQ.getString("send.sms.tab.ppmm"));
itemComp = new Composite(folder, SWT.NONE);
itemComp.setLayout(new GridLayout());
item.setControl(itemComp);
lblTemp = new Label(itemComp, SWT.NONE);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
lblTemp.setText("This function is under development");
*/
Composite middleBar = new Composite(shell, SWT.NONE);
middleBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
middleBar.setBackground(toolbarBackground);
layout = new GridLayout();
layout.horizontalSpacing = 5;
layout.verticalSpacing = 0;
layout.marginHeight = 1;
layout.marginWidth = 5;
layout.numColumns = 6;
middleBar.setLayout(layout);
middleBar.addPaintListener(new AroundBorderPaintListener(new Class[] { Text.class }));
// 提示标签
lblHint = new Label(middleBar, SWT.LEFT);
lblHint.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
lblHint.setBackground(toolbarBackground);
// 发送者名称
Label lblTemp = new Label(middleBar, SWT.RIGHT);
lblTemp.setText(LumaQQ.getString("send.sms.label.sender"));
lblTemp.setBackground(toolbarBackground);
lblTemp.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
textSenderName = new Text(middleBar, SWT.SINGLE);
GridData gd = new GridData(GridData.VERTICAL_ALIGN_CENTER);
gd.widthHint = 100;
textSenderName.setLayoutData(gd);
textSenderName.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
shortenName();
calcAvailable();
shortenMessage();
setHint();
}
});
// 分条发送
chkSplit = new Button(middleBar, SWT.CHECK);
chkSplit.setBackground(toolbarBackground);
chkSplit.setText(LumaQQ.getString("send.sms.button.split"));
chkSplit.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
chkSplit.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
calcAvailable();
shortenMessage();
setHint();
}
});
// 免提短信
chkHandfree = new Button(middleBar, SWT.CHECK);
chkHandfree.setBackground(toolbarBackground);
chkHandfree.setText(LumaQQ.getString("send.sms.button.handfree"));
chkHandfree.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
// 闪烁
btnBlink = new CoolButton(middleBar, SWT.RIGHT | MySWT.FLAT | MySWT.HOVER, LumaQQ.getString("send.sms.button.blink"), icons.getImage(IconHolder.icoSMSBlink));
btnBlink.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_CENTER));
btnBlink.setBackground(toolbarBackground);
btnBlink.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
// 检查字符数是否足够
if(available < 4) {
MessageDialog.openWarning(shell, LumaQQ.getString("message.box.common.warning.title"), LumaQQ.getString("message.box.cannot.insert.blink"));
return;
}
// 如果够,则插入闪烁效果
if(textInput.getSelectionCount() > 0) {
Point range = textInput.getSelectionRange();
StyleRange style = new StyleRange(range.x, range.y, display.getSystemColor(SWT.COLOR_RED), null);
textInput.setStyleRange(style);
calcAvailable();
setHint();
}
}
});
// 输入框
comp = new Composite(shell, SWT.NONE);
gd = new GridData(GridData.FILL_BOTH);
gd.heightHint = 80;
comp.setLayoutData(gd);
comp.setLayout(new GridLayout());
comp.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
comp.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
Composite c = (Composite)e.getSource();
Rectangle rect = c.getClientArea();
rect.width--;
rect.height--;
e.gc.drawRectangle(rect);
}
});
textInput = new StyledText(comp, SWT.MULTI | SWT.WRAP);
textInput.setLayoutData(new GridData(GridData.FILL_BOTH));
textInput.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent e) {
calcAvailable();
shortenMessage();
setHint();
}
});
textInput.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.stateMask == SWT.ALT) {
if(e.character == 's' || e.character == 'S')
send();
else if(e.character == 'c' || e.character == 'C')
shell.close();
}
}
});
// 按钮区
comp = new Composite(shell, SWT.NONE);
comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
layout = new GridLayout();
layout.numColumns = 2;
layout.makeColumnsEqualWidth = false;
comp.setLayout(layout);
CoolButton btnClose = new CoolButton(comp, SWT.CENTER | MySWT.HOVER, LumaQQ.getString("send.sms.button.close"), null);
gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
gd.grabExcessHorizontalSpace = true;
gd.widthHint = 80;
btnClose.setLayoutData(gd);
btnClose.setBackground(buttonColor);
btnClose.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
shell.close();
}
});
btnSend = new CoolButton(comp, SWT.CENTER | MySWT.HOVER, LumaQQ.getString("send.sms.button.send"), null);
gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
gd.widthHint = 80;
btnSend.setLayoutData(gd);
btnSend.setBackground(buttonColor);
btnSend.addMouseListener(new MouseAdapter() {
public void mouseUp(MouseEvent e) {
send();
}
});
// 设置发送者名称
textSenderName.setText(main.getMyModel().getName());
// 设置提示信息
shortenName();
calcAvailable();
setHint();
}
/**
* 开始发送
*/
protected void send() {
// 检查用户是否输入了内容
if(textInput.getText().equals("")) {
MessageDialog.openWarning(shell, LumaQQ.getString("message.box.common.warning.title"), LumaQQ.getString("message.box.please.enter.sms"));
return;
}
// 检查用户是否选择了接收者
if(receiver == null) {
MessageDialog.openWarning(shell, LumaQQ.getString("message.box.common.warning.title"), LumaQQ.getString("message.box.please.specify.receiver"));
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -