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 @ 44004
History | View | Annotate | Download (6.25 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 |
mapControlDrawer.setColor(Color.black);
|
95 |
|
96 |
// Borramos el anterior
|
97 |
Rectangle r = new Rectangle(); |
98 |
|
99 |
// Dibujamos el actual
|
100 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
101 |
r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint); |
102 |
mapControlDrawer.drawRect(r.x, r.y, r.width, r.height); |
103 |
} |
104 |
} |
105 |
|
106 |
/*
|
107 |
* (non-Javadoc)
|
108 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
109 |
*/
|
110 |
public void mousePressed(MouseEvent e) { |
111 |
if( !isMyButton(e) ) {
|
112 |
return;
|
113 |
} |
114 |
if (e.getButton() == MouseEvent.BUTTON1) { |
115 |
m_FirstPoint = e.getPoint(); |
116 |
getMapControl().repaint(); |
117 |
} |
118 |
if (listener.cancelDrawing()) {
|
119 |
getMapControl().cancelDrawing(); |
120 |
getMapControl().repaint(); |
121 |
} |
122 |
} |
123 |
|
124 |
/*
|
125 |
* (non-Javadoc)
|
126 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
|
127 |
*/
|
128 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
129 |
if( !isMyButton(e) ) {
|
130 |
return;
|
131 |
} |
132 |
if (m_FirstPoint == null) return; |
133 |
Point2D p1;
|
134 |
Point2D p2;
|
135 |
Point pScreen = e.getPoint();
|
136 |
|
137 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
138 |
|
139 |
p1 = vp.toMapPoint(m_FirstPoint); |
140 |
p2 = vp.toMapPoint(pScreen); |
141 |
|
142 |
if (e.getButton() == MouseEvent.BUTTON1) { |
143 |
// Fijamos el nuevo extent
|
144 |
|
145 |
double x=0; |
146 |
double y=0; |
147 |
double x1=0; |
148 |
double y1=0; |
149 |
if (p1.getX()<p2.getX()){
|
150 |
x=p1.getX(); |
151 |
x1=p2.getX(); |
152 |
}else{
|
153 |
x=p2.getX(); |
154 |
x1=p1.getX(); |
155 |
} |
156 |
if (p1.getY()<p2.getY()){
|
157 |
y=p1.getY(); |
158 |
y1=p2.getY(); |
159 |
}else{
|
160 |
y=p2.getY(); |
161 |
y1=p1.getY(); |
162 |
} |
163 |
Envelope r; |
164 |
try {
|
165 |
r = geomManager.createEnvelope(x,y,x1,y1, SUBTYPES.GEOM2D); |
166 |
r.setProjection(vp.getProjection()); |
167 |
Rectangle2D rectPixel = new Rectangle(); |
168 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
169 |
|
170 |
EnvelopeEvent event = new EnvelopeEvent(r, e, rectPixel);
|
171 |
listener.rectangle(event); |
172 |
} catch (CreateEnvelopeException e1) {
|
173 |
logger.error("Error creating the envelope", e);
|
174 |
}//Rectangle2D.Double();
|
175 |
|
176 |
// r.setFrameFromDiagonal(p1, p2);
|
177 |
|
178 |
|
179 |
} |
180 |
|
181 |
m_FirstPoint = null;
|
182 |
m_LastPoint = null;
|
183 |
} |
184 |
|
185 |
/*
|
186 |
* (non-Javadoc)
|
187 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
188 |
*/
|
189 |
public void mouseDragged(MouseEvent e) { |
190 |
if( !isMyButton(e) ) {
|
191 |
return;
|
192 |
} |
193 |
m_LastPoint = e.getPoint(); |
194 |
getMapControl().repaint(); |
195 |
} |
196 |
|
197 |
/**
|
198 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
199 |
*
|
200 |
* @param listener a <code>RectangleListener</code> object for this behavior
|
201 |
*/
|
202 |
public void setListener(ToolListener listener) { |
203 |
this.listener = (RectangleListener) listener;
|
204 |
} |
205 |
|
206 |
/*
|
207 |
* (non-Javadoc)
|
208 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
209 |
*/
|
210 |
public ToolListener getListener() {
|
211 |
return listener;
|
212 |
} |
213 |
} |