001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.chat.client;
021:
022: import java.awt.BorderLayout;
023: import java.awt.Dimension;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.net.InetSocketAddress;
027: import java.net.SocketAddress;
028:
029: import javax.swing.AbstractAction;
030: import javax.swing.BorderFactory;
031: import javax.swing.Box;
032: import javax.swing.BoxLayout;
033: import javax.swing.JButton;
034: import javax.swing.JFrame;
035: import javax.swing.JLabel;
036: import javax.swing.JOptionPane;
037: import javax.swing.JPanel;
038: import javax.swing.JScrollBar;
039: import javax.swing.JTextArea;
040: import javax.swing.JTextField;
041: import javax.swing.border.EmptyBorder;
042:
043: import org.apache.mina.example.chat.client.SwingChatClientHandler.Callback;
044: import org.apache.mina.transport.socket.nio.NioSocketConnector;
045:
046: /**
047: * Simple chat client based on Swing & MINA that implements the chat protocol.
048: *
049: * @author The Apache MINA Project (dev@mina.apache.org)
050: * @version $Rev$, $Date$
051: */
052: public class SwingChatClient extends JFrame implements Callback {
053: private static final long serialVersionUID = 1538675161745436968L;
054:
055: private JTextField inputText;
056:
057: private JButton loginButton;
058:
059: private JButton quitButton;
060:
061: private JButton closeButton;
062:
063: private JTextField serverField;
064:
065: private JTextField nameField;
066:
067: private JTextArea area;
068:
069: private JScrollBar scroll;
070:
071: private ChatClientSupport client;
072:
073: private SwingChatClientHandler handler;
074:
075: private NioSocketConnector connector;
076:
077: public SwingChatClient() {
078: super ("Chat Client based on Apache MINA");
079:
080: connector = new NioSocketConnector();
081:
082: loginButton = new JButton(new LoginAction());
083: loginButton.setText("Connect");
084: quitButton = new JButton(new LogoutAction());
085: quitButton.setText("Disconnect");
086: closeButton = new JButton(new QuitAction());
087: closeButton.setText("Quit");
088: inputText = new JTextField(30);
089: inputText.setAction(new BroadcastAction());
090: area = new JTextArea(10, 50);
091: area.setLineWrap(true);
092: area.setEditable(false);
093: scroll = new JScrollBar();
094: scroll.add(area);
095: nameField = new JTextField(10);
096: nameField.setEditable(false);
097: serverField = new JTextField(10);
098: serverField.setEditable(false);
099:
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: client.quit();
145: connector.dispose();
146: dispose();
147: }
148: });
149: setLoggedOut();
150: setDefaultCloseOperation(EXIT_ON_CLOSE);
151: }
152:
153: public class LoginAction extends AbstractAction {
154: private static final long serialVersionUID = 3596719854773863244L;
155:
156: public void actionPerformed(ActionEvent e) {
157:
158: ConnectDialog dialog = new ConnectDialog(
159: SwingChatClient.this );
160: dialog.pack();
161: dialog.setVisible(true);
162:
163: if (dialog.isCancelled()) {
164: return;
165: }
166:
167: SocketAddress address = parseSocketAddress(dialog
168: .getServerAddress());
169: String name = dialog.getUsername();
170:
171: handler = new SwingChatClientHandler(SwingChatClient.this );
172: client = new ChatClientSupport(name, handler);
173: nameField.setText(name);
174: serverField.setText(dialog.getServerAddress());
175:
176: if (!client.connect(connector, address, dialog.isUseSsl())) {
177: JOptionPane.showMessageDialog(SwingChatClient.this ,
178: "Could not connect to "
179: + dialog.getServerAddress() + ". ");
180: }
181: }
182: }
183:
184: private class LogoutAction extends AbstractAction {
185: private static final long serialVersionUID = 1655297424639924560L;
186:
187: public void actionPerformed(ActionEvent e) {
188: try {
189: client.quit();
190: setLoggedOut();
191: } catch (Exception e1) {
192: JOptionPane.showMessageDialog(SwingChatClient.this ,
193: "Session could not be closed.");
194: }
195: }
196: }
197:
198: private class BroadcastAction extends AbstractAction {
199: /**
200: *
201: */
202: private static final long serialVersionUID = -6276019615521905411L;
203:
204: public void actionPerformed(ActionEvent e) {
205: client.broadcast(inputText.getText());
206: inputText.setText("");
207: }
208: }
209:
210: private class QuitAction extends AbstractAction {
211: private static final long serialVersionUID = -6389802816912005370L;
212:
213: public void actionPerformed(ActionEvent e) {
214: if (client != null) {
215: client.quit();
216: }
217: SwingChatClient.this .dispose();
218: }
219: }
220:
221: private void setLoggedOut() {
222: inputText.setEnabled(false);
223: quitButton.setEnabled(false);
224: loginButton.setEnabled(true);
225: }
226:
227: private void setLoggedIn() {
228: area.setText("");
229: inputText.setEnabled(true);
230: quitButton.setEnabled(true);
231: loginButton.setEnabled(false);
232: }
233:
234: private void append(String text) {
235: area.append(text);
236: }
237:
238: private void notifyError(String message) {
239: JOptionPane.showMessageDialog(this , message);
240: }
241:
242: private SocketAddress parseSocketAddress(String s) {
243: s = s.trim();
244: int colonIndex = s.indexOf(":");
245: if (colonIndex > 0) {
246: String host = s.substring(0, colonIndex);
247: int port = parsePort(s.substring(colonIndex + 1));
248: return new InetSocketAddress(host, port);
249: } else {
250: int port = parsePort(s.substring(colonIndex + 1));
251: return new InetSocketAddress(port);
252: }
253: }
254:
255: private int parsePort(String s) {
256: try {
257: return Integer.parseInt(s);
258: } catch (NumberFormatException nfe) {
259: throw new IllegalArgumentException("Illegal port number: "
260: + s);
261: }
262: }
263:
264: public void connected() {
265: //client.login();
266: }
267:
268: public void disconnected() {
269: append("Connection closed.\n");
270: setLoggedOut();
271: }
272:
273: public void error(String message) {
274: notifyError(message + "\n");
275: }
276:
277: public void loggedIn() {
278: setLoggedIn();
279: append("You have joined the chat session.\n");
280: }
281:
282: public void loggedOut() {
283: append("You have left the chat session.\n");
284: setLoggedOut();
285: }
286:
287: public void messageReceived(String message) {
288: append(message + "\n");
289: }
290:
291: public static void main(String[] args) {
292: SwingChatClient client = new SwingChatClient();
293: client.pack();
294: client.setVisible(true);
295: }
296: }
|