svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / geolocation / behavior / TranslateBehavior.java @ 12825
History | View | Annotate | Download (8.9 KB)
1 | 12610 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program 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
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.rastertools.geolocation.behavior; |
||
20 | |||
21 | import java.awt.AlphaComposite; |
||
22 | import java.awt.Component; |
||
23 | import java.awt.Cursor; |
||
24 | import java.awt.Graphics; |
||
25 | import java.awt.Graphics2D; |
||
26 | import java.awt.HeadlessException; |
||
27 | import java.awt.Image; |
||
28 | import java.awt.Point; |
||
29 | import java.awt.Toolkit; |
||
30 | import java.awt.event.MouseEvent; |
||
31 | import java.awt.geom.AffineTransform; |
||
32 | import java.awt.geom.NoninvertibleTransformException; |
||
33 | import java.awt.geom.Point2D; |
||
34 | |||
35 | import javax.swing.ImageIcon; |
||
36 | import javax.swing.JOptionPane; |
||
37 | |||
38 | import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
||
39 | 12751 | nacho | import org.gvsig.raster.datastruct.Extent; |
40 | 12610 | nacho | |
41 | import com.iver.andami.PluginServices; |
||
42 | 12793 | nacho | import com.iver.andami.messages.NotificationManager; |
43 | 12610 | nacho | import com.iver.cit.gvsig.fmap.MapControl; |
44 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
45 | import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
||
46 | import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
||
47 | |||
48 | |||
49 | /**
|
||
50 | * Behaviour que espera un listener de tipo GeoMoveListener.
|
||
51 | * Nacho Brodin (nachobrodin@gmail.com)
|
||
52 | *
|
||
53 | */
|
||
54 | public class TranslateBehavior extends TransformationBehavior { |
||
55 | |||
56 | 12768 | nacho | private final Image handCursor = new ImageIcon(MapControl.class.getResource("images/Hand.gif")).getImage(); |
57 | 12610 | nacho | |
58 | private Cursor cur = null; |
||
59 | private boolean isMoveable = false; |
||
60 | |||
61 | /**
|
||
62 | * Variable booleana que est? a true si el cursor por defecto est?
|
||
63 | * activo y a false si hay otro.
|
||
64 | */
|
||
65 | private boolean defaultCursorActive = true; |
||
66 | |||
67 | /**
|
||
68 | * Puntos de inicio y final para el arrastre de la imagen.
|
||
69 | */
|
||
70 | private Point2D ptoIni = null; |
||
71 | private Point2D ptoFin = null; |
||
72 | |||
73 | /**
|
||
74 | * Crea un nuevo RectangleBehavior.
|
||
75 | *
|
||
76 | * @param zili listener.
|
||
77 | */
|
||
78 | 12825 | nacho | public TranslateBehavior(GeoRasterBehavior grb, Cursor cur, ITransformIO windowIO) { |
79 | 12610 | nacho | grBehavior = grb; |
80 | defaultCursor = cur; |
||
81 | 12825 | nacho | this.trIO = windowIO;
|
82 | 12610 | nacho | } |
83 | |||
84 | /**
|
||
85 | * Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la
|
||
86 | * imagen.
|
||
87 | */
|
||
88 | public boolean mouseMoved(MouseEvent e) throws BehaviorException { |
||
89 | |||
90 | ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
||
91 | |||
92 | try{
|
||
93 | lyr = grBehavior.getLayer(); |
||
94 | if(lyr == null) { |
||
95 | setActiveTool(false);
|
||
96 | return false; |
||
97 | } |
||
98 | |||
99 | Point2D pto = vp.toMapPoint(e.getX(), e.getY());
|
||
100 | |||
101 | try {
|
||
102 | if( lyr.isInside(pto)){
|
||
103 | grBehavior.getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
|
||
104 | new Point(16, 16), "")); |
||
105 | defaultCursorActive = false;
|
||
106 | setActiveTool(true);
|
||
107 | return true; |
||
108 | }else{
|
||
109 | if(!defaultCursorActive){
|
||
110 | grBehavior.getMapControl().setCursor(defaultCursor); |
||
111 | defaultCursorActive = true;
|
||
112 | setActiveTool(false);
|
||
113 | return false; |
||
114 | } |
||
115 | } |
||
116 | } catch (HeadlessException e1) { |
||
117 | e1.printStackTrace(); |
||
118 | } catch (IndexOutOfBoundsException e1) { |
||
119 | e1.printStackTrace(); |
||
120 | } |
||
121 | } catch(ClassCastException exc){ |
||
122 | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
||
123 | PluginServices.getText(this, "error_capa_puntos")); |
||
124 | } |
||
125 | setActiveTool(false);
|
||
126 | return false; |
||
127 | } |
||
128 | |||
129 | /**
|
||
130 | * Obtiene el cursor actual
|
||
131 | */
|
||
132 | public Cursor getCursor() { |
||
133 | return this.cur; |
||
134 | } |
||
135 | |||
136 | /**
|
||
137 | * Si no est? activo el cursor por defecto capturamos el punto
|
||
138 | * seleccionado en coordenadas del mundo real.
|
||
139 | */
|
||
140 | public void mousePressed(MouseEvent e) throws BehaviorException { |
||
141 | if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive) { |
||
142 | setActiveTool(true);
|
||
143 | lyr = grBehavior.getLayer(); |
||
144 | |||
145 | if(lyr == null) |
||
146 | return;
|
||
147 | |||
148 | ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
||
149 | ptoIni = vp.toMapPoint(e.getPoint()); |
||
150 | |||
151 | isMoveable = true;
|
||
152 | } |
||
153 | } |
||
154 | |||
155 | /**
|
||
156 | * Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
|
||
157 | * de destino calculando el extent nuevamente.
|
||
158 | */
|
||
159 | public void mouseReleased(MouseEvent e) throws BehaviorException { |
||
160 | if(!isActiveTool())
|
||
161 | return;
|
||
162 | if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){ |
||
163 | |||
164 | FLyrRasterSE lyr = grBehavior.getLayer(); |
||
165 | if(lyr == null) |
||
166 | return;
|
||
167 | |||
168 | ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
||
169 | ptoFin = vp.toMapPoint(e.getPoint()); |
||
170 | |||
171 | //Asignamos la nueva matriz de transformaci?n a la capa
|
||
172 | AffineTransform atOld = lyr.getAffineTransform();
|
||
173 | AffineTransform atNew = null; |
||
174 | |||
175 | double distX = ptoFin.getX() - ptoIni.getX();
|
||
176 | double distY = ptoFin.getY() - ptoIni.getY();
|
||
177 | |||
178 | //La nueva matriz de transformaci?n es la vieja m?s la distancia desplazada
|
||
179 | atNew = new AffineTransform(atOld.getScaleX(), atOld.getShearY(), |
||
180 | atOld.getShearX(), atOld.getScaleY(), |
||
181 | atOld.getTranslateX() + distX, |
||
182 | atOld.getTranslateY() + distY); |
||
183 | lyr.setAffineTransform(atNew); |
||
184 | |||
185 | grBehavior.getMapControl().getMapContext().invalidate(); |
||
186 | |||
187 | isMoveable = false;
|
||
188 | } |
||
189 | |||
190 | } |
||
191 | |||
192 | /**
|
||
193 | 12793 | nacho | * <P>
|
194 | * Funci?n de pintado del canvas. Pintamos un marco a la imagen para saber
|
||
195 | 12610 | nacho | * donde la movemos.
|
196 | 12793 | nacho | * </P>
|
197 | * <P>
|
||
198 | * Para dibujar el marco alrededor del raster hacemos lo mismo que para pintar el raster
|
||
199 | * rotado. En realidad dibujamos un cuadrado y luego le aplicamos las transformaciones necesarias
|
||
200 | * para que se vea con la misma forma del raster al que representa.
|
||
201 | * </P>
|
||
202 | 12610 | nacho | */
|
203 | public void paintComponent(Graphics g) { |
||
204 | 12819 | nacho | if(isMoveable && lyr != null && ptoFin != null && ptoIni != null) { |
205 | 12610 | nacho | try {
|
206 | 12793 | nacho | ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
207 | 12751 | nacho | AffineTransform at = (AffineTransform)lyr.getAffineTransform().clone(); |
208 | at.preConcatenate(vp.getAffineTransform()); |
||
209 | 12817 | nacho | Extent ext = lyr.getFullRasterExtent(); |
210 | 12610 | nacho | |
211 | 12751 | nacho | Point2D pt = new Point2D.Double(ext.getULX(), ext.getULY()); |
212 | vp.getAffineTransform().transform(pt, pt); |
||
213 | at.inverseTransform(pt, pt); |
||
214 | |||
215 | Point2D size = new Point2D.Double(ext.getLRX(), ext.getLRY()); |
||
216 | vp.getAffineTransform().transform(size, size); |
||
217 | at.inverseTransform(size, size); |
||
218 | |||
219 | 12793 | nacho | double distX = ptoFin.getX() - ptoIni.getX();
|
220 | double distY = ptoFin.getY() - ptoIni.getY();
|
||
221 | 12751 | nacho | Point2D d = new Point2D.Double(ext.getULX() + distX, ext.getULY() + distY); |
222 | vp.getAffineTransform().transform(d, d); |
||
223 | at.inverseTransform(d, d); |
||
224 | 12610 | nacho | |
225 | //Giramos el graphics se dibuja y se vuelve a dejar como estaba
|
||
226 | ((Graphics2D)g).transform(at);
|
||
227 | 12751 | nacho | g.setColor(rectangleColor); |
228 | AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f); |
||
229 | ((Graphics2D)g).setComposite(alpha);
|
||
230 | g.fillRect((int)pt.getX() + (int)d.getX(), (int)pt.getY() + (int)d.getY(), (int)size.getX(), (int)size.getY()); |
||
231 | 12610 | nacho | ((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); |
232 | 12751 | nacho | g.drawRect((int)pt.getX() + (int)d.getX(), (int)pt.getY() + (int)d.getY(), (int)size.getX(), (int)size.getY()); |
233 | 12610 | nacho | ((Graphics2D)g).transform(at.createInverse());
|
234 | } catch (NoninvertibleTransformException e1) { |
||
235 | 12793 | nacho | NotificationManager.addError("No se puede calcular la transformaci?n inversa de alguna esquina del raster", e1);
|
236 | 12610 | nacho | } |
237 | } |
||
238 | } |
||
239 | |||
240 | /**
|
||
241 | * Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
|
||
242 | * poder ver el marco de arrastre.
|
||
243 | */
|
||
244 | public void mouseDragged(MouseEvent e) throws BehaviorException { |
||
245 | if(!isActiveTool())
|
||
246 | return;
|
||
247 | if(isMoveable){
|
||
248 | ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
||
249 | ptoFin = vp.toMapPoint(e.getPoint()); |
||
250 | 12825 | nacho | |
251 | //Asignaci?n de las coordenadas temporales al dialogo
|
||
252 | assignTransformToDialog(); |
||
253 | |||
254 | 12610 | nacho | grBehavior.getMapControl().repaint(); |
255 | } |
||
256 | } |
||
257 | |||
258 | 12825 | nacho | /**
|
259 | * Asigna las coordenadas temporales en el dialogo.
|
||
260 | */
|
||
261 | public void assignTransformToDialog() { |
||
262 | AffineTransform atNew = null; |
||
263 | double distX = ptoFin.getX() - ptoIni.getX();
|
||
264 | double distY = ptoFin.getY() - ptoIni.getY();
|
||
265 | AffineTransform atOld = grBehavior.getLayer().getAffineTransform();
|
||
266 | //La nueva matriz de transformaci?n es la vieja m?s la distancia desplazada
|
||
267 | atNew = new AffineTransform(atOld.getScaleX(), atOld.getShearY(), |
||
268 | atOld.getShearX(), atOld.getScaleY(), |
||
269 | atOld.getTranslateX() + distX, |
||
270 | atOld.getTranslateY() + distY); |
||
271 | trIO.loadTransform(atNew); |
||
272 | } |
||
273 | |||
274 | 12610 | nacho | /* (non-Javadoc)
|
275 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
||
276 | */
|
||
277 | public ToolListener getListener() {
|
||
278 | return null; |
||
279 | } |
||
280 | |||
281 | |||
282 | } |