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 ; 8 import javax.swing.filechooser.FileSystemView ; 9 10 14 15 public class FolderSelectorPane extends LabelValuePropertyEditor { 16 17 JLabel label; 18 JTextField valueDisplay; 19 JButton inputButton; 20 21 28 public void configureEditor(String propertyName, String template, String 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 (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 (150, valueDisplay.getMinimumSize().height)); 56 tmpPanel.setMaximumSize(new java.awt.Dimension (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 70 public JButton createInputButton() { 71 getLogger().fine("creating an input button."); 72 try { 73 java.net.URL 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 (icon.getIconHeight(), icon.getIconWidth())); 83 newButton.addActionListener(new AbstractAction() { 84 public void actionPerformed(ActionEvent e) { 85 selectNewFolder(); 86 } 87 }); 88 89 getLogger().fine("returning button."); 90 91 return newButton; 92 } 93 } catch (java.util.MissingResourceException 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 e) { 101 selectNewFolder(); 102 } 103 }); 104 105 return newButton; 106 } 107 108 112 public void selectNewFolder() { 113 114 FileSystemView mfsv = createFileSystemView(); 115 116 String defaultRoot = valueDisplay.getText(); 117 if (defaultRoot.equals("")) { 118 defaultRoot = "/"; 119 try { 120 String 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 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 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 wrapper = jfc.getSelectedFile(); 148 valueDisplay.setText(wrapper.getAbsolutePath()); 149 } 150 151 } 152 153 159 public FileSystemView createFileSystemView() { 160 161 FileSystemView 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 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 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 )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 )valueDisplay.getText()); 198 } 199 200 public java.util.Properties getValue() { 201 java.util.Properties retProps = new java.util.Properties (); 202 203 retProps.setProperty(property, (String )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 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 |