KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > mina > example > chat > client > SwingChatClient


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20 package org.apache.mina.example.chat.client;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.net.InetSocketAddress JavaDoc;
27 import java.net.SocketAddress JavaDoc;
28
29 import javax.swing.AbstractAction JavaDoc;
30 import javax.swing.BorderFactory JavaDoc;
31 import javax.swing.Box JavaDoc;
32 import javax.swing.BoxLayout JavaDoc;
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JFrame JavaDoc;
35 import javax.swing.JLabel JavaDoc;
36 import javax.swing.JOptionPane JavaDoc;
37 import javax.swing.JPanel JavaDoc;
38 import javax.swing.JScrollBar JavaDoc;
39 import javax.swing.JTextArea JavaDoc;
40 import javax.swing.JTextField JavaDoc;
41 import javax.swing.border.EmptyBorder JavaDoc;
42
43 import org.apache.mina.example.chat.client.SwingChatClientHandler.Callback;
44 import org.apache.mina.transport.socket.nio.SocketConnector;
45
46 /**
47  * Simple chat client based on Swing & MINA that implements the chat protocol.
48  *
49  * @author The Apache Directory Project (mina-dev@directory.apache.org)
50  * @version $Rev$, $Date$
51  */

52 public class SwingChatClient extends JFrame JavaDoc implements Callback {
53     private static final long serialVersionUID = 1538675161745436968L;
54
55     private JTextField JavaDoc inputText;
56
57     private JButton JavaDoc loginButton;
58
59     private JButton JavaDoc quitButton;
60
61     private JButton JavaDoc closeButton;
62
63     private JTextField JavaDoc serverField;
64
65     private JTextField JavaDoc nameField;
66
67     private JTextArea JavaDoc area;
68
69     private JScrollBar JavaDoc 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 JavaDoc(new LoginAction());
83         loginButton.setText("Connect");
84         quitButton = new JButton JavaDoc(new LogoutAction());
85         quitButton.setText("Disconnect");
86         closeButton = new JButton JavaDoc(new QuitAction());
87         closeButton.setText("Quit");
88         inputText = new JTextField JavaDoc(30);
89         inputText.setAction(new BroadcastAction());
90         area = new JTextArea JavaDoc(10, 50);
91         area.setLineWrap(true);
92         area.setEditable(false);
93         scroll = new JScrollBar JavaDoc();
94         scroll.add(area);
95         nameField = new JTextField JavaDoc(10);
96         nameField.setEditable(false);
97         serverField = new JTextField JavaDoc(10);
98         serverField.setEditable(false);
99
100         JPanel JavaDoc h = new JPanel JavaDoc();
101         h.setLayout(new BoxLayout JavaDoc(h, BoxLayout.LINE_AXIS));
102         h.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
103         JLabel JavaDoc nameLabel = new JLabel JavaDoc("Name: ");
104         JLabel JavaDoc serverLabel = new JLabel JavaDoc("Server: ");
105         h.add(nameLabel);
106         h.add(Box.createRigidArea(new Dimension JavaDoc(10, 0)));
107         h.add(nameField);
108         h.add(Box.createRigidArea(new Dimension JavaDoc(10, 0)));
109         h.add(Box.createHorizontalGlue());
110         h.add(Box.createRigidArea(new Dimension JavaDoc(10, 0)));
111         h.add(serverLabel);
112         h.add(Box.createRigidArea(new Dimension JavaDoc(10, 0)));
113         h.add(serverField);
114
115         JPanel JavaDoc p = new JPanel JavaDoc();
116         p.setLayout(new BoxLayout JavaDoc(p, BoxLayout.LINE_AXIS));
117         p.setBorder(new EmptyBorder JavaDoc(10, 10, 10, 10));
118
119         JPanel JavaDoc left = new JPanel JavaDoc();
120         left.setLayout(new BoxLayout JavaDoc(left, BoxLayout.PAGE_AXIS));
121         left.add(area);
122         left.add(Box.createRigidArea(new Dimension JavaDoc(0, 5)));
123         left.add(Box.createHorizontalGlue());
124         left.add(inputText);
125
126         JPanel JavaDoc right = new JPanel JavaDoc();
127         right.setLayout(new BoxLayout JavaDoc(right, BoxLayout.PAGE_AXIS));
128         right.add(loginButton);
129         right.add(Box.createRigidArea(new Dimension JavaDoc(0, 5)));
130         right.add(quitButton);
131         right.add(Box.createHorizontalGlue());
132         right.add(Box.createRigidArea(new Dimension JavaDoc(0, 25)));
133         right.add(closeButton);
134
135         p.add(left);
136         p.add(Box.createRigidArea(new Dimension JavaDoc(10, 0)));
137         p.add(right);
138
139         getContentPane().add(h, BorderLayout.NORTH);
140         getContentPane().add(p);
141
142         closeButton.addActionListener(new ActionListener JavaDoc() {
143             public void actionPerformed(ActionEvent JavaDoc e) {
144                 dispose();
145             }
146         });
147         setLoggedOut();
148         setDefaultCloseOperation(EXIT_ON_CLOSE);
149     }
150
151     public class LoginAction extends AbstractAction JavaDoc {
152         private static final long serialVersionUID = 3596719854773863244L;
153
154         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc address = parseSocketAddress(dialog
165                     .getServerAddress());
166             String JavaDoc 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 JavaDoc {
182         private static final long serialVersionUID = 1655297424639924560L;
183
184         public void actionPerformed(ActionEvent JavaDoc e) {
185             try {
186                 client.quit();
187                 setLoggedOut();
188             } catch (Exception JavaDoc e1) {
189                 JOptionPane.showMessageDialog(SwingChatClient.this,
190                         "Session could not be closed.");
191             }
192         }
193     }
194
195     private class BroadcastAction extends AbstractAction JavaDoc {
196         /**
197          *
198          */

199         private static final long serialVersionUID = -6276019615521905411L;
200
201         public void actionPerformed(ActionEvent JavaDoc e) {
202             client.broadcast(inputText.getText());
203             inputText.setText("");
204         }
205     }
206
207     private class QuitAction extends AbstractAction JavaDoc {
208         private static final long serialVersionUID = -6389802816912005370L;
209
210         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc text) {
232         area.append(text);
233     }
234
235     private void notifyError(String JavaDoc message) {
236         JOptionPane.showMessageDialog(this, message);
237     }
238
239     private SocketAddress JavaDoc parseSocketAddress(String JavaDoc s) {
240         s = s.trim();
241         int colonIndex = s.indexOf(":");
242         if (colonIndex > 0) {
243             String JavaDoc host = s.substring(0, colonIndex);
244             int port = parsePort(s.substring(colonIndex + 1));
245             return new InetSocketAddress JavaDoc(host, port);
246         } else {
247             int port = parsePort(s.substring(colonIndex + 1));
248             return new InetSocketAddress JavaDoc(port);
249         }
250     }
251
252     private int parsePort(String JavaDoc s) {
253         try {
254             return Integer.parseInt(s);
255         } catch (NumberFormatException JavaDoc nfe) {
256             throw new IllegalArgumentException JavaDoc("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 JavaDoc 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 JavaDoc message) {
283         append(message + "\n");
284     }
285
286     public static void main(String JavaDoc[] args) {
287         SwingChatClient client = new SwingChatClient();
288         client.pack();
289         client.setVisible(true);
290     }
291 }
292
Popular Tags