Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / CompoundBehavior.java @ 42036

History | View | Annotate | Download (11.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 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 3
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
package org.gvsig.fmap.mapcontrol.tools;
25

    
26
import java.awt.Image;
27
import java.awt.event.MouseEvent;
28
import java.awt.event.MouseWheelEvent;
29
import java.util.ArrayList;
30

    
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33
import org.slf4j.spi.LoggerFactoryBinder;
34

    
35
import org.gvsig.fmap.mapcontrol.MapControl;
36
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
37
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
38
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
39

    
40

    
41

    
42
/**
43
 * <p>Allows having multiple behaviors when user works with the associated
44
 *  <code>MapControl</code>. Each one with its associated tool listener.</p>
45
 *
46
 * @author Fernando Gonz?lez Cort?s
47
 * @author Pablo Piqueras Bartolom?
48
 */
49
public class  CompoundBehavior extends Behavior {
50
    private static final Logger logger = LoggerFactory.getLogger(CompoundBehavior.class);
51
        /**
52
         * List of all behaviors that compound this one.
53
         */
54
        private ArrayList behaviors = new ArrayList(); //<Behavior>
55

    
56
        /**
57
         * List that determines which behaviors of this one will be real-time painted.
58
         */
59
        private ArrayList draws = new ArrayList(); //<Boolean>
60

    
61
        /**
62
         * Static <code>behavior</code> that will be used for any <code>MapControl</code>.</p>
63
         */
64
        private static Behavior behavior = null;
65

    
66
        /**
67
          * <p>Creates a new behavior as a composition of others.</p>
68
          *
69
         * @param tools atomic behaviors that will compound this one</code>
70
         */
71
        public CompoundBehavior(Behavior[] behaviors){
72

    
73
        for (int i = 0; i < behaviors.length; i++) {
74
            if (behaviors[i] == null) {
75
                throw new IllegalArgumentException("behaviors");
76
            }
77
        }
78

    
79

    
80
        boolean first = true;
81
                for (int i = 0; i < behaviors.length; i++) {
82

    
83
                    if (!containsBehavior(behaviors[i])) {
84
                        this.behaviors.add(behaviors[i]);
85
                        /*
86
                         * Only the first in set as real-time drawn
87
                         */
88
                        if (first) {
89
                            draws.add(Boolean.TRUE);
90
                        } else {
91
                        draws.add(Boolean.FALSE);
92
                        }
93
                        first = false;
94
                    }
95
                }
96
        }
97

    
98
        /**
99
         * <p>Adds a new behavior, setting if will be real-time (when user is working with it) drawn or not.</p>
100
         *
101
         * <p>When user works with a compound behavior, he/she will see on real-time the graphical changes produced at
102
         *  the associated <code>MapControl</code>, only by those which have their associated <i>draw</i> flag to <code>true</code>.</p>
103
         *
104
         * @param mt the new behavior
105
         * @param draw flag determining if will be real-time drawn or no
106
         */
107
        public void addMapBehavior(Behavior mt, boolean draw){
108

    
109
            if (mt == null) {
110
                throw new IllegalArgumentException("mt");
111
            }
112

    
113
            if (!containsBehavior(mt)) {
114
                behaviors.add(mt);
115
                draws.add(new Boolean(draw));
116
            }
117
        }
118

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

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

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

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

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

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

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

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

    
213
                if(behavior != null)
214
                        behavior.mouseClicked(e);
215
        }
216

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

    
227
                if(behavior != null)
228
                        behavior.mouseDragged(e);
229
        }
230

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

    
241
                if(behavior != null)
242
                        behavior.mouseEntered(e);
243
        }
244

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

    
255
                if(behavior != null)
256
                        behavior.mouseExited(e);
257
        }
258

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

    
269
                if(behavior != null)
270
                        behavior.mouseMoved(e);
271
        }
272

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

    
283
                if(behavior != null)
284
                        behavior.mousePressed(e);
285
        }
286

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

    
297
                if(behavior != null)
298
                        behavior.mouseReleased(e);
299
        }
300

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

    
311
                if(behavior != null)
312
                        behavior.mouseWheelMoved(e);
313
        }
314

    
315
        /*
316
         * (non-Javadoc)
317
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
318
         */
319
        public void paintComponent(MapControlDrawer renderer) {
320
            paintComponent(renderer,false);
321
        }
322

    
323
    public void paintComponent(MapControlDrawer renderer, boolean clean) {
324
        if(clean){
325
            clean(renderer);
326
        }
327
        for (int i = 0; i < behaviors.size(); i++) {
328
            Behavior mapTool = (Behavior) behaviors.get(i);
329
            if (((Boolean) draws.get(i)).booleanValue()){
330
                mapTool.paintComponent(renderer, false);
331
            }
332
        }
333
    }
334

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

    
347
        /*
348
         * (non-Javadoc)
349
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
350
         */
351
        public ToolListener getListener() {
352
                return null;
353
        }
354

    
355
        /*
356
         * (non-Javadoc)
357
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
358
         */
359
        public void setMapControl(MapControl mc) {
360
                Behavior[] behaviors=(Behavior[])this.behaviors.toArray(new Behavior[0]);
361
                for (int i=0;i<behaviors.length;i++ ) {
362
                        behaviors[i].setMapControl(mc);
363
                }
364

    
365
                super.setMapControl(mc);
366
        }
367

    
368
        /**
369
         * <p>Sets the <code>Behavior</code> that will be used for any <code>MapControl</code>.</p>
370
         *
371
         * @param behavior the multi-view <code>Behavior</code>
372
         */
373
        public static void setAllControlsBehavior(Behavior behavior) {
374
                CompoundBehavior.behavior = behavior;
375
        }
376

    
377
        /**
378
         * <p>Gets the <code>Behavior</code> that will be used for any <code>MapControl</code>.</p>
379
         *
380
         * @return the multi-view <code>Behavior</code>
381
         */
382
        public static Behavior getAllControlsBehavior() {
383
                return CompoundBehavior.behavior;
384
        }
385
}
386