001 package com.sptci.rwt.webui;
002
003 import nextapp.echo2.app.Column;
004 import nextapp.echo2.app.Component;
005 import nextapp.echo2.app.Grid;
006 import nextapp.echo2.app.Label;
007 import nextapp.echo2.app.PasswordField;
008 import nextapp.echo2.app.Row;
009 import nextapp.echo2.app.SelectField;
010 import nextapp.echo2.app.TextField;
011
012 import nextapp.echo2.app.event.ActionEvent;
013 import nextapp.echo2.app.event.ActionListener;
014
015 import consultas.echo2consultas.LiveTextField;
016 import echopointng.PopUp;
017
018 import com.sptci.echo2.Configuration;
019 import com.sptci.echo2.Dimensions;
020 import com.sptci.echo2.Utilities;
021 import com.sptci.echo2.WindowPane;
022 import com.sptci.echo2.style.Extent;
023
024 /**
025 * The dialogue used to initiate a new JDBC {@link java.sql.Connection}.
026 * Also used to save/edit pre-configured connections.
027 *
028 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
029 * @author Rakesh Vidyadharan 2007-09-30
030 * @version $Id: ConnectionDialogue.java 4123 2008-05-25 21:49:01Z rakesh $
031 */
032 public class ConnectionDialogue extends WindowPane
033 {
034 /** The main controller for the application. */
035 private final MainController controller;
036
037 /** The container used to display all the components. */
038 private final Column column = new Column();
039
040 /** The component used to display configured database engines. */
041 private SelectField databaseType;
042
043 /** The component used to specify the hostname of the database server. */
044 private TextField host;
045
046 /** The port on wich the database listens for connections. */
047 private LiveTextField port;
048
049 /** The name of the database to connect to. */
050 private TextField database;
051
052 /** The component used to enter the user name to login as. */
053 private TextField userName;
054
055 /** The component used to enter the password to logon with. */
056 private PasswordField password;
057
058 /**
059 * Create a new instance blank of the view.
060 *
061 * @param controller The {@link #controller} to use.
062 */
063 public ConnectionDialogue( final MainController controller )
064 {
065 super();
066 this.controller = controller;
067 setMaximizable( false );
068 setMinimizable( false );
069 initComponents();
070 add( column );
071 }
072
073 /**
074 * Initialises all the view components.
075 *
076 * @see com.sptci.echo2.Utilities#createTextField( String, String, String, Object )
077 * @see #createDatabaseType
078 * @see #createPort
079 * @see #createButtons
080 */
081 private void initComponents()
082 {
083 Grid grid = new Grid();
084 grid.add( Utilities.createLabel(
085 getClass().getName(), "databaseType", "Title.Label" ) );
086 grid.add( createDatabaseType() );
087
088 grid.add( Utilities.createLabel(
089 getClass().getName(), "host", "Title.Label" ) );
090 grid.add( Utilities.createTextField( getClass().getName(), "host", this ) );
091
092 grid.add( Utilities.createLabel(
093 getClass().getName(), "port", "Title.Label" ) );
094 grid.add( createPort() );
095
096 grid.add( Utilities.createLabel(
097 getClass().getName(), "database", "Title.Label" ) );
098 grid.add( Utilities.createTextField(
099 getClass().getName(), "database", this ) );
100
101 grid.add( Utilities.createLabel(
102 getClass().getName(), "userName", "Title.Label" ) );
103 grid.add( Utilities.createTextField(
104 getClass().getName(), "userName", this ) );
105
106 grid.add( Utilities.createLabel(
107 getClass().getName(), "password", "Title.Label" ) );
108 grid.add( Utilities.createTextField( getClass().getName(), "password",
109 new ConnectListener( controller ), this ) );
110
111 column.add( grid );
112 column.add( createButtons() );
113 }
114
115 /**
116 * Initialise the {@link #databaseType} field with the localised values.
117 *
118 * @return The initialised {@link #databaseType} component.
119 */
120 protected Component createDatabaseType()
121 {
122 String[] values =
123 Configuration.getString( this, "databaseTypes" ).split( "," );
124 for ( int i = 0; i < values.length; ++i )
125 {
126 values[i] = values[i].trim();
127 }
128
129 databaseType = new SelectField( values );
130 databaseType.setWidth( Extent.getInstance(
131 Dimensions.getInt( this, "databaseType.width" ) ) );
132 databaseType.addActionListener( new DatabaseTypeListener() );
133 return databaseType;
134 }
135
136 /**
137 * Initialise the {@link #port} field with the localised values.
138 *
139 * @return The initialised {@link #port} component.
140 */
141 protected Component createPort()
142 {
143 port = new LiveTextField();
144 port.setRegexp( "[1-9][0-9]*" );
145 return port;
146 }
147
148 /**
149 * Create the component used to display the control butons for the
150 * dialogue.
151 *
152 * @see #createSave
153 * @return The container component with the buttons.
154 */
155 protected Component createButtons()
156 {
157 Row row = new Row();
158
159 row.add( Utilities.createButton( getClass().getName(),
160 "connect", "Accept.Button", new ConnectListener( controller ) ) );
161 row.add( Utilities.createButton( getClass().getName(),
162 "cancel", "Cancel.Button", new CancelListener() ) );
163 row.add( createSave() );
164
165 return row;
166 }
167
168 /**
169 * Create the {@link echopointng.PopUp} component used to display the
170 * component used to capture user input on the name to assign to save
171 * the JDBC connection parameters represented by this component.
172 *
173 * @return The component used to capture user input.
174 */
175 protected Component createSave()
176 {
177 PopUp popup = new PopUp();
178 popup.setTarget( Utilities.createLabel( getClass().getName(),
179 "save", "Save.Label" ) );
180 popup.setPopUp( new SaveConnectionComponent( controller ) );
181
182 return popup;
183 }
184
185 /**
186 * Return the database engine that was selected.
187 *
188 * @return The database type selected.
189 */
190 protected String getDatabaseType()
191 {
192 return (String) databaseType.getSelectedItem();
193 }
194
195 /**
196 * Sets the selected item in {@link #databaseType} to the value specified.
197 *
198 * @param value The name to set as selected.
199 */
200 protected void setDatabaseType( final String value )
201 {
202 databaseType.setSelectedItem( value );
203 }
204
205 /**
206 * Return the hostname of the database server as entered in the view.
207 *
208 * @return The hastname of the server to connect to.
209 */
210 protected String getHost()
211 {
212 return host.getText();
213 }
214
215 /**
216 * Sets the value displayed in the {@link #host} component.
217 *
218 * @param value The value to display.
219 */
220 protected void setHost( final String value )
221 {
222 host.setText( value );
223 }
224
225 /**
226 * Return the port on which the database server listens for connections.
227 *
228 * @return The port number.
229 */
230 protected int getPort()
231 {
232 return ( ( port.getText().length() == 0 ) ? 0 :
233 Integer.parseInt( port.getText() ) );
234 }
235
236 /**
237 * Sets the value displayed in {@link #port} component.
238 *
239 * @param value The value to display.
240 */
241 protected void setPort( final int value )
242 {
243 port.setText( String.valueOf( value ) );
244 }
245
246 /**
247 * Return the name of the database (instance) to connect to.
248 *
249 * @return The name of the database to connect to.
250 */
251 protected String getDatabase()
252 {
253 return database.getText();
254 }
255
256 /**
257 * Sets the value displayed in the {@link #database} component.
258 *
259 * @param value The value to display.
260 */
261 protected void setDatabase( final String value )
262 {
263 database.setText( value );
264 }
265
266 /**
267 * Return the user name to logon to the database server as.
268 *
269 * @return The user name as entered.
270 */
271 protected String getUserName()
272 {
273 return userName.getText();
274 }
275
276 /**
277 * Sets the value displayed in the {@link #userName} component.
278 *
279 * @param value The value to display.
280 */
281 protected void setUserName( final String value )
282 {
283 userName.setText( value );
284 }
285
286 /**
287 * Return the password to use to login to the database server.
288 *
289 * @return The password as entered.
290 */
291 protected String getPassword()
292 {
293 return password.getText();
294 }
295
296 /**
297 * Sets the value displayed in the {@link #password} component.
298 *
299 * @param value The value to display.
300 */
301 protected void setPassword( final String value )
302 {
303 password.setText( value );
304 }
305
306 /**
307 * The {@link nextapp.echo2.app.event.ActionListener} that updates
308 * {@link #port} depending upon the selections made in {@link
309 * #databaseType}.
310 */
311 protected class DatabaseTypeListener implements ActionListener
312 {
313 /**
314 * The action listener implementation. Updates the port value to
315 * the default value for the selected database type.
316 *
317 * @param event The event that was triggered.
318 */
319 public void actionPerformed( final ActionEvent event )
320 {
321 port.setText( Configuration.getString( ConnectionDialogue.this,
322 databaseType.getSelectedItem().toString() + ".port" ) );
323 }
324 }
325
326 /**
327 * The {@link nextapp.echo2.app.event.ActionListener} that cancels
328 * (closes) this connection dialogue.
329 */
330 protected class CancelListener implements ActionListener
331 {
332 /**
333 * The action listener implementation. Closes this dialogue.
334 *
335 * @param event The event that was triggered.
336 */
337 public void actionPerformed( final ActionEvent event )
338 {
339 ConnectionDialogue.this.userClose();
340 }
341 }
342 }