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