Revision 159 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

View differences:

EditingCompoundBehavior.java
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

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

  
3 27
import java.awt.Image;
......
11 35
import org.gvsig.fmap.mapcontrol.tools.PointSelectionListener;
12 36
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
13 37
import org.gvsig.fmap.mapcontrol.tools.Behavior.IBehavior;
14
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseMovementBehavior;
15 38
import org.gvsig.fmap.mapcontrol.tools.Behavior.MouseWheelBehavior;
16 39
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
17 40
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
18 41

  
19 42
public class EditingCompoundBehavior extends CompoundBehavior {
20 43

  
21
	private enum Mode {
22
		EDITING, SELECTION
23
	};
44
    private enum Mode {
45
        EDITING, SELECTION
46
    };
24 47

  
25
	private Mode mode = Mode.EDITING;
26
	private IBehavior editing;
27
	private IBehavior selection;
28
	private IBehavior zoom;
48
    private Mode mode = Mode.EDITING;
49
    private IBehavior editing;
50
    private IBehavior selection;
51
    private IBehavior zoom;
29 52

  
30
	public final static int EDITING_INDEX = 0;
31
	public final static int SELECTION_INDEX = 1;
53
    public final static int EDITING_INDEX = 0;
54
    public final static int SELECTION_INDEX = 1;
32 55

  
33
	public EditingCompoundBehavior(IBehavior editing) {
34
		super(new Behavior[0]);
35
		this.editing = editing;
56
    public EditingCompoundBehavior(IBehavior editing) {
57
        super(new Behavior[0]);
58
        this.editing = editing;
36 59

  
37
		this.selection = null;
38
		this.zoom = new MouseWheelBehavior();
39
	}
60
        this.selection = null;
61
        this.zoom = new MouseWheelBehavior();
62
    }
40 63

  
41
	public void addMapBehavior(Behavior mt, boolean draw) {
42
		if (mt instanceof MouseWheelBehavior) {
43
			return;
44
		}
45
		throw new UnsupportedOperationException();
46
	}
64
    public void addMapBehavior(Behavior mt, boolean draw) {
65
        if (mt instanceof MouseWheelBehavior) {
66
            return;
67
        }
68
        throw new UnsupportedOperationException();
69
    }
47 70

  
48
	public void removeMapBehavior(Behavior mt) {
49
		throw new UnsupportedOperationException();
50
	}
71
    public void removeMapBehavior(Behavior mt) {
72
        throw new UnsupportedOperationException();
73
    }
51 74

  
52
	public boolean containsBehavior(Behavior mt) {
53
		return (mt == this.editing || mt == this.selection);
54
	}
75
    public boolean containsBehavior(Behavior mt) {
76
        return (mt == this.editing || mt == this.selection);
77
    }
55 78

  
56
	public Behavior getBehavior(int index) {
57
		switch (index) {
58
		case EDITING_INDEX:
59
			return (Behavior) this.editing;
60
		case SELECTION_INDEX:
61
			return (Behavior) this.selection;
62
		default:
63
			throw new IndexOutOfBoundsException();
64
		}
65
	}
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
    }
66 89

  
67
	public boolean isDrawnBehavior(int index) {
68
		switch (mode) {
69
		case EDITING:
70
			return index == EDITING_INDEX;
71
		case SELECTION:
72
			return index == SELECTION_INDEX;
73
		default:
74
			return false;
75
		}
76
	}
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
    }
77 100

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

  
99
	public int size() {
100
		return 2;
101
	}
122
    public int size() {
123
        return 2;
124
    }
102 125

  
103
	public Image getImageCursor() {
104
		switch (mode) {
105
		default:
106
		case EDITING:
107
			return this.editing.getImageCursor();
108
		case SELECTION:
109
			return this.selection.getImageCursor();
110
		}
111
	}
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
    }
112 135

  
113
	public void mouseClicked(MouseEvent e) throws BehaviorException {
114
		switch (mode) {
115
		default:
116
		case EDITING:
117
			this.editing.mouseClicked(e);
118
			break;
119
		case SELECTION:
120
			this.selection.mouseClicked(e);
121
		}
122
		this.zoom.mouseClicked(e);
123
	}
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
    }
124 147

  
125
	public void mouseDragged(MouseEvent e) throws BehaviorException {
126
		switch (mode) {
127
		default:
128
		case EDITING:
129
			this.editing.mouseDragged(e);
130
			break;
131
		case SELECTION:
132
			this.selection.mouseDragged(e);
133
		}
134
		this.zoom.mouseDragged(e);
135
	}
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
    }
136 159

  
137
	public void mouseEntered(MouseEvent e) throws BehaviorException {
138
		switch (mode) {
139
		default:
140
		case EDITING:
141
			this.editing.mouseEntered(e);
142
			break;
143
		case SELECTION:
144
			this.selection.mouseEntered(e);
145
		}
146
		this.zoom.mouseEntered(e);
147
	}
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
    }
148 171

  
149
	public void mouseExited(MouseEvent e) throws BehaviorException {
150
		switch (mode) {
151
		default:
152
		case EDITING:
153
			this.editing.mouseExited(e);
154
			break;
155
		case SELECTION:
156
			this.selection.mouseExited(e);
157
		}
158
		this.zoom.mouseExited(e);
159
	}
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
    }
160 183

  
161
	public void mouseMoved(MouseEvent e) throws BehaviorException {
162
		switch (mode) {
163
		default:
164
		case EDITING:
165
			this.editing.mouseMoved(e);
166
			break;
167
		case SELECTION:
168
			this.selection.mouseMoved(e);
169
		}
170
		this.zoom.mouseMoved(e);
171
	}
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
    }
172 195

  
173
	public void mousePressed(MouseEvent e) throws BehaviorException {
174
		switch (mode) {
175
		default:
176
		case EDITING:
177
			this.editing.mousePressed(e);
178
			break;
179
		case SELECTION:
180
			this.selection.mousePressed(e);
181
		}
182
		this.zoom.mousePressed(e);
183
	}
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
    }
184 207

  
185
	public void mouseReleased(MouseEvent e) throws BehaviorException {
186
		switch (mode) {
187
		default:
188
		case EDITING:
189
			this.editing.mouseReleased(e);
190
			break;
191
		case SELECTION:
192
			this.selection.mouseReleased(e);
193
		}
194
		this.zoom.mouseReleased(e);
195
	}
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
    }
196 219

  
197
	public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
198
		switch (mode) {
199
		default:
200
		case EDITING:
201
			this.editing.mouseWheelMoved(e);
202
			break;
203
		case SELECTION:
204
			this.selection.mouseWheelMoved(e);
205
		}
206
		this.zoom.mouseWheelMoved(e);
207
	}
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
    }
208 231

  
209
	public void paintComponent(MapControlDrawer renderer) {
210
		switch (mode) {
211
		default:
212
		case EDITING:
213
			this.editing.paintComponent(renderer);
214
			break;
215
		case SELECTION:
216
			this.selection.paintComponent(renderer);
217
		}
218
		this.zoom.paintComponent(renderer);
219
	}
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
    }
220 243

  
221
	public void setListener(ToolListener listener) {
222
		if (listener != null) {
223
			throw new UnsupportedOperationException(
224
					"CompoundBehavior does not have listeners");
225
		}
226
	}
244
    public void setListener(ToolListener listener) {
245
        if (listener != null) {
246
            throw new UnsupportedOperationException(
247
                "CompoundBehavior does not have listeners");
248
        }
249
    }
227 250

  
228
	public ToolListener getListener() {
229
		return null;
230
	}
251
    public ToolListener getListener() {
252
        return null;
253
    }
231 254

  
232
	public void setMapControl(MapControl mc) {
233
		this.editing.setMapControl(mc);
234
		this.zoom.setMapControl(mc);
255
    public void setMapControl(MapControl mc) {
256
        this.editing.setMapControl(mc);
257
        this.zoom.setMapControl(mc);
235 258

  
236
		if (this.selection == null){
237
			if (mc !=null) {
238
				PointSelectionListener psl = new PointSelectionListener(mc);
239
				this.selection = new CompoundBehavior(new Behavior[] {
240
						new PointBehavior(psl) });
241
				this.selection.setMapControl(mc);
242
			}
243
		} else {
244
			this.selection.setMapControl(mc);
245
		}
246
	}
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
    }
247 271

  
248 272
}

Also available in: Unified diff