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: package com.vividsolutions.jump.workbench.datasource;
033:
034: import com.vividsolutions.jump.I18N;
035: import com.vividsolutions.jump.io.datasource.StandardReaderWriterFileDataSource;
036: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
037: import com.vividsolutions.jump.workbench.ui.ErrorHandler;
038: import com.vividsolutions.jump.workbench.ui.FileNamePanel;
039:
040: import java.awt.Component;
041:
042: import java.io.File;
043:
044: import java.util.HashMap;
045: import java.util.Map;
046:
047: import javax.swing.JFileChooser;
048: import javax.swing.JOptionPane;
049: import javax.swing.SwingUtilities;
050:
051: /**
052: * Adds to the JUMP Workbench the UIs for opening and saving GML files.
053: * Called by InstallStandardDataSourceQueryChoosersPlugIn.
054: * @see InstallStandardDataSourceQueryChoosersPlugIn
055: */
056: public class GMLDataSourceQueryChooserInstaller {
057: private static final String GML_DESCRIPTION = "GML 2.0";
058:
059: public void addSaveGMLFileDataSourceQueryChooser(
060: final PlugInContext context) {
061: DataSourceQueryChooserManager
062: .get(context.getWorkbenchContext().getBlackboard())
063: .addSaveDataSourceQueryChooser(
064: new SaveFileDataSourceQueryChooser(
065: StandardReaderWriterFileDataSource.GML.class,
066: GML_DESCRIPTION,
067: InstallStandardDataSourceQueryChoosersPlugIn
068: .extensions(StandardReaderWriterFileDataSource.GML.class),
069: context.getWorkbenchContext()) {
070: public boolean isInputValid() {
071: return isValid(getTemplateFileNamePanel())
072: && super .isInputValid();
073: }
074:
075: protected Map toProperties(File file) {
076: HashMap properties = new HashMap(super
077: .toProperties(file));
078: properties
079: .put(
080: StandardReaderWriterFileDataSource.OUTPUT_TEMPLATE_FILE_KEY,
081: getTemplateFileNamePanel()
082: .getSelectedFile()
083: .getPath());
084:
085: return properties;
086: }
087:
088: private FileNamePanel templateFileNamePanel;
089:
090: private FileNamePanel getTemplateFileNamePanel() {
091: //Lazily initialize to prevent NullPointerExceptions in WindowsFileChooserUI
092: //[Jon Aquino 2004-01-19]
093: if (templateFileNamePanel == null) {
094: templateFileNamePanel = createTemplateFileNamePanel(
095: "Output Template: ",
096: getFileChooserPanel()
097: .getChooser(),
098: context.getErrorHandler());
099: }
100: return templateFileNamePanel;
101: }
102:
103: protected Component getSouthComponent1() {
104: return getTemplateFileNamePanel();
105: }
106: });
107: }
108:
109: private boolean isValid(FileNamePanel templateFileNamePanel) {
110: if (!templateFileNamePanel.isInputValid()) {
111: JOptionPane
112: .showMessageDialog(
113: SwingUtilities
114: .windowForComponent(templateFileNamePanel),
115: I18N
116: .get("datasource.GMLDataSourceQueryChooserInstaller.template-file")
117: + " "
118: + templateFileNamePanel
119: .getValidationError(),
120: I18N
121: .get("datasource.GMLDataSourceQueryChooserInstaller.error"),
122: JOptionPane.ERROR_MESSAGE);
123:
124: return false;
125: }
126:
127: return true;
128: }
129:
130: public void addLoadGMLFileDataSourceQueryChooser(
131: final PlugInContext context) {
132: DataSourceQueryChooserManager
133: .get(context.getWorkbenchContext().getBlackboard())
134: .addLoadDataSourceQueryChooser(
135: new LoadFileDataSourceQueryChooser(
136: StandardReaderWriterFileDataSource.GML.class,
137: GML_DESCRIPTION,
138: InstallStandardDataSourceQueryChoosersPlugIn
139: .extensions(StandardReaderWriterFileDataSource.GML.class),
140: context.getWorkbenchContext()) {
141: protected void addFileFilters(
142: JFileChooser chooser) {
143: super .addFileFilters(chooser);
144: InstallStandardDataSourceQueryChoosersPlugIn
145: .addCompressedFileFilter(
146: GML_DESCRIPTION,
147: chooser);
148: }
149:
150: public boolean isInputValid() {
151: return isValid(getTemplateFileNamePanel())
152: && super .isInputValid();
153: }
154:
155: protected Map toProperties(File file) {
156: HashMap properties = new HashMap(super
157: .toProperties(file));
158: properties
159: .put(
160: StandardReaderWriterFileDataSource.INPUT_TEMPLATE_FILE_KEY,
161: getTemplateFileNamePanel()
162: .getSelectedFile()
163: .getPath());
164:
165: return properties;
166: }
167:
168: private FileNamePanel templateFileNamePanel;
169:
170: private FileNamePanel getTemplateFileNamePanel() {
171: //Lazily initialize to facilitate CoordinateSystemSupport flag [Jon Aquino]
172: if (templateFileNamePanel == null) {
173: templateFileNamePanel = createTemplateFileNamePanel(
174: "Input Template: ",
175: getFileChooserPanel()
176: .getChooser(),
177: context.getErrorHandler());
178: }
179:
180: return templateFileNamePanel;
181: }
182:
183: protected Component getSouthComponent1() {
184: return getTemplateFileNamePanel();
185: }
186: });
187: }
188:
189: private FileNamePanel createTemplateFileNamePanel(
190: String description, final JFileChooser fileChooser,
191: ErrorHandler errorHandler) {
192: return new TemplateFileNamePanel(
193: I18N
194: .get("datasource.GMLDataSourceQueryChooserInstaller.input-template")
195: + " ", errorHandler) {
196:
197: {
198: setFileMustExist(true);
199: }
200:
201: protected File getInitialFile() {
202: File initialFile = super .getInitialFile();
203:
204: if (!initialFile.exists()
205: && ((initialFile.getParent() == null) || !initialFile
206: .getParentFile().exists())) {
207: return fileChooser.getCurrentDirectory();
208: }
209:
210: return initialFile;
211: }
212: };
213: }
214:
215: private class TemplateFileNamePanel extends FileNamePanel {
216: public TemplateFileNamePanel(String description,
217: ErrorHandler errorHandler) {
218: super (errorHandler);
219:
220: //"" gives upper description zero height. [Jon Aquino]
221: setUpperDescription("");
222: setLeftDescription(description);
223: }
224: }
225: }
|