Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / colortable / ui / ColorTableListener.java @ 2340

History | View | Annotate | Download (13.5 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
import java.util.List;
26

    
27
import javax.swing.JComponent;
28

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

    
80
        /**
81
         * Devuelve el panel de ColorTable
82
         * @return
83
         */
84
        private ColorTablePanel getColorTablePanel() {
85
                return colorTablePanel;
86
        }
87

    
88
        private ColorTableData getColorTableData() {
89
                return colorTableData;
90
        }
91

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

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

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

    
128
                filterList.remove("colortable");
129
                getColorTablePanel().getLayer().setLastLegend(null);
130

    
131
                if (getColorTableData().isEnabled()) {
132
                        filterList.remove("enhanced_stretch");
133
                        filterList.remove("tailTrim");
134

    
135
                        // Asignamos la transparencia del render actual al filterList
136
                        filterList.addEnvParam("Transparency", rendering.getRender().getRenderingTransparency());
137

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

    
152
                rendering.getRender().setFilterList(filterList);
153
        }
154

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

    
171
        public class StatusComponent {
172
                private JComponent object;
173
                private boolean enabled;
174

    
175
                public boolean isEnabled() {
176
                        return enabled;
177
                }
178

    
179
                public void setEnabled(boolean enabled) {
180
                        this.enabled = enabled;
181
                }
182

    
183
                public JComponent getObject() {
184
                        return object;
185
                }
186

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

    
199
                for (int i = 0; i < component.getComponentCount(); i++)
200
                        if (component.getComponent(i) instanceof JComponent)
201
                                saveComponentsStatus((JComponent) component.getComponent(i));
202
        }
203

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

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

    
228
                        // Desactivar componente
229
                        component.setEnabled(false);
230
                }
231

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

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

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

    
296
                if (isNewSelection)
297
                        getColorTableData().getColorTable().removeDuplicatedValues();
298

    
299
                getLastColorTableUI().setColorTable(getColorTableData().getColorTable());
300
        }
301

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

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

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

    
351
                getColorTableData().setColorTable((ColorTable) colorTableUI.getColorTable().clone());
352
                getColorTableData().refreshPreview();
353
                //getLastColorTableUI().setColorTable(getColorTableData().getColorTable());
354
        }
355

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