View Javadoc
1 /***
2 * Copyright (c) 2003 held jointly by the individual authors.
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; with out even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17 *
18 * > http://www.gnu.org/copyleft/lesser.html
19 * > http://www.opensource.org/licenses/lgpl-license.php
20 */
21 package net.mlw.gfw.ext.basic.client.component;
22
23 import java.awt.BorderLayout;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.util.logging.Logger;
27
28 import javax.swing.JButton;
29 import javax.swing.JPanel;
30 import javax.swing.JScrollPane;
31 import javax.swing.JTextArea;
32 import javax.swing.JTextField;
33 import javax.swing.SwingUtilities;
34
35 import net.mlw.gfw.client.ClientContext;
36 import net.mlw.gfw.event.Event;
37 import net.mlw.gfw.event.InvalidEventException;
38 import net.mlw.gfw.ext.basic.event.ChatEvent;
39
40 /***
41 * @author Matthew L. Wilson
42 * @version $Revision: 1.2 $ $Date: 2004/06/29 20:32:58 $
43 */
44 public class ChatPanel extends BaseJPanel
45 {
46 private static Logger LOGGER = Logger.getLogger(ChatPanel.class.getName());
47
48 final JTextArea history = new JTextArea();
49 final JButton send = new JButton("send");
50 final JTextField input = new JTextField();
51
52 /***
53 * @see net.mlw.gfw.client.ClientEventHandler#init(net.mlw.gfw.client.ClientContext)
54 */
55 public void init(final ClientContext context)
56 {
57 setLayout(new BorderLayout());
58
59 history.setRows(3);
60 history.setEditable(false);
61 add(new JScrollPane(history), BorderLayout.CENTER);
62
63 JPanel inputPanel = new JPanel(new BorderLayout());
64 inputPanel.add(input, BorderLayout.CENTER);
65 inputPanel.add(send, BorderLayout.EAST);
66 add(inputPanel, BorderLayout.SOUTH);
67
68 ActionListener sendListener = new ActionListener()
69 {
70
71 public void actionPerformed(ActionEvent e)
72 {
73 String text = input.getText();
74 if (text != null && text.length() > 0)
75 {
76 LOGGER.info("Sending event to listeners: " + getEventHandlers());
77 getEventHandlers().onEvent(new ChatEvent(context.getUserName(), text));
78 input.setText("");
79 }
80 }
81 };
82
83 send.addActionListener(sendListener);
84 input.addActionListener(sendListener);
85 }
86
87 /***
88 * @seenet.mlw.gfw.event.EventHandler#onEvent(net.mlw.gfw.event.Event)
89 *
90 * @todo check for muted players.
91 */
92 public void onEvent(final Event event)
93 {
94 if (event instanceof ChatEvent)
95 {
96 SwingUtilities.invokeLater(new Runnable()
97 {
98 public void run()
99 {
100 String text = ((ChatEvent) event).getText();
101 String user = ((ChatEvent) event).getUserName();
102 history.setText(history.getText() + "\n" + user + ": " + text);
103 history.setCaretPosition(history.getText().length());
104 }
105 });
106 } else
107 {
108 throw new InvalidEventException(this, event);
109 }
110 }
111 }
This page was automatically generated by Maven