svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / MoveBehavior.java @ 41964
History | View | Annotate | Download (4.48 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.mapcontrol.tools.Behavior; |
25 |
|
26 |
import java.awt.AlphaComposite; |
27 |
import java.awt.event.MouseEvent; |
28 |
import java.awt.geom.Point2D; |
29 |
import java.awt.image.BufferedImage; |
30 |
|
31 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
32 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
33 |
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent; |
34 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener; |
35 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
36 |
|
37 |
/**
|
38 |
* <p>
|
39 |
* Behavior that permits user to move the image of the associated
|
40 |
* <code>MapControl</code> using a {@link PanListener PanListener}.
|
41 |
* </p>
|
42 |
*
|
43 |
* @author Vicente Caballero Navarro
|
44 |
* @author Pablo Piqueras Bartolom?
|
45 |
*/
|
46 |
public class MoveBehavior extends Behavior { |
47 |
|
48 |
/**
|
49 |
* First point of the path in image coordinates.
|
50 |
*/
|
51 |
protected Point2D m_FirstPoint; |
52 |
|
53 |
/**
|
54 |
* Last point of the path in image coordinates.
|
55 |
*/
|
56 |
protected Point2D m_LastPoint; |
57 |
|
58 |
/**
|
59 |
* Tool listener used to work with the <code>MapControl</code> object.
|
60 |
*
|
61 |
* @see #getListener()
|
62 |
* @see #setListener(ToolListener)
|
63 |
*/
|
64 |
private PanListener listener;
|
65 |
private boolean isMyButton; |
66 |
|
67 |
/**
|
68 |
* <p>
|
69 |
* Creates a new behavior for moving the mouse.
|
70 |
* </p>
|
71 |
*
|
72 |
* @param pli
|
73 |
* listener used to permit this object to work with the
|
74 |
* associated <code>MapControl</code>
|
75 |
*/
|
76 |
public MoveBehavior(PanListener pli, int mouseButton) { |
77 |
super(mouseButton);
|
78 |
listener = pli; |
79 |
} |
80 |
|
81 |
public MoveBehavior(PanListener pli) {
|
82 |
this(pli,LEFT);
|
83 |
} |
84 |
|
85 |
public void paintComponent(MapControlDrawer renderer) { |
86 |
BufferedImage image = getMapControl().getImage();
|
87 |
if (image != null) { |
88 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
89 |
renderer.setComposite(AlphaComposite.getInstance(
|
90 |
AlphaComposite.SRC_OVER, 0.5f)); |
91 |
renderer.drawImage(image, |
92 |
(int) (m_LastPoint.getX() - m_FirstPoint.getX()),
|
93 |
(int) (m_LastPoint.getY() - m_FirstPoint.getY()));
|
94 |
} |
95 |
} |
96 |
} |
97 |
|
98 |
public void mousePressed(MouseEvent e) { |
99 |
if (isMyButton(e)) {
|
100 |
isMyButton = true;
|
101 |
m_FirstPoint = e.getPoint(); |
102 |
} |
103 |
|
104 |
if (listener.cancelDrawing()) {
|
105 |
getMapControl().cancelDrawing(); |
106 |
} |
107 |
} |
108 |
|
109 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
110 |
if (isMyButton(e) && m_FirstPoint != null) { |
111 |
doMouseReleased(e); |
112 |
} |
113 |
} |
114 |
|
115 |
protected void doMouseReleased(MouseEvent e) throws BehaviorException { |
116 |
MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e);
|
117 |
listener.move(event); |
118 |
|
119 |
getMapControl().drawMap(true);
|
120 |
|
121 |
m_FirstPoint = null;
|
122 |
isMyButton = false;
|
123 |
} |
124 |
|
125 |
|
126 |
|
127 |
public void mouseDragged(MouseEvent e) { |
128 |
if (isMyButton) {
|
129 |
m_LastPoint = e.getPoint(); |
130 |
getMapControl().repaint(); |
131 |
} |
132 |
} |
133 |
|
134 |
/**
|
135 |
* <p>
|
136 |
* Sets a tool listener to work with the <code>MapControl</code> using this
|
137 |
* behavior.
|
138 |
* </p>
|
139 |
*
|
140 |
* @param listener
|
141 |
* a <code>PanListener</code> object for this behavior
|
142 |
*/
|
143 |
public void setListener(ToolListener listener) { |
144 |
this.listener = (PanListener) listener;
|
145 |
} |
146 |
|
147 |
public ToolListener getListener() {
|
148 |
return listener;
|
149 |
} |
150 |
} |