01: /*
02: * uDig - User Friendly Desktop Internet GIS client
03: * http://udig.refractions.net
04: * (C) 2004, Refractions Research Inc.
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: */
17: package net.refractions.udig.project.internal.commands;
18:
19: import net.refractions.udig.catalog.IGeoResource;
20: import net.refractions.udig.project.ILayer;
21: import net.refractions.udig.project.command.AbstractCommand;
22: import net.refractions.udig.project.command.UndoableMapCommand;
23: import net.refractions.udig.project.internal.Layer;
24: import net.refractions.udig.project.internal.Messages;
25:
26: import org.eclipse.core.runtime.IProgressMonitor;
27:
28: /**
29: * Add a layer to a map.
30: * @author Jesse
31: * @since 1.0.0
32: */
33: public class AddLayerCommand extends AbstractCommand implements
34: UndoableMapCommand {
35:
36: private Layer layer;
37: private int index = -1;
38: private ILayer selectedLayer;
39:
40: /**
41: * Construct <code>AddLayerCommand</code>.
42: *
43: * @param layer the layer that will be added.
44: */
45: public AddLayerCommand(Layer layer) {
46: this .layer = layer;
47: }
48:
49: /**
50: * Construct <code>AddLayerCommand</code>.
51: *
52: * @param layer the layer that will be added.
53: * @param index the zorder that the layer will be added.
54: */
55: public AddLayerCommand(Layer layer, int index) {
56: this .layer = layer;
57: this .index = index;
58: }
59:
60: /**
61: * Remove the layer that was added during execution.
62: *
63: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
64: */
65: public void rollback(IProgressMonitor monitor) throws Exception {
66: getMap().getLayersInternal().remove(layer);
67: getMap().getEditManagerInternal().setSelectedLayer(
68: (Layer) selectedLayer);
69: }
70:
71: /**
72: * Adds a layer to the map. Defensive programming is recommended but command framework protects
73: * against exceptions raised in commands.
74: *
75: * @see net.refractions.udig.project.command.MapCommand#run()
76: */
77: public void run(IProgressMonitor monitor) throws Exception {
78: selectedLayer = getMap().getEditManager().getSelectedLayer();
79: if (index < 0 || index > getMap().getLayersInternal().size())
80: getMap().getLayersInternal().add(layer);
81: else
82: getMap().getLayersInternal().add(index, layer);
83: }
84:
85: /**
86: * Each command has a name that is displayed with the undo/redo buttons.
87: *
88: * @see net.refractions.udig.project.command.MapCommand#getName()
89: */
90: public String getName() {
91: return Messages.AddLayerCommand_Name + layer.getName();
92: }
93:
94: }
|