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 @ 45680
History | View | Annotate | Download (5.03 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 40435 | jjdelcerro | * This program is free software; you can redistribute it and/or
|
7 | * modify it under the terms of the GNU General Public License
|
||
8 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * of the License, or (at your option) any later version.
|
10 | 40559 | jjdelcerro | *
|
11 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | *
|
16 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | 40435 | jjdelcerro | * MA 02110-1301, USA.
|
20 | 40559 | jjdelcerro | *
|
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 | 40435 | jjdelcerro | */
|
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 | 41971 | jjdelcerro | import org.slf4j.Logger; |
37 | import org.slf4j.LoggerFactory; |
||
38 | 40435 | jjdelcerro | |
39 | /**
|
||
40 | * <p>
|
||
41 | * Behavior that permits user to move the image of the associated
|
||
42 | * <code>MapControl</code> using a {@link PanListener PanListener}.
|
||
43 | * </p>
|
||
44 | 41867 | fdiaz | *
|
45 | 40435 | jjdelcerro | * @author Vicente Caballero Navarro
|
46 | * @author Pablo Piqueras Bartolom?
|
||
47 | */
|
||
48 | public class MoveBehavior extends Behavior { |
||
49 | 41971 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(MoveBehavior.class); |
50 | 40435 | jjdelcerro | /**
|
51 | * First point of the path in image coordinates.
|
||
52 | */
|
||
53 | 41867 | fdiaz | protected Point2D m_FirstPoint; |
54 | 40435 | jjdelcerro | |
55 | /**
|
||
56 | * Last point of the path in image coordinates.
|
||
57 | */
|
||
58 | 41867 | fdiaz | protected Point2D m_LastPoint; |
59 | 40435 | jjdelcerro | |
60 | /**
|
||
61 | * Tool listener used to work with the <code>MapControl</code> object.
|
||
62 | 41867 | fdiaz | *
|
63 | 40435 | jjdelcerro | * @see #getListener()
|
64 | * @see #setListener(ToolListener)
|
||
65 | */
|
||
66 | private PanListener listener;
|
||
67 | |||
68 | /**
|
||
69 | * <p>
|
||
70 | * Creates a new behavior for moving the mouse.
|
||
71 | * </p>
|
||
72 | 41867 | fdiaz | *
|
73 | 40435 | jjdelcerro | * @param pli
|
74 | * listener used to permit this object to work with the
|
||
75 | * associated <code>MapControl</code>
|
||
76 | 41972 | jjdelcerro | * @param mouseButton
|
77 | 40435 | jjdelcerro | */
|
78 | 41964 | jjdelcerro | public MoveBehavior(PanListener pli, int mouseButton) { |
79 | super(mouseButton);
|
||
80 | 40435 | jjdelcerro | listener = pli; |
81 | } |
||
82 | 42036 | fdiaz | |
83 | 41964 | jjdelcerro | public MoveBehavior(PanListener pli) {
|
84 | 41971 | jjdelcerro | this(pli,BUTTON_LEFT);
|
85 | 41964 | jjdelcerro | } |
86 | 40435 | jjdelcerro | |
87 | 41972 | jjdelcerro | @Override
|
88 | 40435 | jjdelcerro | public void paintComponent(MapControlDrawer renderer) { |
89 | 41974 | jjdelcerro | if( ! this.isMyButton() ) { |
90 | return;
|
||
91 | } |
||
92 | 41972 | jjdelcerro | super.paintComponent(renderer);
|
93 | 40435 | jjdelcerro | BufferedImage image = getMapControl().getImage();
|
94 | if (image != null) { |
||
95 | if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
||
96 | renderer.setComposite(AlphaComposite.getInstance(
|
||
97 | AlphaComposite.SRC_OVER, 0.5f)); |
||
98 | renderer.drawImage(image, |
||
99 | (int) (m_LastPoint.getX() - m_FirstPoint.getX()),
|
||
100 | (int) (m_LastPoint.getY() - m_FirstPoint.getY()));
|
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | 41972 | jjdelcerro | @Override
|
106 | 40435 | jjdelcerro | public void mousePressed(MouseEvent e) { |
107 | 41974 | jjdelcerro | if (this.isMyButton(e)) { |
108 | 40435 | jjdelcerro | m_FirstPoint = e.getPoint(); |
109 | 41971 | jjdelcerro | if (listener.cancelDrawing()) {
|
110 | getMapControl().cancelDrawing(); |
||
111 | } |
||
112 | 40435 | jjdelcerro | } |
113 | |||
114 | 41971 | jjdelcerro | |
115 | 40435 | jjdelcerro | } |
116 | |||
117 | 41972 | jjdelcerro | @Override
|
118 | 40435 | jjdelcerro | public void mouseReleased(MouseEvent e) throws BehaviorException { |
119 | 41974 | jjdelcerro | if (this.isMyButton(e) && m_FirstPoint != null) { |
120 | 41867 | fdiaz | doMouseReleased(e); |
121 | 42036 | fdiaz | this.resetMyButton();
|
122 | 40435 | jjdelcerro | } |
123 | 41867 | fdiaz | } |
124 | 40435 | jjdelcerro | |
125 | 41867 | fdiaz | protected void doMouseReleased(MouseEvent e) throws BehaviorException { |
126 | 45680 | fdiaz | // Point2D adjustedPoint = getMapControl().convertToMapPoint(e.getPoint(), this.getUseSnapping());
|
127 | MoveEvent event = new MoveEvent(m_FirstPoint, e.getPoint(), e); //adjustedPoint, e); |
||
128 | 41867 | fdiaz | listener.move(event); |
129 | |||
130 | 43616 | jjdelcerro | getMapControl().drawMap(false);
|
131 | 41867 | fdiaz | |
132 | 40435 | jjdelcerro | m_FirstPoint = null;
|
133 | } |
||
134 | |||
135 | 41867 | fdiaz | |
136 | |||
137 | 41972 | jjdelcerro | @Override
|
138 | 40435 | jjdelcerro | public void mouseDragged(MouseEvent e) { |
139 | 41974 | jjdelcerro | if (this.isMyButton(e)) { |
140 | 45680 | fdiaz | // Point2D adjustedPoint = getMapControl().convertToMapPoint(e.getPoint(), this.getUseSnapping());
|
141 | 40435 | jjdelcerro | m_LastPoint = e.getPoint(); |
142 | getMapControl().repaint(); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /**
|
||
147 | * <p>
|
||
148 | * Sets a tool listener to work with the <code>MapControl</code> using this
|
||
149 | * behavior.
|
||
150 | * </p>
|
||
151 | 41867 | fdiaz | *
|
152 | 40435 | jjdelcerro | * @param listener
|
153 | * a <code>PanListener</code> object for this behavior
|
||
154 | */
|
||
155 | public void setListener(ToolListener listener) { |
||
156 | this.listener = (PanListener) listener;
|
||
157 | } |
||
158 | |||
159 | public ToolListener getListener() {
|
||
160 | return listener;
|
||
161 | } |
||
162 | } |