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

History | View | Annotate | Download (7.69 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

    
52
    public final static int EDITING_INDEX = 0;
53
    public final static int SELECTION_INDEX = 1;
54

    
55
    public EditingCompoundBehavior(IBehavior editing) {
56
        super(new Behavior[0]);
57
        this.editing = editing;
58

    
59
        this.selection = null;
60
    }
61

    
62
    @Override
63
    public void addMapBehavior(Behavior mt, boolean draw) {
64
        if (mt instanceof MouseWheelBehavior) {
65
            return;
66
        }
67
        throw new UnsupportedOperationException();
68
    }
69

    
70
    @Override
71
    public void removeMapBehavior(Behavior mt) {
72
        throw new UnsupportedOperationException();
73
    }
74

    
75
    @Override
76
    public boolean containsBehavior(Behavior mt) {
77
        return ((mt == this.editing) || (mt == this.selection));
78
    }
79

    
80
    @Override
81
    public Behavior getBehavior(int index) {
82
        switch (index) {
83
        case EDITING_INDEX:
84
            return (Behavior) this.editing;
85
        case SELECTION_INDEX:
86
            return (Behavior) this.selection;
87
        default:
88
            throw new IndexOutOfBoundsException();
89
        }
90
    }
91

    
92
    @Override
93
    public boolean isDrawnBehavior(int index) {
94
        switch (mode) {
95
        case EDITING:
96
            return index == EDITING_INDEX;
97
        case SELECTION:
98
            return index == SELECTION_INDEX;
99
        default:
100
            return false;
101
        }
102
    }
103

    
104
    @Override
105
    public void setDrawnBehavior(int index, boolean draw) {
106
        switch (index) {
107
        case EDITING_INDEX:
108
            if (draw) {
109
                mode = Mode.EDITING;
110
            } else {
111
                mode = Mode.SELECTION;
112
            }
113
            break;
114
        case SELECTION_INDEX:
115
            if (draw) {
116
                mode = Mode.SELECTION;
117
            } else {
118
                mode = Mode.EDITING;
119
            }
120
            break;
121
        default:
122
            throw new IndexOutOfBoundsException();
123
        }
124
    }
125

    
126
    @Override
127
    public int size() {
128
        return 2;
129
    }
130

    
131
    @Override
132
    public Image getImageCursor() {
133
        switch (mode) {
134
        default:
135
        case EDITING:
136
            return this.editing.getImageCursor();
137
        case SELECTION:
138
            return this.selection.getImageCursor();
139
        }
140
    }
141

    
142
    @Override
143
    public void mouseClicked(MouseEvent e) throws BehaviorException {
144
        switch (mode) {
145
        default:
146
        case EDITING:
147
            this.editing.mouseClicked(e);
148
            break;
149
        case SELECTION:
150
            this.selection.mouseClicked(e);
151
        }
152
    }
153

    
154
    @Override
155
    public void mouseDragged(MouseEvent e) throws BehaviorException {
156
        switch (mode) {
157
        default:
158
        case EDITING:
159
            this.editing.mouseDragged(e);
160
            break;
161
        case SELECTION:
162
            this.selection.mouseDragged(e);
163
        }
164
    }
165

    
166
    @Override
167
    public void mouseEntered(MouseEvent e) throws BehaviorException {
168
        switch (mode) {
169
        default:
170
        case EDITING:
171
            this.editing.mouseEntered(e);
172
            break;
173
        case SELECTION:
174
            this.selection.mouseEntered(e);
175
        }
176
    }
177

    
178
    @Override
179
    public void mouseExited(MouseEvent e) throws BehaviorException {
180
        switch (mode) {
181
        default:
182
        case EDITING:
183
            this.editing.mouseExited(e);
184
            break;
185
        case SELECTION:
186
            this.selection.mouseExited(e);
187
        }
188
    }
189

    
190
    @Override
191
    public void mouseMoved(MouseEvent e) throws BehaviorException {
192
        switch (mode) {
193
        default:
194
        case EDITING:
195
            this.editing.mouseMoved(e);
196
            break;
197
        case SELECTION:
198
            this.selection.mouseMoved(e);
199
        }
200
    }
201

    
202
    @Override
203
    public void mousePressed(MouseEvent e) throws BehaviorException {
204
        switch (mode) {
205
        default:
206
        case EDITING:
207
            this.editing.mousePressed(e);
208
            break;
209
        case SELECTION:
210
            this.selection.mousePressed(e);
211
        }
212
    }
213

    
214
    @Override
215
    public void mouseReleased(MouseEvent e) throws BehaviorException {
216
        switch (mode) {
217
        default:
218
        case EDITING:
219
            this.editing.mouseReleased(e);
220
            break;
221
        case SELECTION:
222
            this.selection.mouseReleased(e);
223
        }
224
    }
225

    
226
    @Override
227
    public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
228
        switch (mode) {
229
        default:
230
        case EDITING:
231
            this.editing.mouseWheelMoved(e);
232
            break;
233
        case SELECTION:
234
            this.selection.mouseWheelMoved(e);
235
        }
236
    }
237

    
238

    
239
    //FIXME: Tras el primer build de gvSIG 2.2 descomentarizar las siguientes l?neas
240
//    public void paintComponent(MapControlDrawer renderer, boolean clean) {
241
//        if(clean){
242
//            clean(renderer);
243
//        }
244
//        paintComponent(renderer);
245
//    }
246

    
247
    @Override
248
    public void paintComponent(MapControlDrawer renderer) {
249
        switch (mode) {
250
        default:
251
        case EDITING:
252
            this.editing.paintComponent(renderer);
253
            break;
254
        case SELECTION:
255
            this.selection.paintComponent(renderer);
256
        }
257
    }
258

    
259
    @Override
260
    public void setListener(ToolListener listener) {
261
        if (listener != null) {
262
            throw new UnsupportedOperationException(
263
                "CompoundBehavior does not have listeners");
264
        }
265
    }
266

    
267
    @Override
268
    public ToolListener getListener() {
269
        return null;
270
    }
271

    
272
    @Override
273
    public void setMapControl(MapControl mc) {
274
        this.editing.setMapControl(mc);
275

    
276
        if (this.selection == null) {
277
            if (mc != null) {
278
                PointSelectionListener psl = new PointSelectionListener(mc);
279
                this.selection =
280
                    new CompoundBehavior(
281
                        new Behavior[] { new PointBehavior(psl) });
282
                this.selection.setMapControl(mc);
283
            }
284
        } else {
285
            this.selection.setMapControl(mc);
286
        }
287
    }
288

    
289
}