Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / gui / properties / dialog / RegistrableTabPanel.java @ 13622

History | View | Annotate | Download (6.6 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.gui.properties.dialog;
20

    
21
import java.lang.reflect.Constructor;
22
import java.lang.reflect.InvocationTargetException;
23
import java.util.Hashtable;
24
import java.util.Iterator;
25
import java.util.logging.Logger;
26

    
27
import javax.swing.JPanel;
28
import javax.swing.event.ChangeEvent;
29
import javax.swing.event.ChangeListener;
30

    
31
import org.gvsig.raster.util.RasterToolsUtil;
32

    
33
import com.iver.cit.gvsig.fmap.layers.FLayer;
34
import com.iver.utiles.extensionPoints.ExtensionPoint;
35
import com.iver.utiles.extensionPoints.ExtensionPoints;
36
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
37
/**
38
 * Panel que contiene todos los paneles de los tabs del cuadro de dialogo
39
 * de bandas y transparencias
40
 *
41
 * @author Nacho Brodin (brodin_ign@gva.es)
42
 */
43
public class RegistrableTabPanel extends DefaultTabPanel implements ChangeListener {
44
        final private static long serialVersionUID = 0;
45

    
46
        private ExtensionPoints extensionPoints   = null;
47
        private ExtensionPoint  extensionPoint    = null;
48

    
49
        static public Hashtable initialProperties = new Hashtable();
50
        /**
51
   * Tama?o en X del panel
52
   */
53
        protected int           sizePanelX        = 0;
54

    
55
        /**
56
   * Tama?o en Y del panel
57
   */
58
        protected int           sizePanelY        = 0;
59

    
60
        /**
61
         * Constructor. Inicializa los paneles y propiedades
62
         * @param props        Propiedades
63
         * @throws EmptyTabsException 
64
         */
65
        public RegistrableTabPanel(FLayer lyr) throws EmptyTabsException {
66
    loadTabsFromExtensionPoints(lyr);
67
                getTabPane().addChangeListener(this);
68
        }
69

    
70
        /**
71
         * Load register tabs inside the panel, reading these from extension points.
72
         * @throws EmptyTabsException 
73
         */
74
        private void loadTabsFromExtensionPoints(FLayer lyr) throws EmptyTabsException {
75
                extensionPoints = ExtensionPointsSingleton.getInstance();
76
                extensionPoint = (ExtensionPoint) extensionPoints.get("RasterSEPropertiesDialog");
77
                if (extensionPoint == null)
78
                        return;
79
                Iterator iterator = extensionPoint.keySet().iterator();
80

    
81
                Exception lastException = null;
82
                int contTabs = 0;
83
                while (iterator.hasNext()) {
84
                        try {
85
                                String key = (String) iterator.next();
86

    
87
                                Class class1 = (Class) extensionPoint.get(key);
88
                                Constructor constructor = class1.getConstructor(null);
89
                                JPanel panel = (JPanel) constructor.newInstance(null);
90

    
91
                                if (panel != null)
92
                                        throw new NoSuchMethodException();
93

    
94
                                if (panel instanceof IRegistrablePanel) {
95
                                        ((IRegistrablePanel) panel).setLayer(lyr);
96
                                        if (((IRegistrablePanel) panel).isVisiblePanel() && ((IRegistrablePanel) panel).getLayerClass().isInstance(lyr)) {
97
                                                ((IRegistrablePanel) panel).initializeUI();
98
                                                super.addTab(((IRegistrablePanel) panel).getID(), panel);
99
                                                contTabs++;
100
                                        }
101
                                }
102
                        } catch (NoSuchMethodException e) {
103
                                lastException = e;
104
                        } catch (IllegalArgumentException e) {
105
                                lastException = e;
106
                        } catch (InstantiationException e) {
107
                                lastException = e;
108
                        } catch (IllegalAccessException e) {
109
                                lastException = e;
110
                        } catch (InvocationTargetException e) {
111
                                lastException = e;
112
                        } catch (ClassCastException e) {
113
                                lastException = e;
114
                        }
115
                }
116

    
117
                if (lastException != null) {
118
                        RasterToolsUtil.messageBoxInfo("No se pudo crear correctamente el panel de propiedades.", this);
119
                        Logger.getLogger(getClass().getName()).finest(lastException.getMessage());
120
                }
121
                
122
                if (contTabs == 0)
123
                        throw new EmptyTabsException(lastException); 
124
        }
125

    
126
        /**
127
   * Obtiene el indice del tab seleccionado
128
   * @return N?mero de tab seleccionado
129
   */
130
        public int getSelectedTab() {
131
                return super.getTab().getSelectedIndex();
132
        }
133

    
134
        /**
135
         * Ejecuta la acci?n de aceptar en el panel seleccionado
136
         */
137
        public void acceptSelectedTab() {
138
                execAction(0);
139
        }
140

    
141
        /**
142
         * Ejecuta la acci?n de aplicar en el panel seleccionado
143
         */
144
        public void applySelectedTab() {
145
                execAction(1);
146
        }
147

    
148
        /**
149
         * Ejecuta la acci?n de cancelar en el panel seleccionado
150
         */
151
        public void cancelSelectedTab() {
152
                execAction(2);
153
        }
154

    
155
        /**
156
         * Ejecuta la acci?n en el panel seleccionado que se le indica en el
157
         * par?metro
158
         * @param action indicador de la acci?n a realizar
159
         */
160
        private void execAction(int action) {
161
                if (extensionPoint == null)
162
                        return;
163
                if (getTab().getSelectedIndex() == -1)
164
                        return;
165
//                String title = getTab().getTitleAt(getTab().getSelectedIndex());
166

    
167
                Iterator iterator = extensionPoint.keySet().iterator();
168
                while (iterator.hasNext()) {
169
                        try {
170
                                String key = (String) iterator.next();
171
                                IRegistrablePanel panel = (IRegistrablePanel) extensionPoint.get(key);
172
//                                if (key.equals(title)) {
173
                                switch (action) {
174
                                        case 0:
175
                                                panel.accept();
176
                                                break;
177
                                        case 1:
178
                                                panel.apply();
179
                                                break;
180
                                        case 2:
181
                                                panel.cancel();
182
                                                break;
183
                                }
184
//                                }
185
                        } catch (ClassCastException e) {
186
                                // No se ejecuta
187
                                continue;
188
                        }
189
                }
190
        }
191

    
192
        /**
193
         * Asigna la visibilidad de un tab a verdadero o falso. La
194
         * selecci?n del tab se hace por el identificador.
195
         * @param tab        Identificador del tab. Variable nom del mismo
196
         * @param active        True o false para visible o invisible.
197
         */
198
        public void setTabVisible(String tab, boolean active){
199

    
200
        }
201

    
202
        /**
203
         * Selecciona el panel indicado por index
204
         * @param index        panel seleccionado
205
         */
206
        public void setSelectedIndex(int index) {
207
                tabbedPane.setSelectedIndex(index);
208
        }
209

    
210
        /**
211
         * Propagamos el evento de cambiar de pesta?a a todos los paneles.
212
         */
213
        public void stateChanged(ChangeEvent e) {
214
                        if(extensionPoint == null)
215
                        return;
216
                        if(getTab().getSelectedIndex() == -1)
217
                                return;
218
                        String title = getTab().getTitleAt(getTab().getSelectedIndex());
219

    
220
                        Iterator iterator = extensionPoint.keySet().iterator();
221
                while (iterator.hasNext()) {
222
                        try {
223
                                String key = (String)iterator.next();
224
                                if(key.equals(title)){
225
                                        IRegistrablePanel panel = (IRegistrablePanel)extensionPoint.get(key);
226
                                        panel.selectTab(key);
227
                                }
228
                        } catch (ClassCastException ex) {
229
                                //No se ejecuta
230
                                continue;
231
                        }
232
                }
233
        }
234
}