Statistics
| Revision:

root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / BandSetupPanel.java @ 2249

History | View | Annotate | Download (11.2 KB)

1
/*
2
 * Created on 21-jun-2005
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.cresques.ui.raster;
48

    
49
import java.awt.Component;
50
import java.awt.Dimension;
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53
import java.awt.image.DataBuffer;
54
import java.io.File;
55
import java.util.Vector;
56

    
57
import javax.swing.AbstractCellEditor;
58
import javax.swing.JList;
59
import javax.swing.JPanel;
60
import javax.swing.JRadioButton;
61
import javax.swing.JScrollPane;
62
import javax.swing.JTable;
63
import javax.swing.SwingConstants;
64
import javax.swing.SwingUtilities;
65
import javax.swing.event.TableModelEvent;
66
import javax.swing.event.TableModelListener;
67
import javax.swing.table.DefaultTableModel;
68
import javax.swing.table.TableCellEditor;
69
import javax.swing.table.TableCellRenderer;
70
import javax.swing.table.TableColumn;
71

    
72
import org.cresques.io.GeoRasterFile;
73

    
74
/**
75
 * Selecciona las bandas visibles en un raster
76
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
77
 */
78
public class BandSetupPanel extends JPanel implements TableModelListener {
79
        public String nom = "Bandas";
80
        public static final int RED_BAND        = GeoRasterFile.RED_BAND;
81
        public static final int GREEN_BAND        = GeoRasterFile.GREEN_BAND;
82
        public static final int BLUE_BAND        = GeoRasterFile.BLUE_BAND;
83
        private FileList fileList = null;
84
        private JTable rgbTable = null;
85
        private JScrollPane rgbBandAssignPane = null;
86
        RGBBandAsignTableModel tableModel = null;
87
        private int sizeX = 445, sizeY = 174;
88
        /**
89
         * This method initializes 
90
         * 
91
         */
92
        public BandSetupPanel() {
93
                super();
94
                initialize();
95
        }
96
        /**
97
         * This method initializes this
98
         * 
99
         * @return void
100
         */
101
        void initialize() {
102
        this.setPreferredSize(new Dimension(sizeX, sizeY));
103
        this.setLayout(null);
104
        this.setLocation(0, 0);
105
        this.setSize(445, 239);
106
        this.add(getFileList(), null);
107
        this.add(getRGBBandAssignPane(), null);
108
        }
109
        
110
        /**
111
         * A?ade la lista de georasterfiles a la tabla
112
         * @param files
113
         */
114
        public void addFiles(GeoRasterFile [] files) {
115
                for (int i=0; i<files.length; i++) {
116
                        String fName = files[i].getName();
117
                        getFileList().addFName(fName);
118
                        //((DefaultListModel)fileList.getJList().getModel()).addElement(fName);
119
                        String bandName = new File(fName).getName();
120
                        String bandType = "";
121
                        switch(files[i].getDataType()){
122
                         case DataBuffer.TYPE_BYTE:bandType = "8U";break;
123
                         case DataBuffer.TYPE_INT:bandType = "32";break;
124
                         case DataBuffer.TYPE_DOUBLE:bandType = "64";break;
125
                         case DataBuffer.TYPE_FLOAT:bandType = "32";break;
126
                         case DataBuffer.TYPE_SHORT        :bandType = "16";break;
127
                         case DataBuffer.TYPE_USHORT:bandType = "16U";break;
128
                         case DataBuffer.TYPE_UNDEFINED:bandType = "??";break;
129
                        }
130
                        if (files[i].getBandCount() > 1) {
131
                                for (int b=0; b<files[i].getBandCount(); b++)
132
                                        addBand((b+1)+" ["+bandType+"] "+bandName);
133
                        } else
134
                                addBand("1 ["+bandType+"] "+bandName);
135
                }
136
        }
137
        
138
        /**
139
         * Elimina un fichero de la lista
140
         * @param file Nombre del fichero a eliminar
141
         */
142
        public void removeFile(String file){
143
        getFileList().removeFName(file);
144
        for(int i=0; i<((DefaultTableModel) rgbTable.getModel()).getRowCount();i++){
145
                
146
                //Si el fichero borrado estaba seleccionado como banda visible. Pasaremos
147
                //esta visibilidad a la banda inmediata superior y si esta acci?n produce 
148
                //una excepci?n (porque no hay) se pasa al inmediato inferior.
149
        
150
                if(((String)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,3)).endsWith(file)){
151
                
152
                        try{
153
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,0)).booleanValue() )
154
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 0);            
155
                        }catch(ArrayIndexOutOfBoundsException exc){
156
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 0);
157
                        }
158
                        
159
                        try{
160
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,1)).booleanValue() )
161
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 1);            
162
                        }catch(ArrayIndexOutOfBoundsException exc){
163
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 1);
164
                        }
165
                        
166
                        try{
167
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,2)).booleanValue() )
168
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 2);            
169
                        }catch(ArrayIndexOutOfBoundsException exc){
170
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 2);
171
                        }
172
 
173
                ((DefaultTableModel) rgbTable.getModel()).removeRow(i);
174
                i--; //Ojo! que hemos eliminado una fila
175
            }
176
        }
177
        }
178
        
179
        
180
        public FileList getFileList() {
181
                if (fileList == null) {
182
                        fileList = new FileList();
183
                        fileList.setBounds(9, 9, 428, 110);
184
                }
185
                return fileList;
186
        }
187

    
188
        private final static String[] columnNames = {"R","G","B","Band"};
189
/*        private Object[][] data = {
190
                {new Boolean(true),new Boolean(true),new Boolean(true),""},
191
                {new Boolean(false),new Boolean(false),new Boolean(false),""},
192
//                {new Boolean(false),new Boolean(false),new Boolean(false),""}
193
        };*/
194
         
195
        class RadioColumnEditor extends AbstractCellEditor implements TableCellEditor {
196
                public JRadioButton theRadioButton;
197
 
198
                public RadioColumnEditor() {
199
                        super();
200
                        theRadioButton = new JRadioButton();
201
                        theRadioButton.addActionListener(new ActionListener() {
202
                                public void actionPerformed(ActionEvent event) {
203
                                        fireEditingStopped();
204
                                }
205
                        });
206
                }
207
 
208
                public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int col) {
209
                        theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
210
                        Boolean lValueAsBoolean = (Boolean)obj;
211
                        theRadioButton.setSelected(lValueAsBoolean.booleanValue());
212
                        return theRadioButton;
213
                }
214
 
215
                public Object getCellEditorValue() {
216
                        return new Boolean(theRadioButton.isSelected());
217
                }
218
        }
219
 
220
        class RadioColumnRenderer extends JRadioButton implements TableCellRenderer {
221
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
222
                        if (value == null) {
223
                                this.setSelected(false);
224
                        }
225
                        Boolean ValueAsBoolean = (Boolean)value;
226
                        this.setSelected(ValueAsBoolean.booleanValue());
227
                        this.setHorizontalAlignment(SwingConstants.CENTER);
228
                        return this;
229
                }
230
        }
231
 
232
        class RGBBandAsignTableModel extends DefaultTableModel {
233
                public RGBBandAsignTableModel() {
234
                        super(new Object[0][4], columnNames);
235
                }
236
         
237
                public Class getColumnClass(int c) {
238
                        if (c <3) return Boolean.class;
239
                        return String.class;
240
                }
241
         
242
                public void setValueAt(Object value, int row, int col) {
243
                        if (col < 3 && ((Boolean)value).booleanValue()) {
244
                                for (int i = 0; i < getRowCount(); i++) {
245
                                        if (i != row) {
246
                                                setValueAt(new Boolean(false), i, col);
247
                                        }
248
                                }
249
                        }
250
                        super.setValueAt(value, row, col);
251
                }
252
         
253
                public void addNew() {
254
                        super.addRow(new Object[]{new Boolean(false),new Boolean(false),new Boolean(false),""});
255
                }
256
        }
257

    
258
        /**
259
         * This method initializes jTable        
260
         *         
261
         * @return javax.swing.JTable
262
         */
263
        private JScrollPane getRGBBandAssignPane() {
264
                if (rgbBandAssignPane == null) {
265
                        rgbBandAssignPane = new JScrollPane(getRGBTable());
266
                        rgbBandAssignPane.setBounds(10, 123, 427, 104);
267
                        TableColumn column = null;
268
                        for (int i=0; i<3; i++) {
269
                                column = rgbTable.getColumnModel().getColumn(i);
270
                                column.setCellRenderer(new RadioColumnRenderer());
271
                                column.setCellEditor(new RadioColumnEditor());
272
                                column.setMaxWidth(22);
273
                                column.setMinWidth(22);
274
                        }
275
//                        frame.setContentPane(scrollPane);
276
                }
277
                return rgbBandAssignPane;
278
        }
279
        
280
        public JTable getRGBTable() {
281
                if (rgbTable == null) {
282
                        tableModel =  new RGBBandAsignTableModel();
283
                        tableModel.addTableModelListener(this);
284
                        rgbTable = new JTable(tableModel);
285
                        rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72));
286
                }
287
                return rgbTable;
288
        }
289
                
290
        private void addBand(String bandName) {
291
                Vector v = new Vector();
292
                v.add(new Boolean(false));
293
                v.add(new Boolean(false));
294
                v.add(new Boolean(false));
295
                v.add(bandName);
296
                ((DefaultTableModel) rgbTable.getModel()).addRow(v);
297
        }
298

    
299
        /**
300
         * Obtiene el n?mero de bandas de la lista
301
         * @return
302
         */
303
        public int getNBands(){
304
                return ((DefaultTableModel) rgbTable.getModel()).getRowCount();
305
        }
306
        
307
        /**
308
         * Obtiene el nombre de la banda de la posici?n i de la tabla
309
         * @param i
310
         * @return
311
         */
312
        public String getBandName(int i){
313
                String s = (String)((DefaultTableModel) rgbTable.getModel()).getValueAt(i,3);
314
                return s.substring(s.lastIndexOf("[8U]")+5, s.length());
315
        }
316
        
317
        public void assignBand(int nBand, int flag) {
318
                Boolean t = new Boolean(true);
319
                if ((flag & RED_BAND) == RED_BAND)
320
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 0);
321
                if ((flag & GREEN_BAND) == GREEN_BAND)
322
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 1);
323
                if ((flag & BLUE_BAND) == BLUE_BAND)
324
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 2);
325
        }
326
        
327
        public int getAssignedBand(int flag) {
328
                if ((flag & RED_BAND) == RED_BAND)
329
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
330
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 0)).booleanValue())
331
                                        return nBand;
332
                if ((flag & GREEN_BAND) == GREEN_BAND)
333
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
334
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 1)).booleanValue())
335
                                        return nBand;
336
                if ((flag & BLUE_BAND) == BLUE_BAND)
337
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
338
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 2)).booleanValue())
339
                                        return nBand;
340
                return -1;
341
        }
342
        
343
        /* (non-Javadoc)
344
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
345
         */
346
        public void tableChanged(TableModelEvent e) {
347
                rgbTable.revalidate();
348
                rgbBandAssignPane.revalidate();
349
                revalidate();
350
        }
351
 }  //  @jve:decl-index=0:visual-constraint="10,10"