001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gis.display;
031
032import java.awt.BorderLayout;
033import java.awt.event.WindowAdapter;
034import java.awt.event.WindowEvent;
035import java.io.File;
036import java.io.FileReader;
037import java.io.StringReader;
038
039import javax.swing.JFrame;
040
041import com.vividsolutions.jump.feature.FeatureCollection;
042import com.vividsolutions.jump.io.DriverProperties;
043import com.vividsolutions.jump.io.GMLInputTemplate;
044import com.vividsolutions.jump.io.GMLReader;
045import com.vividsolutions.jump.io.ShapefileReader;
046import com.vividsolutions.jump.workbench.WorkbenchContext;
047import com.vividsolutions.jump.workbench.model.Layer;
048import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
049import com.vividsolutions.jump.workbench.ui.ErrorHandler;
050import com.vividsolutions.jump.workbench.ui.GUIUtil;
051
052/**
053 * Name: JumpFrame.java Purpose:Given a GML string or an ESRI shapefile name, it
054 * obtains the geometric data and send them to a JUMP MapTab for display.
055 * Author: Jianting Zhang Date: August, 2005
056 */
057public class JumpFrame extends JFrame implements ErrorHandler {
058
059        private JumpMapTab mapTab = new JumpMapTab(this);
060        static GMLReader reader = new GMLReader();
061
062        /**
063         * Constructor for the JumpFrame object
064         * 
065         *@param title
066         *            Description of the Parameter
067         *@exception Exception
068         *                Description of the Exception
069         */
070        public JumpFrame(String title) throws Exception {
071
072                setTitle(title);
073                setSize(700, 700);
074
075                try {
076                        jbInit();
077                } catch (Exception e) {
078                        e.printStackTrace();
079                }
080
081                addWindowListener(new WindowAdapter() {
082                        public void windowOpened(WindowEvent e) {
083                                try {
084                                        mapTab.initialize();
085                                } catch (Throwable t) {
086                                        handleThrowable(t);
087                                }
088                        }
089                });
090        }
091
092        /**
093         * Description of the Method
094         * 
095         *@exception Exception
096         *                Description of the Exception
097         */
098        private void jbInit() throws Exception {
099                /*
100                 * addWindowListener(new WindowAdapter() { public void
101                 * windowClosing(WindowEvent e) { System.exit(0); } });
102                 */
103                this.getContentPane().add(mapTab, BorderLayout.CENTER);
104        }
105
106        /**
107         * Description of the Method
108         * 
109         *@param t
110         *            Description of the Parameter
111         */
112        public void handleThrowable(final Throwable t) {
113                GUIUtil.handleThrowable(t, JumpFrame.this);
114        }
115
116        // All the attribute data are skipped (assuming schema is null or emtpy)
117        // To allow attribute data, schema has to be in the format defined by
118        // VividSolution.com.
119        /**
120         * Adds a feature to the GMLLayer attribute of the JumpFrame object
121         * 
122         *@param gml
123         *            The feature to be added to the GMLLayer attribute
124         *@param schema
125         *            The feature to be added to the GMLLayer attribute
126         *@param layerName
127         *            The feature to be added to the GMLLayer attribute
128         *@exception Exception
129         *                Description of the Exception
130         */
131        public void addGMLLayer(String gml, String schema, String layerName)
132                        throws Exception {
133                WorkbenchContext context = mapTab.getWorkbenchContext();
134                String s = "";
135                String collectionElement = "gml:FeatureCollection";
136                String featureElement = "gml:Feature";
137                String geometryElement = "gml:Geometry";
138                s += "<?xml version='1.0' encoding='UTF-8'?>";
139                s += "<JCSGMLInputTemplate>";
140                s += ("<CollectionElement>" + collectionElement + "</CollectionElement>");
141                s += ("<FeatureElement>" + featureElement + "</FeatureElement>");
142                s += ("<GeometryElement>" + geometryElement + "</GeometryElement>");
143                if ((schema == null) || ((schema != null) && (schema.equals("")))) {
144                        s += "<ColumnDefinitions></ColumnDefinitions>";
145                }
146                // no attributes read
147                else {
148                        s += "<ColumnDefinitions>" + schema + "</ColumnDefinitions>";
149                }
150                s += "</JCSGMLInputTemplate>";
151                GMLInputTemplate template = new GMLInputTemplate();
152                StringReader sr = new StringReader(s);
153                template.load(sr);
154                reader.setInputTemplate(template);
155                sr = new StringReader(gml);
156                FeatureCollection fc = reader.read(sr);
157                // System.out.println(fc.size());
158                Layer layer = context.createPlugInContext().addLayer(
159                                StandardCategoryNames.WORKING, layerName, fc);
160                // layer.setDescription("ABCDE");
161        }
162
163        /**
164         * Adds a feature to the SHPLayer attribute of the JumpFrame object
165         * 
166         *@param fileName
167         *            The feature to be added to the SHPLayer attribute
168         *@param layerName
169         *            The feature to be added to the SHPLayer attribute
170         *@exception Exception
171         *                Description of the Exception
172         */
173        public void addSHPLayer(String fileName, String layerName) throws Exception {
174                WorkbenchContext context = mapTab.getWorkbenchContext();
175                ShapefileReader reader = new ShapefileReader();
176                FeatureCollection fc = reader.read(new DriverProperties(fileName));
177                // System.out.println(fc.size());
178                Layer layer = context.createPlugInContext().addLayer(
179                                StandardCategoryNames.WORKING, layerName, fc);
180                // layer.setDescription("ABCDE");
181        }
182
183        /*
184         * private void removeAllCategories() { LayerManager layerManager=
185         * mapTab.getWorkbenchContext().getLayerManager(); for (Iterator i =
186         * layerManager.getCategories().iterator(); i.hasNext();) { Category cat =
187         * (Category) i.next(); layerManager.removeIfEmpty(cat); } }
188         */
189        /**
190         * The main program for the JumpFrame class
191         * 
192         *@param args
193         *            The command line arguments
194         *@exception Exception
195         *                Description of the Exception
196         */
197        public static void main(String[] args) throws Exception {
198                JumpFrame fm = new JumpFrame("test");
199                fm.setVisible(true);
200
201                File f = new File("z:/ctws/miscs/gml2.txt");
202                char[] s = new char[(int) f.length()];
203                FileReader fr = new FileReader(f);
204                fr.read(s);
205                String gml = new String(s);
206                // System.out.println(gml);
207                fm.addGMLLayer(gml, "", "test1");
208                fm.addSHPLayer("c:/temp/test.shp", "test2");
209        }
210
211}