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.driver;
034:
035: import java.io.File;
036: import java.io.FileNotFoundException;
037: import java.io.IOException;
038:
039: import com.vividsolutions.jump.feature.FeatureCollection;
040: import com.vividsolutions.jump.io.CompressedFile;
041: import com.vividsolutions.jump.io.DriverProperties;
042: import com.vividsolutions.jump.io.ParseException;
043: import com.vividsolutions.jump.io.WKTReader;
044: import com.vividsolutions.jump.workbench.model.Layer;
045: import com.vividsolutions.jump.workbench.model.LayerManager;
046: import com.vividsolutions.jump.workbench.ui.AbstractDriverPanel;
047: import com.vividsolutions.jump.workbench.ui.BasicFileDriverPanel;
048: import com.vividsolutions.jump.workbench.ui.ErrorHandler;
049: import com.vividsolutions.jump.workbench.ui.GUIUtil;
050: import com.vividsolutions.jump.workbench.ui.WorkbenchFileFilter;
051:
052: public class WKTFileInputDriver extends AbstractInputDriver {
053: private WKTReader reader = new WKTReader();
054: private BasicFileDriverPanel panel;
055:
056: public WKTFileInputDriver() {
057: }
058:
059: public String toString() {
060: return GUIUtil.wktDesc;
061: }
062:
063: public AbstractDriverPanel getPanel() {
064: return panel;
065: }
066:
067: public void input(LayerManager layerManager, String categoryName)
068: throws FileNotFoundException, IOException, ParseException,
069: com.vividsolutions.jts.io.ParseException,
070: com.vividsolutions.jump.io.IllegalParametersException,
071: Exception {
072: String extension;
073: File selectedFile = panel.getSelectedFile();
074: String layerName = GUIUtil.nameWithoutExtension(selectedFile);
075: String fname = selectedFile.getAbsolutePath();
076:
077: //<<TODO:AESTHETICS>> Source code formatting needs to be fixed here. [Jon Aquino]
078: extension = fname.substring(fname.length() - 3);
079:
080: DriverProperties dp = new DriverProperties();
081:
082: if (extension.equalsIgnoreCase("zip")) {
083: String internalName;
084:
085: dp.set("CompressedFile", fname);
086: internalName = CompressedFile
087: .getInternalZipFnameByExtension(".wkt", fname);
088:
089: if (internalName == null) {
090: internalName = CompressedFile
091: .getInternalZipFnameByExtension(".txt", fname);
092: }
093:
094: if (internalName == null) {
095: throw new Exception(
096: "Couldnt find a .wkt, or .txt file inside the .zip file: "
097: + fname);
098: }
099:
100: dp.set("File", internalName);
101: } else if (extension.equalsIgnoreCase(".gz")) {
102: dp.set("CompressedFile", fname);
103: dp.set("File", fname); // not useed
104: } else {
105: dp.set("File", fname);
106: }
107:
108: FeatureCollection featureCollection = reader.read(dp);
109: Layer layer = layerManager.addLayer(categoryName, layerName,
110: featureCollection);
111: }
112:
113: public void initialize(DriverManager driverManager,
114: ErrorHandler errorHandler) {
115: super .initialize(driverManager, errorHandler);
116: panel = new BasicFileDriverPanel(errorHandler);
117: panel.setFileDescription(GUIUtil.wktDesc);
118: panel.setFileFilter(new WorkbenchFileFilter(GUIUtil.wktDesc));
119: panel.setFileMustExist(true);
120: }
121: }
|