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;
034:
035: import java.awt.Event;
036: import java.awt.event.KeyEvent;
037: import java.awt.event.KeyListener;
038: import java.util.ArrayList;
039: import java.util.Collection;
040: import java.util.Iterator;
041:
042: import com.vividsolutions.jts.geom.Geometry;
043: import com.vividsolutions.jump.util.StringUtil;
044: import com.vividsolutions.jump.workbench.WorkbenchContext;
045: import com.vividsolutions.jump.workbench.model.Layer;
046: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
047: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
048: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
049: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
050: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
051: import com.vividsolutions.jump.workbench.ui.EditTransaction;
052: import com.vividsolutions.jump.workbench.ui.GeometryEditor;
053: import com.vividsolutions.jump.workbench.ui.SelectionManagerProxy;
054:
055: //Say "delete" for features but "remove" for layers; otherwise, "delete layers" may
056: //sound to a user that we're actually deleting the file from the disk. [Jon Aquino]
057: public class DeleteSelectedItemsPlugIn extends AbstractPlugIn {
058: public DeleteSelectedItemsPlugIn() {
059: }
060:
061: private GeometryEditor geometryEditor = new GeometryEditor();
062:
063: public boolean execute(final PlugInContext context)
064: throws Exception {
065: reportNothingToUndoYet(context);
066: ArrayList transactions = new ArrayList();
067: for (Iterator i = ((SelectionManagerProxy) context
068: .getActiveInternalFrame()).getSelectionManager()
069: .getLayersWithSelectedItems().iterator(); i.hasNext();) {
070: Layer layerWithSelectedItems = (Layer) i.next();
071: transactions.add(EditTransaction
072: .createTransactionOnSelection(
073: new EditTransaction.SelectionEditor() {
074: public Geometry edit(
075: Geometry geometryWithSelectedItems,
076: Collection selectedItems) {
077: Geometry g = geometryWithSelectedItems;
078: for (Iterator i = selectedItems
079: .iterator(); i.hasNext();) {
080: Geometry selectedItem = (Geometry) i
081: .next();
082: g = geometryEditor.remove(g,
083: selectedItem);
084: }
085: return g;
086: }
087: }, ((SelectionManagerProxy) context
088: .getActiveInternalFrame()), context
089: .getWorkbenchFrame(), getName(),
090: layerWithSelectedItems,
091: isRollingBackInvalidEdits(context), true));
092: }
093: return EditTransaction.commit(transactions);
094: }
095:
096: public static MultiEnableCheck createEnableCheck(
097: WorkbenchContext workbenchContext) {
098: EnableCheckFactory checkFactory = new EnableCheckFactory(
099: workbenchContext);
100:
101: return new MultiEnableCheck()
102: .add(
103: checkFactory
104: .createWindowWithSelectionManagerMustBeActiveCheck())
105: .add(
106: checkFactory
107: .createAtLeastNItemsMustBeSelectedCheck(1))
108: .add(
109: checkFactory
110: .createSelectedItemsLayersMustBeEditableCheck());
111: }
112:
113: public void initialize(PlugInContext context) throws Exception {
114: super .initialize(context);
115: registerDeleteKey(context.getWorkbenchContext());
116: }
117:
118: private void registerDeleteKey(final WorkbenchContext context) {
119: context.getWorkbench().getFrame()
120: .addKeyboardShortcut(KeyEvent.VK_DELETE, 0, this,
121: createEnableCheck(context));
122: }
123:
124: }
|