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