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.project.command.AbstractCommand;
20: import net.refractions.udig.project.command.UndoableMapCommand;
21: import net.refractions.udig.project.internal.Map;
22: import net.refractions.udig.project.internal.Messages;
23:
24: import org.eclipse.core.runtime.IProgressMonitor;
25: import org.opengis.referencing.crs.CoordinateReferenceSystem;
26:
27: import com.vividsolutions.jts.geom.Coordinate;
28: import com.vividsolutions.jts.geom.Envelope;
29:
30: /**
31: * Change the CRS of a map.
32: *
33: * @author Jesse
34: * @since 1.0.0
35: */
36: public class ChangeCRSCommand extends AbstractCommand implements
37: UndoableMapCommand {
38: private static final String NAME = Messages.ChangeCRSCommand_name;
39: private Map map;
40: private CoordinateReferenceSystem crs;
41: private CoordinateReferenceSystem oldCRS;
42: private double height;
43: private double width;
44: private Envelope bounds;
45: private Coordinate center;
46:
47: /**
48: * Construct <code>AddLayerCommand</code>.
49: *
50: * @param layer the layer that will be added.
51: */
52: public ChangeCRSCommand(Map map, CoordinateReferenceSystem crs) {
53: this .map = map;
54: this .crs = crs;
55: this .oldCRS = map.getViewportModelInternal().getCRS();
56: this .height = map.getViewportModelInternal().getHeight();
57: this .width = map.getViewportModelInternal().getWidth();
58: this .bounds = map.getViewportModelInternal().getBounds();
59: this .center = map.getViewportModelInternal().getCenter();
60: }
61:
62: /**
63: * Remove the layer that was added during execution.
64: *
65: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
66: */
67: public void rollback(IProgressMonitor monitor) throws Exception {
68: monitor.beginTask(Messages.ChangeCRSCommand_undoName + NAME, 1);
69: map.getViewportModelInternal().setCRS(oldCRS);
70: map.getViewportModelInternal().setCenter(center);
71: map.getViewportModelInternal().setHeight(height);
72: map.getViewportModelInternal().setWidth(width);
73: map.getViewportModelInternal().setBounds(bounds);
74: monitor.done();
75: }
76:
77: /**
78: * Changes the maps' CRS. Defensive programming is recommended but command framework protects
79: * against exceptions raised in commands.
80: *
81: * @see net.refractions.udig.project.command.MapCommand#run()
82: */
83: public void run(IProgressMonitor monitor) throws Exception {
84: monitor.beginTask(NAME, 1);
85: map.getViewportModelInternal().setCRS(crs);
86: monitor.done();
87: }
88:
89: /**
90: * Each command has a name that is displayed with the undo/redo buttons.
91: *
92: * @see net.refractions.udig.project.command.MapCommand#getName()
93: */
94: public String getName() {
95: return NAME;
96: }
97:
98: }
|