KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > FileOptionComponent


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui.config;
35
36 import javax.swing.*;
37 import javax.swing.event.*;
38 import edu.rice.cs.drjava.config.*;
39 import edu.rice.cs.drjava.*;
40 import edu.rice.cs.util.swing.FileSelectorComponent;
41 import java.awt.*;
42 import java.io.File JavaDoc;
43 import javax.swing.filechooser.FileFilter JavaDoc;
44
45 /** Graphical form of a FileOption.
46  *
47  * TO DO: Replace the internal components here with an edu.rice.cs.util.swing.FileSelectorComponent.
48  *
49  * @version $Id: FileOptionComponent.java 3839 2006-05-14 20:28:51Z rcartwright $
50  */

51 public class FileOptionComponent extends OptionComponent<File JavaDoc> implements OptionConstants {
52
53 // private JButton _button;
54
// private JTextField _jtf;
55
// private File _file;
56
// private JFileChooser _jfc;
57
// private FileFilter _fileFilter; // null if not customized
58
private FileSelectorComponent _component;
59
60   public FileOptionComponent (FileOption opt, String JavaDoc text, Frame parent, JFileChooser jfc) {
61     super(opt, text, parent);
62 // _button = new JButton();
63
// _button.addActionListener(new ActionListener() {
64
// public void actionPerformed(ActionEvent e) {
65
// chooseFile();
66
// }
67
// });
68
// _button.setText("...");
69
// _button.setMaximumSize(new Dimension(10,10));
70
// _button.setMinimumSize(new Dimension(10,10));
71
//
72
// _jtf = new JTextField();
73
// _jtf.setColumns(30);
74
//
75
// _jtf.setFont(_jtf.getFont().deriveFont(10f));
76
// _jtf.addActionListener(new ActionListener() {
77
// public void actionPerformed(ActionEvent e) {
78
// chooseFileFromField();
79
// }
80
// });
81
//
82
// _file = DrJava.getConfig().getSetting(_option);
83
// _updateTextField(_file);
84
//
85
// _jfc = jfc;
86
// _fileFilter = null;
87
//
88
// _panel = new JPanel();
89
// _panel.setLayout(new BorderLayout());
90
//
91
// _panel.add(_jtf, BorderLayout.CENTER);
92
// _panel.add(_button, BorderLayout.EAST);
93
_component = new FileSelectorComponent(parent, jfc, 30, 10f);
94     File JavaDoc setting = DrJava.getConfig().getSetting(_option);
95     if (setting != _option.getDefault()) {
96       _component.setFileField(setting);
97     }
98     _component.getFileField().getDocument().addDocumentListener(new DocumentListener() {
99       public void insertUpdate(DocumentEvent e) { notifyChangeListeners(); }
100       public void removeUpdate(DocumentEvent e) { notifyChangeListeners(); }
101       public void changedUpdate(DocumentEvent e) { notifyChangeListeners(); }
102     });
103   }
104
105   /** Constructor that allows for a tooltip description. */
106   public FileOptionComponent (FileOption opt, String JavaDoc text, Frame parent, String JavaDoc description, JFileChooser jfc) {
107     this(opt, text, parent, jfc);
108     setDescription(description);
109   }
110
111   /** Sets the tooltip description text for this option.
112    * @param description the tooltip text
113    */

114   public void setDescription(String JavaDoc description) {
115     _component.setToolTipText(description);
116 // _button.setToolTipText(description);
117
// _jtf.setToolTipText(description);
118
_label.setToolTipText(description);
119   }
120
121   /**
122    * Updates the config object with the new setting.
123    * @return true if the new value is set successfully
124    */

125   public boolean updateConfig() {
126     File JavaDoc componentFile = _component.getFileFromField();
127     File JavaDoc currentFile = DrJava.getConfig().getSetting(_option);
128     
129     if (componentFile != null && !componentFile.equals(currentFile)) {
130       DrJava.getConfig().setSetting(_option, componentFile);
131     }
132     else if (componentFile == null) {
133       DrJava.getConfig().setSetting(_option, _option.getDefault());
134     }
135
136     return true;
137   }
138
139   /** Displays the given value. */
140   public void setValue(File JavaDoc value) {
141 // _file = value;
142
// _updateTextField(value);
143
_component.setFileField(value);
144   }
145
146   /**
147    * Return's this OptionComponent's configurable component.
148    */

149   public JComponent getComponent() {
150     return _component;
151   }
152
153 // /**
154
// * Updates the text field to display the given file.
155
// */
156
// private void _updateTextField(File c) {
157
//// _jtf.setText(c.getAbsolutePath());
158
// _component.setFileField(c);
159
// }
160

161   /**
162    * Set the file filter for this file option component
163    */

164   public void setFileFilter(FileFilter JavaDoc fileFilter) {
165 // _fileFilter = fileFilter;
166
_component.setFileFilter(fileFilter);
167   }
168
169 // /**
170
// * Shows a file chooser to pick a new file. Allows picking directories.
171
// */
172
// public void chooseFile() {
173
// if (_file != FileOption.NULL_FILE && _file.getParent() != null) {
174
// _jfc.setCurrentDirectory(new File(_file.getParent()));
175
// }
176
//
177
// if (_fileFilter != null) {
178
// _jfc.setFileFilter(_fileFilter);
179
// }
180
// File c = null;
181
// int returnValue = _jfc.showDialog(_parent,
182
// null);
183
// if (returnValue == JFileChooser.APPROVE_OPTION)
184
// c = _jfc.getSelectedFile();
185
// if (c != null) {
186
// _file = c;
187
// _updateTextField(_file);
188
// }
189
// }
190

191 // /**
192
// * The chooser method for the validation of filenames that are manually entered
193
// * into the text field.
194
// * @return False, if file does not exist. True, otherwise.
195
// */
196
// public boolean chooseFileFromField() {
197
// String newValue = _jtf.getText().trim();
198
// String currentValue = DrJava.getConfig().getSetting(_option).getAbsolutePath();
199
//
200
// if (newValue.equals(currentValue)) {
201
// return true;
202
// }
203
//
204
// File newFile = _option.parse(newValue);
205
//
206
// if (newFile != null && !newFile.exists()) {
207
// JOptionPane.showMessageDialog(_parent,
208
// "The file '"+ newValue + "' is an invalid selection for\n" +
209
// getLabelText() + " because it does not exist.",
210
// "Invalid File Chosen for " + getLabelText() + "!",
211
// JOptionPane.ERROR_MESSAGE);
212
// return false;
213
// }
214
// else {
215
// _file = newFile;
216
// return true;
217
// }
218
// }
219
}
220
Popular Tags