1 package net.suberic.util.gui.propedit; 2 import java.io.*; 3 import javax.swing.*; 4 import java.awt.event.ActionEvent ; 5 6 10 11 public class FileSelectorPane extends LabelValuePropertyEditor { 12 13 JLabel label; 14 JTextField valueDisplay; 15 JButton inputButton; 16 17 int fileSelection; 18 19 26 public void configureEditor(String propertyName, String template, String propertyBaseName, PropertyEditorManager newManager) { 27 configureBasic(propertyName, template, propertyBaseName, newManager); 28 29 String 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 (150 - inputButton.getPreferredSize().width, valueDisplay.getMinimumSize().height)); 40 41 String 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 (150, valueDisplay.getMinimumSize().height)); 64 tmpPanel.setMaximumSize(new java.awt.Dimension (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 78 public JButton createInputButton() { 79 try { 80 java.net.URL 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 (icon.getIconHeight(), icon.getIconWidth())); 87 newButton.addActionListener(new AbstractAction() { 88 public void actionPerformed(ActionEvent e) { 89 selectNewFolder(); 90 } 91 }); 92 93 return newButton; 94 } 95 } catch (java.util.MissingResourceException mre) { 96 } 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 JFileChooser jfc = 114 new JFileChooser((String )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 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 143 147 public void setValue() throws PropertyValueVetoException { 148 if (isEditorEnabled()) { 149 validateProperty(); 150 if (isChanged()) { 151 manager.setProperty(property, (String )valueDisplay.getText()); 152 originalValue = valueDisplay.getText(); 153 } 154 } 155 } 156 157 160 public void validateProperty() throws PropertyValueVetoException { 161 if (isEditorEnabled()) { 162 firePropertyCommittingEvent((String )valueDisplay.getText()); 163 } 164 } 165 166 170 public java.util.Properties getValue() { 171 java.util.Properties retProps = new java.util.Properties (); 172 173 retProps.setProperty(property, (String )valueDisplay.getText()); 174 175 return retProps; 176 } 177 178 182 public void resetDefaultValue() { 183 valueDisplay.setText(originalValue); 184 } 185 186 189 public boolean isChanged() { 190 return (!(originalValue.equals(valueDisplay.getText()))); 191 } 192 193 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 211 public String parseValue(String origString) { 212 StringBuffer newValue = new StringBuffer (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 variable = origString.substring(nextVar +2, end); 219 220 String 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 238 public boolean acceptDefaultFocus() { 239 if (isEditorEnabled()) { 240 valueDisplay.requestFocusInWindow(); 242 return true; 243 } else { 244 return false; 245 } 246 } 247 248 } 249 | Popular Tags |