svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.fmap.mapcontext.swing / org.gvsig.fmap.mapcontext.swing.impl / src / main / java / org / gvsig / fmap / mapcontext / raster / swing / impl / DefaultSelectableBandDescriptorsTableModel.java @ 43876
History | View | Annotate | Download (8.34 KB)
1 |
package org.gvsig.fmap.mapcontext.raster.swing.impl; |
---|---|
2 |
/* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2017 gvSIG Association
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.Collections; |
28 |
import java.util.List; |
29 |
|
30 |
import javax.swing.table.AbstractTableModel; |
31 |
|
32 |
import org.gvsig.fmap.dal.raster.BandDescriptor; |
33 |
import org.gvsig.fmap.mapcontext.raster.swing.SelectableBandDescriptorsTableModel; |
34 |
import org.gvsig.raster.swing.buffer.SelectableBandsTableModel; |
35 |
import org.gvsig.tools.ToolsLocator; |
36 |
import org.gvsig.tools.i18n.I18nManager; |
37 |
|
38 |
|
39 |
/**
|
40 |
* @author fdiaz
|
41 |
*
|
42 |
*/
|
43 |
public class DefaultSelectableBandDescriptorsTableModel extends AbstractTableModel implements SelectableBandDescriptorsTableModel { |
44 |
|
45 |
/**
|
46 |
*
|
47 |
*/
|
48 |
private static final long serialVersionUID = -3653794540611142562L; |
49 |
|
50 |
private List<SelectableBand> bands; |
51 |
|
52 |
private static final int COLUMNS = 5; |
53 |
|
54 |
private static final int COLUMN_SELECTED = 0; |
55 |
private static final int COLUMN_NUMBER = 1; |
56 |
private static final int COLUMN_BAND_STORE_NAME = 2; |
57 |
private static final int COLUMN_BAND_NUMBER_IN_STORE = 3; |
58 |
private static final int COLUMN_BAND_NAME = 4; |
59 |
|
60 |
/**
|
61 |
* @param bandDescriptors
|
62 |
*/
|
63 |
public DefaultSelectableBandDescriptorsTableModel(List<BandDescriptor> bandDescriptors) { |
64 |
bands = new ArrayList<DefaultSelectableBandDescriptorsTableModel.SelectableBand>(); |
65 |
for (int i=0; i<bandDescriptors.size(); i++) { |
66 |
SelectableBand selectableBand = new SelectableBand(i, bandDescriptors.get(i), true); |
67 |
bands.add(selectableBand); |
68 |
} |
69 |
} |
70 |
|
71 |
/* (non-Javadoc)
|
72 |
* @see javax.swing.table.TableModel#getRowCount()
|
73 |
*/
|
74 |
@Override
|
75 |
public int getRowCount() { |
76 |
return bands.size();
|
77 |
} |
78 |
|
79 |
/* (non-Javadoc)
|
80 |
* @see javax.swing.table.TableModel#getColumnCount()
|
81 |
*/
|
82 |
@Override
|
83 |
public int getColumnCount() { |
84 |
return COLUMNS;
|
85 |
} |
86 |
|
87 |
/* (non-Javadoc)
|
88 |
* @see javax.swing.table.TableModel#getValueAt(int, int)
|
89 |
*/
|
90 |
@Override
|
91 |
public Object getValueAt(int rowIndex, int columnIndex) { |
92 |
switch (columnIndex) {
|
93 |
case COLUMN_SELECTED:
|
94 |
return bands.get(rowIndex).isSelected();
|
95 |
case COLUMN_NUMBER:
|
96 |
return bands.get(rowIndex).getNumber();
|
97 |
case COLUMN_BAND_STORE_NAME:
|
98 |
return bands.get(rowIndex).getBandDescriptor().getStore().getName();
|
99 |
case COLUMN_BAND_NUMBER_IN_STORE:
|
100 |
return bands.get(rowIndex).getBandDescriptor().getBand();
|
101 |
case COLUMN_BAND_NAME:
|
102 |
return bands.get(rowIndex).getBandDescriptor().getName();
|
103 |
} |
104 |
return null; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
109 |
|
110 |
if (rowIndex < bands.size()) {
|
111 |
if (columnIndex < COLUMNS) {
|
112 |
switch (columnIndex) {
|
113 |
case COLUMN_SELECTED:
|
114 |
if (aValue == null) { |
115 |
bands.get(rowIndex).setSelected(false);
|
116 |
break;
|
117 |
} |
118 |
if (!(aValue instanceof Boolean)) { |
119 |
throw new IllegalArgumentException("The object '" + aValue.toString() + "' must be a Boolean."); |
120 |
} |
121 |
bands.get(rowIndex).setSelected((Boolean) aValue);
|
122 |
this.fireTableDataChanged();
|
123 |
break;
|
124 |
default:
|
125 |
break;
|
126 |
} |
127 |
|
128 |
} |
129 |
} |
130 |
} |
131 |
|
132 |
@Override
|
133 |
public String getColumnName(int column) { |
134 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
135 |
switch (column) {
|
136 |
case COLUMN_SELECTED:
|
137 |
return i18nManager.getTranslation("_selected"); |
138 |
case COLUMN_NUMBER:
|
139 |
return i18nManager.getTranslation("_number"); |
140 |
case COLUMN_BAND_STORE_NAME:
|
141 |
return i18nManager.getTranslation("_store_name"); |
142 |
case COLUMN_BAND_NUMBER_IN_STORE:
|
143 |
return i18nManager.getTranslation("_band_number"); |
144 |
case COLUMN_BAND_NAME:
|
145 |
return i18nManager.getTranslation("_band_name"); |
146 |
} |
147 |
return null; |
148 |
} |
149 |
|
150 |
public boolean isCellEditable(int row, int col) { |
151 |
if (col == COLUMN_SELECTED) {
|
152 |
return true; |
153 |
} |
154 |
return false; |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public Class<?> getColumnClass(int col) { |
159 |
if (col == COLUMN_SELECTED) {
|
160 |
return Boolean.class; |
161 |
} |
162 |
return super.getColumnClass(col); |
163 |
} |
164 |
|
165 |
|
166 |
/**
|
167 |
* @param rowIndex
|
168 |
*/
|
169 |
public void up(int rowIndex){ |
170 |
if(rowIndex>0){ |
171 |
SelectableBand aux = bands.get(rowIndex); |
172 |
bands.set(rowIndex, bands.get(rowIndex-1));
|
173 |
bands.set(rowIndex-1, aux);
|
174 |
this.fireTableDataChanged();
|
175 |
} |
176 |
} |
177 |
|
178 |
/**
|
179 |
* @param rowIndex
|
180 |
*/
|
181 |
public void down(int rowIndex){ |
182 |
if(rowIndex<(bands.size()-1)){ |
183 |
SelectableBand aux = bands.get(rowIndex); |
184 |
bands.set(rowIndex, bands.get(rowIndex+1));
|
185 |
bands.set(rowIndex+1, aux);
|
186 |
this.fireTableDataChanged();
|
187 |
} |
188 |
} |
189 |
|
190 |
/**
|
191 |
* @return a list of selected bands.
|
192 |
*/
|
193 |
public List<Integer> getSelectedBands() { |
194 |
List<Integer> selectedBands = new ArrayList<Integer>(); |
195 |
for (SelectableBand selBand : this.bands) { |
196 |
if(selBand.isSelected()){
|
197 |
selectedBands.add(selBand.getNumber()); |
198 |
} |
199 |
} |
200 |
return Collections.unmodifiableList(selectedBands); |
201 |
} |
202 |
|
203 |
public void selectBand(int band) { |
204 |
this.bands.get(band).setSelected(true); |
205 |
this.fireTableDataChanged();
|
206 |
} |
207 |
|
208 |
public void unselectBand(int band) { |
209 |
this.bands.get(band).setSelected(false); |
210 |
this.fireTableDataChanged();
|
211 |
} |
212 |
|
213 |
public void selectAllBands() { |
214 |
for (SelectableBand selBand : this.bands) { |
215 |
selBand.setSelected(true);
|
216 |
this.fireTableDataChanged();
|
217 |
} |
218 |
} |
219 |
|
220 |
public void unselectAllBands() { |
221 |
for (SelectableBand selBand : this.bands) { |
222 |
selBand.setSelected(false);
|
223 |
this.fireTableDataChanged();
|
224 |
} |
225 |
} |
226 |
|
227 |
/**
|
228 |
* @author fdiaz
|
229 |
*
|
230 |
*/
|
231 |
public static class SelectableBand { |
232 |
|
233 |
private boolean selected; |
234 |
private int number; |
235 |
private BandDescriptor bandDescriptor;
|
236 |
|
237 |
/**
|
238 |
* @param bandDescriptor
|
239 |
* @param selected
|
240 |
*/
|
241 |
public SelectableBand(int number, BandDescriptor bandDescriptor, boolean selected) { |
242 |
|
243 |
this.setNumber(number);
|
244 |
this.setBandDescriptor(bandDescriptor);
|
245 |
this.setSelected(selected);
|
246 |
} |
247 |
|
248 |
/**
|
249 |
* @return the number
|
250 |
*/
|
251 |
public int getNumber() { |
252 |
return number;
|
253 |
} |
254 |
|
255 |
/**
|
256 |
* @param number the number to set
|
257 |
*/
|
258 |
public void setNumber(int number) { |
259 |
this.number = number;
|
260 |
} |
261 |
|
262 |
/**
|
263 |
* @return the selected
|
264 |
*/
|
265 |
public boolean isSelected() { |
266 |
return selected;
|
267 |
} |
268 |
|
269 |
/**
|
270 |
* @param selected the selected to set
|
271 |
*/
|
272 |
public void setSelected(boolean selected) { |
273 |
this.selected = selected;
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @return the bandDescriptor
|
278 |
*/
|
279 |
public BandDescriptor getBandDescriptor() {
|
280 |
return bandDescriptor;
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* @param bandDescriptor the bandDescriptor to set
|
285 |
*/
|
286 |
public void setBandDescriptor(BandDescriptor bandDescriptor) { |
287 |
this.bandDescriptor = bandDescriptor;
|
288 |
} |
289 |
} |
290 |
|
291 |
} |