KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > propedit > FolderSelectorPane


1 package net.suberic.pooka.gui.propedit;
2 import net.suberic.util.gui.propedit.*;
3 import net.suberic.util.VariableBundle;
4 import net.suberic.pooka.gui.filechooser.*;
5 import net.suberic.pooka.*;
6 import javax.swing.*;
7 import java.awt.event.ActionEvent JavaDoc;
8 import javax.swing.filechooser.FileSystemView JavaDoc;
9
10 /**
11  * This displays the currently selected folder (if any), along with a
12  * button which will bring up a dialog to select another folder.
13  */

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

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

70   public JButton createInputButton() {
71     getLogger().fine("creating an input button.");
72     try {
73       java.net.URL JavaDoc url = this.getClass().getResource(manager.getProperty("FolderSelectorPane.inputButton.image", "/net/suberic/util/gui/images/More.gif"));
74       if (url != null) {
75         getLogger().fine("url isn't null.");
76
77         ImageIcon icon = new ImageIcon(url);
78
79         JButton newButton = new JButton(icon);
80         getLogger().fine("new button is created.");
81
82         newButton.setPreferredSize(new java.awt.Dimension JavaDoc(icon.getIconHeight(), icon.getIconWidth()));
83         newButton.addActionListener(new AbstractAction() {
84             public void actionPerformed(ActionEvent JavaDoc e) {
85               selectNewFolder();
86             }
87           });
88
89         getLogger().fine("returning button.");
90
91         return newButton;
92       }
93     } catch (java.util.MissingResourceException JavaDoc mre) {
94     }
95
96     getLogger().fine("error - creating a blank button.");
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 Folder for
110    * the value of the property.
111    */

112   public void selectNewFolder() {
113
114     FileSystemView JavaDoc mfsv = createFileSystemView();
115
116     String JavaDoc defaultRoot = valueDisplay.getText();
117     if (defaultRoot.equals("")) {
118       defaultRoot = "/";
119       try {
120         String JavaDoc storeName = property.substring(property.indexOf('.') + 1, property.indexOf('.', property.indexOf('.') + 1));
121         StoreInfo si = Pooka.getStoreManager().getStoreInfo(storeName);
122         if (si != null) {
123           defaultRoot = storeName;
124         }
125       } catch (Exception JavaDoc e) {
126       }
127     } else {
128
129       if (defaultRoot.lastIndexOf('/') > -1) {
130         defaultRoot = defaultRoot.substring(0, defaultRoot.lastIndexOf('/'));
131       }
132     }
133
134     JFileChooser jfc =
135       new JFileChooser(defaultRoot, mfsv);
136     jfc.setMultiSelectionEnabled(false);
137     jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
138     // workaround for bug in jdk 1.4
139
jfc.setCurrentDirectory(mfsv.createFileObject(defaultRoot));
140
141     int returnValue =
142       jfc.showDialog(Pooka.getMainPanel(),
143                      Pooka.getProperty("FolderEditorPane.Select",
144                                        "Select"));
145
146     if (returnValue == JFileChooser.APPROVE_OPTION) {
147       java.io.File JavaDoc wrapper = jfc.getSelectedFile();
148       valueDisplay.setText(wrapper.getAbsolutePath());
149     }
150
151   }
152
153   /**
154    * Creates the FileSystemView appropriate for this file chooser. This
155    * can either be a view of all the stores and their corresponding
156    * folders, or just the folders of a single store. This is determined
157    * by the 'selectionRoot' subproperty of the edited property's template.
158    */

159   public FileSystemView JavaDoc createFileSystemView() {
160
161     FileSystemView JavaDoc returnValue = null;
162     boolean justSubscribed = manager.getProperty(editorTemplate + ".onlySubscribed", "true").equalsIgnoreCase("true");
163
164     if (manager.getProperty(editorTemplate + ".selectionRoot", "allStores").equals("allStores")) {
165       if (justSubscribed)
166         returnValue = new PookaFileSystemView();
167       else
168         returnValue = new MailFileSystemView();
169     } else {
170       int prefixSize = manager.getProperty(editorTemplate + ".namePrefix", "Store.").length();
171       int suffixSize = manager.getProperty(editorTemplate + ".nameSuffix", ".trashFolder").length();
172       String JavaDoc currentStoreName = property.substring(prefixSize, property.length() - suffixSize);
173       net.suberic.pooka.StoreInfo currentStore = Pooka.getStoreManager().getStoreInfo(currentStoreName);
174       if (currentStore != null) {
175         if (justSubscribed)
176           returnValue = new PookaFileSystemView(currentStore);
177         else
178           returnValue = new MailFileSystemView(currentStore);
179       }
180     }
181
182     return returnValue;
183   }
184
185   // as defined in net.suberic.util.gui.PropertyEditorUI
186

187   public void setValue() throws PropertyValueVetoException {
188     validateProperty();
189     getLogger().fine("calling fsp.setValue. isEnabled() = " + isEnabled() + "; isChanged() = " + isChanged());
190     if (isEditorEnabled() && isChanged())
191       manager.setProperty(property, (String JavaDoc)valueDisplay.getText());
192   }
193
194   public void validateProperty() throws PropertyValueVetoException {
195     getLogger().fine("calling fsp.validateProperty(). isEnabled() = " + isEditorEnabled() + "; isChanged() = " + isChanged());
196     if (isEditorEnabled())
197       firePropertyCommittingEvent((String JavaDoc)valueDisplay.getText());
198   }
199
200   public java.util.Properties JavaDoc getValue() {
201     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
202
203     retProps.setProperty(property, (String JavaDoc)valueDisplay.getText());
204
205     return retProps;
206   }
207
208   public void resetDefaultValue() {
209     valueDisplay.setText(originalValue);
210   }
211
212   public boolean isChanged() {
213     return (!(originalValue.equals(valueDisplay.getText())));
214   }
215
216   /**
217    * Run when the PropertyEditor may have changed enabled states.
218    */

219   protected void updateEditorEnabled() {
220     getLogger().fine("calling fsp.updateEditorEnabled(). isEditorEnabled() = " + isEditorEnabled());
221     if (inputButton != null) {
222       inputButton.setEnabled(isEditorEnabled());
223     }
224     if (valueDisplay != null) {
225       valueDisplay.setEnabled(isEditorEnabled());
226     }
227     if (label != null) {
228       label.setEnabled(isEditorEnabled());
229     }
230     getLogger().fine("set enabled to " + isEditorEnabled());
231
232   }
233
234 }
235
Popular Tags