01: /*
02: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
03: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
04: * under the terms of the GNU Lesser General Public License as published by the Free Software
05: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
06: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
07: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
08: */
09: package net.refractions.udig.project.internal.command.navigation;
10:
11: import java.text.MessageFormat;
12:
13: import net.refractions.udig.project.command.MapCommand;
14: import net.refractions.udig.project.command.NavCommand;
15: import net.refractions.udig.project.internal.Messages;
16: import net.refractions.udig.project.internal.ProjectPlugin;
17: import net.refractions.udig.project.internal.render.ViewportModel;
18:
19: import org.eclipse.core.runtime.IProgressMonitor;
20: import org.geotools.geometry.jts.JTS;
21: import org.geotools.referencing.CRS;
22: import org.opengis.referencing.crs.CoordinateReferenceSystem;
23:
24: import com.vividsolutions.jts.geom.Coordinate;
25:
26: /**
27: * Sets the center of the viewport. The Coordinate must be in world coordinates. The
28: * {@linkplain ViewportModel#pixelToWorld(int, int)}methods can be used to calculate the value.
29: *
30: * @author jeichar
31: * @since TODO provide version
32: */
33: public class SetViewportCenterCommand extends AbstractNavCommand
34: implements NavCommand {
35:
36: private Coordinate center;
37: private CoordinateReferenceSystem crs;
38:
39: /**
40: * Creates a new instance of SetViewportCenterCommand
41: *
42: * @param center Sets the center of the viewport. The Coordinate must be in world coordinates.
43: */
44: public SetViewportCenterCommand(Coordinate center) {
45: this (center, null);
46: }
47:
48: public SetViewportCenterCommand(Coordinate coordinate,
49: CoordinateReferenceSystem crs) {
50: center = coordinate;
51: this .crs = crs;
52: }
53:
54: /**
55: * @see net.refractions.udig.project.internal.command.navigation.AbstractNavCommand#runImpl()
56: */
57: protected void runImpl(IProgressMonitor monitor) throws Exception {
58: Coordinate newCenter = center;
59: if (crs != null)
60: newCenter = transform();
61: model.setCenter(newCenter);
62: }
63:
64: private Coordinate transform() {
65: try {
66: return JTS.transform(center, new Coordinate(), CRS
67: .transform(crs, model.getCRS(), true));
68: } catch (Exception e) {
69: ProjectPlugin.log("", e); //$NON-NLS-1$
70: return null;
71: }
72: }
73:
74: /**
75: * @see net.refractions.udig.project.internal.command.MapCommand#copy()
76: */
77: public MapCommand copy() {
78: return new SetViewportCenterCommand(center);
79: }
80:
81: /**
82: * @see net.refractions.udig.project.command.MapCommand#getName()
83: */
84: public String getName() {
85: return MessageFormat.format(
86: Messages.SetViewportCenterCommand_setViewCenter,
87: new Object[] { center });
88: }
89:
90: }
|