1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.example.chat.client;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.ActionListener;
26 import java.net.InetSocketAddress;
27 import java.net.SocketAddress;
28
29 import javax.swing.AbstractAction;
30 import javax.swing.BorderFactory;
31 import javax.swing.Box;
32 import javax.swing.BoxLayout;
33 import javax.swing.JButton;
34 import javax.swing.JFrame;
35 import javax.swing.JLabel;
36 import javax.swing.JOptionPane;
37 import javax.swing.JPanel;
38 import javax.swing.JScrollBar;
39 import javax.swing.JTextArea;
40 import javax.swing.JTextField;
41 import javax.swing.border.EmptyBorder;
42
43 import org.apache.mina.example.chat.client.SwingChatClientHandler.Callback;
44 import org.apache.mina.transport.socket.nio.SocketConnector;
45
46
47
48
49
50
51
52 public class SwingChatClient extends JFrame implements Callback {
53 private static final long serialVersionUID = 1538675161745436968L;
54
55 private JTextField inputText;
56
57 private JButton loginButton;
58
59 private JButton quitButton;
60
61 private JButton closeButton;
62
63 private JTextField serverField;
64
65 private JTextField nameField;
66
67 private JTextArea area;
68
69 private JScrollBar scroll;
70
71 private ChatClientSupport client;
72
73 private SwingChatClientHandler handler;
74
75 private SocketConnector connector;
76
77 public SwingChatClient() {
78 super("Chat Client based on Apache MINA");
79
80 connector = new SocketConnector();
81
82 loginButton = new JButton(new LoginAction());
83 loginButton.setText("Connect");
84 quitButton = new JButton(new LogoutAction());
85 quitButton.setText("Disconnect");
86 closeButton = new JButton(new QuitAction());
87 closeButton.setText("Quit");
88 inputText = new JTextField(30);
89 inputText.setAction(new BroadcastAction());
90 area = new JTextArea(10, 50);
91 area.setLineWrap(true);
92 area.setEditable(false);
93 scroll = new JScrollBar();
94 scroll.add(area);
95 nameField = new JTextField(10);
96 nameField.setEditable(false);
97 serverField = new JTextField(10);
98 serverField.setEditable(false);
99
100 JPanel h = new JPanel();
101 h.setLayout(new BoxLayout(h, BoxLayout.LINE_AXIS));
102 h.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
103 JLabel nameLabel = new JLabel("Name: ");
104 JLabel serverLabel = new JLabel("Server: ");
105 h.add(nameLabel);
106 h.add(Box.createRigidArea(new Dimension(10, 0)));
107 h.add(nameField);
108 h.add(Box.createRigidArea(new Dimension(10, 0)));
109 h.add(Box.createHorizontalGlue());
110 h.add(Box.createRigidArea(new Dimension(10, 0)));
111 h.add(serverLabel);
112 h.add(Box.createRigidArea(new Dimension(10, 0)));
113 h.add(serverField);
114
115 JPanel p = new JPanel();
116 p.setLayout(new BoxLayout(p, BoxLayout.LINE_AXIS));
117 p.setBorder(new EmptyBorder(10, 10, 10, 10));
118
119 JPanel left = new JPanel();
120 left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
121 left.add(area);
122 left.add(Box.createRigidArea(new Dimension(0, 5)));
123 left.add(Box.createHorizontalGlue());
124 left.add(inputText);
125
126 JPanel right = new JPanel();
127 right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
128 right.add(loginButton);
129 right.add(Box.createRigidArea(new Dimension(0, 5)));
130 right.add(quitButton);
131 right.add(Box.createHorizontalGlue());
132 right.add(Box.createRigidArea(new Dimension(0, 25)));
133 right.add(closeButton);
134
135 p.add(left);
136 p.add(Box.createRigidArea(new Dimension(10, 0)));
137 p.add(right);
138
139 getContentPane().add(h, BorderLayout.NORTH);
140 getContentPane().add(p);
141
142 closeButton.addActionListener(new ActionListener() {
143 public void actionPerformed(ActionEvent e) {
144 dispose();
145 }
146 });
147 setLoggedOut();
148 setDefaultCloseOperation(EXIT_ON_CLOSE);
149 }
150
151 public class LoginAction extends AbstractAction {
152 private static final long serialVersionUID = 3596719854773863244L;
153
154 public void actionPerformed(ActionEvent e) {
155
156 ConnectDialog dialog = new ConnectDialog(SwingChatClient.this);
157 dialog.pack();
158 dialog.setVisible(true);
159
160 if (dialog.isCancelled()) {
161 return;
162 }
163
164 SocketAddress address = parseSocketAddress(dialog
165 .getServerAddress());
166 String name = dialog.getUsername();
167
168 handler = new SwingChatClientHandler(SwingChatClient.this);
169 client = new ChatClientSupport(name, handler);
170 nameField.setText(name);
171 serverField.setText(dialog.getServerAddress());
172
173 if (!client.connect(connector, address, dialog.isUseSsl())) {
174 JOptionPane.showMessageDialog(SwingChatClient.this,
175 "Could not connect to " + dialog.getServerAddress()
176 + ". ");
177 }
178 }
179 }
180
181 private class LogoutAction extends AbstractAction {
182 private static final long serialVersionUID = 1655297424639924560L;
183
184 public void actionPerformed(ActionEvent e) {
185 try {
186 client.quit();
187 setLoggedOut();
188 } catch (Exception e1) {
189 JOptionPane.showMessageDialog(SwingChatClient.this,
190 "Session could not be closed.");
191 }
192 }
193 }
194
195 private class BroadcastAction extends AbstractAction {
196
197
198
199 private static final long serialVersionUID = -6276019615521905411L;
200
201 public void actionPerformed(ActionEvent e) {
202 client.broadcast(inputText.getText());
203 inputText.setText("");
204 }
205 }
206
207 private class QuitAction extends AbstractAction {
208 private static final long serialVersionUID = -6389802816912005370L;
209
210 public void actionPerformed(ActionEvent e) {
211 if (client != null) {
212 client.quit();
213 }
214 SwingChatClient.this.dispose();
215 }
216 }
217
218 private void setLoggedOut() {
219 inputText.setEnabled(false);
220 quitButton.setEnabled(false);
221 loginButton.setEnabled(true);
222 }
223
224 private void setLoggedIn() {
225 area.setText("");
226 inputText.setEnabled(true);
227 quitButton.setEnabled(true);
228 loginButton.setEnabled(false);
229 }
230
231 private void append(String text) {
232 area.append(text);
233 }
234
235 private void notifyError(String message) {
236 JOptionPane.showMessageDialog(this, message);
237 }
238
239 private SocketAddress parseSocketAddress(String s) {
240 s = s.trim();
241 int colonIndex = s.indexOf(":");
242 if (colonIndex > 0) {
243 String host = s.substring(0, colonIndex);
244 int port = parsePort(s.substring(colonIndex + 1));
245 return new InetSocketAddress(host, port);
246 } else {
247 int port = parsePort(s.substring(colonIndex + 1));
248 return new InetSocketAddress(port);
249 }
250 }
251
252 private int parsePort(String s) {
253 try {
254 return Integer.parseInt(s);
255 } catch (NumberFormatException nfe) {
256 throw new IllegalArgumentException("Illegal port number: " + s);
257 }
258 }
259
260 public void connected() {
261 }
262
263 public void disconnected() {
264 append("Connection closed.\n");
265 setLoggedOut();
266 }
267
268 public void error(String message) {
269 notifyError(message + "\n");
270 }
271
272 public void loggedIn() {
273 setLoggedIn();
274 append("You have joined the chat session.\n");
275 }
276
277 public void loggedOut() {
278 append("You have left the chat session.\n");
279 setLoggedOut();
280 }
281
282 public void messageReceived(String message) {
283 append(message + "\n");
284 }
285
286 public static void main(String[] args) {
287 SwingChatClient client = new SwingChatClient();
288 client.pack();
289 client.setVisible(true);
290 }
291 }