001: package net.suberic.util.gui.propedit;
002:
003: import java.io.*;
004: import javax.swing.*;
005: import java.awt.event.ActionEvent;
006:
007: /**
008: * This displays the currently selected file (if any), along with a
009: * button which will bring up a JFileChooser to choose any other file(s).
010: */
011:
012: public class FileSelectorPane extends SwingPropertyEditor {
013:
014: JLabel label;
015: JTextField valueDisplay;
016: JButton inputButton;
017:
018: int fileSelection;
019:
020: /**
021: * @param propertyName The property to be edited.
022: * @param template The property that will define the layout of the
023: * editor.
024: * @param manager The PropertyEditorManager that will manage the
025: * changes.
026: * @param isEnabled Whether or not this editor is enabled by default.
027: */
028: public void configureEditor(String propertyName, String template,
029: PropertyEditorManager newManager, boolean isEnabled) {
030: property = propertyName;
031: manager = newManager;
032: editorTemplate = template;
033: originalValue = manager.getProperty(property, "");
034: String currentValue = parseValue(manager.getProperty(property,
035: ""));
036:
037: if (debug) {
038: System.out.println("property is " + property
039: + "; editorTemplate is " + editorTemplate);
040: }
041:
042: label = createLabel();
043:
044: valueDisplay = new JTextField(currentValue);
045:
046: inputButton = createInputButton();
047:
048: valueDisplay.setPreferredSize(new java.awt.Dimension(
049: 150 - inputButton.getPreferredSize().width,
050: valueDisplay.getMinimumSize().height));
051:
052: String selectionType = manager.getProperty(editorTemplate
053: + ".propertyType", "File");
054: if (selectionType.equalsIgnoreCase("Directory")) {
055: fileSelection = JFileChooser.DIRECTORIES_ONLY;
056: } else {
057: fileSelection = JFileChooser.FILES_ONLY;
058: }
059:
060: this .add(label);
061: labelComponent = label;
062: JPanel tmpPanel = new JPanel(new java.awt.FlowLayout(
063: java.awt.FlowLayout.LEFT, 0, 0));
064: tmpPanel.add(valueDisplay);
065: tmpPanel.add(inputButton);
066: tmpPanel.setPreferredSize(new java.awt.Dimension(150,
067: valueDisplay.getMinimumSize().height));
068: valueComponent = tmpPanel;
069:
070: this .add(tmpPanel);
071:
072: this .setEnabled(isEnabled);
073:
074: manager.registerPropertyEditor(property, this );
075: }
076:
077: /**
078: * Creates a button that will bring up a way to select a new File.
079: */
080: public JButton createInputButton() {
081: try {
082: java.net.URL url = this .getClass().getResource(
083: manager.getProperty(
084: "FileSelectorPane.inputButton.image",
085: "/net/suberic/util/gui/images/More.gif"));
086: if (url != null) {
087: ImageIcon icon = new ImageIcon(url);
088:
089: JButton newButton = new JButton(icon);
090:
091: newButton.setPreferredSize(new java.awt.Dimension(icon
092: .getIconHeight(), icon.getIconWidth()));
093: newButton.addActionListener(new AbstractAction() {
094: public void actionPerformed(ActionEvent e) {
095: selectNewFolder();
096: }
097: });
098:
099: return newButton;
100: }
101: } catch (java.util.MissingResourceException mre) {
102: }
103:
104: JButton newButton = new JButton();
105: newButton.addActionListener(new AbstractAction() {
106: public void actionPerformed(ActionEvent e) {
107: selectNewFolder();
108: }
109: });
110:
111: return newButton;
112: }
113:
114: /**
115: * This actually brings up a JFileChooser to select a new File for
116: * the value of the property.
117: */
118: public void selectNewFolder() {
119: JFileChooser jfc = new JFileChooser((String) valueDisplay
120: .getText());
121: jfc.setMultiSelectionEnabled(false);
122: jfc.setFileSelectionMode(fileSelection);
123: jfc.setFileHidingEnabled(false);
124:
125: int returnValue = jfc.showDialog(this , manager.getProperty(
126: "FolderEditorPane.Select", "Select"));
127:
128: if (returnValue == JFileChooser.APPROVE_OPTION) {
129: File returnFile = jfc.getSelectedFile();
130: String newValue = returnFile.getAbsolutePath();
131:
132: try {
133: firePropertyChangingEvent(newValue);
134: firePropertyChangedEvent(newValue);
135:
136: valueDisplay.setText(newValue);
137:
138: } catch (PropertyValueVetoException pvve) {
139: manager.getFactory().showError(
140: valueDisplay,
141: "Error changing value " + label.getText()
142: + " to " + newValue + ": "
143: + pvve.getReason());
144: }
145: }
146:
147: }
148:
149: // as defined in net.suberic.util.gui.PropertyEditorUI
150:
151: /**
152: * This writes the currently configured value in the PropertyEditorUI
153: * to the source PropertyEditorManager.
154: */
155: public void setValue() {
156: if (isEnabled() && isChanged())
157: manager.setProperty(property, (String) valueDisplay
158: .getText());
159: }
160:
161: /**
162: * Returns the current values of the edited properties as a
163: * java.util.Properties object.
164: */
165: public java.util.Properties getValue() {
166: java.util.Properties retProps = new java.util.Properties();
167:
168: retProps.setProperty(property, (String) valueDisplay.getText());
169:
170: return retProps;
171: }
172:
173: /**
174: * This resets the editor to the original (or latest set, if setValue()
175: * has been called) value of the edited property.
176: */
177: public void resetDefaultValue() {
178: valueDisplay.setText(originalValue);
179: }
180:
181: /**
182: * Returns whether or not this editor has its original value.
183: */
184: public boolean isChanged() {
185: return (!(originalValue.equals(valueDisplay.getText())));
186: }
187:
188: /**
189: * Sets the enabled property of the PropertyEditorUI. Disabled
190: * editors should not be able to do setValue() calls.
191: */
192: public void setEnabled(boolean newValue) {
193: if (inputButton != null) {
194: inputButton.setEnabled(newValue);
195: enabled = newValue;
196: }
197: }
198:
199: /**
200: * Parses any ${} special values out of the string.
201: */
202: public String parseValue(String origString) {
203: StringBuffer newValue = new StringBuffer(origString);
204: int nextVar = origString.indexOf("${");
205: int offset = 0;
206: while (nextVar >= 0) {
207: int end = origString.indexOf("}", nextVar);
208: if (end >= nextVar) {
209: String variable = origString
210: .substring(nextVar + 2, end);
211:
212: String replaceValue = System.getProperty(variable);
213: if (replaceValue == null)
214: replaceValue = "";
215: newValue.replace(nextVar + offset, end + 1 + offset,
216: replaceValue);
217:
218: offset = offset - end + nextVar + replaceValue.length()
219: - 1;
220:
221: nextVar = origString.indexOf("${", end);
222: } else {
223: nextVar = -1;
224: }
225: }
226: return newValue.toString();
227: }
228: }
|