KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > console > wizard > tab > BackendTab


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.console.wizard.tab;
26
27 import java.awt.GridBagConstraints JavaDoc;
28 import java.awt.GridBagLayout JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.awt.event.FocusListener JavaDoc;
33 import java.awt.event.ItemEvent JavaDoc;
34 import java.awt.event.ItemListener JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Hashtable JavaDoc;
37
38 import javax.swing.BorderFactory JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JCheckBox JavaDoc;
41 import javax.swing.JComboBox JavaDoc;
42 import javax.swing.JComponent JavaDoc;
43 import javax.swing.JLabel JavaDoc;
44 import javax.swing.JPanel JavaDoc;
45 import javax.swing.JTextField JavaDoc;
46
47 import org.objectweb.cjdbc.common.i18n.WizardTranslate;
48 import org.objectweb.cjdbc.console.wizard.WizardConstants;
49 import org.objectweb.cjdbc.console.wizard.WizardTab;
50 import org.objectweb.cjdbc.console.wizard.WizardTabs;
51 import org.objectweb.cjdbc.console.wizard.objects.Backend;
52 import org.objectweb.cjdbc.console.wizard.objects.ConnectionInfo;
53 import org.objectweb.cjdbc.console.wizard.objects.ConnectionParameterDialog;
54 import org.objectweb.cjdbc.console.wizard.objects.ConnectionTypeInfo;
55 import org.objectweb.cjdbc.console.wizard.objects.User;
56
57 /**
58  * This tab has the required field to define backend information and their
59  * related connection managers
60  *
61  * @see <code>WizardTab</code>
62  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
63  * @version 1.0
64  */

65 public class BackendTab extends WizardTab
66     implements
67       ItemListener JavaDoc,
68       FocusListener JavaDoc,
69       ActionListener JavaDoc
70 {
71   /** Combo boxes for backends */
72   public JComboBox JavaDoc backendsCombo;
73   private JTextField JavaDoc backendName;
74   private JTextField JavaDoc backendUrl;
75   private JButton JavaDoc buttonAdd;
76   private JButton JavaDoc buttonRemove;
77   private JTextField JavaDoc backendDriver;
78   private JTextField JavaDoc backendDriverPath;
79   private JTextField JavaDoc backendStatement;
80   private JCheckBox JavaDoc gatherSystemTables;
81   private JComboBox JavaDoc dynamicPrecision;
82   private JComboBox JavaDoc users;
83   private GridBagConstraints JavaDoc connectionconstraints;
84   private JPanel JavaDoc connections;
85   private JTextField JavaDoc rLogin;
86   private JTextField JavaDoc rPassword;
87   private JTextField JavaDoc urlparameters;
88   private JComboBox JavaDoc connectiontype;
89   private JButton JavaDoc connectionParameter;
90
91   /**
92    * Creates a new <code>BackendTab</code> object
93    *
94    * @param tabs wizard tabs
95    */

96   public BackendTab(WizardTabs tabs)
97   {
98     super(tabs, WizardConstants.TAB_BACKENDS);
99
100     ///////////////////////////////////////////////////////////////////////////
101
// list panel
102
///////////////////////////////////////////////////////////////////////////
103
JPanel JavaDoc list = new JPanel JavaDoc();
104     list.setBorder(BorderFactory.createTitledBorder(WizardTranslate
105         .get("label.list")));
106     list.setLayout(new GridBagLayout JavaDoc());
107     GridBagConstraints JavaDoc listconstraints = new GridBagConstraints JavaDoc();
108     listconstraints.fill = GridBagConstraints.HORIZONTAL;
109
110     listconstraints.weightx = 1.0;
111     listconstraints.weighty = 1.0;
112     listconstraints.gridy = 0;
113     listconstraints.gridwidth = 2;
114
115     backendsCombo = new JComboBox JavaDoc(new Object JavaDoc[]{});
116     backendsCombo.addItemListener(this);
117
118     list.add(backendsCombo, listconstraints);
119
120     listconstraints.gridy = ++listconstraints.gridy;
121     listconstraints.gridwidth = 1;
122
123     listconstraints.gridx = 0;
124     buttonAdd = new JButton JavaDoc(WizardTranslate.get("label.addbackend"));
125     buttonAdd.setActionCommand(WizardConstants.COMMAND_ADD_BACKEND);
126     buttonAdd.addActionListener(this);
127     list.add(buttonAdd, listconstraints);
128
129     listconstraints.gridx = 1;
130     buttonRemove = new JButton JavaDoc(WizardTranslate.get("label.removebackend"));
131     buttonRemove.setActionCommand(WizardConstants.COMMAND_REMOVE_BACKEND);
132     buttonRemove.addActionListener(this);
133     list.add(buttonRemove, listconstraints);
134
135     this.add(list, constraints);
136
137     constraints.gridy = ++constraints.gridy;
138
139     ///////////////////////////////////////////////////////////////////////////
140
// general panel
141
///////////////////////////////////////////////////////////////////////////
142
JPanel JavaDoc general = new JPanel JavaDoc();
143     general.setBorder(BorderFactory.createTitledBorder(WizardTranslate
144         .get("label.general")));
145     general.setLayout(new GridBagLayout JavaDoc());
146     GridBagConstraints JavaDoc generalconstraints = new GridBagConstraints JavaDoc();
147     generalconstraints.fill = GridBagConstraints.HORIZONTAL;
148     generalconstraints.weightx = 1.0;
149
150     generalconstraints.gridy = ++generalconstraints.gridy;
151     generalconstraints.gridx = 0;
152     general.add(new JLabel JavaDoc(WizardTranslate.get("label.backendName")),
153         generalconstraints);
154     generalconstraints.gridx = 1;
155     backendName = new JTextField JavaDoc();
156     backendName.addFocusListener(this);
157     general.add(backendName, generalconstraints);
158
159     generalconstraints.gridy = ++generalconstraints.gridy;
160     generalconstraints.gridx = 0;
161     general.add(new JLabel JavaDoc(WizardTranslate.get("label.backendUrl")),
162         generalconstraints);
163     generalconstraints.gridx = 1;
164     backendUrl = new JTextField JavaDoc();
165     backendUrl.addFocusListener(this);
166     general.add(backendUrl, generalconstraints);
167
168     generalconstraints.gridy = ++generalconstraints.gridy;
169     generalconstraints.gridx = 0;
170     general.add(new JLabel JavaDoc(WizardTranslate.get("label.backendDriver")),
171         generalconstraints);
172     generalconstraints.gridx = 1;
173     backendDriver = new JTextField JavaDoc();
174     backendDriver.addFocusListener(this);
175     general.add(backendDriver, generalconstraints);
176
177     generalconstraints.gridy = ++generalconstraints.gridy;
178     generalconstraints.gridx = 0;
179     general.add(new JLabel JavaDoc(WizardTranslate.get("label.backendDriverPath")),
180         generalconstraints);
181     generalconstraints.gridx = 1;
182     backendDriverPath = new JTextField JavaDoc();
183     backendDriverPath.addFocusListener(this);
184     general.add(backendDriverPath, generalconstraints);
185
186     generalconstraints.gridy = ++generalconstraints.gridy;
187     generalconstraints.gridx = 0;
188     general.add(new JLabel JavaDoc(WizardTranslate.get("label.backendStatement")),
189         generalconstraints);
190     generalconstraints.gridx = 1;
191     backendStatement = new JTextField JavaDoc();
192     backendStatement.addFocusListener(this);
193     general.add(backendStatement, generalconstraints);
194
195     generalconstraints.gridy = ++generalconstraints.gridy;
196     generalconstraints.gridx = 0;
197     general.add(new JLabel JavaDoc(WizardTranslate.get("label.gatherSystemTables")),
198         generalconstraints);
199     generalconstraints.gridx = 1;
200     gatherSystemTables = new JCheckBox JavaDoc();
201     gatherSystemTables.setName("label.gatherSystemTables");
202     gatherSystemTables.addFocusListener(this);
203     general.add(gatherSystemTables, generalconstraints);
204
205     generalconstraints.gridy = ++generalconstraints.gridy;
206     dynamicPrecision = new JComboBox JavaDoc(WizardConstants.DYNAMIC_PRECISION);
207     dynamicPrecision.setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
208     dynamicPrecision.addItemListener(this);
209     dynamicPrecision.addFocusListener(this);
210     generalconstraints.gridx = 0;
211     general.add(new JLabel JavaDoc(WizardTranslate.get("label.dynamicPrecision")),
212         generalconstraints);
213     generalconstraints.gridx = 1;
214     general.add(dynamicPrecision, generalconstraints);
215
216     this.add(general, constraints);
217
218     constraints.gridy = ++constraints.gridy;
219
220     ///////////////////////////////////////////////////////////////////////////
221
// Connection managers
222
///////////////////////////////////////////////////////////////////////////
223

224     connections = new JPanel JavaDoc();
225     connections.setBorder(BorderFactory.createTitledBorder(WizardTranslate
226         .get("label.connections")));
227     connections.setLayout(new GridBagLayout JavaDoc());
228     connectionconstraints = new GridBagConstraints JavaDoc();
229     connectionconstraints.fill = GridBagConstraints.HORIZONTAL;
230     connectionconstraints.weightx = 1.0;
231
232     // users list
233
setUsersComboBox();
234
235     // rLogin
236
connectionconstraints.gridy = ++connectionconstraints.gridy;
237     connectionconstraints.gridx = 0;
238     connections.add(new JLabel JavaDoc(WizardTranslate.get("label.rLogin")),
239         connectionconstraints);
240     connectionconstraints.gridx = 1;
241     rLogin = new JTextField JavaDoc();
242     rLogin.addFocusListener(this);
243     connections.add(rLogin, connectionconstraints);
244
245     // rPassword
246
connectionconstraints.gridy = ++connectionconstraints.gridy;
247     connectionconstraints.gridx = 0;
248     connections.add(new JLabel JavaDoc(WizardTranslate.get("label.rPassword")),
249         connectionconstraints);
250     connectionconstraints.gridx = 1;
251     rPassword = new JTextField JavaDoc();
252     rPassword.addFocusListener(this);
253     connections.add(rPassword, connectionconstraints);
254
255     // urlparameters
256
connectionconstraints.gridy = ++connectionconstraints.gridy;
257     connectionconstraints.gridx = 0;
258     connections.add(new JLabel JavaDoc(WizardTranslate.get("label.urlparameters")),
259         connectionconstraints);
260     connectionconstraints.gridx = 1;
261     urlparameters = new JTextField JavaDoc();
262     urlparameters.addFocusListener(this);
263     connections.add(urlparameters, connectionconstraints);
264
265     // connectiontype
266
connectionconstraints.gridy = ++connectionconstraints.gridy;
267     connectiontype = new JComboBox JavaDoc(WizardConstants.CONNECTION_MANAGERS);
268     connectionconstraints.gridx = 0;
269     connectiontype.addFocusListener(this);
270     connectiontype.addItemListener(this);
271     connections.add(new JLabel JavaDoc(WizardTranslate.get("label.connectiontype")),
272         connectionconstraints);
273     connectionconstraints.gridx = 1;
274     connections.add(connectiontype, connectionconstraints);
275
276     // edit connection parameters
277
connectionconstraints.gridy = ++connectionconstraints.gridy;
278     connectionParameter = new JButton JavaDoc(WizardTranslate
279         .get("label.edit.connection.parameters"));
280     connectionParameter
281         .setActionCommand(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM);
282     connectionconstraints.gridx = 1;
283     connectionParameter.setEnabled(false);
284     connectionParameter.addActionListener(this);
285     connections.add(connectionParameter, connectionconstraints);
286
287     this.add(connections, constraints);
288
289   }
290
291   /**
292    * Set the user combo box
293    */

294   public void setUsersComboBox()
295   {
296     if (users != null)
297       connections.remove(users);
298     users = new JComboBox JavaDoc(tabs.getUsers().toArray());
299     users.addItemListener(this);
300     connectionconstraints.gridy = 0;
301     connectionconstraints.gridx = 0;
302     connections.add(new JLabel JavaDoc(WizardTranslate.get("label.users")),
303         connectionconstraints);
304     connectionconstraints.gridx = 1;
305     connections.add(users, connectionconstraints);
306   }
307
308   /**
309    * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
310    */

311   public void focusGained(FocusEvent JavaDoc e)
312   {
313
314   }
315
316   /**
317    * @see org.objectweb.cjdbc.console.wizard.listeners.WizardListener#usersChanged()
318    */

319   public void usersChanged()
320   {
321     setUsersComboBox();
322   }
323
324   /**
325    * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
326    */

327   public void focusLost(FocusEvent JavaDoc e)
328   {
329     Backend backend = ((Backend) backendsCombo.getSelectedItem());
330     if (backend == null)
331       return;
332     backend.setName(backendName.getText());
333     backend.setUrl(backendUrl.getText());
334     backend.setConnectionTestStatement(backendStatement.getText());
335     backend.setDriver(backendDriver.getText());
336     backend.setDriverPath(backendDriverPath.getText());
337     String JavaDoc string = (String JavaDoc) dynamicPrecision.getSelectedItem();
338     backend.setDynamicPrecision(string);
339     if (gatherSystemTables.isSelected())
340       backend.setGatherSystemTables("true");
341     else
342       backend.setGatherSystemTables(null);
343
344     setBackendInfo();
345     backendsCombo.repaint();
346   }
347
348   private void setBackendInfo()
349   {
350     Backend backend = ((Backend) backendsCombo.getSelectedItem());
351
352     if (backend == null)
353       return;
354     User user = (User) users.getSelectedItem();
355     if (user != null)
356     {
357       Hashtable JavaDoc con = backend.getConnectionManagers();
358       ConnectionInfo info = (ConnectionInfo) con.get(user);
359
360       if (info == null)
361         info = new ConnectionInfo();
362       info.setRLogin(rLogin.getText());
363       info.setRPassword(rPassword.getText());
364       info.setUrlParameters(urlparameters.getText());
365       ConnectionTypeInfo cinfo = info.getConnectionTypeInfo();
366       if (cinfo == null)
367       {
368         new ConnectionTypeInfo();
369       }
370       cinfo.setType((String JavaDoc) connectiontype.getSelectedItem());
371       info.setConnectionTypeInfo(cinfo);
372       con.put(user, info);
373       backend.setConnectionManagers(con);
374     }
375   }
376
377   /**
378    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
379    */

380   public void actionPerformed(ActionEvent JavaDoc e)
381   {
382     String JavaDoc command = e.getActionCommand();
383
384     if (command.equals(WizardConstants.COMMAND_ADD_BACKEND))
385     {
386
387       String JavaDoc select = WizardTab.showBackendSelectDialog();
388       Backend backend = new Backend();
389
390       if (select != null)
391       {
392         backend.setName(select + backendsCombo.getItemCount());
393         backend.setUrl(types.getString(select + ".url"));
394         backend.setDriver(types.getString(select + ".driver"));
395         backend.setConnectionTestStatement(types.getString(select
396             + ".statement"));
397       }
398
399       backendsCombo.addItem(backend);
400       backendsCombo.setSelectedItem(backend);
401       backendsCombo.validate();
402       tabs.backendListChanged();
403     }
404     else if (command.equals(WizardConstants.COMMAND_REMOVE_BACKEND))
405     {
406       Backend backend = (Backend) backendsCombo.getSelectedItem();
407       backendsCombo.removeItem(backend);
408       backendsCombo.validate();
409       backendsCombo.repaint();
410       tabs.backendListChanged();
411     }
412     else if (command.equals(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM))
413     {
414       Backend backend = (Backend) backendsCombo.getSelectedItem();
415       User user = (User) users.getSelectedItem();
416       if (backend == null || user == null)
417         return;
418
419       Hashtable JavaDoc managers = backend.getConnectionManagers();
420       ConnectionInfo info = ((ConnectionInfo) managers.get(user));
421       ConnectionTypeInfo infoType;
422       if (info == null)
423       {
424         info = new ConnectionInfo();
425         infoType = new ConnectionTypeInfo();
426         infoType.setType((String JavaDoc) connectiontype.getSelectedItem());
427         info.setConnectionTypeInfo(infoType);
428         managers.put(user, info);
429       }
430       else
431         infoType = info.getConnectionTypeInfo();
432
433       ConnectionParameterDialog cpd = new ConnectionParameterDialog(infoType);
434
435       infoType.setValues(cpd.getValues());
436       info.setConnectionTypeInfo(infoType);
437       managers.put(user, info);
438       backend.setConnectionManagers(managers);
439     }
440
441   }
442
443   /**
444    * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
445    */

446   public void itemStateChanged(ItemEvent JavaDoc e)
447   {
448     JComponent JavaDoc comp = (JComponent JavaDoc) e.getSource();
449     if (comp == dynamicPrecision)
450       return;
451
452     Backend backend = (Backend) backendsCombo.getSelectedItem();
453     // selected user changed load it from backend
454
User user = (User) users.getSelectedItem();
455     if (user == null || backend == null)
456       return;
457
458     Hashtable JavaDoc con = backend.getConnectionManagers();
459     ConnectionInfo info = (ConnectionInfo) con.get(user);
460
461     if (info == null)
462     {
463       info = new ConnectionInfo();
464     }
465
466     if (comp == connectiontype)
467     {
468       int selectedIndex = connectiontype.getSelectedIndex();
469       if (selectedIndex == 0)
470         connectionParameter.setEnabled(false);
471       else
472         connectionParameter.setEnabled(true);
473       connectionParameter.repaint();
474       return;
475     }
476
477     rLogin.setText(info.getRLogin());
478     rPassword.setText(info.getRPassword());
479     urlparameters.setText(info.getUrlParameters());
480     connectiontype.setSelectedItem(info.getConnectionTypeInfo().getType());
481
482     if (backend == null)
483     {
484       backendName.setText("");
485       backendUrl.setText("");
486       backendDriver.setText("");
487       backendDriverPath.setText("");
488       backendStatement.setText("");
489       gatherSystemTables.setSelected(false);
490       dynamicPrecision
491           .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
492     }
493     else
494     {
495       backendName.setText(backend.getName());
496       backendUrl.setText(backend.getUrl());
497       backendDriver.setText(backend.getDriver());
498       backendDriverPath.setText(backend.getDriverPath());
499       backendStatement.setText(backend.getConnectionTestStatement());
500       gatherSystemTables.setSelected(backend.getGatherSystemTables() != null);
501       if (backend.getDynamicPrecision() == null)
502         dynamicPrecision
503             .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
504       else
505         dynamicPrecision.setSelectedItem(backend.getDynamicPrecision());
506     }
507
508   }
509
510   /**
511    * Get the list of backends
512    *
513    * @return <tt>ArrayList</tt> of <tt>Backend</tt> objects
514    */

515   public ArrayList JavaDoc getBackends()
516   {
517     return WizardConstants.getItemsFromCombo(this.backendsCombo);
518   }
519 }
Popular Tags