svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / ui / raster / BandSetupPanel.java @ 1751
History | View | Annotate | Download (9.52 KB)
1 | 1527 | luisw | /*
|
---|---|---|---|
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.JPanel; |
||
15 | import javax.swing.JRadioButton; |
||
16 | import javax.swing.JScrollPane; |
||
17 | import javax.swing.JTable; |
||
18 | import javax.swing.SwingConstants; |
||
19 | import javax.swing.SwingUtilities; |
||
20 | import javax.swing.event.TableModelEvent; |
||
21 | import javax.swing.event.TableModelListener; |
||
22 | import javax.swing.table.DefaultTableModel; |
||
23 | import javax.swing.table.TableCellEditor; |
||
24 | import javax.swing.table.TableCellRenderer; |
||
25 | import javax.swing.table.TableColumn; |
||
26 | |||
27 | import org.cresques.io.GeoRasterFile; |
||
28 | |||
29 | /**
|
||
30 | * Selecciona las bandas visibles en un raster
|
||
31 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
32 | */
|
||
33 | public class BandSetupPanel extends JPanel implements TableModelListener { |
||
34 | 1751 | igbrotru | public String nom = "Bandas"; |
35 | 1527 | luisw | public static final int RED_BAND = GeoRasterFile.RED_BAND; |
36 | public static final int GREEN_BAND = GeoRasterFile.GREEN_BAND; |
||
37 | public static final int BLUE_BAND = GeoRasterFile.BLUE_BAND; |
||
38 | private FileList fileList = null; |
||
39 | private JTable rgbTable = null; |
||
40 | private JScrollPane rgbBandAssignPane = null; |
||
41 | RGBBandAsignTableModel tableModel = null;
|
||
42 | /**
|
||
43 | * This method initializes
|
||
44 | *
|
||
45 | */
|
||
46 | public BandSetupPanel() {
|
||
47 | super();
|
||
48 | initialize(); |
||
49 | } |
||
50 | /**
|
||
51 | * This method initializes this
|
||
52 | *
|
||
53 | * @return void
|
||
54 | */
|
||
55 | void initialize() {
|
||
56 | this.setPreferredSize(new Dimension(345,174)); |
||
57 | this.setBounds(0, 0, 345, 174); |
||
58 | this.setLayout(null); |
||
59 | this.add(getFileList(), null); |
||
60 | this.add(getRGBBandAssignPane(), null); |
||
61 | } |
||
62 | |||
63 | public void addFiles(GeoRasterFile [] files) { |
||
64 | System.out.println("BSP addFiles"); |
||
65 | for (int i=0; i<files.length; i++) { |
||
66 | String fName = files[i].getName();
|
||
67 | getFileList().addFName(fName); |
||
68 | //((DefaultListModel)fileList.getJList().getModel()).addElement(fName);
|
||
69 | String bandName = new File(fName).getName(); |
||
70 | String bandType = "8U"; |
||
71 | if (files[i].getBandCount() > 1) { |
||
72 | for (int b=0; b<files[i].getBandCount(); b++) |
||
73 | addBand((b+1)+" ["+bandType+"] "+bandName); |
||
74 | } else
|
||
75 | addBand("1 ["+bandType+"] "+bandName); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | private FileList getFileList() {
|
||
80 | if (fileList == null) { |
||
81 | fileList = new FileList();
|
||
82 | fileList.setBounds(9, 9, 328, 75); |
||
83 | /* listManagerDemoSkin.getListManager().setListener(new ListManagerListener() {
|
||
84 | public Object[] addObjects() {
|
||
85 | fileChooser = new JFileChooser(lastPath);
|
||
86 | fileChooser.setMultiSelectionEnabled(true);
|
||
87 | fileChooser.setAcceptAllFileFilterUsed(false);
|
||
88 | |||
89 | try {
|
||
90 | String[] driverNames = LayerFactory.getDM().getDriverNames();
|
||
91 | FileFilter defaultFileFilter = null, auxF;
|
||
92 | for (int i = 0; i < driverNames.length; i++) {
|
||
93 | System.err.println("DRIVER "+i+" : "+driverNames[i]);
|
||
94 | boolean is = false;
|
||
95 | for (int j = 0; j < driverClasses.length; j++) {
|
||
96 | if (i == 0) System.err.println("DRIVER CLASS "+j+" : "+driverClasses[j].toString());
|
||
97 | if (LayerFactory.getDM().isA(driverNames[i], driverClasses[j]))
|
||
98 | is = true;
|
||
99 | }
|
||
100 | if (is){
|
||
101 | auxF = new DriverFileFilter(driverNames[i]);
|
||
102 | fileChooser.addChoosableFileFilter(auxF);
|
||
103 | if (driverNames[i].indexOf("shp") != -1)
|
||
104 | {
|
||
105 | defaultFileFilter = auxF;
|
||
106 | }
|
||
107 | }
|
||
108 | }
|
||
109 | if (defaultFileFilter != null)
|
||
110 | if (lastFileFilter == null)
|
||
111 | fileChooser.setFileFilter(defaultFileFilter);
|
||
112 | else
|
||
113 | fileChooser.setFileFilter(lastFileFilter);
|
||
114 | } catch (DriverLoadException e1) {
|
||
115 | NotificationManager.addError("No se pudo acceder a los drivers", e1);
|
||
116 | }
|
||
117 | |||
118 | int result = fileChooser.showOpenDialog(FileOpenDialog.this);
|
||
119 | |||
120 | File[] newFiles = null;
|
||
121 | MyFile[] toAdd;
|
||
122 | if (result == JFileChooser.APPROVE_OPTION) {
|
||
123 | lastFileFilter = fileChooser.getFileFilter();
|
||
124 | lastPath = fileChooser.getCurrentDirectory().getAbsolutePath();
|
||
125 | newFiles = fileChooser.getSelectedFiles();
|
||
126 | toAdd = new MyFile[newFiles.length];
|
||
127 | for (int ind=0; ind < newFiles.length; ind++)
|
||
128 | {
|
||
129 | String driverName = ((DriverFileFilter) fileChooser.getFileFilter()).getDescription();
|
||
130 | toAdd[ind] = new MyFile(newFiles[ind], driverName);
|
||
131 | }
|
||
132 | |||
133 | |||
134 | return toAdd;
|
||
135 | }
|
||
136 | else
|
||
137 | return new Object[0];
|
||
138 | |||
139 | // DefaultListModel lstModel = new DefaultListModel();
|
||
140 | |||
141 | }
|
||
142 | |||
143 | public Object getProperties(Object selected) {
|
||
144 | return null;
|
||
145 | }
|
||
146 | });*/
|
||
147 | } |
||
148 | return fileList;
|
||
149 | } |
||
150 | |||
151 | private final static String[] columnNames = {"R","G","B","Band"}; |
||
152 | /* private Object[][] data = {
|
||
153 | {new Boolean(true),new Boolean(true),new Boolean(true),""},
|
||
154 | {new Boolean(false),new Boolean(false),new Boolean(false),""},
|
||
155 | // {new Boolean(false),new Boolean(false),new Boolean(false),""}
|
||
156 | };*/
|
||
157 | |||
158 | class RadioColumnEditor extends AbstractCellEditor implements TableCellEditor { |
||
159 | private JRadioButton theRadioButton; |
||
160 | |||
161 | public RadioColumnEditor() {
|
||
162 | super();
|
||
163 | theRadioButton = new JRadioButton(); |
||
164 | theRadioButton.addActionListener(new ActionListener() { |
||
165 | public void actionPerformed(ActionEvent event) { |
||
166 | fireEditingStopped(); |
||
167 | } |
||
168 | }); |
||
169 | } |
||
170 | |||
171 | public Component getTableCellEditorComponent(JTable table, Object obj, boolean isSelected, int row, int col) { |
||
172 | theRadioButton.setHorizontalAlignment(SwingUtilities.CENTER);
|
||
173 | Boolean lValueAsBoolean = (Boolean)obj; |
||
174 | theRadioButton.setSelected(lValueAsBoolean.booleanValue()); |
||
175 | return theRadioButton;
|
||
176 | } |
||
177 | |||
178 | public Object getCellEditorValue() { |
||
179 | return new Boolean(theRadioButton.isSelected()); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | class RadioColumnRenderer extends JRadioButton implements TableCellRenderer { |
||
184 | public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
||
185 | if (value == null) { |
||
186 | this.setSelected(false); |
||
187 | } |
||
188 | Boolean ValueAsBoolean = (Boolean)value; |
||
189 | this.setSelected(ValueAsBoolean.booleanValue());
|
||
190 | this.setHorizontalAlignment(SwingConstants.CENTER); |
||
191 | return this; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | class RGBBandAsignTableModel extends DefaultTableModel { |
||
196 | public RGBBandAsignTableModel() {
|
||
197 | super(new Object[0][4], columnNames); |
||
198 | } |
||
199 | |||
200 | public Class getColumnClass(int c) { |
||
201 | if (c <3) return Boolean.class; |
||
202 | return String.class; |
||
203 | } |
||
204 | |||
205 | public void setValueAt(Object value, int row, int col) { |
||
206 | if (col < 3 && ((Boolean)value).booleanValue()) { |
||
207 | for (int i = 0; i < getRowCount(); i++) { |
||
208 | if (i != row) {
|
||
209 | setValueAt(new Boolean(false), i, col); |
||
210 | } |
||
211 | } |
||
212 | } |
||
213 | super.setValueAt(value, row, col);
|
||
214 | } |
||
215 | |||
216 | public void addNew() { |
||
217 | super.addRow(new Object[]{new Boolean(false),new Boolean(false),new Boolean(false),""}); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | /**
|
||
222 | * This method initializes jTable
|
||
223 | *
|
||
224 | * @return javax.swing.JTable
|
||
225 | */
|
||
226 | private JScrollPane getRGBBandAssignPane() { |
||
227 | if (rgbBandAssignPane == null) { |
||
228 | rgbBandAssignPane = new JScrollPane(getRGBTable()); |
||
229 | rgbBandAssignPane.setBounds(9, 95, 328, 72); |
||
230 | TableColumn column = null; |
||
231 | for (int i=0; i<3; i++) { |
||
232 | column = rgbTable.getColumnModel().getColumn(i); |
||
233 | column.setCellRenderer(new RadioColumnRenderer());
|
||
234 | column.setCellEditor(new RadioColumnEditor());
|
||
235 | column.setMaxWidth(22);
|
||
236 | column.setMinWidth(22);
|
||
237 | } |
||
238 | // frame.setContentPane(scrollPane);
|
||
239 | } |
||
240 | return rgbBandAssignPane;
|
||
241 | } |
||
242 | |||
243 | private JTable getRGBTable() { |
||
244 | if (rgbTable == null) { |
||
245 | tableModel = new RGBBandAsignTableModel();
|
||
246 | tableModel.addTableModelListener(this);
|
||
247 | rgbTable = new JTable(tableModel); |
||
248 | rgbTable.setPreferredScrollableViewportSize(new Dimension(328, 72)); |
||
249 | } |
||
250 | return rgbTable;
|
||
251 | } |
||
252 | |||
253 | private void addBand(String bandName) { |
||
254 | Vector v = new Vector(); |
||
255 | v.add(new Boolean(false)); |
||
256 | v.add(new Boolean(false)); |
||
257 | v.add(new Boolean(false)); |
||
258 | v.add(bandName); |
||
259 | ((DefaultTableModel) rgbTable.getModel()).addRow(v);
|
||
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 | System.err.println("TableChanged."); |
||
296 | } |
||
297 | } // @jve:decl-index=0:visual-constraint="10,10" |