001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.gui.swing;
017:
018: // J2SE dependencies
019: import java.util.logging.Logger;
020: import javax.swing.JFrame;
021: import javax.swing.JScrollPane;
022: import javax.swing.JTable;
023:
024: // JUnit dependencies
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028:
029: // JTS dependencies
030: import com.vividsolutions.jts.geom.Coordinate;
031: import com.vividsolutions.jts.geom.Geometry;
032: import com.vividsolutions.jts.geom.GeometryFactory;
033: import com.vividsolutions.jts.geom.LinearRing;
034: import com.vividsolutions.jts.geom.TopologyException;
035:
036: // Geotools dependencies
037: import org.geotools.util.logging.Logging;
038: import org.geotools.data.memory.MemoryDataStore;
039: import org.geotools.feature.AttributeType;
040: import org.geotools.feature.AttributeTypeFactory;
041: import org.geotools.feature.Feature;
042: import org.geotools.feature.FeatureCollection;
043: import org.geotools.feature.FeatureType;
044: import org.geotools.feature.FeatureTypeFactory;
045: import org.geotools.feature.IllegalAttributeException;
046: import org.geotools.feature.SchemaException;
047: import org.geotools.gui.swing.table.FeatureTableModel;
048:
049: /**
050: * Tests {@link FeatureTableModel}. The table will be shown only if the test is run from the
051: * main method. Otherwise (i.e. if run from Maven), widgets are invisibles.
052: *
053: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/widgets-swing/src/test/java/org/geotools/gui/swing/FeatureTableModelTest.java $
054: * @version $Id: FeatureTableModelTest.java 27862 2007-11-12 19:51:19Z desruisseaux $
055: * @author James Macgill, CCG
056: */
057: public class FeatureTableModelTest extends TestBase {
058: /**
059: * Standard logging instance.
060: */
061: protected static final Logger LOGGER = Logging
062: .getLogger("org.geotools.filter");
063:
064: /**
065: * The attribute factory instance.
066: */
067: protected static final AttributeTypeFactory attFactory = AttributeTypeFactory
068: .defaultInstance();
069:
070: /**
071: * Feature on which to perform tests.
072: */
073: private static Feature testFeatures[] = null;
074:
075: /**
076: * Feature type on which to perform tests.
077: */
078: private static FeatureType testSchema;
079:
080: /**
081: * Creates a test case.
082: */
083: public FeatureTableModelTest(final String testName) {
084: super (testName);
085: }
086:
087: /**
088: * Run the test case from the command line.
089: */
090: public static void main(final String[] args) {
091: main(args, suite());
092: }
093:
094: /**
095: * Returns the test suite.
096: */
097: public static Test suite() {
098: TestSuite suite = new TestSuite(FeatureTableModelTest.class);
099: return suite;
100: }
101:
102: /**
103: * Display the table.
104: */
105: public void testDisplay() throws Exception {
106: MemoryDataStore datastore = new MemoryDataStore();
107: datastore.addFeature(testFeatures[0]);
108: datastore.addFeature(testFeatures[1]);
109:
110: String typeName = datastore.getTypeNames()[0];
111: FeatureCollection table = datastore.getFeatureSource(typeName)
112: .getFeatures();
113:
114: FeatureTableModel ftm = new FeatureTableModel();
115: ftm.setFeatureCollection(table);
116: JTable jtable = new JTable();
117: jtable.setModel(ftm);
118: JScrollPane scroll = new JScrollPane(jtable);
119: show(scroll, "FeatureTableModel");
120: }
121:
122: /**
123: * Sets up a schema and a test feature.
124: *
125: * @throws SchemaException If there is a problem setting up the schema.
126: * @throws IllegalAttributeException If problem setting up the feature.
127: */
128: protected void setUp() throws SchemaException,
129: IllegalAttributeException, TopologyException {
130: AttributeType geometryAttribute = attFactory.newAttributeType(
131: "testGeometry", Geometry.class);
132: LOGGER.finest("created geometry attribute");
133:
134: AttributeType booleanAttribute = attFactory.newAttributeType(
135: "testBoolean", Boolean.class);
136: LOGGER.finest("created boolean attribute");
137:
138: AttributeType charAttribute = attFactory.newAttributeType(
139: "testCharacter", Character.class);
140: AttributeType byteAttribute = attFactory.newAttributeType(
141: "testByte", Byte.class);
142: AttributeType shortAttribute = attFactory.newAttributeType(
143: "testShort", Short.class);
144: AttributeType intAttribute = attFactory.newAttributeType(
145: "testInteger", Integer.class);
146: AttributeType longAttribute = attFactory.newAttributeType(
147: "testLong", Long.class);
148: AttributeType floatAttribute = attFactory.newAttributeType(
149: "testFloat", Float.class);
150: AttributeType doubleAttribute = attFactory.newAttributeType(
151: "testDouble", Double.class);
152: AttributeType stringAttribute = attFactory.newAttributeType(
153: "testString", String.class);
154:
155: AttributeType[] types = { geometryAttribute, booleanAttribute,
156: charAttribute, byteAttribute, shortAttribute,
157: intAttribute, longAttribute, floatAttribute,
158: doubleAttribute, stringAttribute };
159:
160: // Builds the schema
161: testSchema = FeatureTypeFactory.newFeatureType(types,
162: "testSchema");
163:
164: // Creates coordinates for a linestring
165: Coordinate[] lineCoords = new Coordinate[3];
166: lineCoords[0] = new Coordinate(1, 2);
167: lineCoords[1] = new Coordinate(3, 4);
168: lineCoords[2] = new Coordinate(5, 6);
169:
170: // Creates coordinates for a polygon
171: Coordinate[] polyCoords = new Coordinate[5];
172: polyCoords[0] = new Coordinate(1, 1);
173: polyCoords[1] = new Coordinate(2, 4);
174: polyCoords[2] = new Coordinate(4, 4);
175: polyCoords[3] = new Coordinate(8, 2);
176: polyCoords[4] = new Coordinate(1, 1);
177:
178: GeometryFactory fac = new GeometryFactory();
179:
180: // Builds the test feature
181: Object[] attributesA = new Object[10];
182: attributesA[0] = fac.createLineString(lineCoords);
183: attributesA[1] = new Boolean(true);
184: attributesA[2] = new Character('t');
185: attributesA[3] = new Byte("10");
186: attributesA[4] = new Short("101");
187: attributesA[5] = new Integer(1002);
188: attributesA[6] = new Long(10003);
189: attributesA[7] = new Float(10000.4);
190: attributesA[8] = new Double(100000.5);
191: attributesA[9] = "feature A";
192:
193: Object[] attributesB = new Object[10];
194: LinearRing ring = fac.createLinearRing(polyCoords);
195: attributesB[0] = fac.createPolygon(ring, null);
196: attributesB[1] = new Boolean(false);
197: attributesB[2] = new Character('t');
198: attributesB[3] = new Byte("20");
199: attributesB[4] = new Short("201");
200: attributesB[5] = new Integer(2002);
201: attributesB[6] = new Long(20003);
202: attributesB[7] = new Float(20000.4);
203: attributesB[8] = new Double(200000.5);
204: attributesB[9] = "feature B";
205:
206: // Creates the feature itself
207: testFeatures = new Feature[2];
208: testFeatures[0] = testSchema.create(attributesA);
209: testFeatures[1] = testSchema.create(attributesB);
210: //_log.debug("...flat features created");
211: }
212: }
|