svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / RectangleBehavior.java @ 41971
History | View | Annotate | Download (6.28 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.Color; |
27 |
import java.awt.Point; |
28 |
import java.awt.Rectangle; |
29 |
import java.awt.event.MouseEvent; |
30 |
import java.awt.geom.Point2D; |
31 |
import java.awt.geom.Rectangle2D; |
32 |
import java.awt.image.BufferedImage; |
33 |
|
34 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
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.mapcontrol.MapControlDrawer; |
41 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
42 |
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
43 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
44 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
45 |
import org.slf4j.Logger; |
46 |
import org.slf4j.LoggerFactory; |
47 |
|
48 |
|
49 |
|
50 |
/**
|
51 |
* <p>Behavior that permits user to select a rectangular area on the associated <code>MapControl</code> using
|
52 |
* a {@link RectangleListener RectangleListener}.</p>
|
53 |
*
|
54 |
* @author Vicente Caballero Navarro
|
55 |
* @author Pablo Piqueras Bartolom?
|
56 |
*/
|
57 |
public class RectangleBehavior extends Behavior { |
58 |
private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
59 |
private static final Logger logger = LoggerFactory.getLogger(RectangleBehavior.class); |
60 |
|
61 |
/**
|
62 |
* First point of the rectangle area, that represents a corner.
|
63 |
*/
|
64 |
private Point2D m_FirstPoint; |
65 |
|
66 |
/**
|
67 |
* Second point of the rectangle area, that represents the opposite corner of the first,
|
68 |
* along the diagonal.
|
69 |
*/
|
70 |
private Point2D m_LastPoint; |
71 |
|
72 |
/**
|
73 |
* Tool listener used to work with the <code>MapControl</code> object.
|
74 |
*
|
75 |
* @see #getListener()
|
76 |
* @see #setListener(ToolListener)
|
77 |
*/
|
78 |
private RectangleListener listener;
|
79 |
|
80 |
/**
|
81 |
* <p>Creates a new behavior for selecting rectangle areas.</p>
|
82 |
*
|
83 |
* @param zili listener used to permit this object to work with the associated <code>MapControl</code>
|
84 |
*/
|
85 |
public RectangleBehavior(RectangleListener zili) {
|
86 |
listener = zili; |
87 |
} |
88 |
|
89 |
/*
|
90 |
* (non-Javadoc)
|
91 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
92 |
*/
|
93 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
94 |
BufferedImage img = getMapControl().getImage();
|
95 |
mapControlDrawer.drawImage(img, 0, 0); |
96 |
mapControlDrawer.setColor(Color.black);
|
97 |
|
98 |
// Borramos el anterior
|
99 |
Rectangle r = new Rectangle(); |
100 |
|
101 |
// Dibujamos el actual
|
102 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
103 |
r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint); |
104 |
mapControlDrawer.drawRect(r.x, r.y, r.width, r.height); |
105 |
} |
106 |
} |
107 |
|
108 |
/*
|
109 |
* (non-Javadoc)
|
110 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
111 |
*/
|
112 |
public void mousePressed(MouseEvent e) { |
113 |
if( !isMyButton(e) ) {
|
114 |
return;
|
115 |
} |
116 |
if (e.getButton() == MouseEvent.BUTTON1) { |
117 |
m_FirstPoint = e.getPoint(); |
118 |
getMapControl().repaint(); |
119 |
} |
120 |
if (listener.cancelDrawing()) {
|
121 |
getMapControl().cancelDrawing(); |
122 |
getMapControl().repaint(); |
123 |
} |
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
|
129 |
*/
|
130 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
131 |
if( !isMyButton(e) ) {
|
132 |
return;
|
133 |
} |
134 |
if (m_FirstPoint == null) return; |
135 |
Point2D p1;
|
136 |
Point2D p2;
|
137 |
Point pScreen = e.getPoint();
|
138 |
|
139 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
140 |
|
141 |
p1 = vp.toMapPoint(m_FirstPoint); |
142 |
p2 = vp.toMapPoint(pScreen); |
143 |
|
144 |
if (e.getButton() == MouseEvent.BUTTON1) { |
145 |
// Fijamos el nuevo extent
|
146 |
|
147 |
double x=0; |
148 |
double y=0; |
149 |
double x1=0; |
150 |
double y1=0; |
151 |
if (p1.getX()<p2.getX()){
|
152 |
x=p1.getX(); |
153 |
x1=p2.getX(); |
154 |
}else{
|
155 |
x=p2.getX(); |
156 |
x1=p1.getX(); |
157 |
} |
158 |
if (p1.getY()<p2.getY()){
|
159 |
y=p1.getY(); |
160 |
y1=p2.getY(); |
161 |
}else{
|
162 |
y=p2.getY(); |
163 |
y1=p1.getY(); |
164 |
} |
165 |
Envelope r; |
166 |
try {
|
167 |
r = geomManager.createEnvelope(x,y,x1,y1, SUBTYPES.GEOM2D); |
168 |
Rectangle2D rectPixel = new Rectangle(); |
169 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
170 |
|
171 |
EnvelopeEvent event = new EnvelopeEvent(r, e, rectPixel);
|
172 |
listener.rectangle(event); |
173 |
} catch (CreateEnvelopeException e1) {
|
174 |
logger.error("Error creating the envelope", e);
|
175 |
}//Rectangle2D.Double();
|
176 |
|
177 |
// r.setFrameFromDiagonal(p1, p2);
|
178 |
|
179 |
|
180 |
} |
181 |
|
182 |
m_FirstPoint = null;
|
183 |
m_LastPoint = null;
|
184 |
} |
185 |
|
186 |
/*
|
187 |
* (non-Javadoc)
|
188 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
189 |
*/
|
190 |
public void mouseDragged(MouseEvent e) { |
191 |
if( !isMyButton(e) ) {
|
192 |
return;
|
193 |
} |
194 |
m_LastPoint = e.getPoint(); |
195 |
getMapControl().repaint(); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
200 |
*
|
201 |
* @param listener a <code>RectangleListener</code> object for this behavior
|
202 |
*/
|
203 |
public void setListener(ToolListener listener) { |
204 |
this.listener = (RectangleListener) listener;
|
205 |
} |
206 |
|
207 |
/*
|
208 |
* (non-Javadoc)
|
209 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
210 |
*/
|
211 |
public ToolListener getListener() {
|
212 |
return listener;
|
213 |
} |
214 |
} |