Statistics
| Revision:

svn-gvsig-desktop / tags / Root_v06 / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / BandSetupPanel.java @ 4811

History | View | Annotate | Download (23.6 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.ui.raster;
25

    
26
import org.cresques.io.GeoRasterFile;
27

    
28
import java.awt.Component;
29
import java.awt.Dimension;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.awt.event.KeyEvent;
33
import java.awt.event.KeyListener;
34
import java.awt.image.DataBuffer;
35

    
36
import java.io.File;
37

    
38
import java.util.Vector;
39

    
40
import javax.swing.AbstractCellEditor;
41
import javax.swing.JPanel;
42
import javax.swing.JRadioButton;
43
import javax.swing.JScrollPane;
44
import javax.swing.JTable;
45
import javax.swing.SwingConstants;
46
import javax.swing.SwingUtilities;
47
import javax.swing.event.TableModelEvent;
48
import javax.swing.event.TableModelListener;
49
import javax.swing.table.DefaultTableModel;
50
import javax.swing.table.TableCellEditor;
51
import javax.swing.table.TableCellRenderer;
52
import javax.swing.table.TableColumn;
53

    
54

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

    
67
    /**
68
     * Asigna la banda del rojo
69
     */
70
    public static final int RED_BAND = GeoRasterFile.RED_BAND;
71

    
72
    /**
73
     * Asigna la banda del verde
74
     */
75
    public static final int GREEN_BAND = GeoRasterFile.GREEN_BAND;
76

    
77
    /**
78
     * Asigna banda del azul
79
     */
80
    public static final int BLUE_BAND = GeoRasterFile.BLUE_BAND;
81
    private final static String[] columnNames = { "R", "G", "B", "Band" };
82

    
83
    /**
84
     * Nombre del panel
85
     */
86
    private String nom = "Bandas";
87
    private FileList fileList = null;
88
    private JTable rgbTable = null;
89
    private JScrollPane rgbBandAssignPane = null;
90
    RGBBandAsignTableModel tableModel = null;
91
    private int sizeX = 445;
92
    private int sizeY = 174;
93
    private byte mode = 3;
94
    private int[] col = { 0, 1 }; //Ultima y penultima columnas activadas del jtable para cuando hay 2 bandas seleccionadas en el combo
95

    
96
    /**
97
     * This method initializes
98
     *
99
     */
100
    public BandSetupPanel() {
101
        super();
102
        initialize();
103
    }
104

    
105
    /**
106
     * This method initializes this
107
     *
108
     * @return void
109
     */
110
    void initialize() {
111
        this.setPreferredSize(new Dimension(sizeX, sizeY));
112
        this.setLayout(null);
113
        this.setLocation(0, 0);
114
        this.setSize(445, 239);
115
        this.add(getFileList(), null);
116
        this.add(getRGBBandAssignPane(), null);
117
        getFileList().getJComboBox().addKeyListener(this);
118
        getFileList().getJComboBox().addActionListener(this);
119
    }
120

    
121
    /**
122
     * Obtiene el nombre del panel
123
     * @return Cadena con el nombre del panel
124
     */
125
    public String getName(){
126
            return this.nom;
127
    }
128
    
129
    /**
130
     * A?ade la lista de georasterfiles a la tabla
131
     * @param files
132
     */
133
    public void addFiles(GeoRasterFile[] files) {
134
        for (int i = 0; i < files.length; i++) {
135
            String fName = files[i].getName();
136
            getFileList().addFName(fName);
137

    
138
            //((DefaultListModel)fileList.getJList().getModel()).addElement(fName);
139
            String bandName = new File(fName).getName();
140
            String bandType = "";
141

    
142
            switch (files[i].getDataType()) {
143
            case DataBuffer.TYPE_BYTE:
144
                bandType = "8U";
145

    
146
                break;
147

    
148
            case DataBuffer.TYPE_INT:
149
                bandType = "32";
150

    
151
                break;
152

    
153
            case DataBuffer.TYPE_DOUBLE:
154
                bandType = "64";
155

    
156
                break;
157

    
158
            case DataBuffer.TYPE_FLOAT:
159
                bandType = "32";
160

    
161
                break;
162

    
163
            case DataBuffer.TYPE_SHORT:
164
                bandType = "16";
165

    
166
                break;
167

    
168
            case DataBuffer.TYPE_USHORT:
169
                bandType = "16U";
170

    
171
                break;
172

    
173
            case DataBuffer.TYPE_UNDEFINED:
174
                bandType = "??";
175

    
176
                break;
177
            }
178

    
179
            if (files[i].getBandCount() > 1) {
180
                for (int b = 0; b < files[i].getBandCount(); b++)
181
                    addBand((b + 1) + " [" + bandType + "] " + bandName);
182
            } else {
183
                addBand("1 [" + bandType + "] " + bandName);
184
            }
185
        }
186
    }
187

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

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

    
217
                try {
218
                    if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(i,
219
                                                                                            1)).booleanValue()) {
220
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
221
                                                                             i -
222
                                                                             1,
223
                                                                             1);
224
                    }
225
                } catch (ArrayIndexOutOfBoundsException exc) {
226
                    ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
227
                                                                         i + 1,
228
                                                                         1);
229
                }
230

    
231
                try {
232
                    if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(i,
233
                                                                                            2)).booleanValue()) {
234
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
235
                                                                             i -
236
                                                                             1,
237
                                                                             2);
238
                    }
239
                } catch (ArrayIndexOutOfBoundsException exc) {
240
                    ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
241
                                                                         i + 1,
242
                                                                         2);
243
                }
244

    
245
                ((DefaultTableModel) rgbTable.getModel()).removeRow(i);
246
                i--; //Ojo! que hemos eliminado una fila
247
            }
248
        }
249
    }
250

    
251
    /**
252
    * Cuando cambiamos el combo de seleccion de numero de bandas a visualizar
253
    * debemos resetear la tabla de checkbox para que no haya activados m?s de
254
    * los permitidos
255
    * @param mode
256
    */
257
    private void resetMode(int mode) {
258
        //Reseteamos los checkbox
259
        for (int i = 0; i < (rgbTable.getColumnCount() - 1); i++)
260
            for (int j = 0; j < rgbTable.getRowCount(); j++)
261
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(false),
262
                                                                     j, i);
263

    
264
        //Asignamos los valores
265
        if (this.getNBands() >= 3) {
266
            switch (mode) {
267
            case 3:
268
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
269
                                                                     2, 2);
270

    
271
            case 2:
272
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
273
                                                                     1, 1);
274

    
275
            case 1:
276
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
277
                                                                     0, 0);
278
            }
279
        } else if (this.getNBands() == 2) {
280
            switch (mode) {
281
            case 3:
282
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
283
                                                                     1, 2);
284

    
285
            case 2:
286
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
287
                                                                     1, 1);
288

    
289
            case 1:
290
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
291
                                                                     0, 0);
292
            }
293
        } else if (this.getNBands() == 1) {
294
            switch (mode) {
295
            case 3:
296
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
297
                                                                     0, 2);
298

    
299
            case 2:
300
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
301
                                                                     0, 1);
302

    
303
            case 1:
304
                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),
305
                                                                     0, 0);
306
            }
307
        }
308

    
309
        col[0] = 0;
310
        col[1] = 1;
311
    }
312

    
313
    /**
314
     * Asigna modo 1, 2, o 3 bandas. El modo 1 solo permite seleccionar en la
315
     * tabla un checkbox, el 2 dos checkbox en distintar bandas y el 3 tres checkbox
316
     * tambi?n en distintas bandas.
317
     * @param mode
318
     */
319
    private void setMode(int mode) {
320
        //Solo hay un checkbox activado
321
        if (mode == 1) {
322
            for (int i = 0; i < rgbTable.getRowCount(); i++)
323
                for (int j = 0; j < (rgbTable.getColumnCount() - 1); j++) {
324
                    if ((i != rgbTable.getSelectedRow()) ||
325
                            (j != rgbTable.getSelectedColumn())) {
326
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(false),
327
                                                                             i,
328
                                                                             j);
329
                    }
330
                }
331

    
332
            //Hay dos checkbox activados
333
        } else if (mode == 2) {
334
            int n = 0;
335

    
336
            for (int i = 0; i < (rgbTable.getColumnCount() - 1); i++)
337
                for (int j = 0; j < rgbTable.getRowCount(); j++)
338
                    if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(j,
339
                                                                                            i)).booleanValue()) {
340
                        n++;
341
                    }
342

    
343
            //Si se ha seleccionado 3 bandas hay eliminar una de ellas. Siempre ser? la m?s antigua que se clickeo
344
            if (n > 2) {
345
                for (int i = 0; i < rgbTable.getRowCount(); i++)
346
                    ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(false),
347
                                                                         i,
348
                                                                         col[0]);
349
            }
350

    
351
            //Rotamos el punto pinchado m?s antiguo para que se eliminen alternativamente
352
            if ((col[0] == rgbTable.getSelectedColumn()) ||
353
                    ((col[0] != rgbTable.getSelectedColumn()) &&
354
                    (col[1] != rgbTable.getSelectedColumn()))) {
355
                col[0] = col[1];
356
                col[1] = rgbTable.getSelectedColumn();
357
            }
358

    
359
            //El modo 3 es el comportamiento original
360
        } else if (mode == 3) {
361
            return;
362
        }
363
    }
364

    
365
    /**
366
     * Obtiene el panel que contiene la lista de ficheros por banda.
367
     * @return Panel FileList
368
     */
369
    public FileList getFileList() {
370
        if (fileList == null) {
371
            fileList = new FileList();
372
            fileList.setBounds(9, 9, 428, 110);
373
        }
374

    
375
        return fileList;
376
    }
377

    
378
    /**
379
     * This method initializes jTable
380
     *
381
     * @return javax.swing.JTable
382
     */
383
    private JScrollPane getRGBBandAssignPane() {
384
        if (rgbBandAssignPane == null) {
385
            rgbBandAssignPane = new JScrollPane(getRGBTable());
386
            rgbBandAssignPane.setBounds(10, 123, 427, 104);
387

    
388
            TableColumn column = null;
389

    
390
            for (int i = 0; i < 3; i++) {
391
                column = rgbTable.getColumnModel().getColumn(i);
392
                column.setCellRenderer(new RadioColumnRenderer());
393
                column.setCellEditor(new RadioColumnEditor());
394
                column.setMaxWidth(22);
395
                column.setMinWidth(22);
396
            }
397

    
398
            //                        frame.setContentPane(scrollPane);
399
        }
400

    
401
        return rgbBandAssignPane;
402
    }
403

    
404
    /**
405
     * Obtiene la Tabla
406
     * @return Tabla de bandas de la imagen
407
     */
408
    public JTable getRGBTable() {
409
        if (rgbTable == null) {
410
            tableModel = new RGBBandAsignTableModel();
411
            tableModel.addTableModelListener(this);
412
            rgbTable = new JTable(tableModel);
413
            rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72));
414
        }
415

    
416
        return rgbTable;
417
    }
418

    
419
    /**
420
     * Asigna al combo de n?mero de bandas a mostrar el valor correspondiente
421
     * dependiendo del n?mero de bandas de la imagen.
422
     */
423
    public void setList() {
424
        String[] l = null;
425

    
426
        if (this.getNBands() == 1) {
427
            l = new String[1];
428
            l[0] = "1";
429
        }
430

    
431
        if (this.getNBands() == 2) {
432
            l = new String[2];
433
            l[0] = "1";
434
            l[1] = "2";
435
        }
436

    
437
        if (this.getNBands() == 3) {
438
            l = new String[3];
439
            l[0] = "1";
440
            l[1] = "2";
441
            l[2] = "3";
442
        }
443

    
444
        if (this.getNBands() > 0) {
445
            getFileList().setList(l);
446
        }
447
    }
448

    
449
    /**
450
     * A?ade una banda a la tabla  bandas de la imagen asignandole un
451
     * nombre y valor a los checkbox
452
     * @param bandName        Nombre de la banda
453
     */
454
    private void addBand(String bandName) {
455
        Vector v = new Vector();
456
        v.add(new Boolean(false));
457
        v.add(new Boolean(false));
458
        v.add(new Boolean(false));
459
        v.add(bandName);
460
        ((DefaultTableModel) rgbTable.getModel()).addRow(v);
461
    }
462

    
463
    /**
464
     * Obtiene el n?mero de bandas de la lista
465
     * @return
466
     */
467
    public int getNBands() {
468
        return ((DefaultTableModel) rgbTable.getModel()).getRowCount();
469
    }
470

    
471
    /**
472
     * Obtiene el nombre de la banda de la posici?n i de la tabla
473
     * @param i
474
     * @return
475
     */
476
    public String getBandName(int i) {
477
        String s = (String) ((DefaultTableModel) rgbTable.getModel()).getValueAt(i,
478
                                                                                 3);
479

    
480
        return s.substring(s.lastIndexOf("[8U]") + 5, s.length());
481
    }
482

    
483
    /**
484
     * Mantiene la asignaci?n entre R, G o B y la banda de la imagen que le corresponde 
485
     * @param nBand        Banda de la imagen que corresponde
486
     * @param flag        R, G o B se selecciona por medio de un flag que los identifica
487
     */
488
    public void assignBand(int nBand, int flag) {
489
        Boolean t = new Boolean(true);
490

    
491
        if ((flag & RED_BAND) == RED_BAND) {
492
            ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 0);
493
        }
494

    
495
        if ((flag & GREEN_BAND) == GREEN_BAND) {
496
            ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 1);
497
        }
498

    
499
        if ((flag & BLUE_BAND) == BLUE_BAND) {
500
            ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 2);
501
        }
502
    }
503

    
504
    /**
505
     * Obtiene la correspondencia entre el R, G o B y la banda asignada
506
     * @param flag        R, G o B se selecciona por medio de un flag que los identifica
507
     * @return        Banda de la imagen asignada al flag pasado por par?metro
508
     */
509
    public int getAssignedBand(int flag) {
510
        if ((flag & RED_BAND) == RED_BAND) {
511
            for (int nBand = 0; nBand < rgbTable.getRowCount(); nBand++)
512
                if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand,
513
                                                                                        0)).booleanValue()) {
514
                    return nBand;
515
                }
516
        }
517

    
518
        if ((flag & GREEN_BAND) == GREEN_BAND) {
519
            for (int nBand = 0; nBand < rgbTable.getRowCount(); nBand++)
520
                if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand,
521
                                                                                        1)).booleanValue()) {
522
                    return nBand;
523
                }
524
        }
525

    
526
        if ((flag & BLUE_BAND) == BLUE_BAND) {
527
            for (int nBand = 0; nBand < rgbTable.getRowCount(); nBand++)
528
                if (((Boolean) ((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand,
529
                                                                                        2)).booleanValue()) {
530
                    return nBand;
531
                }
532
        }
533

    
534
        return -1;
535
    }
536

    
537
    /* (non-Javadoc)
538
     * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
539
     */
540
    public void tableChanged(TableModelEvent e) {
541
        rgbTable.revalidate();
542
        rgbBandAssignPane.revalidate();
543
        revalidate();
544
    }
545

    
546
    /* (non-Javadoc)
547
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
548
     */
549
    public void keyPressed(KeyEvent arg0) {
550
        // TODO Auto-generated method stub
551
    }
552

    
553
    /* (non-Javadoc)
554
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
555
     */
556
    public void keyReleased(KeyEvent arg0) {
557
        // TODO Auto-generated method stub
558
    }
559

    
560
    /* (non-Javadoc)
561
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
562
     */
563
    public void keyTyped(KeyEvent arg0) {
564
        // TODO Auto-generated method stub
565
    }
566

    
567
    /**
568
     *Evento que activado cuando se modifica el n?mero de bandas que se desean visualizar.
569
     *Se obtiene este n?mero y se llama a la funci?n que controla el cambio de n?mero
570
     *de bandas visualizadas. 
571
     */
572
    public void actionPerformed(ActionEvent arg0) {
573
        String vBands = (String) getFileList().getJComboBox().getSelectedItem();
574

    
575
        if (vBands != null) {
576
            if (vBands.compareTo("3") == 0) {
577
                mode = 3;
578
            }
579

    
580
            if (vBands.compareTo("2") == 0) {
581
                mode = 2;
582
            }
583

    
584
            if (vBands.compareTo("1") == 0) {
585
                mode = 1;
586
            }
587

    
588
            resetMode(mode);
589
        }
590
    }
591

    
592
    class RadioColumnEditor extends AbstractCellEditor
593
        implements TableCellEditor {
594
        final private static long serialVersionUID = -3370601314380922368L;
595
        public JRadioButton theRadioButton;
596

    
597
        public RadioColumnEditor() {
598
            super();
599
            theRadioButton = new JRadioButton();
600
            theRadioButton.addActionListener(new ActionListener() {
601
                    public void actionPerformed(ActionEvent event) {
602
                        fireEditingStopped();
603
                        setMode(mode);
604
                    }
605
                });
606
        }
607

    
608
        public Component getTableCellEditorComponent(JTable table, Object obj,
609
                                                     boolean isSelected,
610
                                                     int row, int col) {
611
            theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
612

    
613
            Boolean lValueAsBoolean = (Boolean) obj;
614
            theRadioButton.setSelected(lValueAsBoolean.booleanValue());
615

    
616
            return theRadioButton;
617
        }
618

    
619
        public Object getCellEditorValue() {
620
            return new Boolean(theRadioButton.isSelected());
621
        }
622
    }
623

    
624
    class RadioColumnRenderer extends JRadioButton implements TableCellRenderer {
625
        final private static long serialVersionUID = -3370601314380922368L;
626

    
627
        public Component getTableCellRendererComponent(JTable table,
628
                                                       Object value,
629
                                                       boolean isSelected,
630
                                                       boolean hasFocus,
631
                                                       int row, int column) {
632
            if (value == null) {
633
                this.setSelected(false);
634
            }
635

    
636
            Boolean ValueAsBoolean = (Boolean) value;
637
            this.setSelected(ValueAsBoolean.booleanValue());
638
            this.setHorizontalAlignment(SwingConstants.CENTER);
639

    
640
            return this;
641
        }
642
    }
643

    
644
    class RGBBandAsignTableModel extends DefaultTableModel {
645
        final private static long serialVersionUID = -3370601314380922368L;
646

    
647
        public RGBBandAsignTableModel() {
648
            super(new Object[0][4], columnNames);
649
        }
650

    
651
        public Class getColumnClass(int c) {
652
            if (c < 3) {
653
                return Boolean.class;
654
            }
655

    
656
            return String.class;
657
        }
658

    
659
        public void setValueAt(Object value, int row, int col) {
660
            if ((col < 3) && ((Boolean) value).booleanValue()) {
661
                for (int i = 0; i < getRowCount(); i++) {
662
                    if (i != row) {
663
                        setValueAt(new Boolean(false), i, col);
664
                    }
665
                }
666
            }
667

    
668
            super.setValueAt(value, row, col);
669
        }
670

    
671
        public void addNew() {
672
            super.addRow(new Object[] {
673
                             new Boolean(false), new Boolean(false),
674
                             new Boolean(false), ""
675
                         });
676
        }
677
    }
678
} //  @jve:decl-index=0:visual-constraint="10,10"