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 / ColorTablePanel.java @ 723

History | View | Annotate | Download (14.1 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.awt.BorderLayout;
25
import java.io.IOException;
26
import java.util.ArrayList;
27

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

    
32
import org.gvsig.andami.PluginServices;
33
import org.gvsig.fmap.dal.coverage.RasterLocator;
34
import org.gvsig.fmap.dal.coverage.RasterManager;
35
import org.gvsig.fmap.dal.coverage.exception.ColorTableException;
36
import org.gvsig.fmap.dal.coverage.exception.RmfSerializerException;
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.render.Render;
40
import org.gvsig.fmap.dal.coverage.store.RasterDataStore;
41
import org.gvsig.fmap.dal.coverage.store.props.ColorTable;
42
import org.gvsig.fmap.dal.coverage.store.props.Transparency;
43
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
44
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
45
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
46
import org.gvsig.raster.fmap.layers.FLyrRaster;
47
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
48
import org.gvsig.raster.tools.app.basic.raster.bean.previewbase.PreviewBasePanel;
49
import org.gvsig.raster.tools.app.basic.tool.colortable.data.ColorTableData;
50
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.library.ColorTableLibraryEvent;
51
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.library.ColorTableLibraryListener;
52
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.library.ColorTableLibraryPanel;
53
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.tabs.IColorTableUI;
54
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.tabs.TabInterpolated;
55
import org.gvsig.raster.tools.app.basic.tool.colortable.ui.tabs.TabTable;
56
/**
57
 * ColorTablePanel es el panel padre que contendra todos los paneles del interfaz
58
 * de tablas de color.
59
 *
60
 * @version 26/06/2007
61
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
62
 */
63
public class ColorTablePanel extends JPanel implements ChangeListener, ButtonsPanelListener, ColorTableLibraryListener {
64
        private static final long serialVersionUID = 6028780107787443656L;
65

    
66
        private ColorTableListener      colorTableListener = null;
67
        private FLyrRaster              fLayer             = null;
68
        private ArrayList<RasterFilter> filterStatus       = null;
69
        private ColorTableData          colorTableData     = null;
70

    
71
        private IColorTableUI           tabTable           = null;
72
        private IColorTableUI           tabInterpolated    = null;
73
        
74
        private ColorTableLibraryPanel  colorTableLibraryPanel = null;
75

    
76
        private JPanel                  jPanelListView     = null;
77
        private ColorTableGlobalPanel   panelGeneral       = null;
78
        private ColorTableDialog        colorTableDialog   = null;
79
        private PreviewBasePanel        previewBasePanel   = null;
80

    
81
        /**
82
         * Controla si se ha presionado el boton aceptar para el cerrado de la ventana
83
         */
84
        boolean accepted = false;
85

    
86
        /**
87
         * Construir un nuevo ColorTablePanel
88
         * @throws ColorTableException 
89
         */
90
        public ColorTablePanel(FLyrRaster layer, ColorTableDialog colorTableDialog) throws ColorTableException {
91
                this.colorTableDialog = colorTableDialog;
92
                fLayer = layer;
93
                initialize();
94
                setLayer(layer);
95
        }
96
        
97
        public PreviewBasePanel getPreviewBasePanel() {
98
                if (previewBasePanel == null) {
99
                        ArrayList<IColorTableUI> list = new ArrayList<IColorTableUI>();
100
                        list.add(getTabTable());
101
                        list.add(getTabInterpolated());
102
                        getColorTableListener().setLastColorTableUI(getTabTable());
103
                        
104
                        previewBasePanel = new PreviewBasePanel(ButtonsPanel.BUTTONS_NONE, list, (JPanel) getGeneralPanel(), getPanelListView(), getColorTableListener(), (FLyrRaster) fLayer);
105
                        previewBasePanel.getTabbedPane().addChangeListener(this);
106
                        previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "equidistar"), ButtonsPanel.BUTTON_LAST + 3);
107
                        previewBasePanel.getButtonsPanel().addButton(RasterToolsUtil.getText(this, "guardar_predeterminado"), ButtonsPanel.BUTTON_LAST + 2);
108
                        previewBasePanel.getButtonsPanel().addApply();
109
                        previewBasePanel.getButtonsPanel().addAccept();
110
                        previewBasePanel.getButtonsPanel().addCancel();
111
                        previewBasePanel.addButtonPressedListener(this);
112
                }
113
                return previewBasePanel;
114
        }
115
        
116

    
117
        private void initialize() {
118
                setLayout(new BorderLayout());
119
                add(getPreviewBasePanel(), BorderLayout.CENTER);
120
        }
121

    
122
        private IColorTableUI getTabTable() {
123
                if (tabTable == null) {
124
                        tabTable = new TabTable();
125
                        tabTable.addColorTableUIChangedListener(getColorTableListener());
126
                }
127
                return tabTable;
128
        }
129

    
130

    
131
        private IColorTableUI getTabInterpolated() {
132
                if (tabInterpolated == null) {
133
                        tabInterpolated = new TabInterpolated();
134
                        tabInterpolated.addColorTableUIChangedListener(getColorTableListener());
135
                }
136
                return tabInterpolated;
137
        }
138

    
139
        public JPanel getPanelListView() {
140
                if (jPanelListView == null) {
141
                        jPanelListView = new JPanel();
142
                        jPanelListView.setBorder(javax.swing.BorderFactory.createTitledBorder(null, RasterToolsUtil.getText(this, "libreria"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
143
                        jPanelListView.setLayout(new BorderLayout());
144
                        
145
                        jPanelListView.add(getColorTableLibraryPanel());
146
                }
147
                return jPanelListView;
148
        }
149
        
150
        public ColorTableLibraryPanel getColorTableLibraryPanel() {
151
                if (colorTableLibraryPanel == null) {
152
                        colorTableLibraryPanel = new ColorTableLibraryPanel(getColorTableData());
153
                        colorTableLibraryPanel.addColorTableLibraryListener(this);
154
                        colorTableLibraryPanel.selectDefault();
155
                }
156
                return colorTableLibraryPanel;
157
        }
158

    
159
        /**
160
         * Obtener el componente que hara la carga pesada de los listeners
161
         * @return
162
         */
163
        private ColorTableListener getColorTableListener() {
164
                if (colorTableListener == null) {
165
                        colorTableListener = new ColorTableListener(this, getColorTableData());
166
                }
167
                return colorTableListener;
168
        }
169

    
170
        public ColorTableGlobalPanel getGeneralPanel() {
171
                if (panelGeneral == null) {
172
                        panelGeneral = new ColorTableGlobalPanel(getColorTableData());
173
                }
174
                return panelGeneral;
175
        }
176

    
177
        /**
178
         * Volvemos todo a la normalidad cuando se cancela
179
         */
180
        private boolean restoreFilters() {
181
                if (getLayer() != null) {
182
                        Render rendering = getLayer().getRender();
183
                        if(rendering.getFilterList() == null)
184
                                return false;
185
                        rendering.getFilterList().setStatus(getFilterStatus());
186
                        getLayer().getRender().setFilterList(rendering.getFilterList());
187
                }
188
                return true;
189
        }
190

    
191
        /**
192
         * Devuelve el arrayList de filtros inicial
193
         * @return
194
         */
195
        private ArrayList<RasterFilter> getFilterStatus() {
196
                return filterStatus;
197
        }
198

    
199
        /**
200
         * Carga la tabla de color que hay seleccionada en el componente
201
         * ListViewComponent
202
         * @throws ColorTableException 
203
         */
204
        public void reloadPanelsFromLibraryPanel() throws ColorTableException {
205
                ColorTable colorTable = getColorTableLibraryPanel().getColorTableSelected();
206
                if(colorTable == null)
207
                        throw new ColorTableException(ColorTableException.MESSAGE_NOTLOADED);
208
                getColorTableData().setColorTable((ColorTable) colorTable.clone());
209
                if (colorTableData.isLimitsEnabled()) {
210
                        double min = getColorTableData().getMinim();
211
                        double max = getColorTableData().getMaxim();
212
                        getColorTableData().getColorTable().createColorTableInRange(min, max, false);
213
                }
214
                getColorTableListener().refreshItems(true);
215
                getColorTableData().refreshPreview();
216
        }
217

    
218
        /**
219
         * Salva la tabla de color al fichero rmf.
220
         * @param fName
221
         * @throws IOException
222
         */
223
        private void saveColorTable() {
224
                RasterDataStore dataset = getLayer().getDataStore();
225
                try {
226
                        if (getColorTableData().isEnabled())
227
                                dataset.saveColorTableToRmf(getColorTableData().getColorTable());
228
                        else
229
                                dataset.saveColorTableToRmf(null);
230
                } catch (RmfSerializerException e) {
231
                        RasterToolsUtil.messageBoxError("error_salvando_rmf", this, e);
232
                }
233
        }
234

    
235
        /**
236
         * Restaura el estado de los filtros como estaban al abrir la ventana y quita
237
         * la leyenda del TOC
238
         */
239
        private void restoreSettings() {
240
                if (getLayer() == null)
241
                        return;
242

    
243
                // Restauramos la vista con los filtros originales antes de cualquier acci?n
244
                if(!restoreFilters())
245
                        return;
246

    
247
                getLayer().setLastLegend(null);
248

    
249
                RasterFilterList rasterFilterList = getLayer().getRender().getFilterList();
250
                RasterFilter colorTableFilter = rasterFilterList.getByName("colortable");
251
                if (colorTableFilter != null) {
252
                        ColorTable colorTable = (ColorTable)colorTableFilter.getParam("colorTable");
253
                        getLayer().setLastLegend(colorTable);
254
                }
255

    
256
                // Quitamos la banda de transparencia por si el filtro de color la ha activado
257
                try {
258
                        Transparency transparency = getLayer().getRender().getLastTransparency();
259
                        transparency.setTransparencyBand(-1);
260
                        //transparency.activeTransparency();
261
                        transparency.setAlphaBand(null);
262
                } catch (NullPointerException ex) {
263
                        //No quitamos la banda de transparencia
264
                }
265
        }
266

    
267
        /**
268
         * Devuelve el componente que trata los datos
269
         * @return
270
         */
271
        private ColorTableData getColorTableData() {
272
                if (colorTableData == null) {
273
                        colorTableData = new ColorTableData();
274
                }
275
                return colorTableData;
276
        }
277

    
278
        /**
279
         * Especificar el layer para la tabla de color
280
         * @param layer
281
         * @throws ColorTableException 
282
         */
283
        private void setLayer(FLyrRaster fLayer) throws ColorTableException {
284
                try {
285
                        this.fLayer = fLayer;
286
                        RasterManager rManager = RasterLocator.getManager();
287

    
288
                        getGeneralPanel().setLayer(fLayer);
289

    
290
                        getColorTableListener().setLayer(fLayer);
291

    
292
                        RasterFilterList rasterFilterList = getLayer().getRender().getFilterList();
293

    
294
                        filterStatus = rasterFilterList.getStatusCloned();
295

    
296
                        RasterFilter colorTableFilter = rasterFilterList.getByName("colortable");
297
                        if (colorTableFilter != null) {
298
                                ColorTable colorTable = (ColorTable)colorTableFilter.getParam("colorTable");
299
                                if (colorTable.isCompressible()) {
300
                                        if (RasterToolsUtil.messageBoxYesOrNot("comprimir_paleta", this)) {
301
                                                colorTable.compressPalette();
302
                                                colorTable.setInterpolated(true);
303
                                        }
304
                                }
305

    
306
                                ColorTable ct = rManager.getDataStructFactory().getColorTableLibrary().createColorTable();
307
                                ct.setName(RasterToolsUtil.getText(this, "tabla_actual"));
308
                                ct.createPaletteFromColorItems(colorTable.getColorItems(), false);
309
                                ct.setInterpolated(colorTable.isInterpolated());
310
                                getGeneralPanel().setCheckBoxInterpolated(colorTable.isInterpolated());
311

    
312
                                getColorTableLibraryPanel().addColorTable(0, ct);
313

    
314
                                getGeneralPanel().setLimitsEnabled(false);
315
                                getGeneralPanel().setCheckBoxEnabled(true);
316
                        } else {
317
                                getColorTableLibraryPanel().selectDefault();
318

    
319
                                getGeneralPanel().setLimitsEnabled(true);
320
                                getGeneralPanel().setCheckBoxEnabled(false);
321
                        }
322
                        reloadPanelsFromLibraryPanel();
323
                }catch (Exception e) {
324
                        throw new ColorTableException(ColorTableException.MESSAGE_NOTLOADED);
325
                }
326
        }
327

    
328
        /**
329
         * Especificar el layer para el recorte
330
         * @param layer
331
         */
332
        public FLyrRaster getLayer() {
333
                return fLayer;
334
        }
335

    
336
        /**
337
         * Cuando se aceptan los cambios en el panel ejecutaremos el aceptar del
338
         * listener
339
         */
340
        private void accept() {
341
                getColorTableListener().accept();
342
        }
343

    
344
        /**
345
         * Volvemos todo a la normalidad cuando se cancela
346
         */
347
        private void cancel() {
348
                getColorTableListener().closePreviewLayer();
349
                getLayer().getMapContext().invalidate();
350
        }
351

    
352
        /**
353
         * Se dispara cuando se selecciona una pesta?a en el panel
354
         */
355
        public void stateChanged(ChangeEvent e) {
356
                switch (getPreviewBasePanel().getTabbedPane().getSelectedIndex()) {
357
                        case 1:
358
                                getColorTableListener().setLastColorTableUI(getTabInterpolated());
359
                                break;
360
                        default:
361
                                getColorTableListener().setLastColorTableUI(getTabTable());
362
                                break;
363
                }
364

    
365
                getColorTableListener().refreshItems(true);
366
        }
367

    
368
        /*
369
         * (non-Javadoc)
370
         * @see org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener#actionButtonPressed(org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent)
371
         */
372
        public void actionButtonPressed(ButtonsPanelEvent e) {
373
                restoreSettings();
374

    
375
                // Al pulsar Aceptar o Aplicar se ejecuta el aceptar del panel
376
                if (e.getButton() == ButtonsPanel.BUTTON_APPLY || e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
377
                        accept();
378
                }
379

    
380
                // Al pulsar Cancelar la ventana se cierra y se refresca la vista
381
                if (e.getButton() == ButtonsPanel.BUTTON_CANCEL) {
382
                        cancel();
383
                        close();
384
                }
385

    
386
                // Al pulsar Aceptar simplemente la ventana se cierra
387
                if (e.getButton() == ButtonsPanel.BUTTON_ACCEPT) {
388
                        accepted = true;
389
                        close();
390
                }
391

    
392
                if (e.getButton() == (ButtonsPanel.BUTTON_LAST + 2)) {
393
                        if (RasterToolsUtil.messageBoxYesOrNot("guardar_como_predeterminado", this)) {
394
                                saveColorTable();
395
                                accept();
396
                        }
397
                }
398

    
399
                if (e.getButton() == (ButtonsPanel.BUTTON_LAST + 3)) {
400
                        getColorTableListener().equidistar();
401
                }
402
        }
403

    
404
        /**
405
         * Acciones a ejecutar cuando se cancela
406
         */
407
        private void close() {
408
                if (colorTableDialog == null)
409
                        return;
410
                try {
411
                        PluginServices.getMDIManager().closeWindow(colorTableDialog);
412
                } catch (ArrayIndexOutOfBoundsException e) {
413
                        // Si la ventana no se puede eliminar no hacemos nada
414
                }
415
        }
416

    
417
        public void windowClosed() {
418
                if (!accepted) {
419
                        restoreSettings();
420
                        cancel();
421
                }
422
        }
423

    
424
        /*
425
         * (non-Javadoc)
426
         * @see org.gvsig.rastertools.colortable.library.ColorTableLibraryListener#actionColorTableChanged(org.gvsig.rastertools.colortable.library.ColorTableLibraryEvent)
427
         */
428
        public void actionColorTableChanged(ColorTableLibraryEvent e) {
429
                try {
430
                        reloadPanelsFromLibraryPanel();
431
                } catch (ColorTableException e1) {
432
                        //Esta excepci?n ya se ha debido capturar en la carga del dialogo por lo que no deber?a darse
433
                }
434
        }
435
}