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.ShapefileReader;
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 ShapeFileInputDriver extends AbstractInputDriver {
053: private ShapefileReader reader = new ShapefileReader();
054: private BasicFileDriverPanel panel;
055:
056: public ShapeFileInputDriver() {
057: }
058:
059: public String toString() {
060: return GUIUtil.shpDesc;
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.jump.io.ParseException,
070: com.vividsolutions.jump.io.IllegalParametersException,
071: Exception {
072: String extension;
073: File selectedFile = panel.getSelectedFile();
074: String name = GUIUtil.nameWithoutExtension(selectedFile);
075: String fname = selectedFile.getAbsolutePath();
076:
077: extension = fname.substring(fname.length() - 3);
078:
079: DriverProperties dp = new DriverProperties();
080:
081: if (extension.equalsIgnoreCase("zip")) {
082: String internalName;
083:
084: dp.set("CompressedFile", fname);
085: internalName = CompressedFile
086: .getInternalZipFnameByExtension(".shp", fname);
087:
088: if (internalName == null) {
089: throw new Exception(
090: "Couldnt find a .shp file inside the .zip file: "
091: + fname);
092: }
093:
094: dp.set("File", internalName);
095: } else {
096: dp.set("File", fname);
097: }
098:
099: FeatureCollection featureCollection = reader.read(dp);
100: Layer layer = layerManager.addLayer(categoryName, name,
101: featureCollection);
102: }
103:
104: public void initialize(DriverManager driverManager,
105: ErrorHandler errorHandler) {
106: super .initialize(driverManager, errorHandler);
107: panel = new BasicFileDriverPanel(errorHandler);
108: panel.setFileDescription(GUIUtil.shpDesc);
109: panel.setFileFilter(new WorkbenchFileFilter(GUIUtil.shpDesc));
110: panel.setFileMustExist(true);
111: }
112: }
|