001: package net.suberic.pooka.gui.propedit;
002:
003: import net.suberic.util.gui.propedit.*;
004: import net.suberic.util.VariableBundle;
005: import net.suberic.pooka.gui.filechooser.*;
006: import net.suberic.pooka.*;
007: import javax.swing.*;
008: import java.awt.event.ActionEvent;
009: import javax.swing.filechooser.FileSystemView;
010:
011: /**
012: * This displays the currently selected folder (if any), along with a
013: * button which will bring up a dialog to select another folder.
014: */
015:
016: public class FolderSelectorPane extends SwingPropertyEditor {
017:
018: JLabel label;
019: JTextField valueDisplay;
020: JButton inputButton;
021:
022: /**
023: * @param propertyName The property to be edited.
024: * @param template The property that will define the layout of the
025: * editor.
026: * @param manager The PropertyEditorManager that will manage the
027: * changes.
028: * @param isEnabled Whether or not this editor is enabled by default.
029: */
030: public void configureEditor(String propertyName, String template,
031: PropertyEditorManager newManager, boolean isEnabled) {
032: property = propertyName;
033: manager = newManager;
034: editorTemplate = template;
035: originalValue = manager.getProperty(property, "");
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(originalValue);
045:
046: inputButton = createInputButton();
047:
048: valueDisplay.setPreferredSize(new java.awt.Dimension(
049: 150 - inputButton.getPreferredSize().width,
050: valueDisplay.getMinimumSize().height));
051:
052: this .add(label);
053: labelComponent = label;
054: JPanel tmpPanel = new JPanel(new java.awt.FlowLayout(
055: java.awt.FlowLayout.LEFT, 0, 0));
056: tmpPanel.add(valueDisplay);
057: tmpPanel.add(inputButton);
058: tmpPanel.setPreferredSize(new java.awt.Dimension(150,
059: valueDisplay.getMinimumSize().height));
060: valueComponent = tmpPanel;
061: //this.add(valueDisplay);
062: //this.add(inputButton);
063: this .add(tmpPanel);
064:
065: this .setEnabled(isEnabled);
066:
067: }
068:
069: /**
070: * Creates a button that will bring up a way to select a folder.
071: */
072: public JButton createInputButton() {
073: if (Pooka.isDebug())
074: System.out.println("creating an input button.");
075: try {
076: java.net.URL url = this .getClass().getResource(
077: manager.getProperty(
078: "FolderSelectorPane.inputButton.image",
079: "/net/suberic/util/gui/images/More.gif"));
080: if (url != null) {
081: if (Pooka.isDebug())
082: System.out.println("url isn't null.");
083:
084: ImageIcon icon = new ImageIcon(url);
085:
086: JButton newButton = new JButton(icon);
087: if (Pooka.isDebug())
088: System.out.println("new button is created.");
089:
090: newButton.setPreferredSize(new java.awt.Dimension(icon
091: .getIconHeight(), icon.getIconWidth()));
092: newButton.addActionListener(new AbstractAction() {
093: public void actionPerformed(ActionEvent e) {
094: selectNewFolder();
095: }
096: });
097:
098: if (Pooka.isDebug())
099: System.out.println("returning button.");
100:
101: return newButton;
102: }
103: } catch (java.util.MissingResourceException mre) {
104: }
105:
106: if (Pooka.isDebug())
107: System.out.println("error - creating a blank button.");
108:
109: JButton newButton = new JButton();
110: newButton.addActionListener(new AbstractAction() {
111: public void actionPerformed(ActionEvent e) {
112: selectNewFolder();
113: }
114: });
115:
116: return newButton;
117: }
118:
119: /**
120: * This actually brings up a JFileChooser to select a new Folder for
121: * the value of the property.
122: */
123: public void selectNewFolder() {
124:
125: FileSystemView mfsv = createFileSystemView();
126:
127: String defaultRoot = valueDisplay.getText();
128: if (defaultRoot.equals("")) {
129: defaultRoot = "/";
130: try {
131: String storeName = property.substring(property
132: .indexOf('.') + 1, property.indexOf('.',
133: property.indexOf('.') + 1));
134: StoreInfo si = Pooka.getStoreManager().getStoreInfo(
135: storeName);
136: if (si != null) {
137: defaultRoot = storeName;
138: }
139: } catch (Exception e) {
140: }
141: } else {
142:
143: if (defaultRoot.lastIndexOf('/') > -1) {
144: defaultRoot = defaultRoot.substring(0, defaultRoot
145: .lastIndexOf('/'));
146: }
147: }
148:
149: JFileChooser jfc = new JFileChooser(defaultRoot, mfsv);
150: jfc.setMultiSelectionEnabled(false);
151: jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
152: // workaround for bug in jdk 1.4
153: jfc.setCurrentDirectory(mfsv.createFileObject(defaultRoot));
154:
155: int returnValue = jfc.showDialog(Pooka.getMainPanel(), Pooka
156: .getProperty("FolderEditorPane.Select", "Select"));
157:
158: if (returnValue == JFileChooser.APPROVE_OPTION) {
159: java.io.File wrapper = jfc.getSelectedFile();
160: valueDisplay.setText(wrapper.getAbsolutePath());
161: }
162:
163: }
164:
165: /**
166: * Creates the FileSystemView appropriate for this file chooser. This
167: * can either be a view of all the stores and their corresponding
168: * folders, or just the folders of a single store. This is determined
169: * by the 'selectionRoot' subproperty of the edited property's template.
170: */
171: public FileSystemView createFileSystemView() {
172:
173: FileSystemView returnValue = null;
174: boolean justSubscribed = manager.getProperty(
175: editorTemplate + ".onlySubscribed", "true")
176: .equalsIgnoreCase("true");
177:
178: if (manager.getProperty(editorTemplate + ".selectionRoot",
179: "allStores").equals("allStores")) {
180: if (justSubscribed)
181: returnValue = new PookaFileSystemView();
182: else
183: returnValue = new MailFileSystemView();
184: } else {
185: int prefixSize = manager.getProperty(
186: editorTemplate + ".namePrefix", "Store.").length();
187: int suffixSize = manager.getProperty(
188: editorTemplate + ".nameSuffix", ".trashFolder")
189: .length();
190: String currentStoreName = property.substring(prefixSize,
191: property.length() - suffixSize);
192: net.suberic.pooka.StoreInfo currentStore = Pooka
193: .getStoreManager().getStoreInfo(currentStoreName);
194: if (currentStore != null) {
195: if (justSubscribed)
196: returnValue = new PookaFileSystemView(currentStore);
197: else
198: returnValue = new MailFileSystemView(currentStore);
199: }
200: }
201:
202: return returnValue;
203: }
204:
205: // as defined in net.suberic.util.gui.PropertyEditorUI
206:
207: public void setValue() {
208: if (Pooka.isDebug())
209: System.out.println("calling fsp.setValue. isEnabled() = "
210: + isEnabled() + "; isChanged() = " + isChanged());
211: if (isEnabled() && isChanged())
212: manager.setProperty(property, (String) valueDisplay
213: .getText());
214: }
215:
216: public java.util.Properties getValue() {
217: java.util.Properties retProps = new java.util.Properties();
218:
219: retProps.setProperty(property, (String) valueDisplay.getText());
220:
221: return retProps;
222: }
223:
224: public void resetDefaultValue() {
225: valueDisplay.setText(originalValue);
226: }
227:
228: public boolean isChanged() {
229: return (!(originalValue.equals(valueDisplay.getText())));
230: }
231:
232: public void setEnabled(boolean newValue) {
233: if (Pooka.isDebug())
234: System.out.println("calling fsp.setEnabled(" + newValue
235: + ")");
236: if (inputButton != null) {
237: inputButton.setEnabled(newValue);
238: }
239: if (valueDisplay != null) {
240: valueDisplay.setEnabled(newValue);
241: }
242: if (Pooka.isDebug())
243: System.out.println("set enabled to " + newValue);
244:
245: enabled = newValue;
246: }
247:
248: }
|