PageRenderTime 24ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/elements/ChatComponent.java

http://inequity.googlecode.com/
Java | 120 lines | 80 code | 17 blank | 23 comment | 1 complexity | 98f5f5fce0ea39bef5eed85833b9d078 MD5 | raw file
  1package elements;
  2
  3import java.awt.BorderLayout;
  4import java.awt.Dimension;
  5import javax.swing.Box;
  6import javax.swing.JButton;
  7import javax.swing.JPanel;
  8import javax.swing.JScrollPane;
  9import javax.swing.JTextArea;
 10import javax.swing.JTextField;
 11import javax.swing.SwingUtilities;
 12import network.ClientController;
 13import packets.ChatReq;
 14
 15/**
 16 * A chat widget in the form of a JPanel
 17 * @author Joel Garboden
 18 */
 19public class ChatComponent extends JPanel
 20{
 21  private ClientController controller;
 22  private JTextArea chatLog;
 23  private JScrollPane chatPane;
 24  private JTextField message;
 25  private JButton send;
 26
 27  /**
 28   * Constructor<br/>
 29   * Initiates the chat component and links to it's controller
 30   * @param controller client's controller
 31   * @param size preferred size of the chat widget
 32   */
 33  public ChatComponent(ClientController controller, Dimension size)
 34  {
 35    this.controller = controller;
 36
 37    setPreferredSize(size);
 38    
 39    setLayout(new BorderLayout());
 40    chatLog = new JTextArea();
 41    chatPane = new JScrollPane(chatLog);
 42    chatLog.setEditable(false);
 43   
 44    message = new JTextField(Consts.DEFAULT_CHAT_PROMPT);
 45
 46    message.setMaximumSize(new Dimension(2000, 20));
 47    message.setEditable(false);
 48
 49    send = new JButton("Send");
 50    send.setEnabled(false);
 51
 52    Box messagingBox = Box.createHorizontalBox();
 53    add(chatPane, BorderLayout.CENTER);
 54    messagingBox.add(message);
 55    messagingBox.add(send);
 56    add(messagingBox, BorderLayout.SOUTH);
 57
 58    TextSelector.install();
 59
 60    send.addActionListener(new java.awt.event.ActionListener()
 61    {
 62      public void actionPerformed(java.awt.event.ActionEvent evt)
 63      {
 64        sendActionPerformed(evt);
 65      }
 66    });
 67
 68    message.addActionListener(new java.awt.event.ActionListener()
 69    {
 70      public void actionPerformed(java.awt.event.ActionEvent evt)
 71      {
 72        sendActionPerformed(evt);
 73      }
 74    });
 75    
 76    setEnabled(false);
 77  }
 78
 79  /**
 80   * Action method for the sending of a chat message
 81   * @param evt (unused) required for ActionListener
 82   */
 83  private void sendActionPerformed(java.awt.event.ActionEvent evt)
 84  {
 85    if(!message.getText().equals(""))
 86    {
 87      controller.sendRequest(new ChatReq(controller.playerName, message.getText()));
 88      message.setText("");
 89      
 90      SwingUtilities.invokeLater(new Runnable()
 91      {
 92        public void run()
 93        {
 94          chatLog.setCaretPosition(chatLog.getText().length());
 95        }
 96      });
 97    }
 98  }
 99
100  /**
101   * Posts a formatted chat message to the chat log
102   * @param name posting player's name (or System)
103   * @param chatMsg the message to be displayed
104   */
105  public void postChat(String name, String chatMsg)
106  {
107    chatLog.append('\n' + name + ": " + chatMsg);
108  }
109
110  /**
111   * Toggles the enabling of chat
112   * @param enabled whether chat is enabled or not
113   */
114  public void enableChat(boolean enabled)
115  {
116    message.setEditable(enabled);
117    send.setEnabled(enabled);
118    setEnabled(enabled);
119  }
120}