View Javadoc

1   package com.explosion.utilities.preferences.dialogs;
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  import java.awt.Component;
23  import java.util.ArrayList;
24  import java.util.Vector;
25  
26  import javax.swing.DefaultCellEditor;
27  import javax.swing.JButton;
28  import javax.swing.JComboBox;
29  import javax.swing.JComponent;
30  import javax.swing.JTable;
31  import javax.swing.table.DefaultTableModel;
32  import javax.swing.table.TableCellEditor;
33  import javax.swing.table.TableCellRenderer;
34  import javax.swing.table.TableModel;
35  
36  import com.explosion.utilities.ListComboBoxModel;
37  import com.explosion.utilities.preferences.Preference;
38  import com.explosion.utilities.preferences.PreferenceChangeListener;
39  import com.explosion.utilities.preferences.editandrender.table.BooleanPreferenceEditor;
40  import com.explosion.utilities.preferences.editandrender.table.BooleanPreferenceRenderer;
41  import com.explosion.utilities.preferences.editandrender.table.CollectionPreferenceEditor;
42  import com.explosion.utilities.preferences.editandrender.table.CollectionPreferenceRenderer;
43  import com.explosion.utilities.preferences.editandrender.table.ColorPreferenceEditor;
44  import com.explosion.utilities.preferences.editandrender.table.ColorPreferenceRenderer;
45  import com.explosion.utilities.preferences.editandrender.table.FilePreferenceEditor;
46  import com.explosion.utilities.preferences.editandrender.table.FilePreferenceRenderer;
47  import com.explosion.utilities.preferences.editandrender.table.FontPreferenceEditor;
48  import com.explosion.utilities.preferences.editandrender.table.FontPreferenceRenderer;
49  import com.explosion.utilities.preferences.editandrender.table.PasswordPreferenceEditor;
50  import com.explosion.utilities.preferences.editandrender.table.PasswordPreferenceRenderer;
51  
52  /***
53   * This table contains Preference objects
54   * It contains a list of perferences laid out nicely.
55   * Each cell in the right hand column contains an actionable / editable property.
56   * Each cell in the left hand column contains the short name for that property.
57   * The user of this class is required to pass in the set of preferences which are 
58   * to be edited as a vector.
59   */
60  public class PreferencesTable extends JTable
61  {
62      private Component owner;
63      
64     /***
65       * Constructor, give it the model it assumes that the model is a Vector
66       * filled with Preference objects in the second column
67       * 
68       * @param model
69       */
70      public PreferencesTable(Vector prefences, Component theOwner, PreferenceChangeListener listener)
71      {
72          super(constructPropertiesModel(prefences, theOwner, listener));
73          this.owner = theOwner;
74      }
75      
76      /***
77       * Constructor, give it a Preference object, it will construct a table with one row and one columns
78       * and no headings.
79       * 
80       * @param model
81       */
82      public PreferencesTable(Preference preference, Component theOwner, PreferenceChangeListener listener)
83      {
84          super(constructPropertiesModel(preference, theOwner, listener));
85          this.owner = theOwner;
86      }
87  
88      /***
89       * Returns
90       */
91      public boolean isCellEditable(int row, int column)
92      {
93          if (column == 1)
94          {
95              Preference preference = (Preference) super.getModel().getValueAt(row, column);
96              if (preference != null)
97                  return preference.isEditable();
98              else
99                  return true;
100         } else
101             return false;
102     }
103 
104     /***
105      * Overrides TableInterface getCellRenederer. Bases it's decision on what
106      * renderer to return by looking at the property type for the Preference
107      * in the given row
108      * 
109      * @param row
110      * @param column
111      * @return
112      */
113     public TableCellRenderer getCellRenderer(int row, int column)
114     {
115         if (column == 1)
116         {
117             Preference preference = (Preference) super.getModel().getValueAt(row, column);
118             if (preference != null)
119             {
120                 TableCellRenderer renderer = getRendererForType(preference.getType(), null);
121                 if (renderer == null) renderer = super.getCellRenderer(row, column);
122 
123                 if (renderer instanceof JComponent) ((JComponent) renderer).setToolTipText(preference.getDescription());
124 
125                 return renderer;
126             }
127         } else if (column == 0)
128         {
129             Preference preference = (Preference) super.getModel().getValueAt(row, column + 1);
130             if (preference != null)
131             {
132                 TableCellRenderer renderer = super.getCellRenderer(row, column);
133                 if (renderer != null && renderer instanceof JComponent) ((JComponent) renderer).setToolTipText(preference.getDescription());
134 
135                 return renderer;
136             }
137         }
138 
139         return super.getCellRenderer(row, column);
140 
141     }
142 
143     /***
144      * Overrides TableInterface getCellEditor. Bases it's decision on what
145      * editor to return by looking at the property type for the Preference in
146      * the given row
147      * 
148      * @param row
149      * @param column
150      * @return
151      */
152     public TableCellEditor getCellEditor(int row, int column)
153     {
154         if (column == 1)
155         {
156             Preference preference = (Preference) super.getModel().getValueAt(row, column);
157             TableCellEditor editor = getEditorForPreference(preference, owner);
158             if (editor != null)
159               return editor;
160         }
161 
162         return super.getCellEditor(row, column);
163     }
164    /***
165     * Updates the preference if required.
166     * @see javax.swing.JTable#setValueAt(java.lang.Object, int, int)
167     * @param aValue
168     * @param row
169     * @param column
170     */
171     public void setValueAt(Object aValue, int row, int column)
172     {
173         try
174         {
175             if (column == 1)
176             {
177                 if (aValue instanceof Component || aValue instanceof Preference)
178                 {
179                     super.setValueAt(aValue, row, column);
180                 } else
181                 {
182                     Preference preference = (Preference) super.getModel().getValueAt(row, column);
183                     if (preference != null) preference.setValue(aValue);
184                 }
185             }
186         } catch (Exception e)
187         {
188             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e, null);
189         }
190 
191     }
192 
193     /***
194      * This method will construct a table model from the preferences vector provided.
195      * It excludes all preferences that are not visible
196      * @param propertiesVector
197      * @param theOwner
198      * @param listener
199      * @return
200      */
201     public static TableModel constructPropertiesModel(Vector propertiesVector, Component theOwner, PreferenceChangeListener listener)
202     {
203         Vector rows = new Vector();
204         Vector columnNames = new Vector();
205         columnNames.addElement("Name");
206         columnNames.addElement("Value");
207         DefaultTableModel model = new DefaultTableModel(rows, columnNames);
208 
209         for (int i = 0; i < propertiesVector.size(); i++)
210         {
211             Preference preference = (Preference) propertiesVector.elementAt(i);
212             if (preference.isVisible())
213             {
214                 Vector columns = new Vector();
215                 if (listener != null) preference.addPreferenceChangeListener(listener);
216 
217                 columns.addElement(preference.getLongName());
218                 columns.addElement(preference);
219                 rows.addElement(columns);
220             }
221         }
222         return model;
223     }
224     
225     /***
226      * This method will construct a table model from the preferences vector provided.
227      * It excludes all preferences that are not visible
228      * @param propertiesVector
229      * @param theOwner
230      * @param listener
231      * @return
232      */
233     public static TableModel constructPropertiesModel(Preference preference, Component theOwner, PreferenceChangeListener listener)
234     {
235         Vector rows = new Vector();
236         DefaultTableModel model = new DefaultTableModel(rows, null);
237 
238         if (preference.isVisible())
239         {
240            Vector columns = new Vector();
241            if (listener != null) preference.addPreferenceChangeListener(listener);
242            columns.addElement(preference);
243            rows.addElement(columns);
244         }
245         return model;
246     }
247     
248     /***
249      * This method works out the best editor to use for this preference
250      * @param preference
251      * @param theOwner
252      * @return
253      */
254     public static TableCellEditor getEditorForPreference(Preference preference, Component theOwner)
255     {
256         if (preference == null)
257             return null;
258         else
259             return getEditorForType(preference.getType(), theOwner, preference);
260         
261     }
262 
263     /***
264      * This method returns the correct editor to use for a particular preference type
265      * @param type
266      * @param theOwner
267      * @param preference
268      * @return
269      */
270     public static TableCellEditor getEditorForType(int type, Component theOwner, Preference preference)
271     {
272        switch (type)
273         {
274         case (Preference.PROPERTY_TYPE_TEXT):
275             return null;
276         case (Preference.PROPERTY_TYPE_FILE):
277             return new FilePreferenceEditor(new JButton("..."), theOwner,Preference.PROPERTY_TYPE_FILE, preference.getLongName());
278         case (Preference.PROPERTY_TYPE_DIRECTORY):
279             return new FilePreferenceEditor(new JButton("..."), theOwner,Preference.PROPERTY_TYPE_DIRECTORY, preference.getLongName());
280         case (Preference.PROPERTY_TYPE_COLOR):
281             return new ColorPreferenceEditor(new JButton("..."), theOwner);
282         case (Preference.PROPERTY_TYPE_INT):
283             return null;
284         case (Preference.PROPERTY_TYPE_FLOAT):
285             return null;
286         case (Preference.PROPERTY_TYPE_STRING_CHOICE):
287             return new DefaultCellEditor(getComboBox(preference));
288         case (Preference.PROPERTY_TYPE_INT_CHOICE):
289             return new DefaultCellEditor(getComboBox(preference));
290         case (Preference.PROPERTY_TYPE_BOOLEAN):
291             return new BooleanPreferenceEditor();
292         case (Preference.PROPERTY_TYPE_FONT):
293             return new FontPreferenceEditor(new JButton("..."), theOwner);
294         case (Preference.PROPERTY_TYPE_ENCRYPTED):
295             return new PasswordPreferenceEditor();
296         case (Preference.PROPERTY_TYPE_COLLECTION):
297             return new CollectionPreferenceEditor(new JButton("..."), theOwner);
298         default:
299             return null;
300         }
301     }
302     
303     /***
304      * Creates a valid JComboBox for this preference
305      * @param preference
306      * @return
307      */
308     private static JComboBox getComboBox(Preference preference)
309     {
310         JComboBox box = new JComboBox();
311         box.setModel(new ListComboBoxModel((preference.getChoiceValues() == null ? new ArrayList() : preference.getChoiceValues())));
312         return box;
313     }
314 
315     /***
316      * This method returns the best renderer to use for this particular preference type
317      * @param type
318      * @param owner
319      * @return
320      */
321     public static TableCellRenderer getRendererForType(int type, Component owner)
322     {
323         switch (type)
324         {
325         case (Preference.PROPERTY_TYPE_TEXT):
326             return null;
327         case (Preference.PROPERTY_TYPE_FILE):
328             return new FilePreferenceRenderer();
329         case (Preference.PROPERTY_TYPE_DIRECTORY):
330             return new FilePreferenceRenderer();
331         case (Preference.PROPERTY_TYPE_COLOR):
332             return new ColorPreferenceRenderer(true);
333         case (Preference.PROPERTY_TYPE_INT):
334             return null;
335         case (Preference.PROPERTY_TYPE_FLOAT):
336             return null;
337         case (Preference.PROPERTY_TYPE_STRING_CHOICE):
338             return null;
339         case (Preference.PROPERTY_TYPE_INT_CHOICE):
340             return null;
341         case (Preference.PROPERTY_TYPE_BOOLEAN):
342             return new BooleanPreferenceRenderer();
343         case (Preference.PROPERTY_TYPE_FONT):
344             return new FontPreferenceRenderer();
345         case (Preference.PROPERTY_TYPE_ENCRYPTED):
346             return new PasswordPreferenceRenderer();
347         case (Preference.PROPERTY_TYPE_COLLECTION):
348             return new CollectionPreferenceRenderer();
349         default:
350             return null;
351         }
352     }
353 
354 }
355