📄 chatwin.java
字号:
package com.jim.client;
import java.util.Date;
import org.apache.log4j.Logger;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TypedEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import com.jim.database.JIMUser;
import com.jim.net.Message;
import com.swtdesigner.SWTResourceManager;
public class ChatWin extends Shell {
private StyledText inputText;
private StyledText recordText;
private Logger log = Logger.getLogger(ChatWin.class);
private int key;
private int myJimno;
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Display display = Display.getDefault();
ChatWin shell = new ChatWin(display, SWT.SHELL_TRIM);
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the shell
* @param display
* @param style
*/
public ChatWin(Display display, int style) {
super(display, style);
createContents();
}
/**
* Create contents of the window
*/
protected void createContents() {
setText("【聊天】");
setSize(500, 375);
setLayout(new FormLayout());
setImage(SWTResourceManager.getImage(ChatWin.class, "resources/1.bmp"));
setMinimumSize(new Point(250, 200));
final SashForm composite = new SashForm(this, SWT.BORDER | SWT.VERTICAL);
recordText = new StyledText(composite, SWT.V_SCROLL | SWT.MULTI | SWT.READ_ONLY | SWT.WRAP);
recordText.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
composite.setOrientation(SWT.VERTICAL);
recordText.setText("");
final FormData formData = new FormData();
formData.right = new FormAttachment(100, -5);
formData.top = new FormAttachment(0, 5);
formData.left = new FormAttachment(0, 5);
composite.setLayoutData(formData);
composite.setLayout(new FormLayout());
Button closeB;
closeB = new Button(this, SWT.NONE);
closeB.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
close(event);
}
});
formData.bottom = new FormAttachment(closeB, -13, SWT.DEFAULT);
inputText = new StyledText(composite, SWT.MULTI | SWT.BORDER | SWT.WRAP);
inputText.setFont(SWTResourceManager.getFont("宋体", 11, SWT.NONE));
inputText.setTextLimit(500);
inputText.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent event) {
//log.debug(event);
if(event.keyCode == 13 && event.stateMask == SWT.CTRL){
event.doit = false;
sendMsg(event);
}
}
});
composite.setWeights(new int[] {2, 1 });
final FormData formData_1 = new FormData();
formData_1.bottom = new FormAttachment(100, -14);
formData_1.top = new FormAttachment(100, -36);
formData_1.right = new FormAttachment(100, -127);
formData_1.left = new FormAttachment(100, -192);
closeB.setLayoutData(formData_1);
closeB.setText("关闭");
Button sendB;
sendB = new Button(this, SWT.NONE);
sendB.setToolTipText("使用Ctrl+Enter发送消息");
sendB.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
sendMsg(event);
}
});
final FormData formData_2 = new FormData();
formData_2.bottom = new FormAttachment(100, -14);
formData_2.top = new FormAttachment(100, -36);
formData_2.right = new FormAttachment(100, -42);
formData_2.left = new FormAttachment(100, -107);
sendB.setLayoutData(formData_2);
sendB.setText("发送");
//
}
protected void close(SelectionEvent event) {
MainWin.removeWin(key);
this.close();
this.dispose();
}
protected void sendMsg(TypedEvent event) {
/*String inStr = inputText.getText();
if(inStr.equals("abc"))
recordsText.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_GREEN));
else
recordsText.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK));
inStr += inputText.getLineDelimiter();
recordsText.append(inStr);
inputText.setText("");*/
String inStr = inputText.getText();
log.debug(inStr.length());
if(event instanceof KeyEvent){
if(inStr.endsWith("\r\n"))
inStr = inStr.substring(0, inStr.length()-2);
}
if(inStr == null || inStr.equals("")){
inputText.setFocus();
log.debug("empty message");
return;
}
inStr += inputText.getLineDelimiter();
Message msg = new Message(MainWin.client.getUser().getJimno(),key);
msg.addContent(inStr);
msg.setSendTime(new Date());
MainWin.client.sendMessage(msg);
appendMsg(msg);
/*int start = recordText.getText().length();
recordText.append(inStr);
recordText.setSelection(recordText.getCharCount());
int length = inStr.length();
if (inStr.startsWith("abc")) {
StyleRange sr = new StyleRange(start, length, Display
.getDefault().getSystemColor(SWT.COLOR_GREEN), recordText
.getBackground());
recordText.setStyleRange(sr);
}*/
inputText.setText("");
inputText.setFocus();
}
@Override
protected void checkSubclass() {
// Disable the check that prevents subclassing of SWT components
}
public synchronized void appendMsg(Message msg){
int start = recordText.getText().length();
int length = msg.getContent().length();
JIMUser u = MainWin.client.getUser();
if(myJimno != msg.getFrom() ){
u = MainWin.client.getUser().getFriend(msg.getFrom());
}
String toAppend = "";
if (u != null) {
if (u.getNicknm() == null || u.getNicknm().equals(""))
toAppend +="[" +u.getJimno()+"]";
else
toAppend +="["+ u.getNicknm()+"]";
}
toAppend += " "+msg.getSendTime().toLocaleString()+ "\r\n";
length += toAppend.length();
recordText.append(toAppend);
recordText.append(msg.getContent());
recordText.setSelection(recordText.getCharCount());
if(myJimno != msg.getFrom() ){
StyleRange sr = new StyleRange(start, length, Display
.getDefault().getSystemColor(SWT.COLOR_GREEN), recordText
.getBackground());
recordText.setStyleRange(sr);
}
}
public synchronized int getMyJimno() {
return myJimno;
}
public synchronized void setMyJimno(int myJimno) {
this.myJimno = myJimno;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -