View Javadoc

1   package com.explosion.utilities.preferences.editandrender.table;
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  /***
24   * Author: Stephen Cowx Date: Nov 9, 2002 Time: 7:50:42 PM
25   * This class double as a class that can throw up and editor for plain FIle objects
26   * as well as thrown uo and editor for Preference Objects of type FILE
27   */
28  
29  import java.awt.Color;
30  import java.awt.Component;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;
33  import java.io.File;
34  
35  import javax.swing.DefaultCellEditor;
36  import javax.swing.JButton;
37  import javax.swing.JCheckBox;
38  import javax.swing.JFileChooser;
39  import javax.swing.JTable;
40  
41  import com.explosion.utilities.FileSystemUtils;
42  import com.explosion.utilities.preferences.Preference;
43  
44  public class FilePreferenceEditor extends DefaultCellEditor
45  {
46  
47      /***
48       * The value in the cell
49       */
50      private Object value;
51      
52      private int type = -1;
53  
54  	private String description;
55  
56      /***
57       * Coonstructor for this object
58       * @param button
59       * @param owner
60       */
61      public FilePreferenceEditor(JButton button, Component owner, int type, String description)
62      {
63          super(new JCheckBox()); //Unfortunately, the constructor expects a
64                                  // check box, combo box, or text field.
65          this.description = description;
66          this.type = type;
67          this.editorComponent = button;
68          ((JButton) editorComponent).setBackground(Color.WHITE);
69  
70          setClickCountToStart(1); //This is usually 1 or 2.
71  
72          ((JButton) editorComponent).addActionListener(new FileButtonActionListener(this, owner));
73  
74      }
75  
76      protected void fireEditingStopped()
77      {
78          super.fireEditingStopped();
79      }
80  
81      public Object getCellEditorValue()
82      {
83          return value;
84      }
85  
86      public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
87      {
88      	this.value = value;
89          ((JButton) editorComponent).setText("...");
90  
91          return ((JButton) editorComponent);
92      }
93  
94      /***
95       * Returns the file value for this editor
96       * @return
97       */
98      public File getFile()
99      {
100         if (value instanceof Preference)
101         {
102           return (File) ((Preference)value).getValue();
103         }
104         else if (value instanceof File)
105         {
106            return (File) value;  
107         }
108         
109         return null;
110     }
111     
112     /***
113      * Sets the file value for this editor
114      * @param file
115      * @throws Exception
116      */
117     public void setFile(File file) throws Exception
118     {
119         if (value instanceof Preference)
120         {
121           ((Preference)value).setValue(file);
122         }
123         else if (value instanceof File)
124         {
125            value = file;  
126         }
127     }
128     
129     public String getDescription()
130     {
131     	return description;
132     }
133     
134     public int getType()
135     {
136     	return type;
137     }
138 }
139 
140 /***
141  * Opens up a fileChooserDialog
142  */
143 
144 class FileButtonActionListener implements ActionListener
145 {
146 
147     private JFileChooser filerChooser = null;
148 
149     FilePreferenceEditor fileEditor = null;
150 
151     private Component owner;
152 
153     public FileButtonActionListener(FilePreferenceEditor fileEditor, Component owner)
154     {
155         this.fileEditor = fileEditor;
156         filerChooser = new JFileChooser();
157         filerChooser.setMultiSelectionEnabled(false);
158         this.owner = owner;
159     }
160 
161     public void actionPerformed(ActionEvent e)
162     {
163         try
164         {
165             File[] file = null;
166             if (fileEditor.getType() == Preference.PROPERTY_TYPE_FILE)
167                 file = FileSystemUtils.chooseFiles(owner, FileSystemUtils.OPENTYPE, false, fileEditor.getFile(), FileSystemUtils.FILES_ONLY, "Select '"
168                         + fileEditor.getDescription() + "'");
169             else if (fileEditor.getType() == Preference.PROPERTY_TYPE_DIRECTORY)
170                     file = FileSystemUtils.chooseFiles(owner, FileSystemUtils.OPENTYPE, false, fileEditor.getFile(), FileSystemUtils.DIRECTORIES_ONLY, "Select '"
171                             + fileEditor.getDescription() + "'");
172 
173             if (file != null && file[0] != null) fileEditor.setFile(file[0]);
174                         
175         } catch (Exception e1)
176         {
177             com.explosion.utilities.exception.ExceptionManagerFactory.getExceptionManager().manageException(e1, null);
178         }
179         finally
180 		{
181         	fileEditor.fireEditingStopped();
182 		}
183     }
184 }