View Javadoc

1   package com.explosion.expfmodules.rdbmsconn;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.Driver;
24  import java.util.List;
25  import java.util.StringTokenizer;
26  import java.util.Vector;
27  
28  import javax.swing.DefaultCellEditor;
29  import javax.swing.JComboBox;
30  import javax.swing.table.DefaultTableCellRenderer;
31  import javax.swing.table.TableCellEditor;
32  
33  import org.apache.log4j.LogManager;
34  import org.apache.log4j.Logger;
35  
36  import com.explosion.utilities.ListComboBoxModel;
37  import com.explosion.utilities.classes.ClassUtilities;
38  import com.explosion.utilities.exception.ExceptionManagerFactory;
39  import com.explosion.utilities.preferences.Preference;
40  import com.explosion.utilities.preferences.PreferenceChangeEvent;
41  import com.explosion.utilities.preferences.PreferenceChangeListener;
42  
43  /***
44   * @author Stephen Cowx Date created:@24-Mar-2003
45   */
46  public class DriverDescriptorPreference extends Preference implements PreferenceChangeListener
47  {
48  
49      private static Logger log = LogManager.getLogger(DriverDescriptorPreference.class);
50      private DefaultCellEditor editor;
51      private DefaultTableCellRenderer renderer = null;
52      private Preference rVals;
53      private Driver driver = null;
54      
55  
56      public DriverDescriptorPreference(String uniqueIdentifier, int type, Object defaultValue, Object referenceToStore) throws Exception
57      {
58          super(uniqueIdentifier, type, defaultValue, referenceToStore);
59          editor = new DefaultCellEditor(new JComboBox()); 
60      }
61      
62      /***
63       * This method will go and search the file lists defined by the classpath and derive the possible values
64       * for the drivers and set the correct / remembered ones,
65       * @param locations
66       * @param rememberedPossibleValues
67       */
68      public void initialiseNonEmbeddedDriver(Preference locations, Preference rememberedPossibleValues) {
69          if (rVals == null)
70              rVals = rememberedPossibleValues;
71          
72          List files = locations.getValues();
73          
74          /* if the file exists then look for drivers, otherwise, show warning */
75          if (files.size() > 0)
76          {
77              if (((String) rememberedPossibleValues.getValue()).trim().equalsIgnoreCase(""))
78              {
79                  try
80                  {
81                      log.debug("Loading drivers of type java.sql.Driver.");
82                      List l = ClassUtilities.findClassesOfType(Class.forName("java.sql.Driver"), files);
83                      log.debug("Loaded " + l.size() + " drivers.");
84                      setChoiceValues(l);
85                      String possibles = "";
86                      for (int i = 0; i < l.size(); i++)
87                          possibles += (l.get(i) + (i < (l.size() - 1) ? "," : ""));
88                      rememberedPossibleValues.setValue(possibles);
89                      rememberedPossibleValues.save();
90                  } catch (Exception e)
91                  {
92                      e.printStackTrace();
93                  	log.error(e);
94                  	setChoiceValues(new Vector());
95                  }
96                  catch (Throwable e)
97                  {
98                     // Catches noCLassDef Found Error s
99                     ExceptionManagerFactory.getExceptionManager().manageException(e,"Error while loading driver." );
100                    setChoiceValues(new Vector());
101                 }
102                 
103             } 
104             else
105             {
106                 String values = ((String) rememberedPossibleValues.getValue()).trim();
107                 StringTokenizer tokenizer = new StringTokenizer(values, ",");
108                 Vector possibles = new Vector();
109                 while (tokenizer.hasMoreTokens())
110                 {
111                     possibles.addElement(tokenizer.nextToken());
112                 }
113                 setChoiceValues(possibles);
114             }
115         } else
116         {
117             setChoiceValues(new Vector());
118         }
119 
120         ((JComboBox) editor.getComponent()).setModel(new ListComboBoxModel(this.getChoiceValues()));
121 
122         if (getChoiceValues().size() > 0)
123         {
124             try
125             {
126                 setValue(this.getChoiceValues().get(0));
127             } catch (Exception e)
128             {}
129         } else
130         {
131             try
132             {
133                 setValue("");
134             } catch (Exception e)
135             {}
136         }
137     }
138 
139     public void preferenceChanged(PreferenceChangeEvent preferenceChangeEvent)
140     {
141         try
142         {
143             //if (preferenceChangeEvent.getChangeCode() != PreferenceChangeEvent.PROPERTY_COLLECTION_VALUE_CHANGED)
144             //{
145               rVals.setValue("");
146               rVals.save();
147               initialiseNonEmbeddedDriver((Preference) preferenceChangeEvent.getPreference(), rVals);
148             //}
149         } catch (Exception e)
150         {
151             ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while updating preference.");
152         }
153     }
154 
155     public TableCellEditor getEditor()
156     {
157         return editor;
158     }
159 
160     public void setEditor(TableCellEditor editor)
161     {}
162 
163     /***
164      * @return Returns the driver.
165      */
166     public Driver getDriver()
167     {
168         return driver;
169     }
170 
171     /***
172      * @param driver The driver to set.
173      */
174     public void setDriver(Driver driver)
175     {
176         this.driver = driver;
177     }
178 }