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.analysis;
034:
035: import java.util.*;
036:
037: import java.awt.event.*;
038: import javax.swing.*;
039:
040: import com.vividsolutions.jump.I18N;
041: import com.vividsolutions.jump.feature.*;
042: import com.vividsolutions.jump.task.*;
043: import com.vividsolutions.jump.workbench.model.*;
044: import com.vividsolutions.jump.workbench.plugin.*;
045: import com.vividsolutions.jump.workbench.plugin.util.*;
046: import com.vividsolutions.jump.workbench.ui.*;
047:
048: /**
049: * Queries a layer by a spatial predicate.
050: */
051: public class SpatialJoinPlugIn extends AbstractPlugIn implements
052: ThreadedPlugIn {
053: private Layer srcLayerA;
054: private Layer srcLayerB;
055: private JTextField paramField;
056: private Collection functionNames;
057: private MultiInputDialog dialog;
058: private String funcNameToRun;
059: private GeometryPredicate functionToRun = null;
060: private boolean exceptionThrown = false;
061:
062: private double[] params = new double[2];
063:
064: public SpatialJoinPlugIn() {
065: functionNames = GeometryPredicate.getNames();
066: }
067:
068: private String categoryName = StandardCategoryNames.RESULT;
069:
070: public String getName() {
071: return I18N
072: .get("ui.plugin.analysis.SpatialJoinPlugIn.Spatial-Join");
073: }
074:
075: public void setCategoryName(String value) {
076: categoryName = value;
077: }
078:
079: public boolean execute(PlugInContext context) throws Exception {
080: dialog = new MultiInputDialog(context.getWorkbenchFrame(),
081: getName(), true);
082: setDialogValues(dialog, context);
083: GUIUtil.centreOnWindow(dialog);
084: dialog.setVisible(true);
085: if (!dialog.wasOKPressed()) {
086: return false;
087: }
088: getDialogValues(dialog);
089: return true;
090: }
091:
092: public void run(TaskMonitor monitor, PlugInContext context)
093: throws Exception {
094: monitor.allowCancellationRequests();
095:
096: // input-proofing
097: if (functionToRun == null)
098: return;
099: if (srcLayerA == null)
100: return;
101: if (srcLayerB == null)
102: return;
103:
104: monitor
105: .report(I18N
106: .get("ui.plugin.analysis.SpatialJoinPlugIn.Executing-join")
107: + " " + functionToRun.getName() + "...");
108:
109: FeatureCollection srcAFC = srcLayerA
110: .getFeatureCollectionWrapper();
111: FeatureCollection srcBFC = srcLayerB
112: .getFeatureCollectionWrapper();
113:
114: SpatialJoinExecuter executer = new SpatialJoinExecuter(srcAFC,
115: srcBFC);
116: FeatureCollection resultFC = executer.getResultFC();
117: executer.execute(monitor, functionToRun, params, resultFC);
118:
119: if (monitor.isCancelRequested())
120: return;
121:
122: String outputLayerName = I18N
123: .get("ui.plugin.analysis.SpatialJoinPlugIn.Join")
124: + "-" + funcNameToRun;
125: context.getLayerManager().addCategory(categoryName);
126: context.addLayer(categoryName, outputLayerName, resultFC);
127:
128: if (exceptionThrown) {
129: context.getWorkbenchFrame().warnUser(
130: "Errors found while executing query");
131: }
132: }
133:
134: private final static String LAYER_A = GenericNames.LAYER_A;
135: private final static String LAYER_B = GenericNames.LAYER_B;
136: private final static String PREDICATE = GenericNames.RELATION;
137: private final static String PARAM = GenericNames.PARAMETER;
138:
139: private void setDialogValues(MultiInputDialog dialog,
140: PlugInContext context) {
141: //dialog.setSideBarImage(new ImageIcon(getClass().getResource("DiffSegments.png")));
142: dialog
143: .setSideBarDescription(I18N
144: .get("ui.plugin.analysis.SpatialJoinPlugIn.Joins-two-layers-on-a-given-spatial-relationship")
145: + " ("
146: + I18N
147: .get("ui.plugin.analysis.SpatialJoinPlugIn.example")
148: + ")");
149:
150: //Set initial layer values to the first and second layers in the layer list.
151: //In #initialize we've already checked that the number of layers >= 1. [Jon Aquino]
152: Layer initLayer1 = (srcLayerA == null) ? context
153: .getCandidateLayer(0) : srcLayerA;
154: Layer initLayer2 = (srcLayerB == null) ? context
155: .getCandidateLayer(1) : srcLayerB;
156:
157: dialog.addLayerComboBox(LAYER_A, initLayer1, context
158: .getLayerManager());
159:
160: JComboBox functionComboBox = dialog.addComboBox(PREDICATE,
161: funcNameToRun, functionNames, null);
162: functionComboBox.addItemListener(new MethodItemListener());
163: paramField = dialog.addDoubleField(PARAM, params[0], 10);
164:
165: dialog.addLayerComboBox(LAYER_B, initLayer2, context
166: .getLayerManager());
167:
168: updateUIForFunction(funcNameToRun);
169: }
170:
171: private void getDialogValues(MultiInputDialog dialog) {
172: srcLayerA = dialog.getLayer(LAYER_A);
173: srcLayerB = dialog.getLayer(LAYER_B);
174: funcNameToRun = dialog.getText(PREDICATE);
175: functionToRun = GeometryPredicate.getPredicate(funcNameToRun);
176: params[0] = dialog.getDouble(PARAM);
177: }
178:
179: private void updateUIForFunction(String funcName) {
180: boolean paramUsed = false;
181: GeometryPredicate func = GeometryPredicate
182: .getPredicate(funcName);
183: if (func != null) {
184: paramUsed = func.getParameterCount() > 0;
185: }
186: paramField.setEnabled(paramUsed);
187: // this has the effect of making the background gray (disabled)
188: paramField.setOpaque(paramUsed);
189: }
190:
191: private class MethodItemListener implements ItemListener {
192: public void itemStateChanged(ItemEvent e) {
193: updateUIForFunction((String) e.getItem());
194: }
195: }
196:
197: }
|