gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / rasterresolution / ZoomPixelCursorListener.java @ 2595
History | View | Annotate | Download (5.42 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program 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
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.tools.app.basic.tool.rasterresolution; |
23 |
|
24 |
import java.awt.Cursor; |
25 |
import java.awt.Image; |
26 |
import java.awt.Point; |
27 |
import java.awt.Toolkit; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.util.ArrayList; |
30 |
|
31 |
import org.gvsig.andami.IconThemeHelper; |
32 |
import org.gvsig.andami.messages.NotificationManager; |
33 |
import org.gvsig.fmap.dal.exception.ReadException; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.GeometryManager; |
36 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
37 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.mapcontext.ViewPort; |
40 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
41 |
import org.gvsig.fmap.mapcontrol.MapControl; |
42 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
43 |
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent; |
44 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener; |
45 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
46 |
import org.slf4j.Logger; |
47 |
import org.slf4j.LoggerFactory; |
48 |
|
49 |
|
50 |
|
51 |
/**
|
52 |
* Listener para la tool de zoom a la resoluci?n del raster. Este es el encargado de recibir
|
53 |
* la petici?n desde la vista y calcular las coordenadas del raster.
|
54 |
*
|
55 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
56 |
*/
|
57 |
public class ZoomPixelCursorListener implements PointListener { |
58 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
59 |
private static final Logger logger = LoggerFactory.getLogger(ZoomPixelCursorListener.class); |
60 |
private Image img = null; |
61 |
private Cursor cur = null; |
62 |
private MapControl mapCtrl;
|
63 |
|
64 |
|
65 |
/**
|
66 |
* Constructor. Asigna el MapControl
|
67 |
* @param mc
|
68 |
*/
|
69 |
public ZoomPixelCursorListener(MapControl mc) {
|
70 |
this.mapCtrl = mc;
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* Evento de selecci?n de punto sobre la vista. Aquin se calcular?n las coordenadas
|
75 |
* y dimensiones de la petici?n centrando la petici?n en el punto pinchado en la vista.
|
76 |
*/
|
77 |
@SuppressWarnings("deprecation") |
78 |
public void point(PointEvent event) throws BehaviorException { |
79 |
Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
|
80 |
//Point imagePoint = new Point((int) event.getPoint().getX(), (int) event.getPoint().getY());
|
81 |
ViewPort v = mapCtrl.getMapContext().getViewPort(); |
82 |
|
83 |
FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
|
84 |
Envelope ext = null;
|
85 |
try {
|
86 |
ext = actives[0].getFullEnvelope();
|
87 |
} catch (ReadException e1) {
|
88 |
NotificationManager.addError("Error al obtener el extent", e1);
|
89 |
} |
90 |
|
91 |
ArrayList<Object> attr = ((FLyrRaster)actives[0]).getAttributes(); |
92 |
int width = 0, height = 0; |
93 |
for (int i=0; i<attr.size(); i++) { |
94 |
Object[] a = (Object []) attr.get(i); |
95 |
if (a[0].toString().equals("Width")) |
96 |
width = ((Integer)a[1]).intValue(); |
97 |
if (a[0].toString().equals("Height")) |
98 |
height = ((Integer)a[1]).intValue(); |
99 |
} |
100 |
|
101 |
|
102 |
if( ext != null && |
103 |
width != 0 &&
|
104 |
height != 0){
|
105 |
|
106 |
double wcOriginCenterX = pReal.getX();
|
107 |
double wcOriginCenterY = pReal.getY();
|
108 |
|
109 |
//Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
|
110 |
double relacionPixelWcWidth = (ext.getMaximum(0) - ext.getMinimum(0))/width; |
111 |
double relacionPixelWcHeight = (ext.getMaximum(1) - ext.getMinimum(1))/height; |
112 |
//double desplazamientoX = ext.getMinX();
|
113 |
//double desplazamientoY = ext.getMinY();
|
114 |
|
115 |
double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2); |
116 |
double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2); |
117 |
|
118 |
double wcDstMinX = wcOriginX;
|
119 |
double wcDstMinY = wcOriginY;
|
120 |
double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
|
121 |
double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
|
122 |
|
123 |
double wcDstWidth = wcDstMaxX - wcDstMinX;
|
124 |
double wcDstHeight = wcDstMaxY - wcDstMinY;
|
125 |
|
126 |
try {
|
127 |
ext = geomManager.createEnvelope(wcDstMinX, wcDstMinY, wcDstMinX |
128 |
+ wcDstWidth, wcDstMinY + wcDstHeight, SUBTYPES.GEOM2D); |
129 |
} catch (CreateEnvelopeException e) {
|
130 |
logger.error("Error creating the Envelope", e);
|
131 |
} |
132 |
mapCtrl.getMapContext().getViewPort().setEnvelope(ext); |
133 |
} |
134 |
} |
135 |
|
136 |
/**
|
137 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
138 |
*/
|
139 |
public Cursor getCursor() { |
140 |
if(cur == null) |
141 |
cur = Toolkit.getDefaultToolkit().createCustomCursor(getImageCursor(), new Point(16, 16), ""); |
142 |
return cur;
|
143 |
} |
144 |
|
145 |
public boolean cancelDrawing() { |
146 |
return false; |
147 |
} |
148 |
|
149 |
public void pointDoubleClick(PointEvent event) throws BehaviorException { |
150 |
|
151 |
} |
152 |
|
153 |
public Image getImageCursor() { |
154 |
if(img == null) |
155 |
img = IconThemeHelper.getImage("zoom-pixel-cursor");
|
156 |
return img;
|
157 |
} |
158 |
|
159 |
} |