svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / BandSetupPanel.java @ 2360
History | View | Annotate | Download (12.3 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 java.awt.Component; |
27 |
import java.awt.Dimension; |
28 |
import java.awt.event.ActionEvent; |
29 |
import java.awt.event.ActionListener; |
30 |
import java.awt.event.KeyEvent; |
31 |
import java.awt.event.KeyListener; |
32 |
import java.awt.event.MouseEvent; |
33 |
import java.awt.event.MouseListener; |
34 |
import java.awt.image.DataBuffer; |
35 |
import java.io.File; |
36 |
import java.util.Vector; |
37 |
|
38 |
import javax.swing.AbstractCellEditor; |
39 |
import javax.swing.JPanel; |
40 |
import javax.swing.JRadioButton; |
41 |
import javax.swing.JScrollPane; |
42 |
import javax.swing.JTable; |
43 |
import javax.swing.SwingConstants; |
44 |
import javax.swing.SwingUtilities; |
45 |
import javax.swing.event.TableModelEvent; |
46 |
import javax.swing.event.TableModelListener; |
47 |
import javax.swing.table.DefaultTableModel; |
48 |
import javax.swing.table.TableCellEditor; |
49 |
import javax.swing.table.TableCellRenderer; |
50 |
import javax.swing.table.TableColumn; |
51 |
|
52 |
import org.cresques.io.GeoRasterFile; |
53 |
|
54 |
/**
|
55 |
* Selecciona las bandas visibles en un raster
|
56 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
57 |
*/
|
58 |
public class BandSetupPanel extends JPanel implements TableModelListener, KeyListener, ActionListener { |
59 |
|
60 |
|
61 |
/**
|
62 |
* Nombre del panel
|
63 |
*/
|
64 |
public String nom = "Bandas"; |
65 |
/**
|
66 |
* Asigna la banda del rojo
|
67 |
*/
|
68 |
public static final int RED_BAND = GeoRasterFile.RED_BAND; |
69 |
/**
|
70 |
* Asigna la banda del verde
|
71 |
*/
|
72 |
public static final int GREEN_BAND = GeoRasterFile.GREEN_BAND; |
73 |
/**
|
74 |
* Asigna banda del azul
|
75 |
*/
|
76 |
public static final int BLUE_BAND = GeoRasterFile.BLUE_BAND; |
77 |
private FileList fileList = null; |
78 |
private JTable rgbTable = null; |
79 |
private JScrollPane rgbBandAssignPane = null; |
80 |
RGBBandAsignTableModel tableModel = null;
|
81 |
private int sizeX = 445, sizeY = 174; |
82 |
private byte mode = 3; |
83 |
|
84 |
/**
|
85 |
* This method initializes
|
86 |
*
|
87 |
*/
|
88 |
public BandSetupPanel() {
|
89 |
super();
|
90 |
initialize(); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* This method initializes this
|
95 |
*
|
96 |
* @return void
|
97 |
*/
|
98 |
void initialize() {
|
99 |
this.setPreferredSize(new Dimension(sizeX, sizeY)); |
100 |
this.setLayout(null); |
101 |
this.setLocation(0, 0); |
102 |
this.setSize(445, 239); |
103 |
this.add(getFileList(), null); |
104 |
this.add(getRGBBandAssignPane(), null); |
105 |
getFileList().getJComboBox().addKeyListener(this);
|
106 |
getFileList().getJComboBox().addActionListener(this);
|
107 |
} |
108 |
|
109 |
/**
|
110 |
* A?ade la lista de georasterfiles a la tabla
|
111 |
* @param files
|
112 |
*/
|
113 |
public void addFiles(GeoRasterFile [] files) { |
114 |
for (int i=0; i<files.length; i++) { |
115 |
String fName = files[i].getName();
|
116 |
getFileList().addFName(fName); |
117 |
//((DefaultListModel)fileList.getJList().getModel()).addElement(fName);
|
118 |
String bandName = new File(fName).getName(); |
119 |
String bandType = ""; |
120 |
switch(files[i].getDataType()){
|
121 |
case DataBuffer.TYPE_BYTE:bandType = "8U";break; |
122 |
case DataBuffer.TYPE_INT:bandType = "32";break; |
123 |
case DataBuffer.TYPE_DOUBLE:bandType = "64";break; |
124 |
case DataBuffer.TYPE_FLOAT:bandType = "32";break; |
125 |
case DataBuffer.TYPE_SHORT :bandType = "16";break; |
126 |
case DataBuffer.TYPE_USHORT:bandType = "16U";break; |
127 |
case DataBuffer.TYPE_UNDEFINED:bandType = "??";break; |
128 |
} |
129 |
if (files[i].getBandCount() > 1) { |
130 |
for (int b=0; b<files[i].getBandCount(); b++) |
131 |
addBand((b+1)+" ["+bandType+"] "+bandName); |
132 |
} else
|
133 |
addBand("1 ["+bandType+"] "+bandName); |
134 |
} |
135 |
} |
136 |
|
137 |
/**
|
138 |
* Elimina un fichero de la lista
|
139 |
* @param file Nombre del fichero a eliminar
|
140 |
*/
|
141 |
public void removeFile(String file){ |
142 |
getFileList().removeFName(file); |
143 |
for(int i=0; i<((DefaultTableModel) rgbTable.getModel()).getRowCount();i++){ |
144 |
|
145 |
//Si el fichero borrado estaba seleccionado como banda visible. Pasaremos
|
146 |
//esta visibilidad a la banda inmediata superior y si esta acci?n produce
|
147 |
//una excepci?n (porque no hay) se pasa al inmediato inferior.
|
148 |
|
149 |
if(((String)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,3)).endsWith(file)){ |
150 |
|
151 |
try{
|
152 |
if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,0)).booleanValue() ) |
153 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 0); |
154 |
}catch(ArrayIndexOutOfBoundsException exc){ |
155 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 0); |
156 |
} |
157 |
|
158 |
try{
|
159 |
if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,1)).booleanValue() ) |
160 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 1); |
161 |
}catch(ArrayIndexOutOfBoundsException exc){ |
162 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 1); |
163 |
} |
164 |
|
165 |
try{
|
166 |
if( ((Boolean)((DefaultTableModel)rgbTable.getModel()).getValueAt(i,2)).booleanValue() ) |
167 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i-1, 2); |
168 |
}catch(ArrayIndexOutOfBoundsException exc){ |
169 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true),i+1, 2); |
170 |
} |
171 |
|
172 |
((DefaultTableModel) rgbTable.getModel()).removeRow(i);
|
173 |
i--; //Ojo! que hemos eliminado una fila
|
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Obtiene el panel que contiene la lista de ficheros por banda.
|
180 |
* @return Panel FileList
|
181 |
*/
|
182 |
public FileList getFileList() {
|
183 |
if (fileList == null) { |
184 |
fileList = new FileList();
|
185 |
fileList.setBounds(9, 9, 428, 110); |
186 |
} |
187 |
return fileList;
|
188 |
} |
189 |
|
190 |
private final static String[] columnNames = {"R","G","B","Band"}; |
191 |
|
192 |
class RadioColumnEditor extends AbstractCellEditor implements TableCellEditor { |
193 |
public JRadioButton theRadioButton; |
194 |
|
195 |
public RadioColumnEditor() {
|
196 |
super();
|
197 |
theRadioButton = new JRadioButton(); |
198 |
theRadioButton.addActionListener(new ActionListener() { |
199 |
public void actionPerformed(ActionEvent event) { |
200 |
fireEditingStopped(); |
201 |
} |
202 |
}); |
203 |
} |
204 |
|
205 |
public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int col) { |
206 |
theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
|
207 |
Boolean lValueAsBoolean = (Boolean)obj; |
208 |
theRadioButton.setSelected(lValueAsBoolean.booleanValue()); |
209 |
return theRadioButton;
|
210 |
} |
211 |
|
212 |
public Object getCellEditorValue() { |
213 |
return new Boolean(theRadioButton.isSelected()); |
214 |
} |
215 |
} |
216 |
|
217 |
class RadioColumnRenderer extends JRadioButton implements TableCellRenderer { |
218 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
219 |
if (value == null) { |
220 |
this.setSelected(false); |
221 |
} |
222 |
Boolean ValueAsBoolean = (Boolean)value; |
223 |
this.setSelected(ValueAsBoolean.booleanValue());
|
224 |
this.setHorizontalAlignment(SwingConstants.CENTER); |
225 |
return this; |
226 |
} |
227 |
} |
228 |
|
229 |
class RGBBandAsignTableModel extends DefaultTableModel { |
230 |
public RGBBandAsignTableModel() {
|
231 |
super(new Object[0][4], columnNames); |
232 |
} |
233 |
|
234 |
public Class getColumnClass(int c) { |
235 |
if (c <3) return Boolean.class; |
236 |
return String.class; |
237 |
} |
238 |
|
239 |
public void setValueAt(Object value, int row, int col) { |
240 |
if (col < 3 && ((Boolean)value).booleanValue()) { |
241 |
for (int i = 0; i < getRowCount(); i++) { |
242 |
if (i != row) {
|
243 |
setValueAt(new Boolean(false), i, col); |
244 |
} |
245 |
} |
246 |
} |
247 |
super.setValueAt(value, row, col);
|
248 |
} |
249 |
|
250 |
public void addNew() { |
251 |
super.addRow(new Object[]{new Boolean(false),new Boolean(false),new Boolean(false),""}); |
252 |
} |
253 |
} |
254 |
|
255 |
/**
|
256 |
* This method initializes jTable
|
257 |
*
|
258 |
* @return javax.swing.JTable
|
259 |
*/
|
260 |
private JScrollPane getRGBBandAssignPane() { |
261 |
if (rgbBandAssignPane == null) { |
262 |
rgbBandAssignPane = new JScrollPane(getRGBTable()); |
263 |
rgbBandAssignPane.setBounds(10, 123, 427, 104); |
264 |
TableColumn column = null; |
265 |
for (int i=0; i<3; i++) { |
266 |
column = rgbTable.getColumnModel().getColumn(i); |
267 |
column.setCellRenderer(new RadioColumnRenderer());
|
268 |
column.setCellEditor(new RadioColumnEditor());
|
269 |
column.setMaxWidth(22);
|
270 |
column.setMinWidth(22);
|
271 |
} |
272 |
// frame.setContentPane(scrollPane);
|
273 |
} |
274 |
return rgbBandAssignPane;
|
275 |
} |
276 |
|
277 |
public JTable getRGBTable() { |
278 |
if (rgbTable == null) { |
279 |
tableModel = new RGBBandAsignTableModel();
|
280 |
tableModel.addTableModelListener(this);
|
281 |
rgbTable = new JTable(tableModel); |
282 |
rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72)); |
283 |
} |
284 |
return rgbTable;
|
285 |
} |
286 |
|
287 |
/**
|
288 |
* A?ade una banda a la tabla bandas de la imagen asignandole un
|
289 |
* nombre y valor a los checkbox
|
290 |
* @param bandName Nombre de la banda
|
291 |
*/
|
292 |
private void addBand(String bandName) { |
293 |
Vector v = new Vector(); |
294 |
v.add(new Boolean(false)); |
295 |
v.add(new Boolean(false)); |
296 |
v.add(new Boolean(false)); |
297 |
v.add(bandName); |
298 |
((DefaultTableModel) rgbTable.getModel()).addRow(v);
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* Obtiene el n?mero de bandas de la lista
|
303 |
* @return
|
304 |
*/
|
305 |
public int getNBands(){ |
306 |
return ((DefaultTableModel) rgbTable.getModel()).getRowCount(); |
307 |
} |
308 |
|
309 |
/**
|
310 |
* Obtiene el nombre de la banda de la posici?n i de la tabla
|
311 |
* @param i
|
312 |
* @return
|
313 |
*/
|
314 |
public String getBandName(int i){ |
315 |
String s = (String)((DefaultTableModel) rgbTable.getModel()).getValueAt(i,3); |
316 |
return s.substring(s.lastIndexOf("[8U]")+5, s.length()); |
317 |
} |
318 |
|
319 |
public void assignBand(int nBand, int flag) { |
320 |
Boolean t = new Boolean(true); |
321 |
if ((flag & RED_BAND) == RED_BAND)
|
322 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 0); |
323 |
if ((flag & GREEN_BAND) == GREEN_BAND)
|
324 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 1); |
325 |
if ((flag & BLUE_BAND) == BLUE_BAND)
|
326 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(t, nBand, 2); |
327 |
} |
328 |
|
329 |
public int getAssignedBand(int flag) { |
330 |
if ((flag & RED_BAND) == RED_BAND)
|
331 |
for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++) |
332 |
if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 0)).booleanValue()) |
333 |
return nBand;
|
334 |
if ((flag & GREEN_BAND) == GREEN_BAND)
|
335 |
for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++) |
336 |
if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 1)).booleanValue()) |
337 |
return nBand;
|
338 |
if ((flag & BLUE_BAND) == BLUE_BAND)
|
339 |
for (int nBand=0; nBand<rgbTable.getRowCount(); nBand++) |
340 |
if (((Boolean)((DefaultTableModel) rgbTable.getModel()).getValueAt(nBand, 2)).booleanValue()) |
341 |
return nBand;
|
342 |
return -1; |
343 |
} |
344 |
|
345 |
/* (non-Javadoc)
|
346 |
* @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
|
347 |
*/
|
348 |
public void tableChanged(TableModelEvent e) { |
349 |
rgbTable.revalidate(); |
350 |
rgbBandAssignPane.revalidate(); |
351 |
revalidate(); |
352 |
} |
353 |
|
354 |
/* (non-Javadoc)
|
355 |
* @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
|
356 |
*/
|
357 |
public void keyPressed(KeyEvent arg0) { |
358 |
// TODO Auto-generated method stub
|
359 |
} |
360 |
/* (non-Javadoc)
|
361 |
* @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
|
362 |
*/
|
363 |
public void keyReleased(KeyEvent arg0) { |
364 |
// TODO Auto-generated method stub
|
365 |
} |
366 |
/* (non-Javadoc)
|
367 |
* @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
|
368 |
*/
|
369 |
public void keyTyped(KeyEvent arg0) { |
370 |
// TODO Auto-generated method stub
|
371 |
} |
372 |
|
373 |
/* (non-Javadoc)
|
374 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
375 |
*/
|
376 |
public void actionPerformed(ActionEvent arg0) { |
377 |
String vBands = (String)getFileList().getJComboBox().getSelectedItem(); |
378 |
if(vBands.compareTo("BANDAS") == 0 || vBands.compareTo("3") == 0){ |
379 |
mode = 3;
|
380 |
} |
381 |
if(vBands.compareTo("2") == 0){ |
382 |
mode = 2;
|
383 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true), 0, 2); |
384 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true), 1, 1); |
385 |
((DefaultTableModel) rgbTable.getModel()).setValueAt(new Boolean(true), 2, 0); |
386 |
} |
387 |
if(vBands.compareTo("1") == 0){ |
388 |
mode = 1;
|
389 |
} |
390 |
} |
391 |
} // @jve:decl-index=0:visual-constraint="10,10"
|