📄 chatjpanel.java
字号:
private ActionListener getTimerActionListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jLabel1.getIcon() == connectedIcon)
{
jLabel1.setIcon(connectingIcon);
}
else
{
jLabel1.setIcon(connectedIcon);
}
}
};
}
private void update(ChatServiceManager subject, Packet packet) {
if (packet instanceof Presence)
{
// getFrom : malaysia_jstock@conference.jabber.org/yccheok
// getType() : available
final Presence presence = (Presence)packet;
final String[] whos = presence.getFrom().split("/");
final String who = whos.length >= 2 ? whos[1] : whos[0];
Message message = null;
if (presence.getType() == Presence.Type.available) {
message = Message.newInstance(who, who + " entered the room.", Message.Mode.System);
this.addUser(who);
if (MainFrame.getJStockOptions().isChatSoundNotificationEnabled()) {
Utils.playSound(Utils.Sound.LOGIN);
}
}
else {
message = Message.newInstance(who, who + " left the room.", Message.Mode.System);
this.removeUser(who);
if (MainFrame.getJStockOptions().isChatSoundNotificationEnabled()) {
Utils.playSound(Utils.Sound.LOGOUT);
}
}
this.showMessage(message);
}
else if (packet instanceof org.jivesoftware.smack.packet.Message)
{
final org.jivesoftware.smack.packet.Message message = (org.jivesoftware.smack.packet.Message)packet;
final String[] whos = message.getFrom().split("/");
final String who = whos.length >= 2 ? whos[1] : whos[0];
final Message msg = Message.newInstance(who, message.getBody(), MainFrame.getJStockOptions().getChatUsername().equals(who) ? Message.Mode.Mine : Message.Mode.Other);
if (msg.mode == Message.Mode.Mine)
{
if (MainFrame.getJStockOptions().isChatSoundNotificationEnabled()) {
Utils.playSound(Utils.Sound.SEND);
}
}
else
{
if (MainFrame.getJStockOptions().isChatSoundNotificationEnabled()) {
Utils.playSound(Utils.Sound.RECEIVE);
}
}
this.showMessage(msg);
}
else
{
}
}
private void update(ChatServiceManager subject, State state) {
switch(state)
{
case CONNECTING:
if (timer == null)
{
timer = new Timer(TIMER_DELAY, getTimerActionListener());
timer.setInitialDelay(0);
timer.start();
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jLabel1.setToolTipText("Connecting to chat server...");
if (!firstTime) {
// Not sure why. Having the JSplitPane resizeWeight and all JList
// minimum, maximum, preferred size being specified, the divider position
// still not correct. Unless we have some item inside the JList. Weird ?!
ChatJPanel.this.removeAllUsers();
ChatJPanel.this.removeAllChannels();
}
}
});
break;
case CONNECTED:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jLabel1.setToolTipText("Connected");
jLabel1.setText(MainFrame.getJStockOptions().getChatUsername());
}
});
break;
case ACCOUNT_CREATING:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jLabel1.setToolTipText("Creating new account...");
}
});
break;
case ROOM_CREATING:
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jLabel1.setToolTipText("Creating new room...");
}
});
break;
case ROOM_CREATED:
if (timer != null)
{
timer.stop();
timer = null;
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jLabel1.setToolTipText("Room created");
jLabel1.setIcon(connectedIcon);
ChatJPanel.this.addChannel(Utils.getRoomName(MainFrame.getJStockOptions().getCountry()));
}
});
break;
}
}
private MainFrame getMainFrame()
{
if (mainFrame == null)
{
mainFrame = MainFrame.getMe();
}
return mainFrame;
}
private org.yccheok.jstock.engine.Observer<ChatServiceManager, Packet> getChatServiceManagerPacketObserver()
{
return new org.yccheok.jstock.engine.Observer<ChatServiceManager, Packet>() {
@Override
public void update(ChatServiceManager subject, Packet arg) {
ChatJPanel.this.update(subject, arg);
}
};
}
private org.yccheok.jstock.engine.Observer<ChatServiceManager, ChatServiceManager.State> getChatServiceManagerStateObserver()
{
return new org.yccheok.jstock.engine.Observer<ChatServiceManager, ChatServiceManager.State>() {
@Override
public void update(ChatServiceManager subject, State arg) {
ChatJPanel.this.update(subject, arg);
}
};
}
public void clearListsSelection()
{
if (SwingUtilities.isEventDispatchThread()) {
jList1.getSelectionModel().clearSelection();
jList2.getSelectionModel().clearSelection();
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
jList1.getSelectionModel().clearSelection();
jList2.getSelectionModel().clearSelection();
}
});
}
}
private void addUser(final String name)
{
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList2.getModel())).addElement(name);
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (((DefaultListModel)(jList2.getModel())).contains(name) == false) {
((DefaultListModel)(jList2.getModel())).addElement(name);
}
}
});
}
}
private void removeUser(final String name) {
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList2.getModel())).removeElement(name);
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((DefaultListModel)(jList2.getModel())).removeElement(name);
}
});
}
}
private void removeAllUsers() {
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList2.getModel())).removeAllElements();
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((DefaultListModel)(jList2.getModel())).removeAllElements();
}
});
}
}
private void addChannel(final String channel)
{
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList1.getModel())).addElement(channel);
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((DefaultListModel)(jList1.getModel())).addElement(channel);
}
});
}
}
private static class JTextFieldEx extends JTextField implements KeyListener {
private static final int MEMORY_SIZE = 100;
private static final List<String> memories = new ArrayList<String>();
private int read_index =0;
private int write_index =0;
public JTextFieldEx() {
super();
this.addKeyListener(this);
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_UP)
{
this.setText(memories.get(read_index));
read_index--;
if (read_index < 0) {
read_index = (memories.size() - 1);
}
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
this.setText(memories.get(read_index));
read_index++;
read_index = read_index % memories.size();
}
else if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
memories.add(write_index, this.getText());
read_index = write_index;
write_index++;
write_index = write_index % MEMORY_SIZE;
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
private void removeChannel(final String channel) {
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList1.getModel())).removeElement(channel);
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((DefaultListModel)(jList1.getModel())).removeElement(channel);
}
});
}
}
private void removeAllChannels() {
if (SwingUtilities.isEventDispatchThread()) {
((DefaultListModel)(jList1.getModel())).removeAllElements();
}
else {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
((DefaultListModel)(jList1.getModel())).removeAllElements();
}
});
}
}
public boolean isLogin() {
return this.chatServiceManager.isLogin();
}
public boolean changePassword(String newPassword) {
return this.chatServiceManager.changePassword(newPassword);
}
private final ChatServiceManager chatServiceManager = new ChatServiceManager();
private final org.yccheok.jstock.engine.Observer<ChatServiceManager, Packet> chatServiceManagerPacketObserver = this.getChatServiceManagerPacketObserver();
private final org.yccheok.jstock.engine.Observer<ChatServiceManager, ChatServiceManager.State> chatServiceManagerStateObserver = this.getChatServiceManagerStateObserver();
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JEditorPane jEditorPane1;
private javax.swing.JLabel jLabel1;
private javax.swing.JList jList1;
private javax.swing.JList jList2;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JSplitPane jSplitPane2;
private javax.swing.JTextField jTextField1;
// End of variables declaration//GEN-END:variables
private final javax.swing.ImageIcon connectedIcon = new javax.swing.ImageIcon(getClass().getResource("/images/16x16/smile.png"));
private final javax.swing.ImageIcon connectingIcon = new javax.swing.ImageIcon(getClass().getResource("/images/16x16/smile-gray.png"));
private static final Log log = LogFactory.getLog(ChatJPanel.class);
private boolean firstTime = true;
private MainFrame mainFrame = null;
private Timer timer = null;
private static final int TIMER_DELAY = 500;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -