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.project.ui.internal.commands;
16:
17: import net.refractions.udig.core.StaticProvider;
18: import net.refractions.udig.project.ILayer;
19: import net.refractions.udig.project.command.AbstractCommand;
20: import net.refractions.udig.project.command.UndoableMapCommand;
21: import net.refractions.udig.project.internal.Layer;
22: import net.refractions.udig.project.internal.render.ViewportModel;
23: import net.refractions.udig.project.ui.internal.Messages;
24:
25: import org.eclipse.core.runtime.IProgressMonitor;
26: import org.geotools.geometry.jts.ReferencedEnvelope;
27: import org.opengis.referencing.crs.CoordinateReferenceSystem;
28:
29: /**
30: * Sets the CRS of the layer
31: * @author Jesse
32: * @since 1.1.0
33: */
34: public class SetLayerCRSCommand extends AbstractCommand implements
35: UndoableMapCommand {
36: public static final String NAME = Messages.SetLayerCRSCommand_name;
37:
38: private StaticProvider<ILayer> provider;
39: private Layer layer;
40: private CoordinateReferenceSystem crs, old;
41:
42: private ReferencedEnvelope oldBounds = null;
43:
44: public SetLayerCRSCommand(ILayer layer,
45: CoordinateReferenceSystem crs) {
46: this .provider = new StaticProvider<ILayer>(layer);
47: this .crs = crs;
48: }
49:
50: public String getName() {
51: return NAME;
52: }
53:
54: public void run(IProgressMonitor monitor) throws Exception {
55: monitor.beginTask(NAME, 1);
56: if (layer == null)
57: layer = (Layer) provider.get();
58: old = layer.getCRS(monitor);
59: monitor.setTaskName(NAME);
60: layer.setCRS(crs);
61: ViewportModel viewportModel = layer.getMapInternal()
62: .getViewportModelInternal();
63: ReferencedEnvelope bounds = (ReferencedEnvelope) layer
64: .getMapInternal().getViewportModel().getBounds();
65: if (layer.getMapInternal().getMapLayers().size() == 1
66: && !bounds.intersects(layer.getBounds(monitor,
67: viewportModel.getCRS()))) {
68: oldBounds = bounds;
69:
70: viewportModel.zoomToExtent();
71: }
72: monitor.done();
73: }
74:
75: public void rollback(IProgressMonitor monitor) throws Exception {
76: String name = Messages.SetLayerCRSCommand_undoTask;
77: monitor.beginTask(name, 1);
78: Layer layer = (Layer) provider.get();
79: layer.setCRS(old);
80: if (oldBounds != null) {
81: ViewportModel viewportModel = layer.getMapInternal()
82: .getViewportModelInternal();
83: viewportModel.setBounds(oldBounds);
84: }
85: monitor.done();
86: }
87:
88: }
|