Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

BackendTab.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk.
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.console.wizard.tab;
00026 
00027 import java.awt.GridBagConstraints;
00028 import java.awt.GridBagLayout;
00029 import java.awt.event.ActionEvent;
00030 import java.awt.event.ActionListener;
00031 import java.awt.event.FocusEvent;
00032 import java.awt.event.FocusListener;
00033 import java.awt.event.ItemEvent;
00034 import java.awt.event.ItemListener;
00035 import java.util.ArrayList;
00036 import java.util.Hashtable;
00037 
00038 import javax.swing.BorderFactory;
00039 import javax.swing.JButton;
00040 import javax.swing.JCheckBox;
00041 import javax.swing.JComboBox;
00042 import javax.swing.JComponent;
00043 import javax.swing.JLabel;
00044 import javax.swing.JPanel;
00045 import javax.swing.JTextField;
00046 
00047 import org.objectweb.cjdbc.common.i18n.WizardTranslate;
00048 import org.objectweb.cjdbc.console.wizard.WizardConstants;
00049 import org.objectweb.cjdbc.console.wizard.WizardTab;
00050 import org.objectweb.cjdbc.console.wizard.WizardTabs;
00051 import org.objectweb.cjdbc.console.wizard.objects.Backend;
00052 import org.objectweb.cjdbc.console.wizard.objects.ConnectionInfo;
00053 import org.objectweb.cjdbc.console.wizard.objects.ConnectionParameterDialog;
00054 import org.objectweb.cjdbc.console.wizard.objects.ConnectionTypeInfo;
00055 import org.objectweb.cjdbc.console.wizard.objects.User;
00056 
00057 /**
00058  * This tab has the required field to define backend information and their
00059  * related connection managers
00060  * 
00061  * @see <code>WizardTab</code>
00062  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00063  * @version 1.0
00064  */
00065 public class BackendTab extends WizardTab
00066     implements
00067       ItemListener,
00068       FocusListener,
00069       ActionListener
00070 {
00071 
00072   public JComboBox           backendsCombo;
00073   private JTextField         backendName;
00074   private JTextField         backendUrl;
00075   private JButton            buttonAdd;
00076   private JButton            buttonRemove;
00077   private JTextField         backendDriver;
00078   private JTextField         backendDriverPath;
00079   private JTextField         backendStatement;
00080   private JCheckBox          gatherSystemTables;
00081   private JComboBox          dynamicPrecision;
00082   private JComboBox          users;
00083   private GridBagConstraints connectionconstraints;
00084   private JPanel             connections;
00085   private JTextField         rLogin;
00086   private JTextField         rPassword;
00087   private JTextField         urlparameters;
00088   private JComboBox          connectiontype;
00089   private JButton            connectionParameter;
00090 
00091   /**
00092    * Creates a new <code>BackendTab</code> object
00093    * 
00094    * @param tabs
00095    */
00096   public BackendTab(WizardTabs tabs)
00097   {
00098     super(tabs, WizardConstants.TAB_BACKENDS);
00099 
00100     ///////////////////////////////////////////////////////////////////////////
00101     // list panel
00102     ///////////////////////////////////////////////////////////////////////////
00103     JPanel list = new JPanel();
00104     list.setBorder(BorderFactory.createTitledBorder(WizardTranslate
00105         .get("label.list")));
00106     list.setLayout(new GridBagLayout());
00107     GridBagConstraints listconstraints = new GridBagConstraints();
00108     listconstraints.fill = GridBagConstraints.HORIZONTAL;
00109 
00110     listconstraints.weightx = 1.0;
00111     listconstraints.weighty = 1.0;
00112     listconstraints.gridy = 0;
00113     listconstraints.gridwidth = 2;
00114 
00115     backendsCombo = new JComboBox(new Object[]{});
00116     backendsCombo.addItemListener(this);
00117 
00118     list.add(backendsCombo, listconstraints);
00119 
00120     listconstraints.gridy = ++listconstraints.gridy;
00121     listconstraints.gridwidth = 1;
00122 
00123     listconstraints.gridx = 0;
00124     buttonAdd = new JButton(WizardTranslate.get("label.addbackend"));
00125     buttonAdd.setActionCommand(WizardConstants.COMMAND_ADD_BACKEND);
00126     buttonAdd.addActionListener(this);
00127     list.add(buttonAdd, listconstraints);
00128 
00129     listconstraints.gridx = 1;
00130     buttonRemove = new JButton(WizardTranslate.get("label.removebackend"));
00131     buttonRemove.setActionCommand(WizardConstants.COMMAND_REMOVE_BACKEND);
00132     buttonRemove.addActionListener(this);
00133     list.add(buttonRemove, listconstraints);
00134 
00135     this.add(list, constraints);
00136 
00137     constraints.gridy = ++constraints.gridy;
00138 
00139     ///////////////////////////////////////////////////////////////////////////
00140     // general panel
00141     ///////////////////////////////////////////////////////////////////////////
00142     JPanel general = new JPanel();
00143     general.setBorder(BorderFactory.createTitledBorder(WizardTranslate
00144         .get("label.general")));
00145     general.setLayout(new GridBagLayout());
00146     GridBagConstraints generalconstraints = new GridBagConstraints();
00147     generalconstraints.fill = GridBagConstraints.HORIZONTAL;
00148     generalconstraints.weightx = 1.0;
00149 
00150     generalconstraints.gridy = ++generalconstraints.gridy;
00151     generalconstraints.gridx = 0;
00152     general.add(new JLabel(WizardTranslate.get("label.backendName")),
00153         generalconstraints);
00154     generalconstraints.gridx = 1;
00155     backendName = new JTextField();
00156     backendName.addFocusListener(this);
00157     general.add(backendName, generalconstraints);
00158 
00159     generalconstraints.gridy = ++generalconstraints.gridy;
00160     generalconstraints.gridx = 0;
00161     general.add(new JLabel(WizardTranslate.get("label.backendUrl")),
00162         generalconstraints);
00163     generalconstraints.gridx = 1;
00164     backendUrl = new JTextField();
00165     backendUrl.addFocusListener(this);
00166     general.add(backendUrl, generalconstraints);
00167 
00168     generalconstraints.gridy = ++generalconstraints.gridy;
00169     generalconstraints.gridx = 0;
00170     general.add(new JLabel(WizardTranslate.get("label.backendDriver")),
00171         generalconstraints);
00172     generalconstraints.gridx = 1;
00173     backendDriver = new JTextField();
00174     backendDriver.addFocusListener(this);
00175     general.add(backendDriver, generalconstraints);
00176 
00177     generalconstraints.gridy = ++generalconstraints.gridy;
00178     generalconstraints.gridx = 0;
00179     general.add(new JLabel(WizardTranslate.get("label.backendDriverPath")),
00180         generalconstraints);
00181     generalconstraints.gridx = 1;
00182     backendDriverPath = new JTextField();
00183     backendDriverPath.addFocusListener(this);
00184     general.add(backendDriverPath, generalconstraints);
00185 
00186     generalconstraints.gridy = ++generalconstraints.gridy;
00187     generalconstraints.gridx = 0;
00188     general.add(new JLabel(WizardTranslate.get("label.backendStatement")),
00189         generalconstraints);
00190     generalconstraints.gridx = 1;
00191     backendStatement = new JTextField();
00192     backendStatement.addFocusListener(this);
00193     general.add(backendStatement, generalconstraints);
00194 
00195     generalconstraints.gridy = ++generalconstraints.gridy;
00196     generalconstraints.gridx = 0;
00197     general.add(new JLabel(WizardTranslate.get("label.gatherSystemTables")),
00198         generalconstraints);
00199     generalconstraints.gridx = 1;
00200     gatherSystemTables = new JCheckBox();
00201     gatherSystemTables.setName("label.gatherSystemTables");
00202     gatherSystemTables.addFocusListener(this);
00203     general.add(gatherSystemTables, generalconstraints);
00204 
00205     generalconstraints.gridy = ++generalconstraints.gridy;
00206     dynamicPrecision = new JComboBox(WizardConstants.DYNAMIC_PRECISION);
00207     dynamicPrecision.setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
00208     dynamicPrecision.addItemListener(this);
00209     dynamicPrecision.addFocusListener(this);
00210     generalconstraints.gridx = 0;
00211     general.add(new JLabel(WizardTranslate.get("label.dynamicPrecision")),
00212         generalconstraints);
00213     generalconstraints.gridx = 1;
00214     general.add(dynamicPrecision, generalconstraints);
00215 
00216     this.add(general, constraints);
00217 
00218     constraints.gridy = ++constraints.gridy;
00219 
00220     ///////////////////////////////////////////////////////////////////////////
00221     // Connection managers
00222     ///////////////////////////////////////////////////////////////////////////
00223 
00224     connections = new JPanel();
00225     connections.setBorder(BorderFactory.createTitledBorder(WizardTranslate
00226         .get("label.connections")));
00227     connections.setLayout(new GridBagLayout());
00228     connectionconstraints = new GridBagConstraints();
00229     connectionconstraints.fill = GridBagConstraints.HORIZONTAL;
00230     connectionconstraints.weightx = 1.0;
00231 
00232     // users list
00233     setUsersComboBox();
00234 
00235     // rLogin
00236     connectionconstraints.gridy = ++connectionconstraints.gridy;
00237     connectionconstraints.gridx = 0;
00238     connections.add(new JLabel(WizardTranslate.get("label.rLogin")),
00239         connectionconstraints);
00240     connectionconstraints.gridx = 1;
00241     rLogin = new JTextField();
00242     rLogin.addFocusListener(this);
00243     connections.add(rLogin, connectionconstraints);
00244 
00245     // rPassword
00246     connectionconstraints.gridy = ++connectionconstraints.gridy;
00247     connectionconstraints.gridx = 0;
00248     connections.add(new JLabel(WizardTranslate.get("label.rPassword")),
00249         connectionconstraints);
00250     connectionconstraints.gridx = 1;
00251     rPassword = new JTextField();
00252     rPassword.addFocusListener(this);
00253     connections.add(rPassword, connectionconstraints);
00254 
00255     // urlparameters
00256     connectionconstraints.gridy = ++connectionconstraints.gridy;
00257     connectionconstraints.gridx = 0;
00258     connections.add(new JLabel(WizardTranslate.get("label.urlparameters")),
00259         connectionconstraints);
00260     connectionconstraints.gridx = 1;
00261     urlparameters = new JTextField();
00262     urlparameters.addFocusListener(this);
00263     connections.add(urlparameters, connectionconstraints);
00264 
00265     // connectiontype
00266     connectionconstraints.gridy = ++connectionconstraints.gridy;
00267     connectiontype = new JComboBox(WizardConstants.CONNECTION_MANAGERS);
00268     connectionconstraints.gridx = 0;
00269     connectiontype.addFocusListener(this);
00270     connectiontype.addItemListener(this);
00271     connections.add(new JLabel(WizardTranslate.get("label.connectiontype")),
00272         connectionconstraints);
00273     connectionconstraints.gridx = 1;
00274     connections.add(connectiontype, connectionconstraints);
00275 
00276     // edit connection parameters
00277     connectionconstraints.gridy = ++connectionconstraints.gridy;
00278     connectionParameter = new JButton(WizardTranslate
00279         .get("label.edit.connection.parameters"));
00280     connectionParameter
00281         .setActionCommand(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM);
00282     connectionconstraints.gridx = 1;
00283     connectionParameter.setEnabled(false);
00284     connectionParameter.addActionListener(this);
00285     connections.add(connectionParameter, connectionconstraints);
00286 
00287     this.add(connections, constraints);
00288 
00289   }
00290 
00291   public void setUsersComboBox()
00292   {
00293     if (users != null)
00294       connections.remove(users);
00295     users = new JComboBox(tabs.getUsers().toArray());
00296     users.addItemListener(this);
00297     connectionconstraints.gridy = 0;
00298     connectionconstraints.gridx = 0;
00299     connections.add(new JLabel(WizardTranslate.get("label.users")),
00300         connectionconstraints);
00301     connectionconstraints.gridx = 1;
00302     connections.add(users, connectionconstraints);
00303   }
00304 
00305   /**
00306    * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
00307    */
00308   public void focusGained(FocusEvent e)
00309   {
00310 
00311   }
00312 
00313   /**
00314    * @see org.objectweb.cjdbc.console.wizard.listeners.WizardListener#usersChanged()
00315    */
00316   public void usersChanged()
00317   {
00318     setUsersComboBox();
00319   }
00320 
00321   /**
00322    * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
00323    */
00324   public void focusLost(FocusEvent e)
00325   {
00326     Backend backend = ((Backend) backendsCombo.getSelectedItem());
00327     if (backend == null)
00328       return;
00329     backend.setName(backendName.getText());
00330     backend.setUrl(backendUrl.getText());
00331     backend.setConnectionTestStatement(backendStatement.getText());
00332     backend.setDriver(backendDriver.getText());
00333     backend.setDriverPath(backendDriverPath.getText());
00334     String string = (String) dynamicPrecision.getSelectedItem();
00335     backend.setDynamicPrecision(string);
00336     if (gatherSystemTables.isSelected())
00337       backend.setGatherSystemTables("true");
00338     else
00339       backend.setGatherSystemTables(null);
00340 
00341     setBackendInfo();
00342     backendsCombo.repaint();
00343   }
00344 
00345   private void setBackendInfo()
00346   {
00347     Backend backend = ((Backend) backendsCombo.getSelectedItem());
00348 
00349     if (backend == null)
00350       return;
00351     User user = (User) users.getSelectedItem();
00352     if (user != null)
00353     {
00354       Hashtable con = backend.getConnectionManagers();
00355       ConnectionInfo info = (ConnectionInfo) con.get(user);
00356 
00357       if (info == null)
00358         info = new ConnectionInfo();
00359       info.setRLogin(rLogin.getText());
00360       info.setRPassword(rPassword.getText());
00361       info.setUrlParameters(urlparameters.getText());
00362       ConnectionTypeInfo cinfo = info.getConnectionTypeInfo();
00363       if (cinfo == null)
00364       {
00365         new ConnectionTypeInfo();
00366       }
00367       cinfo.setType((String) connectiontype.getSelectedItem());
00368       info.setConnectionTypeInfo(cinfo);
00369       con.put(user, info);
00370       backend.setConnectionManagers(con);
00371     }
00372   }
00373 
00374   /**
00375    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
00376    */
00377   public void actionPerformed(ActionEvent e)
00378   {
00379     String command = e.getActionCommand();
00380 
00381     if (command.equals(WizardConstants.COMMAND_ADD_BACKEND))
00382     {
00383 
00384       String select = WizardTab.showBackendSelectDialog();
00385       Backend backend = new Backend();
00386 
00387       if (select != null)
00388       {
00389         backend.setName(select + backendsCombo.getItemCount());
00390         backend.setUrl(types.getString(select + ".url"));
00391         backend.setDriver(types.getString(select + ".driver"));
00392         backend.setConnectionTestStatement(types.getString(select
00393             + ".statement"));
00394       }
00395 
00396       backendsCombo.addItem(backend);
00397       backendsCombo.setSelectedItem(backend);
00398       backendsCombo.validate();
00399       tabs.backendListChanged();
00400     }
00401     else if (command.equals(WizardConstants.COMMAND_REMOVE_BACKEND))
00402     {
00403       Backend backend = (Backend) backendsCombo.getSelectedItem();
00404       backendsCombo.removeItem(backend);
00405       backendsCombo.validate();
00406       backendsCombo.repaint();
00407       tabs.backendListChanged();
00408     }
00409     else if (command.equals(WizardConstants.COMMAND_EDIT_CONNECTION_PARAM))
00410     {
00411       Backend backend = (Backend) backendsCombo.getSelectedItem();
00412       User user = (User) users.getSelectedItem();
00413       if (backend == null || user == null)
00414         return;
00415 
00416       Hashtable managers = backend.getConnectionManagers();
00417       ConnectionInfo info = ((ConnectionInfo) managers.get(user));
00418       ConnectionTypeInfo infoType;
00419       if (info == null)
00420       {
00421         info = new ConnectionInfo();
00422         infoType = new ConnectionTypeInfo();
00423         infoType.setType((String) connectiontype.getSelectedItem());
00424         info.setConnectionTypeInfo(infoType);
00425         managers.put(user, info);
00426       }
00427       else
00428         infoType = info.getConnectionTypeInfo();
00429 
00430       ConnectionParameterDialog cpd = new ConnectionParameterDialog(infoType);
00431 
00432       infoType.setValues(cpd.getValues());
00433       info.setConnectionTypeInfo(infoType);
00434       managers.put(user, info);
00435       backend.setConnectionManagers(managers);
00436     }
00437 
00438   }
00439 
00440   /**
00441    * @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
00442    */
00443   public void itemStateChanged(ItemEvent e)
00444   {
00445     JComponent comp = (JComponent) e.getSource();
00446     if (comp == dynamicPrecision)
00447       return;
00448 
00449     Backend backend = (Backend) backendsCombo.getSelectedItem();
00450     // selected user changed load it from backend
00451     User user = (User) users.getSelectedItem();
00452     if (user == null || backend == null)
00453       return;
00454 
00455     Hashtable con = backend.getConnectionManagers();
00456     ConnectionInfo info = (ConnectionInfo) con.get(user);
00457 
00458     if (info == null)
00459     {
00460       info = new ConnectionInfo();
00461     }
00462 
00463     if (comp == connectiontype)
00464     {
00465       int selectedIndex = connectiontype.getSelectedIndex();
00466       if (selectedIndex == 0)
00467         connectionParameter.setEnabled(false);
00468       else
00469         connectionParameter.setEnabled(true);
00470       connectionParameter.repaint();
00471       return;
00472     }
00473 
00474     rLogin.setText(info.getRLogin());
00475     rPassword.setText(info.getRPassword());
00476     urlparameters.setText(info.getUrlParameters());
00477     connectiontype.setSelectedItem(info.getConnectionTypeInfo().getType());
00478 
00479     if (backend == null)
00480     {
00481       backendName.setText("");
00482       backendUrl.setText("");
00483       backendDriver.setText("");
00484       backendDriverPath.setText("");
00485       backendStatement.setText("");
00486       gatherSystemTables.setSelected(false);
00487       dynamicPrecision
00488           .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
00489     }
00490     else
00491     {
00492       backendName.setText(backend.getName());
00493       backendUrl.setText(backend.getUrl());
00494       backendDriver.setText(backend.getDriver());
00495       backendDriverPath.setText(backend.getDriverPath());
00496       backendStatement.setText(backend.getConnectionTestStatement());
00497       gatherSystemTables.setSelected(backend.getGatherSystemTables() != null);
00498       if (backend.getDynamicPrecision() == null)
00499         dynamicPrecision
00500             .setSelectedItem(WizardConstants.DEFAULT_DYNAMIC_PRECISION);
00501       else
00502         dynamicPrecision.setSelectedItem(backend.getDynamicPrecision());
00503     }
00504 
00505   }
00506 
00507   /**
00508    * Get the list of backends
00509    * 
00510    * @return <tt>ArrayList</tt> of <tt>Backend</tt> objects
00511    */
00512   public ArrayList getBackends()
00513   {
00514     return WizardConstants.getItemsFromCombo(this.backendsCombo);
00515   }
00516 }

Generated on Mon Apr 11 22:01:29 2005 for C-JDBC by  doxygen 1.3.9.1