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.ui.plugin.test;
034:
035: import java.util.ArrayList;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038: import com.vividsolutions.jts.geom.GeometryFactory;
039: import com.vividsolutions.jts.geom.LineString;
040: import com.vividsolutions.jump.I18N;
041: import com.vividsolutions.jump.feature.AttributeType;
042: import com.vividsolutions.jump.feature.BasicFeature;
043: import com.vividsolutions.jump.feature.Feature;
044: import com.vividsolutions.jump.feature.FeatureCollection;
045: import com.vividsolutions.jump.feature.FeatureDataset;
046: import com.vividsolutions.jump.feature.FeatureSchema;
047: import com.vividsolutions.jump.geom.CoordUtil;
048: import com.vividsolutions.jump.workbench.model.Layer;
049: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
050: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
051: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
052: import com.vividsolutions.jump.workbench.ui.MenuNames;
053: import com.vividsolutions.jump.workbench.ui.renderer.style.ArrowLineStringEndpointStyle;
054:
055: public class RandomArrowsPlugIn extends AbstractPlugIn {
056: private static final int FEATURE_COUNT = 20;
057: private static final double LAYER_SIDE_LENGTH = 100;
058: private static final int MAX_SEGMENT_COUNT = 3;
059: private static final double MAX_SEGMENT_LENGTH = 20;
060: private GeometryFactory geometryFactory = new GeometryFactory();
061:
062: public RandomArrowsPlugIn() {
063: }
064:
065: public void initialize(PlugInContext context) throws Exception {
066: context.getFeatureInstaller().addLayerViewMenuItem(
067: this ,
068: new String[] { MenuNames.TOOLS,
069: MenuNames.TOOLS_GENERATE }, getName());
070: }
071:
072: public boolean execute(PlugInContext context) throws Exception {
073: FeatureSchema schema = new FeatureSchema();
074: schema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
075:
076: FeatureDataset dataset = new FeatureDataset(schema);
077:
078: for (int i = 0; i < FEATURE_COUNT; i++) {
079: dataset.add(createFeature(schema));
080: }
081:
082: addLayer(dataset, context);
083:
084: return true;
085: }
086:
087: private void addLayer(FeatureCollection featureCollection,
088: PlugInContext context) {
089: Layer layer = new Layer(I18N
090: .get("ui.test.RandomArrowsPlugIn.random-arrows"),
091: context.getLayerManager().generateLayerFillColor(),
092: featureCollection, context.getLayerManager());
093: //Can't fire events because this Layer hasn't been added to the
094: //LayerManager yet. [Jon Aquino]
095: boolean firingEvents = context.getLayerManager()
096: .isFiringEvents();
097: context.getLayerManager().setFiringEvents(false);
098: try {
099: layer
100: .addStyle(new ArrowLineStringEndpointStyle.NarrowSolidEnd());
101: } finally {
102: context.getLayerManager().setFiringEvents(firingEvents);
103: }
104:
105: context.getLayerManager().addLayer(
106: StandardCategoryNames.WORKING, layer);
107: }
108:
109: private Feature createFeature(FeatureSchema schema) {
110: ArrayList coordinates = new ArrayList();
111: coordinates.add(CoordUtil.add(new Coordinate(
112: LAYER_SIDE_LENGTH / 2d, LAYER_SIDE_LENGTH / 2d),
113: randomCoordinate(LAYER_SIDE_LENGTH / 2d)));
114:
115: int walkMax = (int) Math.ceil(Math.random()
116: * MAX_SEGMENT_LENGTH);
117: int segmentCount = (int) Math.ceil(Math.random()
118: * MAX_SEGMENT_COUNT);
119:
120: for (int i = 0; i < segmentCount; i++) {
121: Coordinate prevCoordinate = (Coordinate) coordinates
122: .get(coordinates.size() - 1);
123: coordinates.add(CoordUtil.add(prevCoordinate,
124: randomCoordinate(walkMax)));
125: }
126:
127: LineString lineString = geometryFactory
128: .createLineString((Coordinate[]) coordinates
129: .toArray(new Coordinate[] {}));
130: Feature feature = new BasicFeature(schema);
131: feature.setGeometry(lineString);
132:
133: return feature;
134: }
135:
136: private Coordinate randomCoordinate(double walkMax) {
137: return CoordUtil.add(new Coordinate(-walkMax / 2d,
138: -walkMax / 2d), new Coordinate(Math.random() * walkMax,
139: Math.random() * walkMax));
140: }
141: }
|