View Javadoc

1   package net.sourceforge.simplegamenet.framework.gui;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import javax.swing.*;
6   import javax.swing.event.DocumentListener;
7   import javax.swing.event.DocumentEvent;
8   import javax.swing.text.StyledDocument;
9   import net.sourceforge.simplegamenet.framework.model.ChatMessage;
10  import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
11  import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
12  
13  /***
14   * A <code>JPanel</code> which allows a user to send chat messages and shows received chat messages
15   * and events in a log. One instance is shown in the "Chat" tab.
16   *
17   * @author Geoffrey De Smet
18   * @version 1.0, 2003-06-18
19   * @see net.sourceforge.simplegamenet.specs.gui.UtilityPanelFactory#createChatClientPanel()
20   */
21  public class ChatClientPanel extends JPanel implements ActionListener {
22  
23      private ClientGUIFacade clientGUIFacade = null;
24  
25      private JTextPane logTextPane;
26      private JScrollPane logScrollPane;
27      private JTextField messageTextField = new JTextField("", 50);
28      private JButton sendButton = new JButton("Send");
29  
30      public ChatClientPanel(StyledDocument chatLogDocument) {
31          ProportionLayout layout = new ProportionLayout();
32          layout.appendColumn(50, 1.0);                           // 0
33          layout.appendColumn(10);                                // 1 empty
34          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 2
35          layout.appendRow(0, 1.0);                            // 0
36          layout.appendRow(10);                                // 1 empty
37          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 2
38          setLayout(layout);
39  
40          logTextPane = new JTextPane(chatLogDocument);
41          logTextPane.setEnabled(false);
42          logScrollPane = new JScrollPane(logTextPane,
43                  JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
44                  JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
45          chatLogDocument.addDocumentListener(new DocumentListener() {
46              public void changedUpdate(DocumentEvent e) {
47                  scrollToEnd();
48              }
49              public void insertUpdate(DocumentEvent e) {
50                  scrollToEnd();
51              }
52              public void removeUpdate(DocumentEvent e) {
53                  scrollToEnd();
54              }
55              private void scrollToEnd() {
56                  logScrollPane.getVerticalScrollBar().setValue(Integer.MAX_VALUE);
57              }
58          });
59          add(logScrollPane, new ProportionConstraints(0, 0, 3, 1, 0, 0));
60          messageTextField.addActionListener(this);
61          add(messageTextField,
62                  new ProportionConstraints(0, 2, 1, 1, 0, 2, ProportionConstraints.CENTER,
63                          ProportionConstraints.HORIZONTAL));
64          sendButton.addActionListener(this);
65          add(sendButton, new ProportionConstraints(2, 2));
66      }
67  
68      public void gameJoined(ClientGUIFacade clientGUIFacade) {
69          this.clientGUIFacade = clientGUIFacade;
70          setEnabled(true);
71      }
72  
73      public void joinedGameQuit() {
74          setEnabled(false);
75          clientGUIFacade = null;
76      }
77  
78      public void setEnabled(boolean enabled) {
79          messageTextField.setEnabled(enabled);
80          sendButton.setEnabled(enabled);
81      }
82  
83      public void actionPerformed(ActionEvent actionEvent) {
84          if (clientGUIFacade != null && !messageTextField.getText().equals("")) {
85              try {
86                  String message = messageTextField.getText();
87                  if (message.startsWith("/me ")) {
88                      clientGUIFacade.sendChatMessage(ChatMessage.PLAYER_EMOTE,
89                              message.substring(4));
90                  } else if (message.startsWith("/emote ")) {
91                      clientGUIFacade.sendChatMessage(ChatMessage.PLAYER_EMOTE,
92                              message.substring(7));
93                  } else {
94                      clientGUIFacade.sendChatMessage(ChatMessage.PLAYER_MESSAGE,
95                              message);
96                  }
97                  messageTextField.setText("");
98                  messageTextField.requestFocus();
99              } catch (IllegalStateException exception) {
100                 JOptionPane.showMessageDialog(getTopLevelAncestor(),
101                         "You are not allowed to send a chat message at this time.",
102                         "Send chat message failed", JOptionPane.ERROR_MESSAGE);
103             }
104         }
105     }
106 
107 }