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.BorderLayout;
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.JLabel;
046: import javax.swing.JPanel;
047:
048: //JBuilder displays this component as a "Red Bean". There's a trick to
049: //displaying it -- see test.AbstractDriverPanelProxy and
050: //http://www.visi.com/~gyles19/fom-serve/cache/97.html. [Jon Aquino]
051: //<<TODO:REFACTORING>> This class duplicates code in BasicFileDriverPanel. I wonder
052: //how we can refactor them. [Jon Aquino]
053: public class GMLFileDriverPanel extends AbstractDriverPanel {
054: private final static String TEMPLATE_FILE_CACHE_KEY = "TEMPLATE_FILE";
055: BorderLayout borderLayout1 = new BorderLayout();
056: OKCancelPanel okCancelPanel = new OKCancelPanel();
057: JPanel centrePanel = new JPanel();
058: JPanel innerCentrePanel = new JPanel();
059: FileNamePanel templateFileNamePanel;
060: JLabel whitespaceLabel = new JLabel();
061: FileNamePanel gmlFileNamePanel;
062: GridBagLayout gridBagLayout1 = new GridBagLayout();
063: GridBagLayout gridBagLayout2 = new GridBagLayout();
064: private ArrayList possibleTemplateExtensions = new ArrayList();
065:
066: public GMLFileDriverPanel(ErrorHandler errorHandler) {
067: templateFileNamePanel = new FileNamePanel(errorHandler);
068: gmlFileNamePanel = new FileNamePanel(errorHandler);
069:
070: try {
071: jbInit();
072: gmlFileNamePanel.setFileMustExist(true);
073: templateFileNamePanel.setFileMustExist(true);
074: gmlFileNamePanel.addBrowseListener(new ActionListener() {
075: public void actionPerformed(ActionEvent e) {
076: findPossibleTemplateFile();
077: }
078: });
079: } catch (Exception ex) {
080: ex.printStackTrace();
081: }
082: }
083:
084: public void setGMLFileMustExist(boolean gmlFileMustExist) {
085: gmlFileNamePanel.setFileMustExist(gmlFileMustExist);
086: }
087:
088: public void setTemplateFileDescription(String description) {
089: templateFileNamePanel.setUpperDescription(description);
090: }
091:
092: public void setCache(DriverPanelCache cache) {
093: super .setCache(cache);
094:
095: if (cache.get(DriverPanelCache.FILE_CACHE_KEY) != null) {
096: gmlFileNamePanel.setSelectedFile((File) cache
097: .get(DriverPanelCache.FILE_CACHE_KEY));
098: }
099:
100: if (cache.get(TEMPLATE_FILE_CACHE_KEY) != null) {
101: templateFileNamePanel.setSelectedFile((File) cache
102: .get(TEMPLATE_FILE_CACHE_KEY));
103: }
104: }
105:
106: /**
107: * Adds the extension to the list of extensions to use when searching for
108: * a template file to use as the default
109: * @param extension for example, ".jot"
110: */
111: public void addPossibleTemplateExtension(String extension) {
112: possibleTemplateExtensions.add(extension);
113: }
114:
115: public String getValidationError() {
116: if (!gmlFileNamePanel.isInputValid()) {
117: return gmlFileNamePanel.getValidationError();
118: }
119:
120: if (!templateFileNamePanel.isInputValid()) {
121: return templateFileNamePanel.getValidationError();
122: }
123:
124: return null;
125: }
126:
127: public File getGMLFile() {
128: return gmlFileNamePanel.getSelectedFile();
129: }
130:
131: public File getTemplateFile() {
132: return templateFileNamePanel.getSelectedFile();
133: }
134:
135: public DriverPanelCache getCache() {
136: DriverPanelCache cache = super .getCache();
137: cache.put(DriverPanelCache.FILE_CACHE_KEY, gmlFileNamePanel
138: .getSelectedFile());
139: cache.put(TEMPLATE_FILE_CACHE_KEY, templateFileNamePanel
140: .getSelectedFile());
141:
142: return cache;
143: }
144:
145: public void addActionListener(ActionListener l) {
146: okCancelPanel.addActionListener(l);
147: }
148:
149: public void removeActionListener(ActionListener l) {
150: okCancelPanel.removeActionListener(l);
151: }
152:
153: public boolean wasOKPressed() {
154: return okCancelPanel.wasOKPressed();
155: }
156:
157: void jbInit() throws Exception {
158: this .setLayout(borderLayout1);
159: innerCentrePanel.setLayout(gridBagLayout1);
160: templateFileNamePanel
161: .setUpperDescription("Template File Description Goes Here");
162: whitespaceLabel.setText(" ");
163: gmlFileNamePanel.setUpperDescription("GML File");
164: gmlFileNamePanel.setFileFilter(new WorkbenchFileFilter(
165: GUIUtil.gmlDesc));
166: centrePanel.setLayout(gridBagLayout2);
167: this .add(okCancelPanel, BorderLayout.SOUTH);
168: this .add(centrePanel, BorderLayout.CENTER);
169: centrePanel.add(innerCentrePanel, new GridBagConstraints(0, 0,
170: 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
171: GridBagConstraints.BOTH, new Insets(10, 10, 10, 10), 0,
172: 0));
173: innerCentrePanel.add(gmlFileNamePanel, new GridBagConstraints(
174: 0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
175: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
176: innerCentrePanel.add(templateFileNamePanel,
177: new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
178: GridBagConstraints.CENTER,
179: GridBagConstraints.NONE,
180: new Insets(0, 0, 0, 0), 0, 0));
181: innerCentrePanel.add(whitespaceLabel, new GridBagConstraints(0,
182: 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
183: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
184: }
185:
186: /**
187: * Search for a template file using the extensions. If one is found, set
188: * it as the value in the template edit box.
189: */
190: private void findPossibleTemplateFile() {
191: String gmlFile = gmlFileNamePanel.getSelectedFile().toString();
192:
193: if (gmlFile.length() < "a.aaa".length()) {
194: return;
195: }
196:
197: for (Iterator i = possibleTemplateExtensions.iterator(); i
198: .hasNext();) {
199: String extension = (String) i.next();
200: File templateFile = new File(gmlFile.substring(0, gmlFile
201: .length()
202: - ".aaa".length())
203: + extension);
204:
205: if (templateFile.exists()) {
206: templateFileNamePanel.setSelectedFile(templateFile);
207:
208: return;
209: }
210: }
211: }
212: }
|