1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id: ControlView.java,v 1.11 2005/09/10 14:06:22 mat007 Exp $
29 */
30
31 package jtge.sample.chat.client.ui;
32
33 import java.awt.FlowLayout;
34 import java.awt.event.ActionEvent;
35 import java.awt.event.ActionListener;
36 import javax.swing.JButton;
37 import javax.swing.JPanel;
38 import javax.swing.JTextField;
39 import jtge.engine.io.IStatusObserver;
40 import jtge.sample.chat.client.core.IController;
41
42 /***
43 * Implements a view for the client control panel.
44 *
45 * @author Mathieu Champlon
46 * @version $Revision: 1.11 $ $Date: 2005/09/10 14:06:22 $
47 */
48 public class ControlView extends JPanel implements IStatusObserver
49 {
50 private static final long serialVersionUID = 6699740907356879158L;
51 private static final String DEFAULT_HOST = "localhost";
52 private static final int DEFAULT_PORT = 7777;
53 private static final String DEFAULT_NICK_NAME = "nickname";
54 private static final int HOST_FIELD_SIZE = 19;
55 private static final int PORT_FIELD_SIZE = 5;
56 private static final int NICK_NAME_FIELD_SIZE = 10;
57 private final JButton loginButton, logoutButton;
58 private final JTextField hostField, portField, loginField;
59 private final IController controller;
60
61 /***
62 * Create a control view.
63 *
64 * @param controller the controller
65 */
66 public ControlView( final IController controller )
67 {
68 if( controller == null )
69 throw new IllegalArgumentException( "argument 'controller' is null" );
70 this.controller = controller;
71 this.hostField = new JTextField( DEFAULT_HOST, HOST_FIELD_SIZE );
72 this.portField = new JTextField( Integer.toString( DEFAULT_PORT ), PORT_FIELD_SIZE );
73 this.loginField = new JTextField( DEFAULT_NICK_NAME, NICK_NAME_FIELD_SIZE );
74 this.loginButton = createLoginButton();
75 this.logoutButton = createLogoutButton();
76 setLayout( new FlowLayout() );
77 add( hostField );
78 add( portField );
79 add( loginField );
80 add( loginButton );
81 add( logoutButton );
82 controller.register( this );
83 }
84
85 private String getHost()
86 {
87 return hostField.getText();
88 }
89
90 private int getPort()
91 {
92 return Math.max( 0, new Integer( portField.getText() ).intValue() );
93 }
94
95 private String getNickName()
96 {
97 return loginField.getText();
98 }
99
100 private JButton createLogoutButton()
101 {
102 final JButton button = new JButton( "logout" );
103 button.addActionListener( new ActionListener()
104 {
105 public final void actionPerformed( final ActionEvent e )
106 {
107 controller.close();
108 }
109 } );
110 button.setEnabled( false );
111 return button;
112 }
113
114 private JButton createLoginButton()
115 {
116 final JButton button = new JButton( "login" );
117 button.addActionListener( new ActionListener()
118 {
119 public final void actionPerformed( final ActionEvent e )
120 {
121 controller.open( getHost(), getPort() );
122 }
123 } );
124 return button;
125 }
126
127 /***
128 * {@inheritDoc}
129 */
130 public final void connected()
131 {
132 loginButton.setEnabled( false );
133 logoutButton.setEnabled( true );
134 controller.login( getNickName() );
135 }
136
137 /***
138 * {@inheritDoc}
139 */
140 public final void disconnected()
141 {
142 loginButton.setEnabled( true );
143 logoutButton.setEnabled( false );
144 }
145 }