001: /* uDig - User Friendly Desktop Internet GIS client
002: * http://udig.refractions.net
003: * (C) 2004, Refractions Research Inc.
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation;
008: * version 2.1 of the License.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: */
015: package net.refractions.udig.tools.edit.activator;
016:
017: import java.util.Iterator;
018:
019: import net.refractions.udig.project.command.UndoableComposite;
020: import net.refractions.udig.project.ui.ApplicationGIS;
021: import net.refractions.udig.project.ui.internal.ApplicationGISInternal;
022: import net.refractions.udig.tool.edit.internal.Messages;
023: import net.refractions.udig.tools.edit.Activator;
024: import net.refractions.udig.tools.edit.EditPlugin;
025: import net.refractions.udig.tools.edit.EditState;
026: import net.refractions.udig.tools.edit.EditToolHandler;
027: import net.refractions.udig.tools.edit.commands.RemoveAllVerticesCommand;
028: import net.refractions.udig.tools.edit.commands.RemoveSelectedVerticesCommand;
029: import net.refractions.udig.tools.edit.commands.SetCurrentGeomCommand;
030: import net.refractions.udig.tools.edit.commands.SetEditStateCommand;
031: import net.refractions.udig.tools.edit.support.EditBlackboard;
032: import net.refractions.udig.tools.edit.support.EditGeom;
033: import net.refractions.udig.tools.edit.support.Point;
034: import net.refractions.udig.tools.edit.support.PrimitiveShape;
035:
036: import org.eclipse.jface.action.Action;
037: import org.eclipse.jface.action.IAction;
038: import org.eclipse.swt.widgets.Event;
039: import org.eclipse.ui.IActionBars2;
040: import org.eclipse.ui.IKeyBindingService;
041: import org.eclipse.ui.IWorkbenchPart;
042: import org.eclipse.ui.actions.ActionFactory;
043:
044: /**
045: * Sets the Delete Global handler so that the selected vertices are deleted when the delete action
046: * is pressed.
047: *
048: * @author jones
049: * @since 1.1.0
050: */
051: public class DeleteGlobalActionSetterActivator implements Activator {
052:
053: private IAction oldAction;
054: private DeleteVertexHandler deleteVertexHandler;
055:
056: public void activate(EditToolHandler handler) {
057: IActionBars2 actionBars = handler.getContext().getActionBars();
058: if (actionBars == null)
059: return;
060: IWorkbenchPart part = ApplicationGISInternal.getActiveEditor();
061: oldAction = ApplicationGIS.getToolManager().getDELETEAction();
062: IKeyBindingService keyBindingService = part.getSite()
063: .getKeyBindingService();
064: if (oldAction != null)
065: keyBindingService.unregisterAction(oldAction);
066:
067: deleteVertexHandler = new DeleteVertexHandler(handler);
068: if (oldAction != null) {
069: deleteVertexHandler.setImageDescriptor(oldAction
070: .getImageDescriptor());
071: deleteVertexHandler.setDisabledImageDescriptor(oldAction
072: .getDisabledImageDescriptor());
073: }
074: ApplicationGIS.getToolManager().setDELETEAction(
075: deleteVertexHandler, part);
076: actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
077: deleteVertexHandler);
078: actionBars.updateActionBars();
079: keyBindingService.registerAction(deleteVertexHandler);
080: }
081:
082: public void deactivate(EditToolHandler handler) {
083: IActionBars2 actionBars = handler.getContext().getActionBars();
084: if (actionBars == null || oldAction == null)
085: return;
086:
087: IWorkbenchPart part = ApplicationGISInternal.getActiveEditor();
088: IKeyBindingService keyBindingService = part.getSite()
089: .getKeyBindingService();
090: keyBindingService.unregisterAction(deleteVertexHandler);
091: deleteVertexHandler = null;
092:
093: ApplicationGIS.getToolManager()
094: .setDELETEAction(oldAction, part);
095: actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(),
096: oldAction);
097: if (oldAction != null)
098: keyBindingService.registerAction(oldAction);
099:
100: oldAction = null;
101: }
102:
103: public void handleActivateError(EditToolHandler handler,
104: Throwable error) {
105: EditPlugin.log("", error); //$NON-NLS-1$
106: }
107:
108: public void handleDeactivateError(EditToolHandler handler,
109: Throwable error) {
110: EditPlugin.log("", error); //$NON-NLS-1$
111: }
112:
113: static class DeleteVertexHandler extends Action {
114: private EditToolHandler handler;
115:
116: DeleteVertexHandler(EditToolHandler handler) {
117: this .handler = handler;
118: setText(Messages.DeleteGlobalActionSetterActivator_title);
119: setToolTipText(Messages.DeleteGlobalActionSetterActivator_tooltip);
120: }
121:
122: @Override
123: public void runWithEvent(Event event) {
124: EditGeom currentGeom = handler.getCurrentGeom();
125: if (currentGeom == null)
126: return;
127: EditBlackboard editBlackboard = currentGeom
128: .getEditBlackboard();
129: if (editBlackboard.getSelection().isEmpty()
130: || hasNoPoints()) {
131: UndoableComposite composite = new UndoableComposite();
132: composite.getCommands()
133: .add(
134: new SetEditStateCommand(handler,
135: EditState.BUSY));
136: composite.getCommands().add(
137: new RemoveAllVerticesCommand(handler));
138: composite.getCommands().add(
139: handler.getCommand(handler
140: .getAcceptBehaviours()));
141: composite.getCommands().add(
142: new SetCurrentGeomCommand(handler,
143: (PrimitiveShape) null));
144: composite.getFinalizerCommands().add(
145: new SetEditStateCommand(handler,
146: EditState.MODIFYING));
147: handler.getContext().sendASyncCommand(composite);
148: } else {
149: UndoableComposite composite = new UndoableComposite();
150: composite.getFinalizerCommands().add(
151: new SetEditStateCommand(handler,
152: EditState.MODIFYING));
153: composite.getCommands()
154: .add(
155: new SetEditStateCommand(handler,
156: EditState.BUSY));
157: composite.getCommands().add(
158: new RemoveSelectedVerticesCommand(handler));
159: handler.getContext().sendASyncCommand(composite);
160: }
161: }
162:
163: private boolean hasNoPoints() {
164: Iterator<Point> iter = handler.getCurrentGeom().getShell()
165: .iterator();
166: EditBlackboard editBlackboard = handler.getCurrentGeom()
167: .getEditBlackboard();
168: while (iter.hasNext()) {
169: if (!editBlackboard.getSelection()
170: .contains(iter.next()))
171: return false;
172: }
173: return true;
174: }
175: }
176:
177: }
|