root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / tools / Behavior / RectangleBehavior.java @ 20098
History | View | Annotate | Download (5.14 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 com.iver.cit.gvsig.fmap.tools.Behavior; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Graphics; |
45 |
import java.awt.Point; |
46 |
import java.awt.Rectangle; |
47 |
import java.awt.event.MouseEvent; |
48 |
import java.awt.geom.Point2D; |
49 |
import java.awt.geom.Rectangle2D; |
50 |
import java.awt.image.BufferedImage; |
51 |
|
52 |
import com.iver.cit.gvsig.fmap.ViewPort; |
53 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
54 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
55 |
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener; |
56 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
57 |
|
58 |
|
59 |
/**
|
60 |
* <p>Behavior that permits user to select a rectangular area on the associated <code>MapControl</code> using
|
61 |
* a {@link RectangleListener RectangleListener}.</p>
|
62 |
*
|
63 |
* @author Vicente Caballero Navarro
|
64 |
* @author Pablo Piqueras Bartolom?
|
65 |
*/
|
66 |
public class RectangleBehavior extends Behavior { |
67 |
/**
|
68 |
* First point of the rectangle area, that represents a corner.
|
69 |
*/
|
70 |
private Point2D m_FirstPoint; |
71 |
|
72 |
/**
|
73 |
* Second point of the rectangle area, that represents the opposite corner of the first,
|
74 |
* along the diagonal.
|
75 |
*/
|
76 |
private Point2D m_LastPoint; |
77 |
|
78 |
/**
|
79 |
* Tool listener used to work with the <code>MapControl</code> object.
|
80 |
*
|
81 |
* @see #getListener()
|
82 |
* @see #setListener(ToolListener)
|
83 |
*/
|
84 |
private RectangleListener listener;
|
85 |
|
86 |
/**
|
87 |
* <p>Creates a new behavior for selecting rectangle areas.</p>
|
88 |
*
|
89 |
* @param zili listener used to permit this object to work with the associated <code>MapControl</code>
|
90 |
*/
|
91 |
public RectangleBehavior(RectangleListener zili) {
|
92 |
listener = zili; |
93 |
} |
94 |
|
95 |
/*
|
96 |
* (non-Javadoc)
|
97 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
98 |
*/
|
99 |
public void paintComponent(Graphics g) { |
100 |
BufferedImage img = getMapControl().getImage();
|
101 |
g.drawImage(img, 0, 0, null); |
102 |
g.setColor(Color.black);
|
103 |
g.setXORMode(Color.white);
|
104 |
|
105 |
// Borramos el anterior
|
106 |
Rectangle r = new Rectangle(); |
107 |
|
108 |
// Dibujamos el actual
|
109 |
if ((m_FirstPoint != null) && (m_LastPoint != null)) { |
110 |
r.setFrameFromDiagonal(m_FirstPoint, m_LastPoint); |
111 |
g.drawRect(r.x, r.y, r.width, r.height); |
112 |
} |
113 |
g.setPaintMode(); |
114 |
} |
115 |
|
116 |
/*
|
117 |
* (non-Javadoc)
|
118 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
|
119 |
*/
|
120 |
public void mousePressed(MouseEvent e) { |
121 |
if (e.getButton() == MouseEvent.BUTTON1) { |
122 |
m_FirstPoint = e.getPoint(); |
123 |
getMapControl().repaint(); |
124 |
} |
125 |
if (listener.cancelDrawing()) {
|
126 |
getMapControl().cancelDrawing(); |
127 |
getMapControl().repaint(); |
128 |
} |
129 |
} |
130 |
|
131 |
/*
|
132 |
* (non-Javadoc)
|
133 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
|
134 |
*/
|
135 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
136 |
if (m_FirstPoint == null) return; |
137 |
Point2D p1;
|
138 |
Point2D p2;
|
139 |
Point pScreen = e.getPoint();
|
140 |
|
141 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
142 |
|
143 |
p1 = vp.toMapPoint(m_FirstPoint); |
144 |
p2 = vp.toMapPoint(pScreen); |
145 |
|
146 |
if (e.getButton() == MouseEvent.BUTTON1) { |
147 |
// Fijamos el nuevo extent
|
148 |
Rectangle2D.Double r = new Rectangle2D.Double(); |
149 |
r.setFrameFromDiagonal(p1, p2); |
150 |
|
151 |
Rectangle2D rectPixel = new Rectangle(); |
152 |
rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen); |
153 |
|
154 |
RectangleEvent event = new RectangleEvent(r, e, rectPixel);
|
155 |
listener.rectangle(event); |
156 |
} |
157 |
|
158 |
m_FirstPoint = null;
|
159 |
m_LastPoint = null;
|
160 |
} |
161 |
|
162 |
/*
|
163 |
* (non-Javadoc)
|
164 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
|
165 |
*/
|
166 |
public void mouseDragged(MouseEvent e) { |
167 |
m_LastPoint = e.getPoint(); |
168 |
getMapControl().repaint(); |
169 |
} |
170 |
|
171 |
/**
|
172 |
* <p>Sets a tool listener to work with the <code>MapControl</code> using this behavior.</p>
|
173 |
*
|
174 |
* @param listener a <code>RectangleListener</code> object for this behavior
|
175 |
*/
|
176 |
public void setListener(ToolListener listener) { |
177 |
this.listener = (RectangleListener) listener;
|
178 |
} |
179 |
|
180 |
/*
|
181 |
* (non-Javadoc)
|
182 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
183 |
*/
|
184 |
public ToolListener getListener() {
|
185 |
return listener;
|
186 |
} |
187 |
} |