Statistics
| Revision:

svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / BandSetupPanel.java @ 1928

History | View | Annotate | Download (9.36 KB)

1
/*
2
 * Created on 18-feb-2005
3
 */
4
package org.cresques.ui.raster;
5

    
6
import java.awt.Component;
7
import java.awt.Dimension;
8
import java.awt.event.ActionEvent;
9
import java.awt.event.ActionListener;
10
import java.io.File;
11
import java.util.Vector;
12

    
13
import javax.swing.AbstractCellEditor;
14
import javax.swing.JList;
15
import javax.swing.JPanel;
16
import javax.swing.JRadioButton;
17
import javax.swing.JScrollPane;
18
import javax.swing.JTable;
19
import javax.swing.SwingConstants;
20
import javax.swing.SwingUtilities;
21
import javax.swing.event.TableModelEvent;
22
import javax.swing.event.TableModelListener;
23
import javax.swing.table.DefaultTableModel;
24
import javax.swing.table.TableCellEditor;
25
import javax.swing.table.TableCellRenderer;
26
import javax.swing.table.TableColumn;
27

    
28
import org.cresques.io.GeoRasterFile;
29

    
30
/**
31
 * Selecciona las bandas visibles en un raster
32
 * @author Luis W. Sevilla (sevilla_lui@gva.es)
33
 */
34
public class BandSetupPanel extends JPanel implements TableModelListener {
35
        public String nom = "Bandas";
36
        public static final int RED_BAND        = GeoRasterFile.RED_BAND;
37
        public static final int GREEN_BAND        = GeoRasterFile.GREEN_BAND;
38
        public static final int BLUE_BAND        = GeoRasterFile.BLUE_BAND;
39
        private FileList fileList = null;
40
        private JTable rgbTable = null;
41
        private JScrollPane rgbBandAssignPane = null;
42
        RGBBandAsignTableModel tableModel = null;
43
        /**
44
         * This method initializes 
45
         * 
46
         */
47
        public BandSetupPanel() {
48
                super();
49
                initialize();
50
        }
51
        /**
52
         * This method initializes this
53
         * 
54
         * @return void
55
         */
56
        void initialize() {
57
        this.setPreferredSize(new Dimension(345,174));
58
        this.setBounds(0, 0, 345, 174);
59
        this.setLayout(null);
60
        this.add(getFileList(), null);
61
        this.add(getRGBBandAssignPane(), null);
62
        }
63
        
64
        /**
65
         * A?ade la lista de georasterfiles a la tabla
66
         * @param files
67
         */
68
        public void addFiles(GeoRasterFile [] files) {
69
                for (int i=0; i<files.length; i++) {
70
                        String fName = files[i].getName();
71
                        getFileList().addFName(fName);
72
                        //((DefaultListModel)fileList.getJList().getModel()).addElement(fName);
73
                        String bandName = new File(fName).getName();
74
                        String bandType = "8U";
75
                        if (files[i].getBandCount() > 1) {
76
                                for (int b=0; b<files[i].getBandCount(); b++)
77
                                        addBand((b+1)+" ["+bandType+"] "+bandName);
78
                        } else
79
                                addBand("1 ["+bandType+"] "+bandName);
80
                }
81
        }
82
        
83
        /**
84
         * Elimina un fichero de la lista
85
         * @param file Nombre del fichero a eliminar
86
         */
87
        public void removeFile(String file){
88
        getFileList().removeFName(file);
89
        for(int i=0; i<((DefaultTableModel) rgbTable.getModel()).getRowCount();i++){
90
                
91
                //Si el fichero borrado estaba seleccionado como banda visible. Pasaremos
92
                //esta visibilidad a la banda inmediata superior y si esta acci?n produce 
93
                //una excepci?n (porque no hay) se pasa al inmediato inferior.
94
        
95
                if(((String)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,3)).endsWith(file)){
96
                
97
                        try{
98
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,0)).booleanValue() )
99
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 0);            
100
                        }catch(ArrayIndexOutOfBoundsException exc){
101
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 0);
102
                        }
103
                        
104
                        try{
105
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,1)).booleanValue() )
106
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 1);            
107
                        }catch(ArrayIndexOutOfBoundsException exc){
108
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 1);
109
                        }
110
                        
111
                        try{
112
                                if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,2)).booleanValue() )
113
                                            ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 2);            
114
                        }catch(ArrayIndexOutOfBoundsException exc){
115
                                ((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 2);
116
                        }
117
 
118
                ((DefaultTableModel) rgbTable.getModel()).removeRow(i);
119
                i--; //Ojo! que hemos eliminado una fila
120
            }
121
        }
122
        }
123
        
124
        
125
        public FileList getFileList() {
126
                if (fileList == null) {
127
                        fileList = new FileList();
128
                        fileList.setBounds(9, 9, 328, 75);
129
                }
130
                return fileList;
131
        }
132

    
133
        private final static String[] columnNames = {"R","G","B","Band"};
134
/*        private Object[][] data = {
135
                {new Boolean(true),new Boolean(true),new Boolean(true),""},
136
                {new Boolean(false),new Boolean(false),new Boolean(false),""},
137
//                {new Boolean(false),new Boolean(false),new Boolean(false),""}
138
        };*/
139
         
140
        class RadioColumnEditor extends AbstractCellEditor implements TableCellEditor {
141
                public JRadioButton theRadioButton;
142
 
143
                public RadioColumnEditor() {
144
                        super();
145
                        theRadioButton = new JRadioButton();
146
                        theRadioButton.addActionListener(new ActionListener() {
147
                                public void actionPerformed(ActionEvent event) {
148
                                        fireEditingStopped();
149
                                }
150
                        });
151
                }
152
 
153
                public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int col) {
154
                        theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
155
                        Boolean lValueAsBoolean = (Boolean)obj;
156
                        theRadioButton.setSelected(lValueAsBoolean.booleanValue());
157
                        return theRadioButton;
158
                }
159
 
160
                public Object getCellEditorValue() {
161
                        return new Boolean(theRadioButton.isSelected());
162
                }
163
        }
164
 
165
        class RadioColumnRenderer extends JRadioButton implements TableCellRenderer {
166
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
167
                        if (value == null) {
168
                                this.setSelected(false);
169
                        }
170
                        Boolean ValueAsBoolean = (Boolean)value;
171
                        this.setSelected(ValueAsBoolean.booleanValue());
172
                        this.setHorizontalAlignment(SwingConstants.CENTER);
173
                        return this;
174
                }
175
        }
176
 
177
        class RGBBandAsignTableModel extends DefaultTableModel {
178
                public RGBBandAsignTableModel() {
179
                        super(new Object[0][4], columnNames);
180
                }
181
         
182
                public Class getColumnClass(int c) {
183
                        if (c <3) return Boolean.class;
184
                        return String.class;
185
                }
186
         
187
                public void setValueAt(Object value, int row, int col) {
188
                        if (col < 3 && ((Boolean)value).booleanValue()) {
189
                                for (int i = 0; i < getRowCount(); i++) {
190
                                        if (i != row) {
191
                                                setValueAt(new Boolean(false), i, col);
192
                                        }
193
                                }
194
                        }
195
                        super.setValueAt(value, row, col);
196
                }
197
         
198
                public void addNew() {
199
                        super.addRow(new Object[]{new Boolean(false),new Boolean(false),new Boolean(false),""});
200
                }
201
        }
202

    
203
        /**
204
         * This method initializes jTable        
205
         *         
206
         * @return javax.swing.JTable
207
         */
208
        private JScrollPane getRGBBandAssignPane() {
209
                if (rgbBandAssignPane == null) {
210
                        rgbBandAssignPane = new JScrollPane(getRGBTable());
211
                        rgbBandAssignPane.setBounds(9, 95, 328, 72);
212
                        TableColumn column = null;
213
                        for (int i=0; i<3; i++) {
214
                                column = rgbTable.getColumnModel().getColumn(i);
215
                                column.setCellRenderer(new RadioColumnRenderer());
216
                                column.setCellEditor(new RadioColumnEditor());
217
                                column.setMaxWidth(22);
218
                                column.setMinWidth(22);
219
                        }
220
//                        frame.setContentPane(scrollPane);
221
                }
222
                return rgbBandAssignPane;
223
        }
224
        
225
        public JTable getRGBTable() {
226
                if (rgbTable == null) {
227
                        tableModel =  new RGBBandAsignTableModel();
228
                        tableModel.addTableModelListener(this);
229
                        rgbTable = new JTable(tableModel);
230
                        rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72));
231
                }
232
                return rgbTable;
233
        }
234
                
235
        private void addBand(String bandName) {
236
                Vector v = new Vector();
237
                v.add(new Boolean(false));
238
                v.add(new Boolean(false));
239
                v.add(new Boolean(false));
240
                v.add(bandName);
241
                ((DefaultTableModel) rgbTable.getModel()).addRow(v);
242
        }
243

    
244
        /**
245
         * Obtiene el n?mero de bandas de la lista
246
         * @return
247
         */
248
        public int getNBands(){
249
                return ((DefaultTableModel) rgbTable.getModel()).getRowCount();
250
        }
251
        
252
        /**
253
         * Obtiene el nombre de la banda de la posici?n i de la tabla
254
         * @param i
255
         * @return
256
         */
257
        public String getBandName(int i){
258
                String s = (String)((DefaultTableModel) rgbTable.getModel()).getValueAt(i,3);
259
                return s.substring(s.lastIndexOf("[8U]")+5, s.length());
260
        }
261
        
262
        public void assignBand(int nBand, int flag) {
263
                Boolean t = new Boolean(true);
264
                if ((flag & RED_BAND) == RED_BAND)
265
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 0);
266
                if ((flag & GREEN_BAND) == GREEN_BAND)
267
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 1);
268
                if ((flag & BLUE_BAND) == BLUE_BAND)
269
                        ((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 2);
270
        }
271
        
272
        public int getAssignedBand(int flag) {
273
                if ((flag & RED_BAND) == RED_BAND)
274
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
275
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 0)).booleanValue())
276
                                        return nBand;
277
                if ((flag & GREEN_BAND) == GREEN_BAND)
278
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
279
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 1)).booleanValue())
280
                                        return nBand;
281
                if ((flag & BLUE_BAND) == BLUE_BAND)
282
                        for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++)
283
                                if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 2)).booleanValue())
284
                                        return nBand;
285
                return -1;
286
        }
287
        
288
        /* (non-Javadoc)
289
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
290
         */
291
        public void tableChanged(TableModelEvent e) {
292
                rgbTable.revalidate();
293
                rgbBandAssignPane.revalidate();
294
                revalidate();
295
        }
296
 }  //  @jve:decl-index=0:visual-constraint="10,10"