1 package com.explosion.utilities.preferences.editandrender.table;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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());
64
65 this.description = description;
66 this.type = type;
67 this.editorComponent = button;
68 ((JButton) editorComponent).setBackground(Color.WHITE);
69
70 setClickCountToStart(1);
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 }