Statistics
| Revision:

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 @ 78

History | View | Annotate | Download (5.62 KB)

1
package org.gvsig.vectorediting.swing.impl;
2

    
3
import java.awt.Image;
4
import java.awt.event.MouseEvent;
5
import java.awt.event.MouseWheelEvent;
6

    
7
import org.gvsig.app.project.documents.view.toolListeners.PointSelectListener;
8
import org.gvsig.app.project.documents.view.toolListeners.StatusBarListener;
9
import org.gvsig.fmap.mapcontrol.MapControl;
10
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
11
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
12
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior;
13
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
14
import org.gvsig.fmap.mapcontrol.tools.Behavior.IBehavior;
15
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseMovementBehavior;
16
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseWheelBehavior;
17
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
18
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
19

    
20
public class EditingCompoundBehavior extends CompoundBehavior {
21

    
22
        private enum Mode {
23
                EDITING, SELECTION
24
        };
25

    
26
        private Mode mode = Mode.EDITING;
27
        private IBehavior editing;
28
        private IBehavior selection;
29
        private IBehavior zoom;
30

    
31
        public final static int EDITING_INDEX = 0;
32
        public final static int SELECTION_INDEX = 1;
33

    
34
        public EditingCompoundBehavior(IBehavior editing) {
35
                super(new Behavior[0]);
36
                this.editing = editing;
37

    
38
                this.selection = null;
39
                this.zoom = new MouseWheelBehavior();
40
        }
41

    
42
        public void addMapBehavior(Behavior mt, boolean draw) {
43
                if (mt instanceof MouseWheelBehavior) {
44
                        return;
45
                }
46
                throw new UnsupportedOperationException();
47
        }
48

    
49
        public void removeMapBehavior(Behavior mt) {
50
                throw new UnsupportedOperationException();
51
        }
52

    
53
        public boolean containsBehavior(Behavior mt) {
54
                return (mt == this.editing || mt == this.selection);
55
        }
56

    
57
        public Behavior getBehavior(int index) {
58
                switch (index) {
59
                case EDITING_INDEX:
60
                        return (Behavior) this.editing;
61
                case SELECTION_INDEX:
62
                        return (Behavior) this.selection;
63
                default:
64
                        throw new IndexOutOfBoundsException();
65
                }
66
        }
67

    
68
        public boolean isDrawnBehavior(int index) {
69
                switch (mode) {
70
                case EDITING:
71
                        return index == EDITING_INDEX;
72
                case SELECTION:
73
                        return index == SELECTION_INDEX;
74
                default:
75
                        return false;
76
                }
77
        }
78

    
79
        public void setDrawnBehavior(int index, boolean draw) {
80
                switch (index) {
81
                case EDITING_INDEX:
82
                        if (draw) {
83
                                mode = Mode.EDITING;
84
                        } else {
85
                                mode = Mode.SELECTION;
86
                        }
87
                        break;
88
                case SELECTION_INDEX:
89
                        if (draw) {
90
                                mode = Mode.SELECTION;
91
                        } else {
92
                                mode = Mode.EDITING;
93
                        }
94
                        break;
95
                default:
96
                        throw new IndexOutOfBoundsException();
97
                }
98
        }
99

    
100
        public int size() {
101
                return 2;
102
        }
103

    
104
        public Image getImageCursor() {
105
                switch (mode) {
106
                default:
107
                case EDITING:
108
                        return this.editing.getImageCursor();
109
                case SELECTION:
110
                        return this.selection.getImageCursor();
111
                }
112
        }
113

    
114
        public void mouseClicked(MouseEvent e) throws BehaviorException {
115
                switch (mode) {
116
                default:
117
                case EDITING:
118
                        this.editing.mouseClicked(e);
119
                        break;
120
                case SELECTION:
121
                        this.selection.mouseClicked(e);
122
                }
123
                this.zoom.mouseClicked(e);
124
        }
125

    
126
        public void mouseDragged(MouseEvent e) throws BehaviorException {
127
                switch (mode) {
128
                default:
129
                case EDITING:
130
                        this.editing.mouseDragged(e);
131
                        break;
132
                case SELECTION:
133
                        this.selection.mouseDragged(e);
134
                }
135
                this.zoom.mouseDragged(e);
136
        }
137

    
138
        public void mouseEntered(MouseEvent e) throws BehaviorException {
139
                switch (mode) {
140
                default:
141
                case EDITING:
142
                        this.editing.mouseEntered(e);
143
                        break;
144
                case SELECTION:
145
                        this.selection.mouseEntered(e);
146
                }
147
                this.zoom.mouseEntered(e);
148
        }
149

    
150
        public void mouseExited(MouseEvent e) throws BehaviorException {
151
                switch (mode) {
152
                default:
153
                case EDITING:
154
                        this.editing.mouseExited(e);
155
                        break;
156
                case SELECTION:
157
                        this.selection.mouseExited(e);
158
                }
159
                this.zoom.mouseExited(e);
160
        }
161

    
162
        public void mouseMoved(MouseEvent e) throws BehaviorException {
163
                switch (mode) {
164
                default:
165
                case EDITING:
166
                        this.editing.mouseMoved(e);
167
                        break;
168
                case SELECTION:
169
                        this.selection.mouseMoved(e);
170
                }
171
                this.zoom.mouseMoved(e);
172
        }
173

    
174
        public void mousePressed(MouseEvent e) throws BehaviorException {
175
                switch (mode) {
176
                default:
177
                case EDITING:
178
                        this.editing.mousePressed(e);
179
                        break;
180
                case SELECTION:
181
                        this.selection.mousePressed(e);
182
                }
183
                this.zoom.mousePressed(e);
184
        }
185

    
186
        public void mouseReleased(MouseEvent e) throws BehaviorException {
187
                switch (mode) {
188
                default:
189
                case EDITING:
190
                        this.editing.mouseReleased(e);
191
                        break;
192
                case SELECTION:
193
                        this.selection.mouseReleased(e);
194
                }
195
                this.zoom.mouseReleased(e);
196
        }
197

    
198
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
199
                switch (mode) {
200
                default:
201
                case EDITING:
202
                        this.editing.mouseWheelMoved(e);
203
                        break;
204
                case SELECTION:
205
                        this.selection.mouseWheelMoved(e);
206
                }
207
                this.zoom.mouseWheelMoved(e);
208
        }
209

    
210
        public void paintComponent(MapControlDrawer renderer) {
211
                switch (mode) {
212
                default:
213
                case EDITING:
214
                        this.editing.paintComponent(renderer);
215
                        break;
216
                case SELECTION:
217
                        this.selection.paintComponent(renderer);
218
                }
219
                this.zoom.paintComponent(renderer);
220
        }
221

    
222
        public void setListener(ToolListener listener) {
223
                if (listener != null) {
224
                        throw new UnsupportedOperationException(
225
                                        "CompoundBehavior does not have listeners");
226
                }
227
        }
228

    
229
        public ToolListener getListener() {
230
                return null;
231
        }
232

    
233
        public void setMapControl(MapControl mc) {
234
                this.editing.setMapControl(mc);
235
                this.zoom.setMapControl(mc);
236

    
237
                if (this.selection == null){
238
                        if (mc !=null) {
239
                                StatusBarListener sbl = new StatusBarListener(mc);
240
                                PointSelectListener psl = new PointSelectListener(mc);
241
                                this.selection = new CompoundBehavior(new Behavior[] {
242
                                                new PointBehavior(psl), new MouseMovementBehavior(sbl) });
243
                                this.selection.setMapControl(mc);
244
                        }
245
                } else {
246
                        this.selection.setMapControl(mc);
247
                }
248
        }
249

    
250
}