01: /* uDig - User Friendly Desktop Internet GIS client
02: * http://udig.refractions.net
03: * (C) 2004, Refractions Research Inc.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation;
08: * version 2.1 of the License.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: */
15: package net.refractions.udig.tools.edit.commands;
16:
17: import java.util.List;
18:
19: import net.refractions.udig.core.IBlockingProvider;
20: import net.refractions.udig.project.command.AbstractCommand;
21: import net.refractions.udig.project.command.UndoableMapCommand;
22: import net.refractions.udig.tool.edit.internal.Messages;
23: import net.refractions.udig.tools.edit.EditToolHandler;
24: import net.refractions.udig.tools.edit.support.EditUtils;
25: import net.refractions.udig.tools.edit.support.PrimitiveShape;
26:
27: import org.eclipse.core.runtime.IProgressMonitor;
28:
29: /**
30: * Creates a hole in a EditGeom and sets it as the currentShape in the handler if a handler is provided.
31: *
32: * @author jones
33: * @since 1.1.0
34: */
35: public class CreateAndSelectHoleCommand extends AbstractCommand
36: implements UndoableMapCommand {
37:
38: private EditToolHandler handler;
39: private PrimitiveShape hole;
40: private PrimitiveShape oldShape;
41: private IBlockingProvider<PrimitiveShape> newShape;
42:
43: public CreateAndSelectHoleCommand(EditToolHandler handler) {
44: this .handler = handler;
45: newShape = new EditUtils.EditToolHandlerShapeProvider(handler);
46: }
47:
48: public CreateAndSelectHoleCommand(
49: IBlockingProvider<PrimitiveShape> shape) {
50: this .newShape = shape;
51: }
52:
53: public void run(IProgressMonitor monitor) throws Exception {
54: PrimitiveShape primitiveShape = newShape.get(monitor);
55: List<PrimitiveShape> holes = primitiveShape.getEditGeom()
56: .getHoles();
57: for (PrimitiveShape shape : holes) {
58: if (shape.getNumPoints() == 0) {
59: hole = shape;
60: break;
61: }
62: }
63: if (hole == null)
64: this .hole = primitiveShape.getEditGeom().newHole();
65: if (handler != null) {
66: this .oldShape = handler.getCurrentShape();
67: handler.setCurrentShape(hole);
68: }
69: }
70:
71: public String getName() {
72: return Messages.CreateAndSelectHoleCommand_name;
73: }
74:
75: public void rollback(IProgressMonitor monitor) throws Exception {
76: if (handler != null)
77: handler.setCurrentShape(oldShape);
78: newShape.get(monitor).getEditGeom().getHoles().remove(hole);
79: }
80:
81: /**
82: *
83: * @return
84: */
85: public IBlockingProvider<PrimitiveShape> getHoleProvider() {
86: return new ShapeProvider();
87: }
88:
89: class ShapeProvider implements IBlockingProvider<PrimitiveShape> {
90:
91: public PrimitiveShape get(IProgressMonitor monitor) {
92: return hole;
93: }
94:
95: }
96: }
|