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: package com.vividsolutions.jump.workbench.ui.plugin;
033:
034: import java.util.Arrays;
035: import com.vividsolutions.jump.I18N;
036: import com.vividsolutions.jump.feature.Feature;
037: import com.vividsolutions.jump.feature.FeatureCollection;
038: import com.vividsolutions.jump.io.FUTURE_JTS_WKTWriter;
039: import com.vividsolutions.jump.workbench.WorkbenchContext;
040: import com.vividsolutions.jump.workbench.WorkbenchException;
041: import com.vividsolutions.jump.workbench.model.Layer;
042: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
043: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
044: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
045: import com.vividsolutions.jump.workbench.ui.EditTransaction;
046: import com.vividsolutions.jump.workbench.ui.EnterWKTDialog;
047:
048: public class EditSelectedFeaturePlugIn extends WKTPlugIn {
049: private Feature feature;
050:
051: public EditSelectedFeaturePlugIn() {
052: }
053:
054: protected Layer layer(PlugInContext context) {
055: return (Layer) context.getLayerViewPanel()
056: .getSelectionManager().getLayersWithSelectedItems()
057: .iterator().next();
058: }
059:
060: public String getName() {
061: return I18N
062: .get("ui.plugin.EditSelectedFeaturePlugIn.view-edit-selected-feature");
063: }
064:
065: public static MultiEnableCheck createEnableCheck(
066: final WorkbenchContext workbenchContext) {
067: EnableCheckFactory checkFactory = new EnableCheckFactory(
068: workbenchContext);
069: return new MultiEnableCheck()
070: .add(
071: checkFactory
072: .createWindowWithLayerViewPanelMustBeActiveCheck())
073: .add(
074: checkFactory
075: .createExactlyNFeaturesMustHaveSelectedItemsCheck(1));
076: }
077:
078: public boolean execute(PlugInContext context) throws Exception {
079: return execute(context, (Feature) context.getLayerViewPanel()
080: .getSelectionManager().getFeaturesWithSelectedItems()
081: .iterator().next(), true);
082: }
083:
084: public boolean execute(PlugInContext context, Feature feature,
085: boolean editable) throws Exception {
086: this .feature = feature;
087: reportNothingToUndoYet(context);
088: return super .execute(context);
089: }
090:
091: protected void apply(String wkt, PlugInContext context)
092: throws Exception {
093: if (!layer(context).isEditable()) {
094: return;
095: }
096: super .apply(wkt, context);
097: }
098:
099: protected void apply(FeatureCollection c, PlugInContext context)
100: throws WorkbenchException {
101: if (c.size() != 1) {
102: throw new WorkbenchException(
103: I18N
104: .get("ui.plugin.EditSelectedFeaturePlugIn.expected-1-feature-but-found")
105: + " " + c.size());
106: }
107: EditTransaction transaction = new EditTransaction(Arrays
108: .asList(new Feature[] { feature }), getName(), layer,
109: isRollingBackInvalidEdits(context), false, context
110: .getWorkbenchFrame());
111: //Can't simply pass the LayerViewPanel to the transaction because if there is
112: //an attribute viewer up and its TaskFrame has been closed, the LayerViewPanel's
113: //LayerManager will be null. [Jon Aquino]
114: transaction.setGeometry(0, ((Feature) c.iterator().next())
115: .getGeometry());
116: transaction.commit();
117: }
118:
119: protected EnterWKTDialog createDialog(PlugInContext context) {
120: EnterWKTDialog d = super .createDialog(context);
121: d
122: .setTitle((layer(context).isEditable() ? I18N
123: .get("ui.plugin.EditSelectedFeaturePlugIn.edit")
124: + " "
125: : "")
126: + I18N
127: .get("ui.plugin.EditSelectedFeaturePlugIn.feature")
128: + " "
129: + feature.getID()
130: + " "
131: + I18N
132: .get("ui.plugin.EditSelectedFeaturePlugIn.in")
133: + " "
134: + layer
135: + (layer(context).isEditable() ? ""
136: : " ("
137: + I18N
138: .get("ui.plugin.EditSelectedFeaturePlugIn.layer-is-uneditable")
139: + ")"));
140: d.setEditable(layer(context).isEditable());
141: d.setText(helper.format(new FUTURE_JTS_WKTWriter()
142: .write(feature.getGeometry())));
143: return d;
144: }
145:
146: private WKTDisplayHelper helper = new WKTDisplayHelper();
147: }
|