Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / colortable / ui / ColorTableListener.java @ 969

History | View | Annotate | Download (13.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.tools.app.basic.tool.colortable.ui;
23

    
24
import java.util.ArrayList;
25

    
26
import javax.swing.JComponent;
27

    
28
import org.gvsig.andami.PluginServices;
29
import org.gvsig.fmap.dal.coverage.RasterLocator;
30
import org.gvsig.fmap.dal.coverage.datastruct.ColorItem;
31
import org.gvsig.fmap.dal.coverage.datastruct.ColorTableLibrary;
32
import org.gvsig.fmap.dal.coverage.datastruct.Params;
33
import org.gvsig.fmap.dal.coverage.exception.ColorTableException;
34
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException;
35
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
36
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
37
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
38
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
39
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
40
import org.gvsig.fmap.dal.coverage.util.PropertyEvent;
41
import org.gvsig.fmap.dal.coverage.util.PropertyListener;
42
import org.gvsig.gui.beans.imagenavigator.ImageUnavailableException;
43
import org.gvsig.raster.fmap.layers.FLyrRaster;
44
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
45
import org.gvsig.raster.tools.app.basic.raster.bean.previewbase.IPreviewRenderProcess;
46
import org.gvsig.raster.tools.app.basic.tool.colortable.data.ColorTableData;
47
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.library.ColorTableLibraryPanel;
48
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.tabs.ColorTableUIListener;
49
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.tabs.IColorTableUI;
50
/**
51
 * Listener generico para el panel de tablas de color, en el controlaremos el
52
 * refresco de la vista previa, los cambios en las tablas de color y la
53
 * finalizacion del panel.
54
 * 
55
 * @version 27/06/2007
56
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
57
 */
58
public class ColorTableListener implements PropertyListener, ColorTableUIListener, IPreviewRenderProcess {
59
        private ColorTablePanel colorTablePanel  = null;
60
        private FLyrRaster      previewLayer     = null;
61
        private ColorTableData  colorTableData   = null;
62
        private ArrayList<StatusComponent>       
63
                                statusList       = new ArrayList<StatusComponent>();
64
        private IColorTableUI   lastColorTableUI = null;
65
        private boolean         showPreview      = true;
66
        
67
        /**
68
         * Construye un ColorTableListener
69
         * @param colorTablePanel
70
         */
71
        public ColorTableListener(ColorTablePanel colorTablePanel, ColorTableData colorTableData) {
72
                this.colorTablePanel = colorTablePanel;
73
                this.colorTableData = colorTableData;
74
                getColorTableData().addValueChangedListener(this);
75
        }
76

    
77
        /**
78
         * Devuelve el panel de ColorTable
79
         * @return
80
         */
81
        private ColorTablePanel getColorTablePanel() {
82
                return colorTablePanel;
83
        }
84

    
85
        private ColorTableData getColorTableData() {
86
                return colorTableData;
87
        }
88

    
89
        /**
90
         * Asigna la capa raster de la vista
91
         * @param fLayer
92
         */
93
        public void setLayer(FLyrRaster fLayer) {
94
                if (fLayer instanceof FLyrRaster) {
95
                        try {
96
                                previewLayer = (FLyrRaster) fLayer.cloneLayer();
97
                        } catch (Exception e) {
98
                                RasterToolsUtil.messageBoxError("preview_not_available", colorTablePanel, e);
99
                        }
100
                }
101
        }
102

    
103
        /**
104
         * Cierra la capa abierta para previsualizaci?n
105
         */
106
        public void closePreviewLayer() {
107
                if (previewLayer != null) {
108
                        previewLayer.setRemoveRasterFlag(true);
109
                        previewLayer.removeLayerListener(null);
110
                }
111
        }
112

    
113
        /**
114
         * Aqui se aplica el estado de las tablas de color al rendering pasado por
115
         * parametro
116
         * @param rendering
117
         * @return
118
         * @throws FilterTypeException
119
         * @throws FilterManagerException 
120
         */
121
        private void applyColorTable(FLyrRaster rendering, boolean isPreview) throws FilterTypeException, FilterManagerException {
122
                RasterFilterList filterList = rendering.getRender().getFilterList();
123
                RasterFilterListManager cManager = filterList.getManagerByID("ColorTable");
124

    
125
                filterList.remove("colortable");
126
                getColorTablePanel().getLayer().setLastLegend(null);
127

    
128
                if (getColorTableData().isEnabled()) {
129
                        filterList.remove("enhanced_stretch");
130
                        filterList.remove("tailTrim");
131

    
132
                        // Asignamos la transparencia del render actual al filterList
133
                        filterList.addEnvParam("Transparency", rendering.getRender().getLastTransparency());
134

    
135
                        Params params = filterList.createEmptyFilterParams();
136
                        ColorTable colorTable = getColorTableData().getColorTable();
137
                        params.setParam("colorTable", colorTable);
138
                        cManager.addFilter(params);
139
                        
140
                        if (!isPreview) {
141
                                colorTable.compressPalette();
142
                                getColorTablePanel().getLayer().setLastLegend(colorTable);
143
                        }
144
                }
145
                for (int i = 0; i< filterList.lenght(); i++) {
146
                        ((RasterFilter) filterList.get(i)).setEnv(filterList.getEnv());
147
                }
148

    
149
                rendering.getRender().setFilterList(filterList);
150
        }
151

    
152
        /**
153
         * Acciones que se ejecutaran al haber presionado el bot?n aceptar o aplicar
154
         */
155
        public void accept() {
156
                try {
157
                        ColorTableLibrary tableLib = RasterLocator.getManager().getDataStructFactory().getColorTableLibrary();
158
                        applyColorTable(getColorTablePanel().getLayer(), false);
159
                        getColorTablePanel().getLayer().getMapContext().invalidate();
160
                        tableLib.save_to_1_1(ColorTableLibraryPanel.palettesPath, getColorTableData().getColorTable());
161
                } catch (FilterTypeException e) {
162
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e);
163
                } catch (FilterManagerException e) {
164
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e);
165
                }
166
        }
167

    
168
        public class StatusComponent {
169
                private JComponent object;
170
                private boolean enabled;
171

    
172
                public boolean isEnabled() {
173
                        return enabled;
174
                }
175

    
176
                public void setEnabled(boolean enabled) {
177
                        this.enabled = enabled;
178
                }
179

    
180
                public JComponent getObject() {
181
                        return object;
182
                }
183

    
184
                public void setObject(JComponent object) {
185
                        this.object = object;
186
                }
187
        }
188
        
189
        private void saveComponentsStatus(JComponent component) {
190
                // Guardar estado
191
                StatusComponent auxStatus = new StatusComponent();
192
                auxStatus.setEnabled(component.isEnabled());
193
                auxStatus.setObject(component);
194
                statusList.add(auxStatus);
195

    
196
                for (int i = 0; i < component.getComponentCount(); i++)
197
                        if (component.getComponent(i) instanceof JComponent)
198
                                saveComponentsStatus((JComponent) component.getComponent(i));
199
        }
200

    
201
        private void setEnabledRecursive(JComponent component, boolean enabled, int level) {
202
                if (enabled == true) {
203
                        boolean auxEnabled = false;
204
                        boolean finded = false;
205
                        // Buscar estado de dicho componente
206
                        for (int i = 0; i < statusList.size(); i++) {
207
                                StatusComponent auxStatus = (StatusComponent) statusList.get(i);
208
                                if (auxStatus.getObject() == component) {
209
                                        auxEnabled = auxStatus.isEnabled();
210
                                        statusList.remove(i);
211
                                        i--;
212
                                        finded = true;
213
                                        break;
214
                                }
215
                        }
216

    
217
                        // Asignar su estado
218
                        if (finded)
219
                                component.setEnabled(auxEnabled);
220
                } else {
221
                        // Si es la primera llamada, guardamos el estado de todos los componentes
222
                        if (level == 0)
223
                                saveComponentsStatus(component);
224

    
225
                        // Desactivar componente
226
                        component.setEnabled(false);
227
                }
228

    
229
                for (int i = 0; i < component.getComponentCount(); i++)
230
                        if (component.getComponent(i) instanceof JComponent)
231
                                setEnabledRecursive((JComponent) component.getComponent(i), enabled, level + 1);
232
        }        
233
        
234
        /**
235
         * Activa/Desactiva los componentes de las pesta?as segun la pesta?a selecionada
236
         * @param enabled
237
         */
238
        @SuppressWarnings("unused")
239
        private void setEnabledPanel(boolean enabled) {
240
                colorTablePanel.getGeneralPanel().setEnabledPanel(enabled);
241
                colorTablePanel.getPreviewBasePanel().getTabbedPane().setEnabled(enabled);
242
                setEnabledRecursive(getLastColorTableUI().getPanel(), enabled, 0);
243
                setEnabledRecursive(colorTablePanel.getPreviewBasePanel().getImageNavigator(), enabled, 0);
244
                setEnabledRecursive(colorTablePanel.getPanelListView(), enabled, 0);
245
        }
246

    
247
        /*
248
         * (non-Javadoc)
249
         * @see org.gvsig.rastertools.PropertyListener#actionValueChanged(org.gvsig.rastertools.PropertyEvent)
250
         */
251
        public void actionValueChanged(PropertyEvent e) {
252
                if (e.getName().equals("refreshPreview")) {
253
                        colorTablePanel.getPreviewBasePanel().refreshPreview();
254
                        return;
255
                }
256
                
257
                if (e.getName().equals("interpolated")) {
258
                        colorTablePanel.getColorTableLibraryPanel().setInterpolated(((Boolean) e.getValue()).booleanValue());
259
                        getColorTableData().getColorTable().setInterpolated(((Boolean) e.getValue()).booleanValue());
260
                        getColorTableData().refreshPreview();
261
                        getLastColorTableUI().setColorTable(getColorTableData().getColorTable());
262
                        return;
263
                }
264

    
265
                if (e.getName().equals("limits") ||
266
                                e.getName().equals("maxim") ||
267
                                e.getName().equals("minim")) {
268
                        if (getColorTableData().isLimitsEnabled()) {
269
                                double min = getColorTableData().getMinim();
270
                                double max = getColorTableData().getMaxim();
271
                                getLastColorTableUI().getColorTable().createColorTableInRange(min, max, false);
272
                                getLastColorTableUI().setColorTable(getLastColorTableUI().getColorTable());
273
                                getColorTableData().refreshPreview();
274
                        } else {
275
                                try {
276
                                        colorTablePanel.reloadPanelsFromLibraryPanel();
277
                                } catch (ColorTableException e1) {
278
                                        //Esta excepci?n ya se ha debido capturar en la carga del dialogo por lo que no deber?a darse
279
                                }
280
                        }
281
                        return;
282
                }
283
        }
284
        
285
        /**
286
         * Recarga la tabla de elementos
287
         * @param isNewSelection
288
         */
289
        public void refreshItems(boolean isNewSelection) {
290
                if (getColorTableData().getColorTable() == null)
291
                        return;
292

    
293
                if (isNewSelection)
294
                        getColorTableData().getColorTable().removeDuplicatedValues();
295

    
296
                getLastColorTableUI().setColorTable(getColorTableData().getColorTable());
297
        }
298

    
299
        /**
300
         * Pone en separaciones iguales todos los valores de la tabla seleccionada
301
         */
302
        public void equidistar() {
303
                ColorTable colorTable = getColorTableData().getColorTable();
304
                if (colorTable == null)
305
                        return;
306
                colorTable.removeDuplicatedValues();
307
                ArrayList<ColorItem> list = colorTable.getColorItems();
308
                double min2 = Double.POSITIVE_INFINITY;
309
                double max2 = Double.NEGATIVE_INFINITY;
310
                for (int i = 0; i < list.size(); i++) {
311
                        ColorItem colorItem = (ColorItem) list.get(i);
312
                        if (min2 > colorItem.getValue())
313
                                min2 = colorItem.getValue();
314
                        if (max2 < colorItem.getValue())
315
                                max2 = colorItem.getValue();
316
                }
317
                for (int i = 0; i < list.size(); i++) {
318
                        ColorItem colorItem = (ColorItem) list.get(i);
319
                        colorItem.setValue(min2 + (((max2 - min2) / (list.size() - 1)) * i));
320
                }
321
                refreshItems(true);
322
                getColorTableData().refreshPreview();
323
        }
324
        
325
        /**
326
         * @return the lastColorTableUI
327
         */
328
        private IColorTableUI getLastColorTableUI() {
329
                return lastColorTableUI;
330
        }
331

    
332
        /**
333
         * @param lastColorTableUI the lastColorTableUI to set
334
         */
335
        public void setLastColorTableUI(IColorTableUI lastColorTableUI) {
336
                this.lastColorTableUI = lastColorTableUI;
337
        }
338

    
339
        /*
340
         * (non-Javadoc)
341
         * @see org.gvsig.rastertools.colortable.panels.ColorTableUIListener#actionColorTableUIChanged(org.gvsig.rastertools.colortable.panels.IColorTableUI)
342
         */
343
        public void actionColorTableUIChanged(IColorTableUI colorTableUI) {
344
                ColorTable colorTable = colorTablePanel.getColorTableLibraryPanel().getColorTableSelected();
345
                colorTable.createPaletteFromColorItems(colorTableUI.getColorTable().getColorItems(), false);
346
                colorTablePanel.getColorTableLibraryPanel().setColorTableSelected(colorTable);
347

    
348
                getColorTableData().setColorTable((ColorTable) colorTableUI.getColorTable().clone());
349
                getColorTableData().refreshPreview();
350
                //getLastColorTableUI().setColorTable(getColorTableData().getColorTable());
351
        }
352

    
353
        public void process(FLyrRaster rendering) throws ImageUnavailableException {
354
                if(!showPreview)
355
                        throw new ImageUnavailableException(RasterToolsUtil.getText(this, "panel_preview_not_available"));
356
                
357
//                rendering.getRenderFilterList().pushStatus();
358
                try {
359
                        applyColorTable(rendering, true);
360
                } catch (FilterTypeException e1) {
361
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e1);
362
                } catch (FilterManagerException e) {
363
                        RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e);
364
                }
365
        }
366
        
367
        /**
368
         * Obtiene el flag que informa de si se est? mostrando la previsualizaci?n o no.
369
         * En caso de no mostrarse se lanza una excepci?n ImageUnavailableExcepcion con el 
370
         * mensaje "La previsualizaci?n no est? disponible para este panel"
371
         * @return
372
         */
373
        public boolean isShowPreview() {
374
                return showPreview;
375
        }
376
        
377
        /**
378
         * Asigna el flag para mostrar u ocultar la preview. En caso de no mostrarse se lanza una 
379
         * excepci?n ImageUnavailableExcepcion con el mensaje "La previsualizaci?n no est? disponible para
380
         * este panel"
381
         * @param showPreview
382
         */
383
        public void setShowPreview(boolean showPreview) {
384
                this.showPreview = showPreview;
385
        }
386
}