gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / EditingCompoundBehavior.java @ 159
History | View | Annotate | Download (7.57 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 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 2
|
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 |
|
25 |
package org.gvsig.vectorediting.swing.impl; |
26 |
|
27 |
import java.awt.Image; |
28 |
import java.awt.event.MouseEvent; |
29 |
import java.awt.event.MouseWheelEvent; |
30 |
|
31 |
import org.gvsig.fmap.mapcontrol.MapControl; |
32 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
33 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
34 |
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior; |
35 |
import org.gvsig.fmap.mapcontrol.tools.PointSelectionListener; |
36 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
37 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.IBehavior; |
38 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseWheelBehavior; |
39 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
40 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
41 |
|
42 |
public class EditingCompoundBehavior extends CompoundBehavior { |
43 |
|
44 |
private enum Mode { |
45 |
EDITING, SELECTION |
46 |
}; |
47 |
|
48 |
private Mode mode = Mode.EDITING;
|
49 |
private IBehavior editing;
|
50 |
private IBehavior selection;
|
51 |
private IBehavior zoom;
|
52 |
|
53 |
public final static int EDITING_INDEX = 0; |
54 |
public final static int SELECTION_INDEX = 1; |
55 |
|
56 |
public EditingCompoundBehavior(IBehavior editing) {
|
57 |
super(new Behavior[0]); |
58 |
this.editing = editing;
|
59 |
|
60 |
this.selection = null; |
61 |
this.zoom = new MouseWheelBehavior(); |
62 |
} |
63 |
|
64 |
public void addMapBehavior(Behavior mt, boolean draw) { |
65 |
if (mt instanceof MouseWheelBehavior) { |
66 |
return;
|
67 |
} |
68 |
throw new UnsupportedOperationException(); |
69 |
} |
70 |
|
71 |
public void removeMapBehavior(Behavior mt) { |
72 |
throw new UnsupportedOperationException(); |
73 |
} |
74 |
|
75 |
public boolean containsBehavior(Behavior mt) { |
76 |
return (mt == this.editing || mt == this.selection); |
77 |
} |
78 |
|
79 |
public Behavior getBehavior(int index) { |
80 |
switch (index) {
|
81 |
case EDITING_INDEX:
|
82 |
return (Behavior) this.editing; |
83 |
case SELECTION_INDEX:
|
84 |
return (Behavior) this.selection; |
85 |
default:
|
86 |
throw new IndexOutOfBoundsException(); |
87 |
} |
88 |
} |
89 |
|
90 |
public boolean isDrawnBehavior(int index) { |
91 |
switch (mode) {
|
92 |
case EDITING:
|
93 |
return index == EDITING_INDEX;
|
94 |
case SELECTION:
|
95 |
return index == SELECTION_INDEX;
|
96 |
default:
|
97 |
return false; |
98 |
} |
99 |
} |
100 |
|
101 |
public void setDrawnBehavior(int index, boolean draw) { |
102 |
switch (index) {
|
103 |
case EDITING_INDEX:
|
104 |
if (draw) {
|
105 |
mode = Mode.EDITING; |
106 |
} else {
|
107 |
mode = Mode.SELECTION; |
108 |
} |
109 |
break;
|
110 |
case SELECTION_INDEX:
|
111 |
if (draw) {
|
112 |
mode = Mode.SELECTION; |
113 |
} else {
|
114 |
mode = Mode.EDITING; |
115 |
} |
116 |
break;
|
117 |
default:
|
118 |
throw new IndexOutOfBoundsException(); |
119 |
} |
120 |
} |
121 |
|
122 |
public int size() { |
123 |
return 2; |
124 |
} |
125 |
|
126 |
public Image getImageCursor() { |
127 |
switch (mode) {
|
128 |
default:
|
129 |
case EDITING:
|
130 |
return this.editing.getImageCursor(); |
131 |
case SELECTION:
|
132 |
return this.selection.getImageCursor(); |
133 |
} |
134 |
} |
135 |
|
136 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
137 |
switch (mode) {
|
138 |
default:
|
139 |
case EDITING:
|
140 |
this.editing.mouseClicked(e);
|
141 |
break;
|
142 |
case SELECTION:
|
143 |
this.selection.mouseClicked(e);
|
144 |
} |
145 |
this.zoom.mouseClicked(e);
|
146 |
} |
147 |
|
148 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
149 |
switch (mode) {
|
150 |
default:
|
151 |
case EDITING:
|
152 |
this.editing.mouseDragged(e);
|
153 |
break;
|
154 |
case SELECTION:
|
155 |
this.selection.mouseDragged(e);
|
156 |
} |
157 |
this.zoom.mouseDragged(e);
|
158 |
} |
159 |
|
160 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
161 |
switch (mode) {
|
162 |
default:
|
163 |
case EDITING:
|
164 |
this.editing.mouseEntered(e);
|
165 |
break;
|
166 |
case SELECTION:
|
167 |
this.selection.mouseEntered(e);
|
168 |
} |
169 |
this.zoom.mouseEntered(e);
|
170 |
} |
171 |
|
172 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
173 |
switch (mode) {
|
174 |
default:
|
175 |
case EDITING:
|
176 |
this.editing.mouseExited(e);
|
177 |
break;
|
178 |
case SELECTION:
|
179 |
this.selection.mouseExited(e);
|
180 |
} |
181 |
this.zoom.mouseExited(e);
|
182 |
} |
183 |
|
184 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
185 |
switch (mode) {
|
186 |
default:
|
187 |
case EDITING:
|
188 |
this.editing.mouseMoved(e);
|
189 |
break;
|
190 |
case SELECTION:
|
191 |
this.selection.mouseMoved(e);
|
192 |
} |
193 |
this.zoom.mouseMoved(e);
|
194 |
} |
195 |
|
196 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
197 |
switch (mode) {
|
198 |
default:
|
199 |
case EDITING:
|
200 |
this.editing.mousePressed(e);
|
201 |
break;
|
202 |
case SELECTION:
|
203 |
this.selection.mousePressed(e);
|
204 |
} |
205 |
this.zoom.mousePressed(e);
|
206 |
} |
207 |
|
208 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
209 |
switch (mode) {
|
210 |
default:
|
211 |
case EDITING:
|
212 |
this.editing.mouseReleased(e);
|
213 |
break;
|
214 |
case SELECTION:
|
215 |
this.selection.mouseReleased(e);
|
216 |
} |
217 |
this.zoom.mouseReleased(e);
|
218 |
} |
219 |
|
220 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
221 |
switch (mode) {
|
222 |
default:
|
223 |
case EDITING:
|
224 |
this.editing.mouseWheelMoved(e);
|
225 |
break;
|
226 |
case SELECTION:
|
227 |
this.selection.mouseWheelMoved(e);
|
228 |
} |
229 |
this.zoom.mouseWheelMoved(e);
|
230 |
} |
231 |
|
232 |
public void paintComponent(MapControlDrawer renderer) { |
233 |
switch (mode) {
|
234 |
default:
|
235 |
case EDITING:
|
236 |
this.editing.paintComponent(renderer);
|
237 |
break;
|
238 |
case SELECTION:
|
239 |
this.selection.paintComponent(renderer);
|
240 |
} |
241 |
this.zoom.paintComponent(renderer);
|
242 |
} |
243 |
|
244 |
public void setListener(ToolListener listener) { |
245 |
if (listener != null) { |
246 |
throw new UnsupportedOperationException( |
247 |
"CompoundBehavior does not have listeners");
|
248 |
} |
249 |
} |
250 |
|
251 |
public ToolListener getListener() {
|
252 |
return null; |
253 |
} |
254 |
|
255 |
public void setMapControl(MapControl mc) { |
256 |
this.editing.setMapControl(mc);
|
257 |
this.zoom.setMapControl(mc);
|
258 |
|
259 |
if (this.selection == null) { |
260 |
if (mc != null) { |
261 |
PointSelectionListener psl = new PointSelectionListener(mc);
|
262 |
this.selection =
|
263 |
new CompoundBehavior(
|
264 |
new Behavior[] { new PointBehavior(psl) }); |
265 |
this.selection.setMapControl(mc);
|
266 |
} |
267 |
} else {
|
268 |
this.selection.setMapControl(mc);
|
269 |
} |
270 |
} |
271 |
|
272 |
} |