001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.ui;
034:
035: import java.awt.Dimension;
036: import java.awt.GridBagConstraints;
037: import java.awt.GridBagLayout;
038: import java.awt.Insets;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.io.File;
042: import java.util.ArrayList;
043: import java.util.Iterator;
044:
045: import javax.swing.DefaultComboBoxModel;
046: import javax.swing.JButton;
047: import javax.swing.JComboBox;
048: import javax.swing.JFileChooser;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.SwingUtilities;
052: import javax.swing.filechooser.FileFilter;
053:
054: import com.vividsolutions.jts.util.Assert;
055: import com.vividsolutions.jump.I18N;
056:
057: public class FileNamePanel extends JPanel {
058: /**
059: * @deprecated
060: */
061: public void setDescription(String description) {
062: upperDescriptionLabel.setText(description);
063: if (description.equals(GUIUtil.fmeDesc)) {
064: setFileFilter(new WorkbenchFileFilter(GUIUtil.fmeDesc));
065: } else if (description.equals(GUIUtil.gmlDesc)) {
066: setFileFilter(new WorkbenchFileFilter(GUIUtil.gmlDesc));
067: } else if (description.equals(GUIUtil.jmlDesc)) {
068: setFileFilter(new WorkbenchFileFilter(GUIUtil.jmlDesc));
069: } else if (description.equals(GUIUtil.shpDesc)) {
070: setFileFilter(new WorkbenchFileFilter(GUIUtil.shpDesc));
071: } else if (description.equals(GUIUtil.wktDesc)) {
072: setFileFilter(new WorkbenchFileFilter(GUIUtil.wktDesc));
073: } else if (description.equals(GUIUtil.xmlDesc)) {
074: setFileFilter(new WorkbenchFileFilter(GUIUtil.xmlDesc));
075: }
076: }
077:
078: GridBagLayout gridBagLayout1 = new GridBagLayout();
079: JLabel upperDescriptionLabel = new JLabel();
080: JComboBox comboBox = new JComboBox();
081: JButton browseButton = new JButton();
082: private ErrorHandler errorHandler;
083: private DefaultComboBoxModel comboBoxModel = new DefaultComboBoxModel();
084: private boolean fileMustExist;
085: private ArrayList browseListeners = new ArrayList();
086:
087: //Specify a maximum size because eventually we may want to persist the
088: //cache in the JCS Workbench properties file [Jon Aquino]
089: private int MAX_CACHE_SIZE = 10;
090:
091: public FileNamePanel(ErrorHandler errorHandler) {
092: this .errorHandler = errorHandler;
093:
094: try {
095: jbInit();
096: } catch (Exception ex) {
097: ex.printStackTrace();
098: }
099:
100: GUIUtil.fixEditableComboBox(comboBox);
101: }
102:
103: public void setFileMustExist(boolean fileMustExist) {
104: this .fileMustExist = fileMustExist;
105: }
106:
107: private JLabel leftDescriptionLabel = new JLabel("");
108:
109: void jbInit() throws Exception {
110: upperDescriptionLabel.setText(I18N
111: .get("ui.FileNamePanel.description-text-goes-here"));
112: this .setLayout(gridBagLayout1);
113: browseButton.setText(I18N.get("ui.FileNamePanel.browse"));
114: browseButton
115: .addActionListener(new java.awt.event.ActionListener() {
116: public void actionPerformed(ActionEvent e) {
117: browseButton_actionPerformed(e);
118: }
119: });
120: comboBox.setPreferredSize(new Dimension(300, 21));
121: comboBox.setEditable(true);
122: comboBox.setModel(comboBoxModel);
123: this .add(upperDescriptionLabel, new GridBagConstraints(0, 0, 3,
124: 1, 0.0, 0.0, GridBagConstraints.WEST,
125: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
126: this .add(leftDescriptionLabel, new GridBagConstraints(0, 1, 1,
127: 1, 0.0, 0.0, GridBagConstraints.WEST,
128: GridBagConstraints.NONE, new Insets(0, 0, 0, 8), 0, 0));
129: this .add(comboBox, new GridBagConstraints(1, 1, 1, 1, 1, 0.0,
130: GridBagConstraints.CENTER,
131: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 8),
132: 0, 0));
133: this .add(browseButton, new GridBagConstraints(2, 1, 1, 1, 0.0,
134: 0.0, GridBagConstraints.CENTER,
135: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
136: }
137:
138: /**
139: * Side effect: adds file to the combobox list of recently selected files.
140: */
141: public File getSelectedFile() {
142: Assert.isTrue(isInputValid(), getValidationError());
143:
144: //Store file in local variable, because we will lose the combobox text
145: //when we call #addToComboBox. [Jon Aquino]
146: File file = new File(getComboBoxText());
147: addToComboBox(file, comboBoxModel);
148:
149: return file;
150: }
151:
152: public boolean isInputValid() {
153: return null == getValidationError();
154: }
155:
156: private String getComboBoxText() {
157: return (String) comboBox.getEditor().getItem();
158: }
159:
160: /**
161: * @param file the initial value for the GML file field, or null to clear the field
162: */
163: public void setSelectedFile(File file) {
164: if (file == null) {
165: comboBox.getEditor().setItem("");
166:
167: return;
168: }
169:
170: comboBox.getEditor().setItem(file.getAbsolutePath());
171: }
172:
173: public String getValidationError() {
174: if (getComboBoxText().trim().equals("")) {
175: return I18N.get("ui.FileNamePanel.no-file-was-specified");
176: }
177:
178: File file = new File(getComboBoxText());
179:
180: if (fileMustExist && !file.exists()) {
181: return I18N
182: .get("ui.FileNamePanel.specified-file-does-not-exist")
183: + " " + getComboBoxText();
184: }
185:
186: if (fileMustExist && file.isDirectory()) {
187: return I18N
188: .get("ui.FileNamePanel.specified-file-is-a-directory")
189: + " " + getComboBoxText();
190: }
191:
192: if (fileMustExist && !file.isFile()) {
193: return I18N
194: .get("ui.FileNamePanel.specified-file-is-not-normal")
195: + " " + getComboBoxText();
196: }
197:
198: if (!fileMustExist && (file.getParentFile() == null)) {
199: return I18N
200: .get("ui.FileNamePanel.specified-parent-directory-is-not-specified")
201: + " " + getComboBoxText();
202: }
203:
204: if (!fileMustExist && !file.getParentFile().exists()) {
205: return I18N
206: .get("ui.FileNamePanel.specified-parent-directory-does-not-exist")
207: + " " + getComboBoxText();
208: }
209:
210: if (!fileMustExist && !file.getParentFile().isDirectory()) {
211: return I18N
212: .get("ui.FileNamePanel.specified-parent-is-not-a-directory")
213: + " " + getComboBoxText();
214: }
215:
216: return null;
217: }
218:
219: void browseButton_actionPerformed(ActionEvent e) {
220: try {
221: File file = browse();
222:
223: if (file == null) {
224: return;
225: }
226:
227: comboBox.getEditor().setItem(file.getAbsolutePath());
228: fireBrowseEvent(e);
229: } catch (Throwable t) {
230: errorHandler.handleThrowable(t);
231: }
232: }
233:
234: private void fireBrowseEvent(ActionEvent e) {
235: for (Iterator i = browseListeners.iterator(); i.hasNext();) {
236: ActionListener l = (ActionListener) i.next();
237: l.actionPerformed(e);
238: }
239: }
240:
241: /**
242: * Notify the ActionListener whenever the user picks a file using the
243: * Browse button
244: */
245: public void addBrowseListener(ActionListener l) {
246: browseListeners.add(l);
247: }
248:
249: private FileFilter fileFilter = null;
250:
251: //<<TODO:FIX>> Sometimes after the Browse button is pressed, the file dialog
252: //takes a minute or so to open. [Jon Aquino]
253: private File browse() {
254: JFileChooser fileChooser = fileMustExist ? GUIUtil
255: .createJFileChooserWithExistenceChecking()
256: : new JFileChooser();
257: fileChooser.setDialogTitle(I18N.get("ui.FileNamePanel.browse"));
258:
259: fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
260:
261: GUIUtil.removeChoosableFileFilters(fileChooser);
262: fileChooser.addChoosableFileFilter(fileChooser
263: .getAcceptAllFileFilter());
264: if (fileFilter != null) {
265: fileChooser.addChoosableFileFilter(fileFilter);
266: fileChooser.setFileFilter(fileFilter);
267: }
268:
269: File initialFile = getInitialFile();
270:
271: if (initialFile.exists() && initialFile.isFile()) {
272: fileChooser.setSelectedFile(initialFile);
273: } else if (initialFile.exists() && initialFile.isDirectory()) {
274: fileChooser.setCurrentDirectory(initialFile);
275: } else if (initialFile.getParentFile() != null
276: && initialFile.getParentFile().exists()) {
277: fileChooser
278: .setCurrentDirectory(initialFile.getParentFile());
279: }
280:
281: fileChooser.setMultiSelectionEnabled(false);
282:
283: if (JFileChooser.APPROVE_OPTION != fileChooser
284: .showOpenDialog(SwingUtilities.windowForComponent(this ))) {
285: return null;
286: }
287:
288: //We used to have code here to check if the file exists, but I removed it
289: //because we'll save that for #getValidationError. At this point, we just
290: //want to get some path information into the text area to save the user
291: //some keystrokes. [Jon Aquino]
292:
293: return fileChooser.getSelectedFile();
294: }
295:
296: protected File getInitialFile() {
297: return new File(getComboBoxText());
298: }
299:
300: private void addToComboBox(File file,
301: DefaultComboBoxModel comboBoxModel) {
302: //First do a removeElement so that if the file is already in the list it
303: //gets moved to the top [Jon Aquino]
304: comboBoxModel.removeElement(file.getAbsolutePath());
305: comboBoxModel.insertElementAt(file.getAbsolutePath(), 0);
306:
307: if (comboBoxModel.getSize() > MAX_CACHE_SIZE) {
308: comboBoxModel.removeElementAt(comboBoxModel.getSize() - 1);
309: }
310:
311: comboBox.setSelectedIndex(0);
312: }
313:
314: public void setUpperDescription(String description) {
315: upperDescriptionLabel.setText(description);
316: }
317:
318: public void setLeftDescription(String description) {
319: leftDescriptionLabel.setText(description);
320: }
321:
322: public void setFileFilter(FileFilter fileFilter) {
323: this.fileFilter = fileFilter;
324: }
325:
326: }
|