KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > FileSelectorPane


1 package net.suberic.util.gui.propedit;
2 import java.io.*;
3 import javax.swing.*;
4 import java.awt.event.ActionEvent JavaDoc;
5
6 /**
7  * This displays the currently selected file (if any), along with a
8  * button which will bring up a JFileChooser to choose any other file(s).
9  */

10
11 public class FileSelectorPane extends LabelValuePropertyEditor {
12
13   JLabel label;
14   JTextField valueDisplay;
15   JButton inputButton;
16
17   int fileSelection;
18
19   /**
20    * @param propertyName The property to be edited.
21    * @param template The property that will define the layout of the
22    * editor.
23    * @param manager The PropertyEditorManager that will manage the
24    * changes.
25    */

26   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
27     configureBasic(propertyName, template, propertyBaseName, newManager);
28
29     String JavaDoc currentValue = parseValue(manager.getProperty(property, ""));
30
31     getLogger().fine("property is " + property + "; editorTemplate is " + editorTemplate);
32
33     label = createLabel();
34
35     valueDisplay = new JTextField(currentValue);
36
37     inputButton = createInputButton();
38
39     valueDisplay.setPreferredSize(new java.awt.Dimension JavaDoc(150 - inputButton.getPreferredSize().width, valueDisplay.getMinimumSize().height));
40
41     String JavaDoc selectionType = manager.getProperty(editorTemplate + ".propertyType", "File");
42     if (selectionType.equalsIgnoreCase("Directory")) {
43       fileSelection = JFileChooser.DIRECTORIES_ONLY;
44     } else {
45       fileSelection = JFileChooser.FILES_ONLY;
46     }
47
48     this.add(label);
49     labelComponent = label;
50     JPanel tmpPanel = new JPanel();
51     SpringLayout layout = new SpringLayout();
52     tmpPanel.setLayout(layout);
53
54     tmpPanel.add(valueDisplay);
55     tmpPanel.add(inputButton);
56
57     layout.putConstraint(SpringLayout.NORTH, valueDisplay, 0, SpringLayout.NORTH, tmpPanel);
58     layout.putConstraint(SpringLayout.WEST, valueDisplay, 0, SpringLayout.WEST, tmpPanel);
59     layout.putConstraint(SpringLayout.SOUTH, tmpPanel, 0, SpringLayout.SOUTH, valueDisplay);
60     layout.putConstraint(SpringLayout.WEST, inputButton, 5, SpringLayout.EAST, valueDisplay);
61     layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, inputButton);
62
63     tmpPanel.setPreferredSize(new java.awt.Dimension JavaDoc(150, valueDisplay.getMinimumSize().height));
64     tmpPanel.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, valueDisplay.getMinimumSize().height));
65
66     valueComponent = tmpPanel;
67
68     this.add(tmpPanel);
69
70     manager.registerPropertyEditor(property, this);
71
72     updateEditorEnabled();
73   }
74
75   /**
76    * Creates a button that will bring up a way to select a new File.
77    */

78   public JButton createInputButton() {
79     try {
80       java.net.URL JavaDoc url = this.getClass().getResource(manager.getProperty("FileSelectorPane.inputButton.image", "/net/suberic/util/gui/images/More.gif"));
81       if (url != null) {
82         ImageIcon icon = new ImageIcon(url);
83
84         JButton newButton = new JButton(icon);
85
86         newButton.setPreferredSize(new java.awt.Dimension JavaDoc(icon.getIconHeight(), icon.getIconWidth()));
87         newButton.addActionListener(new AbstractAction() {
88             public void actionPerformed(ActionEvent JavaDoc e) {
89               selectNewFolder();
90             }
91           });
92
93         return newButton;
94       }
95     } catch (java.util.MissingResourceException JavaDoc mre) {
96     }
97
98     JButton newButton = new JButton();
99     newButton.addActionListener(new AbstractAction() {
100         public void actionPerformed(ActionEvent JavaDoc e) {
101           selectNewFolder();
102         }
103       });
104
105     return newButton;
106   }
107
108   /**
109    * This actually brings up a JFileChooser to select a new File for
110    * the value of the property.
111    */

112   public void selectNewFolder() {
113     JFileChooser jfc =
114       new JFileChooser((String JavaDoc)valueDisplay.getText());
115     jfc.setMultiSelectionEnabled(false);
116     jfc.setFileSelectionMode(fileSelection);
117     jfc.setFileHidingEnabled(false);
118
119     int returnValue =
120       jfc.showDialog(this,
121                      manager.getProperty("FolderEditorPane.Select",
122                                          "Select"));
123
124     if (returnValue == JFileChooser.APPROVE_OPTION) {
125       File returnFile = jfc.getSelectedFile();
126       String JavaDoc newValue = returnFile.getAbsolutePath();
127
128       try {
129         firePropertyChangingEvent(newValue);
130         firePropertyChangedEvent(newValue);
131
132         valueDisplay.setText(newValue);
133
134       } catch (PropertyValueVetoException pvve) {
135         manager.getFactory().showError(valueDisplay, "Error changing value " + label.getText() + " to " + newValue + ": " + pvve.getReason());
136       }
137     }
138
139   }
140
141   // as defined in net.suberic.util.gui.PropertyEditorUI
142

143   /**
144    * This writes the currently configured value in the PropertyEditorUI
145    * to the source PropertyEditorManager.
146    */

147   public void setValue() throws PropertyValueVetoException {
148     if (isEditorEnabled()) {
149       validateProperty();
150       if (isChanged()) {
151         manager.setProperty(property, (String JavaDoc)valueDisplay.getText());
152         originalValue = valueDisplay.getText();
153       }
154     }
155   }
156
157   /**
158    * Validates the selected value.
159    */

160   public void validateProperty() throws PropertyValueVetoException {
161     if (isEditorEnabled()) {
162       firePropertyCommittingEvent((String JavaDoc)valueDisplay.getText());
163     }
164   }
165
166   /**
167    * Returns the current values of the edited properties as a
168    * java.util.Properties object.
169    */

170   public java.util.Properties JavaDoc getValue() {
171     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
172
173     retProps.setProperty(property, (String JavaDoc)valueDisplay.getText());
174
175     return retProps;
176   }
177
178   /**
179    * This resets the editor to the original (or latest set, if setValue()
180    * has been called) value of the edited property.
181    */

182   public void resetDefaultValue() {
183     valueDisplay.setText(originalValue);
184   }
185
186   /**
187    * Returns whether or not this editor has its original value.
188    */

189   public boolean isChanged() {
190     return (!(originalValue.equals(valueDisplay.getText())));
191   }
192
193   /**
194    * Run when the PropertyEditor may have changed enabled states.
195    */

196   protected void updateEditorEnabled() {
197     if (inputButton != null) {
198       inputButton.setEnabled(isEditorEnabled());
199     }
200     if (valueDisplay != null) {
201       valueDisplay.setEnabled(isEditorEnabled());
202     }
203     if (label != null) {
204       label.setEnabled(isEditorEnabled());
205     }
206   }
207
208   /**
209    * Parses any ${} special values out of the string.
210    */

211   public String JavaDoc parseValue(String JavaDoc origString) {
212     StringBuffer JavaDoc newValue = new StringBuffer JavaDoc(origString);
213     int nextVar = origString.indexOf("${");
214     int offset = 0;
215     while (nextVar >= 0) {
216       int end = origString.indexOf("}", nextVar);
217       if (end >= nextVar) {
218         String JavaDoc variable = origString.substring(nextVar +2, end);
219
220         String JavaDoc replaceValue = System.getProperty(variable);
221         if (replaceValue == null)
222           replaceValue = "";
223         newValue.replace(nextVar + offset, end + 1 + offset, replaceValue);
224
225         offset = offset - end + nextVar + replaceValue.length() - 1;
226
227         nextVar = origString.indexOf("${", end);
228       } else {
229         nextVar = -1;
230       }
231     }
232     return newValue.toString();
233   }
234
235   /**
236    * Accepts or rejects the initial focus for this component.
237    */

238   public boolean acceptDefaultFocus() {
239     if (isEditorEnabled()) {
240       // for some reason this returns false in dialogs
241
valueDisplay.requestFocusInWindow();
242       return true;
243     } else {
244       return false;
245     }
246   }
247
248 }
249
Popular Tags