Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / properties / panels / BandSetupPanel.java @ 12198

History | View | Annotate | Download (21.8 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.rastertools.properties.panels;
20

    
21
import java.awt.Component;
22
import java.awt.Dimension;
23
import java.awt.event.ActionEvent;
24
import java.awt.event.ActionListener;
25
import java.awt.event.ComponentEvent;
26
import java.awt.event.ComponentListener;
27
import java.awt.image.DataBuffer;
28
import java.io.File;
29
import java.util.ArrayList;
30
import java.util.Vector;
31

    
32
import javax.swing.AbstractCellEditor;
33
import javax.swing.JPanel;
34
import javax.swing.JRadioButton;
35
import javax.swing.JScrollPane;
36
import javax.swing.JTable;
37
import javax.swing.SwingConstants;
38
import javax.swing.SwingUtilities;
39
import javax.swing.event.TableModelEvent;
40
import javax.swing.event.TableModelListener;
41
import javax.swing.table.DefaultTableModel;
42
import javax.swing.table.TableCellEditor;
43
import javax.swing.table.TableCellRenderer;
44
import javax.swing.table.TableColumn;
45

    
46
import org.gvsig.raster.dataset.MultiRasterDataset;
47
import org.gvsig.raster.dataset.RasterDataset;
48
import org.gvsig.raster.shared.IRasterDataset;
49
import org.gvsig.raster.shared.IRasterProperties;
50
import org.gvsig.raster.shared.IRasterRendering;
51
import org.gvsig.rastertools.properties.control.BandSetupListener;
52
import org.gvsig.rastertools.properties.dialog.IRegistrablePanel;
53
import org.gvsig.rastertools.properties.dialog.RasterPropertiesTocMenuEntry;
54
import org.gvsig.rastertools.properties.dialog.RegistrableTabPanel;
55

    
56
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
/**
59
 * Selecciona las bandas visibles en un raster. Contiene una tabla con una fila
60
 * por cada banda de la imagen. Por medio de checkbox se selecciona para cada
61
 * RGB que banda de la imagen ser? visualizada.
62
 * 
63
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
64
 * @author Nacho Brodin (brodin_ign@gva.es)
65
 */
66
public class BandSetupPanel extends JPanel implements TableModelListener, ComponentListener, IRegistrablePanel {
67
        final private static long serialVersionUID = -3370601314380922368L;
68

    
69
        /**
70
         * Variables para la asignaci?n de tama?o de los componentes del panel.
71
         */
72
        private int                                                wComp = 445, hComp = 239;
73
        private int                                                wFileList = wComp, hFileList = (int)Math.round(hComp * 0.46);
74
        private int                                                wBand = wFileList, hBand = hComp - hFileList - 20;
75

    
76
        /**
77
         * Asigna la banda del rojo
78
         */
79
        public static final int RED_BAND = RasterDataset.RED_BAND;
80

    
81
        /**
82
         * Asigna la banda del verde
83
         */
84
        public static final int GREEN_BAND = RasterDataset.GREEN_BAND;
85

    
86
        /**
87
         * Asigna banda del azul
88
         */
89
        public static final int BLUE_BAND = RasterDataset.BLUE_BAND;
90
        private final static String[]         columnNames = { "R", "G", "B", "Band" };
91

    
92
        FLayer fLayer = null;
93

    
94
        /**
95
         * Nombre del panel
96
         */
97
        private String                id                = "bands_panel";
98
        private BandSetupFileList     fileList          = null;
99
        private JTable                rgbTable          = null;
100
        private JScrollPane           rgbBandAssignPane = null;
101
        RGBBandAsignTableModel        tableModel        = null;
102
        private byte                  mode              = 3;
103
        // Ultima y penultima columnas activadas del jtable para cuando hay 2 bandas seleccionadas en el combo
104
        private int[]                 col               = { 0, 1 };
105
        private BandSetupListener     panelListener     = null;
106
        private IRasterProperties     prop              = null;
107
        private IRasterDataset        dataset           = null;
108

    
109
        /**
110
         * This method initializes
111
         */
112
        public BandSetupPanel() {
113
                super();
114
                id = PluginServices.getText(this, id);
115
                initialize();
116
                panelListener = new BandSetupListener(this);
117
        }
118

    
119
        /**
120
         * This method initializes this
121
         *
122
         * @return void
123
         */
124
        void initialize() {
125
                // this.setPreferredSize(new Dimension(wComp, hComp));
126
                this.setLayout(null);
127
                this.setLocation(0, 0);
128
                this.add(getFileList(), null);
129
                this.add(getRGBBandAssignPane(), null);
130
                this.addComponentListener(this);
131
                this.setComponentSize(wComp, hComp);
132
        }
133

    
134
        /**
135
         * A?ade la lista de georasterfiles a la tabla
136
         *
137
         * @param files
138
         */
139
        public void addFiles(MultiRasterDataset mDataset) {
140
                getFileList().clear();
141
                clear();
142
                for (int i = 0; i < mDataset.getDatasetCount(); i++) {
143
                        String fName = mDataset.getDataset(i).getFName();
144
                        getFileList().addFName(fName);
145

    
146
                        String bandName = new File(fName).getName();
147
                        String bandType = "";
148

    
149
                        switch (mDataset.getDataset(i).getDataType()) {
150
                                case DataBuffer.TYPE_BYTE:
151
                                        bandType = "8U";
152
                                        break;
153
                                case DataBuffer.TYPE_INT:
154
                                        bandType = "32";
155
                                        break;
156
                                case DataBuffer.TYPE_DOUBLE:
157
                                        bandType = "64";
158
                                        break;
159
                                case DataBuffer.TYPE_FLOAT:
160
                                        bandType = "32";
161
                                        break;
162
                                case DataBuffer.TYPE_SHORT:
163
                                        bandType = "16";
164
                                        break;
165
                                case DataBuffer.TYPE_USHORT:
166
                                        bandType = "16U";
167
                                        break;
168
                                case DataBuffer.TYPE_UNDEFINED:
169
                                        bandType = "??";
170
                                        break;
171
                        }
172

    
173
                        for (int b = 0; b < mDataset.getDataset(i).getBandCount(); b++)
174
                                addBand((b + 1) + " [" + bandType + "] " + bandName);
175

    
176
/*
177
                        if (mDataset.getDataset(i).getBandCount() > 1) {
178
                                for (int b = 0; b < mDataset.getDataset(i).getBandCount(); b++)
179
                                        addBand((b + 1) + " [" + bandType + "] " + bandName);
180
                        } else {
181
                                addBand("1 [" + bandType + "] " + bandName);
182
                        }
183
*/
184
                }
185
                readDrawedBands();
186
                saveStatus();
187
        }
188

    
189
        /**
190
         * Elimina un fichero de la lista
191
         * @param file Nombre del fichero a eliminar
192
         */
193
        public void removeFile(String file) {
194
                getFileList().removeFName(file);
195

    
196
                for (int i = 0; i < ((DefaultTableModel) getRGBTable().getModel()).getRowCount(); i++) {
197
                        // Si el fichero borrado estaba seleccionado como banda visible. Pasaremos
198
                        // esta visibilidad a la banda inmediata superior y si esta acci?n produce
199
                        // una excepci?n (porque no hay) se pasa al inmediato inferior.
200
                        if (((String) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(i, 3)).endsWith(file)) {
201
                                try {
202
                                        if (((Boolean) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(i, 0)).booleanValue()) {
203
                                                ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i - 1, 0);
204
                                        }
205
                                } catch (ArrayIndexOutOfBoundsException exc) {
206
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i + 1, 0);
207
                                }
208

    
209
                                try {
210
                                        if (((Boolean) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(i, 1)).booleanValue()) {
211
                                                ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i - 1, 1);
212
                                        }
213
                                } catch (ArrayIndexOutOfBoundsException exc) {
214
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i + 1, 1);
215
                                }
216

    
217
                                try {
218
                                        if (((Boolean) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(i, 2)).booleanValue()) {
219
                                                ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i - 1, 2);
220
                                        }
221
                                } catch (ArrayIndexOutOfBoundsException exc) {
222
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), i + 1, 2);
223
                                }
224

    
225
                                ((DefaultTableModel) getRGBTable().getModel()).removeRow(i);
226
                                i--; // Ojo! que hemos eliminado una fila
227
                        }
228
                }
229
                panelListener.setNewBandsPositionInRendering();
230
        }
231

    
232
        public void assignMode(byte mode) {
233
                this.mode = mode;
234
        }
235

    
236
        public byte getMode() {
237
                return mode;
238
        }
239

    
240
        /**
241
         * Cuando cambiamos el combo de seleccion de numero de bandas a visualizar
242
         * debemos resetear la tabla de checkbox para que no haya activados m?s de los
243
         * permitidos
244
         * @param mode
245
         */
246
        public void resetMode(int mode) {
247
                // Reseteamos los checkbox
248
                for (int i = 0; i < (getRGBTable().getColumnCount() - 1); i++)
249
                        for (int j = 0; j < getRGBTable().getRowCount(); j++)
250
                                ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(false), j, i);
251

    
252
                // Asignamos los valores
253
                if (this.getNBands() >= 3) {
254
                        switch (mode) {
255
                                case 3:
256
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 2, 2);
257
                                case 2:
258
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 1, 1);
259
                                case 1:
260
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 0, 0);
261
                        }
262
                } else if (this.getNBands() == 2) {
263
                        switch (mode) {
264
                                case 3:
265
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 1, 2);
266
                                case 2:
267
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 1, 1);
268
                                case 1:
269
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 0, 0);
270
                        }
271
                } else if (this.getNBands() == 1) {
272
                        switch (mode) {
273
                                case 3:
274
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 0, 2);
275
                                case 2:
276
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 0, 1);
277
                                case 1:
278
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(true), 0, 0);
279
                        }
280
                }
281

    
282
                col[0] = 0;
283
                col[1] = 1;
284
        }
285

    
286
        /**
287
         * Asigna modo 1, 2, o 3 bandas. El modo 1 solo permite seleccionar en la
288
         * tabla un checkbox, el 2 dos checkbox en distintar bandas y el 3 tres
289
         * checkbox tambi?n en distintas bandas.
290
         * @param mode
291
         */
292
        private void setMode(int mode) {
293
                // Solo hay un checkbox activado
294
                if (mode == 1) {
295
                        for (int i = 0; i < getRGBTable().getRowCount(); i++)
296
                                for (int j = 0; j < (getRGBTable().getColumnCount() - 1); j++) {
297
                                        if ((i != getRGBTable().getSelectedRow()) || (j != getRGBTable().getSelectedColumn())) {
298
                                                ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(false), i, j);
299
                                        }
300
                                }
301

    
302
                        // Hay dos checkbox activados
303
                } else if (mode == 2) {
304
                        int n = 0;
305

    
306
                        for (int i = 0; i < (getRGBTable().getColumnCount() - 1); i++)
307
                                for (int j = 0; j < getRGBTable().getRowCount(); j++)
308
                                        if (((Boolean) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(j, i)).booleanValue()) {
309
                                                n++;
310
                                        }
311

    
312
                        // Si se ha seleccionado 3 bandas hay eliminar una de ellas. Siempre ser?
313
                        // la m?s antigua que se clickeo
314
                        if (n > 2) {
315
                                for (int i = 0; i < getRGBTable().getRowCount(); i++)
316
                                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(new Boolean(false), i, col[0]);
317
                        }
318

    
319
                        // Rotamos el punto pinchado m?s antiguo para que se eliminen
320
                        // alternativamente
321
                        if ((col[0] == getRGBTable().getSelectedColumn()) || ((col[0] != getRGBTable().getSelectedColumn()) && (col[1] != getRGBTable().getSelectedColumn()))) {
322
                                col[0] = col[1];
323
                                col[1] = getRGBTable().getSelectedColumn();
324
                        }
325

    
326
                        // El modo 3 es el comportamiento original
327
                } else if (mode == 3) {
328
                        return;
329
                }
330
        }
331

    
332
        /**
333
         * Obtiene el panel que contiene la lista de ficheros por banda.
334
         * @return Panel FileList
335
         */
336
        public BandSetupFileList getFileList() {
337
                if (fileList == null) {
338
                        fileList = new BandSetupFileList();
339
                }
340

    
341
                return fileList;
342
        }
343

    
344
        /**
345
         * This method initializes jTable
346
         * @return javax.swing.JTable
347
         */
348
        private JScrollPane getRGBBandAssignPane() {
349
                if (rgbBandAssignPane == null) {
350
                        rgbBandAssignPane = new JScrollPane(getRGBTable());
351

    
352
                        TableColumn column = null;
353

    
354
                        for (int i = 0; i < 3; i++) {
355
                                column = getRGBTable().getColumnModel().getColumn(i);
356
                                column.setCellRenderer(new RadioColumnRenderer());
357
                                column.setCellEditor(new RadioColumnEditor());
358
                                column.setMaxWidth(22);
359
                                column.setMinWidth(22);
360
                        }
361
                }
362

    
363
                return rgbBandAssignPane;
364
        }
365

    
366
        /**
367
         * Obtiene la Tabla
368
         * @return Tabla de bandas de la imagen
369
         */
370
        public JTable getRGBTable() {
371
                if (rgbTable == null) {
372
                        tableModel = new RGBBandAsignTableModel();
373
                        tableModel.addTableModelListener(this);
374
                        rgbTable = new JTable(tableModel);
375
                        rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72));
376
                }
377
                return rgbTable;
378
        }
379

    
380
        /**
381
         * Asigna al combo de n?mero de bandas a mostrar el valor correspondiente
382
         * dependiendo del n?mero de bandas de la imagen.
383
         */
384
        public void setList() {
385
                String[] l = null;
386

    
387
                if (this.getNBands() == 1) {
388
                        l = new String[1];
389
                        l[0] = "1";
390
                }
391

    
392
                if (this.getNBands() == 2) {
393
                        l = new String[2];
394
                        l[0] = "1";
395
                        l[1] = "2";
396
                }
397

    
398
                if (this.getNBands() == 3) {
399
                        l = new String[3];
400
                        l[0] = "1";
401
                        l[1] = "2";
402
                        l[2] = "3";
403
                }
404

    
405
                if (this.getNBands() > 0) {
406
                        getFileList().setList(l);
407
                }
408
        }
409

    
410
        /**
411
         * A?ade una banda a la tabla bandas de la imagen asignandole un nombre y
412
         * valor a los checkbox
413
         * @param bandName Nombre de la banda
414
         */
415
        private void addBand(String bandName) {
416
                Vector v = new Vector();
417
                v.add(new Boolean(false));
418
                v.add(new Boolean(false));
419
                v.add(new Boolean(false));
420
                v.add(bandName);
421
                ((DefaultTableModel) getRGBTable().getModel()).addRow(v);
422
        }
423

    
424
        /**
425
         * Elimina todas las entradas de la tabla de bandas.
426
         */
427
        private void clear() {
428
                int rows = ((DefaultTableModel) getRGBTable().getModel()).getRowCount();
429
                if (rows > 0) {
430
                        for (int i = 0; i < rows; i++)
431
                                ((DefaultTableModel) getRGBTable().getModel()).removeRow(0);
432
                }
433
        }
434

    
435
        /**
436
         * Obtiene el n?mero de bandas de la lista
437
         *
438
         * @return
439
         */
440
        public int getNBands() {
441
                return ((DefaultTableModel) getRGBTable().getModel()).getRowCount();
442
        }
443

    
444
        /**
445
         * Obtiene el nombre de la banda de la posici?n i de la tabla
446
         *
447
         * @param i
448
         * @return
449
         */
450
        public String getBandName(int i) {
451
                String s = (String) ((DefaultTableModel) getRGBTable().getModel()).getValueAt(i, 3);
452
                return s.substring(s.lastIndexOf("[8U]") + 5, s.length());
453
        }
454

    
455
        /**
456
         * Mantiene la asignaci?n entre R, G o B y la banda de la imagen que le
457
         * corresponde
458
         *
459
         * @param nBand Banda de la imagen que corresponde
460
         * @param flag R, G o B se selecciona por medio de un flag que los identifica
461
         */
462
        public void assignBand(int nBand, int flag) {
463
                Boolean t = new Boolean(true);
464

    
465
                if ((flag & RED_BAND) == RED_BAND)
466
                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(t, nBand, 0);
467

    
468
                if ((flag & GREEN_BAND) == GREEN_BAND)
469
                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(t, nBand, 1);
470

    
471
                if ((flag & BLUE_BAND) == BLUE_BAND)
472
                        ((DefaultTableModel) getRGBTable().getModel()).setValueAt(t, nBand, 2);
473
        }
474

    
475
        /**
476
         * Obtiene la correspondencia entre el R, G o B y la banda asignada
477
         *
478
         * @param flag R, G o B se selecciona por medio de un flag que los identifica
479
         * @return Banda de la imagen asignada al flag pasado por par?metro
480
         */
481
        public int getAssignedBand(int flag) {
482
                DefaultTableModel model = ((DefaultTableModel) getRGBTable().getModel());
483
                if ((flag & RED_BAND) == RED_BAND) {
484
                        for (int nBand = 0; nBand < getRGBTable().getRowCount(); nBand++)
485
                                if (((Boolean) model.getValueAt(nBand, 0)).booleanValue())
486
                                        return nBand;
487
                }
488

    
489
                if ((flag & GREEN_BAND) == GREEN_BAND) {
490
                        for (int nBand = 0; nBand < getRGBTable().getRowCount(); nBand++)
491
                                if (((Boolean) model.getValueAt(nBand, 1)).booleanValue())
492
                                        return nBand;
493
                }
494

    
495
                if ((flag & BLUE_BAND) == BLUE_BAND) {
496
                        for (int nBand = 0; nBand < getRGBTable().getRowCount(); nBand++)
497
                                if (((Boolean) model.getValueAt(nBand, 2)).booleanValue())
498
                                        return nBand;
499
                }
500

    
501
                return -1;
502
        }
503

    
504
        /*
505
         * (non-Javadoc)
506
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
507
         */
508
        public void tableChanged(TableModelEvent e) {
509
                getRGBTable().revalidate();
510
                rgbBandAssignPane.revalidate();
511
                revalidate();
512
        }
513

    
514
        class RadioColumnEditor extends AbstractCellEditor implements TableCellEditor {
515
                final private static long serialVersionUID = -3370601314380922368L;
516
                public JRadioButton       theRadioButton;
517

    
518
                public RadioColumnEditor() {
519
                        super();
520
                        theRadioButton = new JRadioButton();
521
                        theRadioButton.addActionListener(new ActionListener() {
522
                                public void actionPerformed(ActionEvent event) {
523
                                        fireEditingStopped();
524
                                        setMode(mode);
525
                                        onlyApply();
526
                                }
527
                        });
528
                }
529

    
530
                public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int col) {
531
                        theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
532

    
533
                        Boolean lValueAsBoolean = (Boolean) obj;
534
                        theRadioButton.setSelected(lValueAsBoolean.booleanValue());
535

    
536
                        return theRadioButton;
537
                }
538

    
539
                public Object getCellEditorValue() {
540
                        return new Boolean(theRadioButton.isSelected());
541
                }
542
        }
543

    
544
        class RadioColumnRenderer extends JRadioButton implements TableCellRenderer {
545
                final private static long serialVersionUID = -3370601314380922368L;
546

    
547
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
548
                        if (value == null) {
549
                                this.setSelected(false);
550
                        }
551

    
552
                        Boolean ValueAsBoolean = (Boolean) value;
553
                        this.setSelected(ValueAsBoolean.booleanValue());
554
                        this.setHorizontalAlignment(SwingConstants.CENTER);
555

    
556
                        return this;
557
                }
558
        }
559

    
560
        class RGBBandAsignTableModel extends DefaultTableModel {
561
                final private static long serialVersionUID = -3370601314380922368L;
562

    
563
                public RGBBandAsignTableModel() {
564
                        super(new Object[0][4], columnNames);
565
                }
566

    
567
                public Class getColumnClass(int c) {
568
                        if (c < 3) {
569
                                return Boolean.class;
570
                        }
571

    
572
                        return String.class;
573
                }
574

    
575
                public void setValueAt(Object value, int row, int col) {
576
                        if ((col < 3) && ((Boolean) value).booleanValue()) {
577
                                for (int i = 0; i < getRowCount(); i++) {
578
                                        if (i != row) {
579
                                                setValueAt(new Boolean(false), i, col);
580
                                        }
581
                                }
582
                        }
583

    
584
                        super.setValueAt(value, row, col);
585
                }
586

    
587
                public void addNew() {
588
                        super.addRow(new Object[] { new Boolean(false), new Boolean(false), new Boolean(false), "" });
589
                }
590
        }
591

    
592
        public void setComponentSize(int w, int h) {
593
                wComp = w;
594
                hComp = h;
595
                wFileList = wComp - 18;
596
                hFileList = (int) Math.round(hComp * 0.46);
597
                wBand = wFileList;
598
                hBand = hComp - hFileList - 20;
599

    
600
                this.setPreferredSize(new Dimension(wComp, hComp));
601
                this.setSize(wComp, hComp);
602
                rgbBandAssignPane.setBounds(10, hFileList + 12, wBand, hBand);
603
                fileList.setBounds(9, 9, wFileList, hFileList);
604

    
605
        }
606

    
607
        /**
608
         * Redimensiona el panel cuando se redimensiona el contenedor de ?ste
609
         */
610
        public void componentResized(ComponentEvent e) {
611
                if (e.getSource() == this) {
612
                        int nWidth = this.getSize().width;
613
                        int nHeight = this.getSize().height;
614
                        int difWidth = nWidth - 445;
615
                        int difHeight = nHeight - 239;
616
                        this.fileList.setResize(difWidth, difHeight);
617
                        this.rgbBandAssignPane.setBounds(10, 123 + difHeight / 2, 427 + difWidth, 104 + difHeight / 2);
618

    
619
                }
620
        }
621

    
622

    
623
        /*
624
         * (non-Javadoc)
625
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#setLayer(com.iver.cit.gvsig.fmap.layers.FLyrDefault)
626
         */
627
        public void setLayer(FLayer lyr) {
628
                fLayer = lyr;
629
                clear();
630
                getFileList().clear();
631

    
632
                if (lyr instanceof IRasterRendering) {
633
                        if (((IRasterRendering) lyr).existColorTable()) {
634
                                panelListener.init(null, null, lyr);
635
                                this.setEnabled(false);
636
                                return;
637
                        }
638
                }
639

    
640
                this.setEnabled(true);
641

    
642
                if (lyr instanceof IRasterProperties)
643
                        prop = (IRasterProperties) lyr;
644

    
645
                if (lyr instanceof IRasterDataset) {
646
                        dataset = (IRasterDataset) lyr;
647
                        addFiles(dataset.getGeoRasterMultiDataset());
648
                }
649

    
650
                panelListener.init(dataset, prop, lyr);
651
        }
652

    
653
        /**
654
         * Lee desde el renderizador las bandas que se han dibujado y en que posici?n se ha hecho. 
655
         */
656
        public void readDrawedBands() {
657
                if (prop.getRender() != null) {
658
                        int[] renderBands = prop.getRender().getRenderBands();
659
                        for (int i = 0; i < renderBands.length; i++) {
660
                                if (renderBands[i] >= 0) {
661
                                        switch (i) {
662
                                                case 0:
663
                                                        this.assignBand(renderBands[i], RasterDataset.RED_BAND);
664
                                                        break;
665
                                                case 1:
666
                                                        this.assignBand(renderBands[i], RasterDataset.GREEN_BAND);
667
                                                        break;
668
                                                case 2:
669
                                                        this.assignBand(renderBands[i], RasterDataset.BLUE_BAND);
670
                                                        break;
671
                                        }
672
                                }
673
                        }
674
                }
675
        }
676

    
677
        /*
678
         * (non-Javadoc)
679
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#accept()
680
         */
681
        public void accept() {
682
                onlyApply();
683
        }
684

    
685
        /**
686
         * Aplica y guarda los cambios del panel
687
         */
688
        public void apply() {
689
                onlyApply();
690
                saveStatus();
691
        }
692

    
693
        /**
694
         * Aplicar los cambios sin guardar su estado
695
         */
696
        public void onlyApply() {
697
                if (RasterPropertiesTocMenuEntry.enableEvents)
698
                        panelListener.apply();
699
        }
700

    
701
        /**
702
         * Guarda el estado actual del panel
703
         */
704
        private void saveStatus() {
705
                ArrayList aux = new ArrayList();
706
                int[] renderBands = prop.getRender().getRenderBands();
707
                for (int i = 0; i < renderBands.length; i++) {
708
                        aux.add(new Integer(renderBands[i]));
709
                }
710

    
711
                RegistrableTabPanel.initialProperties.put("renderBands", aux);
712
        }
713

    
714

    
715
        /**
716
         * Deja la capa en el ?ltimo estado guardado y la refresca
717
         */
718
        public void restoreStatus() {
719

    
720
                ArrayList aux = (ArrayList) RegistrableTabPanel.initialProperties.get("renderBands");
721

    
722
                int[] renderBands = new int[aux.size()];
723
                for (int i = 0; i < aux.size(); i++) {
724
                        renderBands[i] = ((Integer) aux.get(i)).intValue();
725
                }
726

    
727
                prop.getRender().setRenderBands(renderBands);
728

    
729
                if (fLayer != null)
730
                        fLayer.getMapContext().invalidate();
731
        }
732

    
733
        /*
734
         * (non-Javadoc)
735
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#cancel()
736
         */
737
        public void cancel() {
738
                restoreStatus();
739
        }
740

    
741
        /*
742
         * (non-Javadoc)
743
         * @see org.gvsig.rastertools.properties.dialog.IRegistrablePanel#getID()
744
         */
745
        public String getID() {
746
                return id;
747
        }
748

    
749
        /**
750
         * Activa y desactiva el control
751
         * @param enabled true para activar y false para desactivar
752
         */
753
        public void setEnabled(boolean enabled) {
754
                if (panelListener != null)
755
                        panelListener.setEnabledPanelAction(enabled);
756
                this.getRGBTable().setEnabled(enabled);
757
                this.getRGBBandAssignPane().setEnabled(enabled);
758
                getFileList().setEnabled(enabled);
759
        }
760

    
761
        public void selectTab(String id) {}
762
        public void componentHidden(ComponentEvent e) {}
763
        public void componentShown(ComponentEvent e) {}
764
        public void componentMoved(ComponentEvent e) {}
765
}