Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / CompoundBehavior.java @ 38604

History | View | Annotate | Download (11.4 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 org.gvsig.fmap.mapcontrol.tools;
42

    
43
import java.awt.Image;
44
import java.awt.event.MouseEvent;
45
import java.awt.event.MouseWheelEvent;
46
import java.util.ArrayList;
47

    
48
import org.gvsig.fmap.mapcontrol.MapControl;
49
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
50
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
51
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
52

    
53

    
54

    
55
/**
56
 * <p>Allows having multiple behaviors when user works with the associated
57
 *  <code>MapControl</code>. Each one with its associated tool listener.</p>
58
 *
59
 * @author Fernando Gonz?lez Cort?s
60
 * @author Pablo Piqueras Bartolom?
61
 */
62
public class CompoundBehavior extends Behavior {
63
        /**
64
         * List of all behaviors that compound this one.
65
         */
66
        private ArrayList behaviors = new ArrayList(); //<Behavior>
67

    
68
        /**
69
         * List that determines which behaviors of this one will be real-time painted.
70
         */
71
        private ArrayList draws = new ArrayList(); //<Boolean>
72

    
73
        /**
74
         * Static <code>behavior</code> that will be used for any <code>MapControl</code>.</p>
75
         */
76
        private static Behavior behavior = null;
77

    
78
        /**
79
          * <p>Creates a new behavior as a composition of others.</p>
80
          *
81
         * @param tools atomic behaviors that will compound this one</code>
82
         */
83
        public CompoundBehavior(Behavior[] behaviors){
84
            
85
        for (int i = 0; i < behaviors.length; i++) {
86
            if (behaviors[i] == null) {
87
                throw new IllegalArgumentException("behaviors");
88
            }
89
        }
90

    
91
                for (int i = 0; i < behaviors.length; i++) {
92
                    
93
                        this.behaviors.add(behaviors[i]);
94

    
95
                        if (i == 0)
96
                                draws.add(Boolean.TRUE);
97
                        else
98
                                draws.add(Boolean.FALSE);
99
                }
100
        }
101

    
102
        /**
103
         * <p>Adds a new behavior, setting if will be real-time (when user is working with it) drawn or not.</p>
104
         *
105
         * <p>When user works with a compound behavior, he/she will see on real-time the graphical changes produced at
106
         *  the associated <code>MapControl</code>, only by those which have their associated <i>draw</i> flag to <code>true</code>.</p>
107
         *
108
         * @param mt the new behavior
109
         * @param draw flag determining if will be real-time drawn or no
110
         */
111
        public void addMapBehavior(Behavior mt, boolean draw){
112
            
113
            if (mt == null) {
114
                throw new IllegalArgumentException("mt");
115
            }
116
            
117
                behaviors.add(mt);
118
                draws.add(new Boolean(draw));
119
        }
120

    
121
        /**
122
         * <p>Removes a <code>Behavior</code> that composes this one.</p>
123
         *
124
         * @param mt the <code>Behavior</code> to be removed
125
         */
126
        public void removeMapBehavior(Behavior mt){
127
                int index = behaviors.indexOf(mt);
128

    
129
                if (index >= 0) {
130
                        behaviors.remove(index);
131
                        draws.remove(index);
132
                }
133
        }
134
        /**
135
         * <p>Searches for <code>mt</code>, returning <code>true</code> if is contained.</p>
136
         *
137
         * @param mt the behavior to search
138
         *
139
         * @return <code>true</code> if is contained; otherwise <code>false</code>
140
         */
141
        public boolean containsBehavior(Behavior mt) {
142
                return behaviors.indexOf(mt) > -1;
143
        }
144

    
145
        /**
146
         * <p>Returns the first-level {@link Behavior Behavior} at the specified position.</p>
147
         *
148
         * @param index index of element to return
149
         *
150
         * @return the element at the specified position.
151
         */
152
        public Behavior getBehavior(int index) {
153
                return (Behavior)behaviors.get(index);
154
        }
155

    
156
        /**
157
         * <p>Returns if it's invoked the method <code>public void paintComponent(Graphics g)</code> of the
158
         *  first-level {@link Behavior Behavior} at the specified position, each time is painted this component.</p>
159
         *
160
         * @param index index of element
161
         *
162
         * @return <code>true</code>  if it's invoked the method <code>public void paintComponent(Graphics g)</code> of the
163
         *   first-level {@link Behavior Behavior} at the specified position, each time is painted this component, otherwise
164
         *   <code>false</code>.
165
         */
166
        public boolean isDrawnBehavior(int index) {
167
                return ((Boolean)draws.get(index)).booleanValue();
168
        }
169

    
170
        /**
171
         * <p>Sets if will be invoked the method <code>public void paintComponent(Graphics g)</code> of the
172
         *  first-level {@link Behavior Behavior} at the specified position, each time is painted this component.</p>
173
         *
174
         * @param index index of element
175
         * @param <code>true</code> if will be invoked the method <code>public void paintComponent(Graphics g)</code> of the
176
         *   first-level {@link Behavior Behavior} at the specified position, each time is painted this component, otherwise
177
         *   <code>false</code>.
178
         */
179
        public void setDrawnBehavior(int index, boolean draw) {
180
                draws.set(index, new Boolean(draw));
181
        }
182

    
183
        /**
184
         * <p>Returns the number of first-level {@link Behavior Behavior}s in this <code>CompoundBehavior</code>.</p>
185
         *
186
         * @return the number of first-level {@link Behavior Behavior}s in this <code>CompoundBehavior</code>
187
         */
188
        public int size() {
189
                return behaviors.size();
190
        }
191

    
192
        /*
193
         * (non-Javadoc)
194
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getCursor()
195
         */
196
        public Image getImageCursor() {
197
                if (behaviors.size() > 0) {
198
                    Behavior beh = (Behavior) behaviors.get(0);
199
                        return beh.getImageCursor();
200
                } else {
201
                        return null;
202
                }
203
        }
204

    
205
        /*
206
         * (non-Javadoc)
207
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseClicked(java.awt.event.MouseEvent)
208
         */
209
        public void mouseClicked(MouseEvent e) throws BehaviorException {
210
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
211
                for (int i=0;i<behaviors.length;i++ ) {
212
                        behaviors[i].mouseClicked(e);
213
                }
214

    
215
                if(behavior != null)
216
                        behavior.mouseClicked(e);
217
        }
218

    
219
        /*
220
         * (non-Javadoc)
221
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseDragged(java.awt.event.MouseEvent)
222
         */
223
        public void mouseDragged(MouseEvent e) throws BehaviorException {
224
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
225
                for (int i=0;i<behaviors.length;i++ ) {
226
                        behaviors[i].mouseDragged(e);
227
                }
228

    
229
                if(behavior != null)
230
                        behavior.mouseDragged(e);
231
        }
232

    
233
        /*
234
         * (non-Javadoc)
235
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseEntered(java.awt.event.MouseEvent)
236
         */
237
        public void mouseEntered(MouseEvent e) throws BehaviorException {
238
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
239
                for (int i=0;i<behaviors.length;i++ ) {
240
                        behaviors[i].mouseEntered(e);
241
                }
242

    
243
                if(behavior != null)
244
                        behavior.mouseEntered(e);
245
        }
246

    
247
        /*
248
         * (non-Javadoc)
249
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseExited(java.awt.event.MouseEvent)
250
         */
251
        public void mouseExited(MouseEvent e) throws BehaviorException {
252
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
253
                for (int i=0;i<behaviors.length;i++ ) {
254
                        behaviors[i].mouseExited(e);
255
                }
256

    
257
                if(behavior != null)
258
                        behavior.mouseExited(e);
259
        }
260

    
261
        /*
262
         * (non-Javadoc)
263
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseMoved(java.awt.event.MouseEvent)
264
         */
265
        public void mouseMoved(MouseEvent e) throws BehaviorException {
266
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
267
                for (int i=0;i<behaviors.length;i++ ) {
268
                        behaviors[i].mouseMoved(e);
269
                }
270

    
271
                if(behavior != null)
272
                        behavior.mouseMoved(e);
273
        }
274

    
275
        /*
276
         * (non-Javadoc)
277
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mousePressed(java.awt.event.MouseEvent)
278
         */
279
        public void mousePressed(MouseEvent e) throws BehaviorException {
280
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
281
                for (int i=0;i<behaviors.length;i++ ) {
282
                        behaviors[i].mousePressed(e);
283
                }
284

    
285
                if(behavior != null)
286
                        behavior.mousePressed(e);
287
        }
288

    
289
        /*
290
         * (non-Javadoc)
291
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseReleased(java.awt.event.MouseEvent)
292
         */
293
        public void mouseReleased(MouseEvent e) throws BehaviorException {
294
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
295
                for (int i=0;i<behaviors.length;i++ ) {
296
                        behaviors[i].mouseReleased(e);
297
                }
298

    
299
                if(behavior != null)
300
                        behavior.mouseReleased(e);
301
        }
302

    
303
        /*
304
         * (non-Javadoc)
305
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
306
         */
307
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
308
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
309
                for (int i=0;i<behaviors.length;i++ ) {
310
                        behaviors[i].mouseWheelMoved(e);
311
                }
312

    
313
                if(behavior != null)
314
                        behavior.mouseWheelMoved(e);
315
        }
316

    
317
        /*
318
         * (non-Javadoc)
319
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
320
         */
321
        public void paintComponent(MapControlDrawer renderer) {
322
                for (int i = 0; i < behaviors.size(); i++) {
323
                        Behavior mapTool = (Behavior) behaviors.get(i);
324
                        if (((Boolean) draws.get(i)).booleanValue())
325
                                mapTool.paintComponent(renderer);
326
                }
327
        }
328

    
329
        /**
330
         * Sets a tool listener to work with a <code>MapControl</code> instance using these behaviors.
331
         *
332
         * @param listener a <code>RectangleListener</code> object for this behavior
333
         */
334
        public void setListener(ToolListener listener) {
335
                if (listener != null) {
336
                        throw new UnsupportedOperationException(
337
                                "CompoundBehavior does not have listeners");
338
                }
339
        }
340

    
341
        /*
342
         * (non-Javadoc)
343
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
344
         */
345
        public ToolListener getListener() {
346
                return null;
347
        }
348

    
349
        /*
350
         * (non-Javadoc)
351
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
352
         */
353
        public void setMapControl(MapControl mc) {
354
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
355
                for (int i=0;i<behaviors.length;i++ ) {
356
                        behaviors[i].setMapControl(mc);
357
                }
358

    
359
                super.setMapControl(mc);
360
        }
361

    
362
        /**
363
         * <p>Sets the <code>Behavior</code> that will be used for any <code>MapControl</code>.</p>
364
         *
365
         * @param behavior the multi-view <code>Behavior</code>
366
         */
367
        public static void setAllControlsBehavior(Behavior behavior) {
368
                CompoundBehavior.behavior = behavior;
369
        }
370

    
371
        /**
372
         * <p>Gets the <code>Behavior</code> that will be used for any <code>MapControl</code>.</p>
373
         *
374
         * @return the multi-view <code>Behavior</code>
375
         */
376
        public static Behavior getAllControlsBehavior() {
377
                return CompoundBehavior.behavior;
378
        }
379
}
380