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