svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / geolocation / behavior / TranslateBehavior.java @ 12610
History | View | Annotate | Download (8.26 KB)
1 |
/* 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.Color; |
23 |
import java.awt.Component; |
24 |
import java.awt.Cursor; |
25 |
import java.awt.Graphics; |
26 |
import java.awt.Graphics2D; |
27 |
import java.awt.HeadlessException; |
28 |
import java.awt.Image; |
29 |
import java.awt.Point; |
30 |
import java.awt.Rectangle; |
31 |
import java.awt.Toolkit; |
32 |
import java.awt.event.MouseEvent; |
33 |
import java.awt.geom.AffineTransform; |
34 |
import java.awt.geom.NoninvertibleTransformException; |
35 |
import java.awt.geom.Point2D; |
36 |
import java.awt.image.BufferedImage; |
37 |
|
38 |
import javax.swing.ImageIcon; |
39 |
import javax.swing.JOptionPane; |
40 |
|
41 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
42 |
|
43 |
import com.iver.andami.PluginServices; |
44 |
import com.iver.cit.gvsig.fmap.MapControl; |
45 |
import com.iver.cit.gvsig.fmap.ViewPort; |
46 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
47 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
48 |
|
49 |
|
50 |
/**
|
51 |
* Behaviour que espera un listener de tipo GeoMoveListener.
|
52 |
* Nacho Brodin (nachobrodin@gmail.com)
|
53 |
*
|
54 |
*/
|
55 |
public class TranslateBehavior extends TransformationBehavior { |
56 |
|
57 |
private final Image handCursor = new ImageIcon(MapControl.class.getResource( |
58 |
"images/Hand.gif")).getImage();
|
59 |
|
60 |
private Cursor cur = null; |
61 |
private boolean isMoveable = false; |
62 |
|
63 |
/**
|
64 |
* Variable booleana que est? a true si el cursor por defecto est?
|
65 |
* activo y a false si hay otro.
|
66 |
*/
|
67 |
private boolean defaultCursorActive = true; |
68 |
|
69 |
/**
|
70 |
* Puntos de inicio y final para el arrastre de la imagen.
|
71 |
*/
|
72 |
private Point2D ptoIni = null; |
73 |
private Point2D ptoFin = null; |
74 |
|
75 |
/**
|
76 |
* Crea un nuevo RectangleBehavior.
|
77 |
*
|
78 |
* @param zili listener.
|
79 |
*/
|
80 |
public TranslateBehavior(GeoRasterBehavior grb, Cursor cur) { |
81 |
grBehavior = grb; |
82 |
defaultCursor = cur; |
83 |
} |
84 |
|
85 |
/**
|
86 |
* Coloca el cursor del rat?n con el icono adecuado cuando entra dentro de la
|
87 |
* imagen.
|
88 |
*/
|
89 |
public boolean mouseMoved(MouseEvent e) throws BehaviorException { |
90 |
|
91 |
ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
92 |
|
93 |
try{
|
94 |
lyr = grBehavior.getLayer(); |
95 |
if(lyr == null) { |
96 |
setActiveTool(false);
|
97 |
return false; |
98 |
} |
99 |
|
100 |
Point2D pto = vp.toMapPoint(e.getX(), e.getY());
|
101 |
|
102 |
try {
|
103 |
if( lyr.isInside(pto)){
|
104 |
grBehavior.getMapControl().setCursor(Toolkit.getDefaultToolkit().createCustomCursor(handCursor,
|
105 |
new Point(16, 16), "")); |
106 |
defaultCursorActive = false;
|
107 |
setActiveTool(true);
|
108 |
return true; |
109 |
}else{
|
110 |
if(!defaultCursorActive){
|
111 |
grBehavior.getMapControl().setCursor(defaultCursor); |
112 |
defaultCursorActive = true;
|
113 |
setActiveTool(false);
|
114 |
return false; |
115 |
} |
116 |
} |
117 |
} catch (HeadlessException e1) { |
118 |
e1.printStackTrace(); |
119 |
} catch (IndexOutOfBoundsException e1) { |
120 |
e1.printStackTrace(); |
121 |
} |
122 |
} catch(ClassCastException exc){ |
123 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), |
124 |
PluginServices.getText(this, "error_capa_puntos")); |
125 |
} |
126 |
setActiveTool(false);
|
127 |
return false; |
128 |
} |
129 |
|
130 |
/**
|
131 |
* Obtiene el cursor actual
|
132 |
*/
|
133 |
public Cursor getCursor() { |
134 |
return this.cur; |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Si no est? activo el cursor por defecto capturamos el punto
|
139 |
* seleccionado en coordenadas del mundo real.
|
140 |
*/
|
141 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
142 |
if(e.getButton() == MouseEvent.BUTTON1 && !defaultCursorActive) { |
143 |
setActiveTool(true);
|
144 |
lyr = grBehavior.getLayer(); |
145 |
|
146 |
if(lyr == null) |
147 |
return;
|
148 |
|
149 |
ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
150 |
ptoIni = vp.toMapPoint(e.getPoint()); |
151 |
|
152 |
isMoveable = true;
|
153 |
} |
154 |
} |
155 |
|
156 |
/**
|
157 |
* Cuando soltamos el bot?n del rat?n desplazamos la imagen a la posici?n
|
158 |
* de destino calculando el extent nuevamente.
|
159 |
*/
|
160 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
161 |
if(!isActiveTool())
|
162 |
return;
|
163 |
if(e.getButton() == MouseEvent.BUTTON1 && isMoveable){ |
164 |
|
165 |
FLyrRasterSE lyr = grBehavior.getLayer(); |
166 |
if(lyr == null) |
167 |
return;
|
168 |
|
169 |
ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
170 |
ptoFin = vp.toMapPoint(e.getPoint()); |
171 |
|
172 |
//Asignamos la nueva matriz de transformaci?n a la capa
|
173 |
AffineTransform atOld = lyr.getAffineTransform();
|
174 |
AffineTransform atNew = null; |
175 |
|
176 |
double distX = ptoFin.getX() - ptoIni.getX();
|
177 |
double distY = ptoFin.getY() - ptoIni.getY();
|
178 |
|
179 |
//La nueva matriz de transformaci?n es la vieja m?s la distancia desplazada
|
180 |
atNew = new AffineTransform(atOld.getScaleX(), atOld.getShearY(), |
181 |
atOld.getShearX(), atOld.getScaleY(), |
182 |
atOld.getTranslateX() + distX, |
183 |
atOld.getTranslateY() + distY); |
184 |
lyr.setAffineTransform(atNew); |
185 |
|
186 |
grBehavior.getMapControl().getMapContext().invalidate(); |
187 |
|
188 |
isMoveable = false;
|
189 |
} |
190 |
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* Funci?n de pintado del canvas.Pintamos un marco a la imagen para saber
|
195 |
* donde la movemos.
|
196 |
*/
|
197 |
public void paintComponent(Graphics g) { |
198 |
if(isMoveable && lyr != null){ |
199 |
BufferedImage img = grBehavior.getMapControl().getImage();
|
200 |
|
201 |
ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
202 |
|
203 |
//Obtenemos las esquinas del rectangulo sin rotar
|
204 |
Rectangle r = new Rectangle(); |
205 |
Point2D ul = vp.fromMapPoint(lyr.getMinX(), lyr.getMinY());
|
206 |
Point2D lr = vp.fromMapPoint(lyr.getMaxX(), lyr.getMaxY());
|
207 |
r.setFrameFromDiagonal(ul, lr); |
208 |
|
209 |
g.drawImage(img, 0, 0, null); |
210 |
g.setColor(Color.red);
|
211 |
AlphaComposite alpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.1f); |
212 |
((Graphics2D)g).setComposite(alpha);
|
213 |
|
214 |
try {
|
215 |
AffineTransform at = new AffineTransform(); |
216 |
at.setToShear( lyr.getAffineTransform().getShearX() / lyr.getAffineTransform().getScaleX(), |
217 |
lyr.getAffineTransform().getShearY() / lyr.getAffineTransform().getScaleY()); |
218 |
|
219 |
//Calculamos el punto inicial y final del desplazamiento. Hay que desrrotar estos para lograr un desplazamiento
|
220 |
//homogeneo independientemente de en que lado de la imagen hallamos pinchado. Si no hacemos esto el puntero del
|
221 |
//rat?n se mover? a distinta velocidad de la imagen y esta variar? dependiendo de en que parte se haya pinchado.
|
222 |
|
223 |
Point2D ini = vp.fromMapPoint(ptoIni.getX() , ptoIni.getY());
|
224 |
Point2D fin = vp.fromMapPoint(ptoFin.getX() , ptoFin.getY());
|
225 |
at.inverseTransform(ini, ini); |
226 |
at.inverseTransform(fin, fin); |
227 |
double distX = fin.getX() - ini.getX();
|
228 |
double distY = fin.getY() - ini.getY();
|
229 |
|
230 |
//Giramos el graphics se dibuja y se vuelve a dejar como estaba
|
231 |
((Graphics2D)g).transform(at);
|
232 |
Point2D pt = new Point2D.Double(r.x, r.y); |
233 |
at.inverseTransform(pt, pt); |
234 |
g.fillRect((int)pt.getX() + (int)distX, (int)pt.getY() + (int)distY, r.width, r.height); |
235 |
((Graphics2D)g).setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); |
236 |
g.drawRect((int)pt.getX() + (int)distX, (int)pt.getY() + (int)distY, r.width, r.height); |
237 |
((Graphics2D)g).transform(at.createInverse());
|
238 |
} catch (NoninvertibleTransformException e1) { |
239 |
e1.printStackTrace(); |
240 |
} |
241 |
} |
242 |
} |
243 |
|
244 |
/**
|
245 |
* Esta funci?n repinta el canvas si se est? arrasrtando la imagen para
|
246 |
* poder ver el marco de arrastre.
|
247 |
*/
|
248 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
249 |
if(!isActiveTool())
|
250 |
return;
|
251 |
if(isMoveable){
|
252 |
ViewPort vp = grBehavior.getMapControl().getMapContext().getViewPort(); |
253 |
ptoFin = vp.toMapPoint(e.getPoint()); |
254 |
grBehavior.getMapControl().repaint(); |
255 |
} |
256 |
} |
257 |
|
258 |
/* (non-Javadoc)
|
259 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
260 |
*/
|
261 |
public ToolListener getListener() {
|
262 |
return null; |
263 |
} |
264 |
|
265 |
|
266 |
} |