001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.util.swing;
038:
039: import javax.swing.*;
040: import javax.swing.filechooser.FileFilter;
041: import java.awt.*;
042: import java.awt.event.ActionEvent;
043: import java.awt.event.ActionListener;
044: import java.awt.event.FocusListener;
045: import java.awt.event.FocusEvent;
046: import java.io.*;
047:
048: /** Just like FileSelectorComponent, but it converts the file to a different string that gets displayed. */
049: public class FileSelectorStringComponent extends JPanel {
050:
051: /** The default number of columns for the text box. */
052: public static final int DEFAULT_NUM_COLS = 30;
053:
054: /** The default font size for the text box. */
055: public static final float DEFAULT_FONT_SIZE = 10f;
056:
057: /** The parent component of this component. */
058: protected final Component _parent;
059:
060: /** Text field with the name of the selected file. */
061: protected final JTextField _textField;
062:
063: /** "..." button to open the file chooser. */
064: protected final JButton _chooserButton;
065:
066: /** File chooser to open when clicking the "..." button. */
067: protected final FileChooser _chooser;
068:
069: /** The current file */
070: protected volatile File _file;
071:
072: /** Creates a new DirectorySelectorStringComponent with default dimensions.
073: * @param parent Parent of this component.
074: * @param chooser File chooser to display from the "..." button. Assumed non-null!
075: */
076: public FileSelectorStringComponent(Component parent,
077: FileChooser chooser) {
078: this (parent, chooser, DEFAULT_NUM_COLS, DEFAULT_FONT_SIZE);
079: }
080:
081: /** Creates a new DirectorySelectorStringComponent.
082: * @param parent Parent of this component.
083: * @param chooser File chooser to display from the "..." button. Assumed non-null!
084: * @param numCols Number of columns to display in the text field
085: * @param fontSize Font size for the text field
086: */
087: public FileSelectorStringComponent(Component parent,
088: FileChooser chooser, int numCols, float fontSize) {
089: _parent = parent;
090: _chooser = chooser;
091: _file = null;
092:
093: _textField = new JTextField(numCols) {
094: public Dimension getMaximumSize() {
095: return new Dimension(Short.MAX_VALUE, super
096: .getPreferredSize().height);
097: }
098: };
099: _textField.setFont(_textField.getFont().deriveFont(fontSize));
100: _textField.setPreferredSize(new Dimension(22, 22));
101:
102: _chooserButton = new JButton("...");
103: _chooserButton.addActionListener(new ActionListener() {
104: public void actionPerformed(ActionEvent e) {
105: _chooseFile();
106: }
107: });
108: _chooserButton.setMaximumSize(new Dimension(22, 22));
109: _chooserButton.setMargin(new Insets(0, 5, 0, 5));
110: // Add components
111: this .setLayout(new BoxLayout(this , BoxLayout.X_AXIS));
112: this .add(_textField);
113: this .add(_chooserButton);
114: }
115:
116: public void setEnabled(boolean isEnabled) {
117: _textField.setEnabled(isEnabled);
118: _chooserButton.setEnabled(isEnabled);
119: super .setEnabled(isEnabled);
120: }
121:
122: /** Returns the file text field. */
123: public JTextField getTextField() {
124: return _textField;
125: }
126:
127: /** Returns the file chooser. */
128: public FileChooser getFileChooser() {
129: return _chooser;
130: }
131:
132: /** Converts a string representation from the text field into a File. */
133: public File convertStringToFile(String s) {
134: s = s.trim();
135: if (s.equals(""))
136: return null;
137: return new File(s);
138: }
139:
140: /** Converts a file to the string representation of the text field. */
141: public String convertFileToString(File f) {
142: if (f == null)
143: return "";
144: return f.toString();
145: }
146:
147: /** Returns the last file that was selected. */
148: public File getFileFromField() {
149: // Get the file from the chooser
150: String newValue = _textField.getText();
151: File newFile = null;
152: if (!newValue.equals("")) {
153: newFile = convertStringToFile(newValue);
154: if (!newFile.isDirectory()
155: && !_chooser.isFileSelectionEnabled())
156: newFile = newFile.getParentFile();
157: }
158:
159: if (newFile != null && !newFile.exists())
160: newFile = _file;
161:
162: return newFile;
163: }
164:
165: /** Returns the string in the text field. */
166: public String getText() {
167: return _textField.getText();
168: }
169:
170: /** Sets the string in the text field. */
171: public void setText(String s) {
172: _textField.setText(s);
173: }
174:
175: /** Sets the text of the file field to be the given file.
176: * @param file File to display in the file field.
177: */
178: public void setFileField(File file) {
179: _file = file;
180: if (file != null && !file.getPath().equals("")) {
181: try {
182: _file = file.getCanonicalFile();
183: } catch (IOException e) { /* do nothing */
184: }
185: }
186: resetFileField();
187: }
188:
189: public void resetFileField() {
190: _textField.setText(convertFileToString(_file));
191: _textField.setCaretPosition(_textField.getText().length());
192: }
193:
194: public void setToolTipText(String text) {
195: super .setToolTipText(text);
196: _textField.setToolTipText(text);
197: _chooserButton.setToolTipText(text);
198: }
199:
200: /** Adds a filter to decide if a directory can be chosen. */
201: public void addChoosableFileFilter(FileFilter filter) {
202: _chooser.addChoosableFileFilter(filter);
203: }
204:
205: /** Removes the given filefilter from the chooser */
206: public void removeChoosableFileFilter(FileFilter filter) {
207: _chooser.removeChoosableFileFilter(filter);
208: }
209:
210: public void clearChoosableFileFilters() {
211: _chooser.resetChoosableFileFilters();
212: }
213:
214: /** Opens the file chooser to select a file, putting the result in the file field. */
215: private void _chooseFile() {
216: File f = getFileFromField();
217: if (f != null && f.exists()) {
218: _chooser.setCurrentDirectory(f);
219: _chooser.setSelectedFile(f);
220: }
221: int returnValue = _chooser.showDialog(_parent, null);
222: if (returnValue == FileChooser.APPROVE_OPTION) {
223: File chosen = _chooser.getSelectedFile();
224: if (chosen != null) {
225: setFileField(chosen);
226: }
227: }
228: }
229:
230: }
|