Statistics
| Revision:

gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.fmap / src / main / java / org / gvsig / raster / util / BasePanel.java @ 463

History | View | Annotate | Download (5.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
*
3
* Copyright (C) 2007-2008 Infrastructures and Transports Department
4
* of the Valencian Government (CIT)
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
*/
22
package org.gvsig.raster.util;
23

    
24
import java.awt.Component;
25
import java.awt.event.ActionListener;
26
import java.awt.event.KeyListener;
27
import java.awt.event.MouseListener;
28
import java.util.ArrayList;
29

    
30
import javax.swing.AbstractButton;
31
import javax.swing.JPanel;
32

    
33
import org.gvsig.raster.fmap.layers.DefaultFLyrRaster;
34
import org.slf4j.Logger;
35
import org.slf4j.LoggerFactory;
36

    
37
/**
38
 * Clase base para los paneles gr?ficos. 
39
 * 
40
 * 17/07/2008
41
 * @author Nacho Brodin nachobrodin@gmail.com
42
 */
43
public abstract class BasePanel extends JPanel {
44
        private static final long    serialVersionUID         = -8877600252349334979L;
45
        private boolean                     enableValueChangedEvent  = true;
46
        
47
        public static final int      KEYLISTENER              = 0;
48
        public static final int      ACTIONLISTENER           = 1;
49
        public static final int      MOUSELISTENER            = 2;
50
        private static final Logger  logger                   = LoggerFactory.getLogger(DefaultFLyrRaster.class);
51
        
52
        /**
53
         * Obtiene una instancia de una clase generica que hereda de BasePanel
54
         * @return BasePanel
55
         */
56
        public static BasePanel getInstance() {
57
                return new GenericBasePanel();
58
        }
59
        
60
        /**
61
         * Obtiene la traducci?n de la cadena de texto
62
         * @param parent Ventana padre
63
         * @param text Cadena a traducir
64
         * @return Cadena de texto traducida
65
         */
66
        public String getText(Object parent, String key) {
67
                 if (key == null)
68
                    return null;
69
                String translation = org.gvsig.i18n.Messages.getText(key, false);
70
                if (translation != null)
71
                    return translation;
72
                else {
73
                    logger.debug("Can't find translation for ''{1}''.", key);
74
                    return key;
75
                }
76
        }
77
        
78
        /**
79
         * Obtiene la traducci?n de la cadena de texto
80
         * @param text Cadena a traducir
81
         * @return Cadena de texto traducida
82
         */
83
        public String getText(String text) {
84
                return getText(this, text);
85
        }
86
        
87
        /**
88
         * Asigna el valor para la activaci?n y desactivaci?n del evento de cambio de valor en
89
         * las cajas de texto.
90
         * @param enableValueChangedEvent
91
         */
92
        public void setEnableValueChangedEvent(boolean enableValueChangedEvent) {
93
                this.enableValueChangedEvent = enableValueChangedEvent;
94
        }
95
        
96
        /**
97
         * Obtiene el valor para la activaci?n y desactivaci?n del evento de cambio de valor en
98
         * las cajas de texto.
99
         * @param enableValueChangedEvent
100
         */
101
        public boolean isEnableValueChangedEvent() {
102
                return this.enableValueChangedEvent;
103
        }
104
        
105
        /**
106
         * Obtiene la lista de componentes del panel. Si dentro tiene alg?n BasePanel
107
         * consulta la lista de componentes de este y la devuelve.
108
         * @return
109
         */
110
        public ArrayList getComponentList() {
111
                ArrayList listComp = new ArrayList();
112
                for (int i = 0; i < this.getComponentCount(); i++) {
113
                        if(getComponent(i) instanceof BasePanel) {
114
                                ArrayList list = ((BasePanel)getComponent(i)).getComponentList();
115
                                for (int j = 0; j < list.size(); j++) 
116
                                        listComp.add(list.get(j));
117
                        } else
118
                                listComp.add(getComponent(i));
119
                }
120
                return listComp;
121
        }
122
        
123
        /**
124
         * Registra un listener en todos los componentes contenidos en este panel.
125
         * @param type Tipo de listener definido por las variables est?ticas en esta clase
126
         * @param listener Objeto listener del tipo correcto
127
         */
128
        public void registerListener(int type, Object listener) {
129
                ArrayList listComp = getComponentList();
130
                if(type == KEYLISTENER) {
131
                        if(listener instanceof KeyListener) {
132
                                for (int i = 0; i < listComp.size(); i++)
133
                                        if(listComp.get(i) instanceof Component)
134
                                                ((Component)listComp.get(i)).addKeyListener((KeyListener)listener);
135
                        }
136
                }
137
                if(type == ACTIONLISTENER) {
138
                        if(listener instanceof ActionListener) {
139
                                for (int i = 0; i < listComp.size(); i++)
140
                                        if(listComp.get(i) instanceof AbstractButton)
141
                                                ((AbstractButton)listComp.get(i)).addActionListener((ActionListener)listener);
142
                        }
143
                }
144
                if(type == MOUSELISTENER) {
145
                        if(listener instanceof MouseListener) {
146
                                for (int i = 0; i < listComp.size(); i++)
147
                                        if(listComp.get(i) instanceof Component)
148
                                                ((Component)listComp.get(i)).addMouseListener((MouseListener)listener);
149
                        }
150
                }
151
        }
152
        
153
        /**
154
         * Traducci?n centralizada de los componentes de una panel
155
         */
156
        protected abstract void translate();
157
        
158
        /**
159
         * Acciones de inicializaci?n
160
         */
161
        protected abstract void init();
162
}