Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / beans / canvas / GCanvas.java @ 19361

History | View | Annotate | Download (7.99 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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
package org.gvsig.raster.beans.canvas;
20

    
21
import java.awt.Color;
22
import java.awt.Cursor;
23
import java.awt.Graphics;
24
import java.awt.event.MouseEvent;
25
import java.awt.event.MouseListener;
26
import java.awt.event.MouseMotionListener;
27
import java.util.ArrayList;
28
import java.util.Iterator;
29

    
30
import javax.swing.JPanel;
31
/**
32
 * Canvas donde se pintan objetos GLine
33
 * 14-oct-2007
34
 * @author Nacho Brodin (nachobrodin@gmail.com)
35
 */
36
public class GCanvas  extends JPanel implements MouseListener, MouseMotionListener {
37
        private static final long   serialVersionUID = 5431466034535083594L;
38
        private Color               backgroundColor = Color.WHITE;
39
        private ArrayList           drawableElements = new ArrayList();
40
        public static final int     DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;
41
        
42
        private boolean hasMouse = false;
43
        
44
        //Tama?o del canvas asignado externamente
45
        private int                 x = -1;
46
        private int                 y = -1;
47
        private int                 w = -1;
48
        private int                 h = -1;
49
        
50
        private ArrayList           actionCommandListeners = new ArrayList();
51
                
52
        /**
53
         * Contructor.Inicializa el objeto asignando color de fondo. Considera que el objeto
54
         * GLine ya tiene su color asignado.
55
         * @param line Objeto l?nea
56
         * @param backgroundColor
57
         */
58
        public GCanvas(Color backgroundColor) {
59
                this.backgroundColor = backgroundColor;
60
                addMouseListener(this);
61
                addMouseMotionListener(this);
62
        }
63
        
64
        /**
65
         * A?adir un listener a la lista de eventos
66
         * @param listener
67
         */
68
        public void addValueChangedListener(IGCanvasListener listener) {
69
                if (!actionCommandListeners.contains(listener))
70
                        actionCommandListeners.add(listener);
71
        }
72

    
73
        /**
74
         * Borrar un listener de la lista de eventos
75
         * @param listener
76
         */
77
        public void removeValueChangedListener(IGCanvasListener listener) {
78
                actionCommandListeners.remove(listener);
79
        }
80
        
81
        /**
82
         * Invocar a los eventos asociados al componente
83
         */
84
        public void callDataChanged(String key, Object value) {
85
                Iterator iterator = actionCommandListeners.iterator();
86
                while (iterator.hasNext()) {
87
                        IGCanvasListener listener = (IGCanvasListener) iterator.next();
88
                        listener.actionDataChanged(new GCanvasEvent(this, key, value));
89
                }
90
        }
91
        
92
        /**
93
         * Invocar a los eventos asociados al componente
94
         */
95
        public void callDataDragged(String key, Object value) {
96
                Iterator iterator = actionCommandListeners.iterator();
97
                while (iterator.hasNext()) {
98
                        IGCanvasListener listener = (IGCanvasListener) iterator.next();
99
                        listener.actionDataDragged(new GCanvasEvent(this, key, value));
100
                }
101
        }
102
        
103
        /**
104
         * Asigna un elemento dibujable a la lista
105
         * @param element
106
         */
107
        public void setDrawableElement(DrawableElement element) {
108
                element.setCanvas(this);
109
                drawableElements.add(element);
110
        }
111
        
112
        /**
113
         * Obtiene la lista de elementos dibujables
114
         * @return
115
         */
116
        public ArrayList getDrawableElements() {
117
                return drawableElements;
118
        }
119
                
120
        /**
121
         * Inicializa el fondo y dibuja el gr?fico sobre el canvas.
122
         */
123
        public void paint(Graphics g) {
124
                g.setColor(backgroundColor);
125
                g.fillRect(getVisibleRect().x, getVisibleRect().y, getVisibleRect().width, getVisibleRect().height);
126
                for (int i = 0; i < drawableElements.size(); i++) 
127
                        ((DrawableElement)drawableElements.get(i)).draw(g);
128
        }
129
        
130
        /**
131
         * Ejecuta las acciones antes del primer dibujado de todos
132
         * los elementos dibujables
133
         */
134
        public void execFirstDrawActions() {
135
                for (int i = 0; i < drawableElements.size(); i++) 
136
                        ((DrawableElement)drawableElements.get(i)).firstDrawActions();
137
        }
138
        
139
        /**
140
         * Obtiene la posici?n m?nima en X del canvas donde comenzar a dibujar
141
         * @return
142
         */
143
        public int getCanvasMinX() {
144
                return (x > -1) ? x : getVisibleRect().x;
145
        }
146
        
147
        /**
148
         * Obtiene la posici?n m?nima en Y del canvas donde comenzar a dibujar
149
         * @return
150
         */
151
        public int getCanvasMinY() {
152
                return (y > -1) ? y : getVisibleRect().y;
153
        }
154
        
155
        /**
156
         * Obtiene la posici?n m?xima en X del canvas donde terminar el dibujado
157
         * @return
158
         */
159
        public int getCanvasMaxX() {
160
                return (x > -1) ? (x + w) : getVisibleRect().width;
161
        }
162
        
163
        /**
164
         * Obtiene la posici?n m?xima en Y del canvas donde terminar el dibujado
165
         * @return
166
         */
167
        public int getCanvasMaxY() {
168
                return (y > -1) ? (y + h) : getVisibleRect().width;
169
        }
170
        
171
        /**
172
         * Obtiene el ancho del canvas sumando a partir de getCanvasX donde termina el ?rea de dibujo
173
         * @return
174
         */
175
        public int getCanvasWidth() {
176
                return (w > -1) ? w : getVisibleRect().width;
177
        }
178
        
179
        /**
180
         * Obtiene el alto del canvas sumando a partir de getCanvasY donde termina el ?rea de dibujo
181
         * @return
182
         */
183
        public int getCanvasHeight() {
184
                return (h > -1) ? h : getVisibleRect().height;
185
        }
186
        
187
        /**
188
         * Asigna el valor para la posici?n m?nima en X del canvas
189
         * @param x Posici?n m?nima en X donde se puede empezar a dibujar
190
         */
191
        public void setCanvasX(int x) {
192
                this.x = x;
193
        }
194
        
195
        /**
196
         * Asigna el valor para la posici?n m?nima en Y del canvas
197
         * @param y Posici?n m?nima en Y donde se puede empezar a dibujar
198
         */
199
        public void setCanvasY(int y) {
200
                this.y = y;
201
        }
202
        
203
        /**
204
         * Asigna el valor para ancho del canvas
205
         * @param w Ancho del canvas 
206
         */
207
        public void setCanvasWidth(int w) {
208
                this.w = w;
209
        }
210
        
211
        /**
212
         * Asigna el valor para el alto del canvas
213
         * @param h Alto del canvas
214
         */
215
        public void setCanvasHeight(int h) {
216
                this.h = h;
217
        }
218

    
219
        /*
220
         * (non-Javadoc)
221
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
222
         */
223
        public void mousePressed(MouseEvent e) {
224
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
225
                        if (!((DrawableElement) drawableElements.get(i)).mousePressed(e))
226
                                return;
227
        }
228

    
229
        /*
230
         * (non-Javadoc)
231
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
232
         */
233
        public void mouseReleased(MouseEvent e) {
234
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
235
                        if (!((DrawableElement) drawableElements.get(i)).mouseReleased(e))
236
                                return;
237
                this.mouseMoved(e);
238
        }
239

    
240
        /*
241
         * (non-Javadoc)
242
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
243
         */
244
        public void mouseDragged(MouseEvent e) {
245
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
246
                        if (!((DrawableElement) drawableElements.get(i)).mouseDragged(e))
247
                                return;
248
        }
249

    
250
        /*
251
         * (non-Javadoc)
252
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
253
         */
254
        public void mouseMoved(MouseEvent e) {
255
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
256
                        if (!((DrawableElement) drawableElements.get(i)).mouseMoved(e))
257
                                return;
258
                setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
259
        }
260
        
261
        /*
262
         * (non-Javadoc)
263
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
264
         */
265
        public void mouseEntered(MouseEvent e) {
266
                hasMouse = true;
267
                repaint();
268
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
269
                        if (!((DrawableElement) drawableElements.get(i)).mouseEntered(e))
270
                                return;
271
        }
272
        
273
        /*
274
         * (non-Javadoc)
275
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
276
         */
277
        public void mouseExited(MouseEvent e) {
278
                hasMouse = false;
279
                repaint();
280
                for (int i = drawableElements.size() - 1; i >= 0 ; i--)
281
                        if (!((DrawableElement) drawableElements.get(i)).mouseExited(e))
282
                                return;
283
        }
284

    
285
        /**
286
         * Devuelve si en ese momento el raton esta sobre el canvas
287
         * @return
288
         */
289
        public boolean isMouse() {
290
                return hasMouse;
291
        }
292

    
293
        public void mouseClicked(MouseEvent e) {}
294
}