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: * JUMP is Copyright (C) 2003 Vivid Solutions
006: *
007: * This program implements extensions to JUMP and is
008: * Copyright (C) Stefan Steiniger.
009: *
010: * This program is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU General Public License
012: * as published by the Free Software Foundation; either version 2
013: * of the License, or (at your option) any later version.
014: *
015: * This program is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
018: * GNU General Public License for more details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with this program; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
023: *
024: * For more information, contact:
025: * Stefan Steiniger
026: * perriger@gmx.de
027: */
028: /*****************************************************
029: * created: 19.05.2005
030: * last modified: 21.05.2005 (copy only one item in new layer)
031: * 18.11.2005 do deep copy (clone)
032: *
033: * @author sstein
034: *
035: * description:
036: * replicates/ copys selected items in a new or existing layer
037: *
038: *****************************************************/package org.openjump.core.ui.plugin.edit;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041: import com.vividsolutions.jump.I18N;
042: import com.vividsolutions.jump.feature.Feature;
043: import com.vividsolutions.jump.feature.FeatureCollection;
044: import com.vividsolutions.jump.feature.FeatureDataset;
045: import com.vividsolutions.jump.feature.FeatureSchema;
046: import com.vividsolutions.jump.feature.FeatureUtil;
047: import com.vividsolutions.jump.task.TaskMonitor;
048: import com.vividsolutions.jump.workbench.WorkbenchContext;
049: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
050: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
051: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
052: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
053: import com.vividsolutions.jump.workbench.plugin.ThreadedPlugIn;
054: import com.vividsolutions.jump.workbench.ui.EditTransaction;
055: import com.vividsolutions.jump.workbench.ui.GUIUtil;
056: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
057: import com.vividsolutions.jump.workbench.ui.MenuNames;
058: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
059:
060: import java.util.Iterator;
061: import com.vividsolutions.jump.workbench.model.Layer;
062: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
063:
064: import java.util.Collection;
065: import javax.swing.JComboBox;
066:
067: /**
068: * @description:
069: * replicates/ copys selected items in a new or existing layer
070: *
071: * @author sstein
072: *
073: **/
074: public class ReplicateSelectedItemsPlugIn extends AbstractPlugIn
075: implements ThreadedPlugIn {
076:
077: private String T1 = "Replicate to new layer?";
078: private String CLAYER = "otherwise select layer";
079: boolean newLayer = true;
080: private Layer itemlayer = null;
081: private boolean copyAsGeometry = false;
082:
083: public void initialize(PlugInContext context) throws Exception {
084:
085: context
086: .getFeatureInstaller()
087: .addPopupMenuItem(
088: LayerViewPanel.popupMenu(),
089: this ,
090: I18N
091: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.replicate-selected-items"),
092: false,
093: null,
094: createEnableCheck(context.getWorkbenchContext()));
095:
096: context
097: .getFeatureInstaller()
098: .addMainMenuItemWithJava14Fix(
099: this ,
100: new String[] { MenuNames.EDIT },
101: I18N
102: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.replicate-selected-items")
103: + "{pos:16}",
104: false,
105: null,
106: createEnableCheck(context.getWorkbenchContext()));
107:
108: this .T1 = I18N
109: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.replicate-to-new-layer");
110: this .CLAYER = I18N
111: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.otherwise-select-target-layer");
112: }
113:
114: public static MultiEnableCheck createEnableCheck(
115: WorkbenchContext workbenchContext) {
116: EnableCheckFactory checkFactory = new EnableCheckFactory(
117: workbenchContext);
118:
119: return new MultiEnableCheck().add(checkFactory
120: .createAtLeastNItemsMustBeSelectedCheck(1));
121: }
122:
123: public boolean execute(PlugInContext context) throws Exception {
124: this .reportNothingToUndoYet(context);
125: MultiInputDialog dialog = new MultiInputDialog(context
126: .getWorkbenchFrame(), getName(), true);
127: setDialogValues(dialog, context);
128: GUIUtil.centreOnWindow(dialog);
129: dialog.setVisible(true);
130: if (!dialog.wasOKPressed()) {
131: return false;
132: }
133: getDialogValues(dialog);
134: return true;
135: }
136:
137: private void setDialogValues(MultiInputDialog dialog,
138: PlugInContext context) {
139: String sidebarString = I18N
140: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.replicates-selected-items-if-all-have-same-attribute-schema");
141: dialog.setSideBarDescription(sidebarString);
142: //"Replicates selected items if all have same feature schema"
143: dialog.addCheckBox(T1, true);
144: JComboBox addLayerComboBoxBuild = dialog.addLayerComboBox(
145: this .CLAYER, context.getCandidateLayer(0), null,
146: context.getLayerManager());
147: }
148:
149: private void getDialogValues(MultiInputDialog dialog) {
150: this .newLayer = dialog.getBoolean(T1);
151: this .itemlayer = dialog.getLayer(this .CLAYER);
152: }
153:
154: public void run(TaskMonitor monitor, PlugInContext context)
155: throws Exception {
156: this .replicate(context, monitor, this .newLayer);
157: System.gc();
158: }
159:
160: private boolean replicate(PlugInContext context,
161: TaskMonitor monitor, boolean newLayer) throws Exception {
162: String statusMessage1 = I18N
163: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.item");
164: String statusMessage2 = I18N
165: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.no-replication-because-different-attribute-schema");
166:
167: boolean hasBeenCalled = false;
168: System.gc(); //flush garbage collector
169: // --------------------------
170: //-- get selected items
171: final Collection features = context.getLayerViewPanel()
172: .getSelectionManager().getFeaturesWithSelectedItems();
173:
174: if (newLayer == false) {
175: EditTransaction transaction = new EditTransaction(features,
176: this .getName(), this .itemlayer, this
177: .isRollingBackInvalidEdits(context), true,
178: context.getWorkbenchFrame());
179: //Collection actualLayerFeatures = this.itemlayer.getFeatureCollectionWrapper().getFeatures();
180: FeatureCollection actualLayerFeatures = this .itemlayer
181: .getFeatureCollectionWrapper().getWrappee();
182: FeatureSchema fschema = this .itemlayer
183: .getFeatureCollectionWrapper().getFeatureSchema();
184: //-- check if schema is the same if yes add the feature (or change Schema)
185: Iterator iter = features.iterator();
186: int i = 0;
187: Feature fi = null;
188: while (iter.hasNext()) {
189: i++;
190: fi = (Feature) iter.next();
191: if (fschema.equals(fi.getSchema())) {
192: actualLayerFeatures.add((Feature) fi.clone());
193: } else {
194: context.getWorkbenchFrame().setStatusMessage(
195: statusMessage1 + ": " + i + " "
196: + statusMessage2);
197: if (hasBeenCalled == false) {
198: this .askWhatToDo(context);
199: hasBeenCalled = true;
200: }
201: if (this .copyAsGeometry == true) {
202: Geometry geom = (Geometry) fi.getGeometry()
203: .clone();
204: Feature newFeature = FeatureUtil.toFeature(
205: geom, fschema);
206: actualLayerFeatures.add(newFeature);
207: }
208: }
209: }
210: transaction.commit();
211: } else {
212: Iterator iter = features.iterator();
213: Feature f = (Feature) iter.next();
214: FeatureCollection myCollA = new FeatureDataset(f
215: .getSchema());
216: myCollA.add((Feature) f.clone()); //copy first Item
217: Feature fi = null;
218: int i = 1;
219: while (iter.hasNext()) {
220: i++;
221: fi = (Feature) iter.next();
222: if (f.getSchema().equals(fi.getSchema())) {
223: myCollA.add((Feature) fi.clone());
224: } else {
225: context.getWorkbenchFrame().setStatusMessage(
226: statusMessage1 + ": " + i + statusMessage2);
227: if (hasBeenCalled == false) {
228: this .askWhatToDo(context);
229: hasBeenCalled = true;
230: }
231: if (this .copyAsGeometry == true) {
232: Geometry geom = (Geometry) fi.getGeometry()
233: .clone();
234: Feature newFeature = FeatureUtil.toFeature(
235: geom, f.getSchema());
236: myCollA.add(newFeature);
237: }
238: }
239: }
240: context
241: .addLayer(
242: StandardCategoryNames.WORKING,
243: I18N
244: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.new"),
245: myCollA);
246: }
247: return true;
248: }
249:
250: private void askWhatToDo(PlugInContext context) {
251: String dialog1 = I18N
252: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.attribute-schemas-are-different");
253: String dialog2 = I18N
254: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.copy-only-geometry");
255: String dialog3 = "("
256: + I18N
257: .get("org.openjump.core.ui.plugin.edit.ReplicateSelectedItemsPlugIn.on-ok-attributes-will-be-lost-on-cancel-items-are-not-copied")
258: + ")";
259:
260: MultiInputDialog dialog = new MultiInputDialog(context
261: .getWorkbenchFrame(), getName(), true);
262: dialog.addLabel(dialog1);
263: dialog.addLabel(dialog2);
264: dialog.addLabel(dialog3);
265: GUIUtil.centreOnWindow(dialog);
266: dialog.setVisible(true);
267: if (!dialog.wasOKPressed()) {
268: this .copyAsGeometry = false;
269: } else {
270: this .copyAsGeometry = true;
271: }
272:
273: }
274:
275: }
|