gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.mapcontext.raster.swing / org.gvsig.fmap.mapcontext.raster.swing.impl / src / main / java / org / gvsig / fmap / mapcontext / raster / swing / impl / bands / BandsTableModel.java @ 6701
History | View | Annotate | Download (11.9 KB)
1 |
/* gvSIG. Desktop Geographic Information System.
|
---|---|
2 |
*
|
3 |
* Copyright ? 2007-2017 gvSIG Association
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
18 |
* MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us
|
21 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.mapcontext.raster.swing.impl.bands; |
24 |
|
25 |
import java.util.ArrayList; |
26 |
import java.util.Arrays; |
27 |
import java.util.List; |
28 |
|
29 |
import javax.swing.table.AbstractTableModel; |
30 |
|
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
import org.gvsig.raster.lib.buffer.api.BufferLocator; |
35 |
import org.gvsig.raster.lib.buffer.api.BufferManager; |
36 |
import org.gvsig.raster.lib.legend.api.ColorInterpretation; |
37 |
import org.gvsig.tools.ToolsLocator; |
38 |
import org.gvsig.tools.dataTypes.CoercionException; |
39 |
import org.gvsig.tools.i18n.I18nManager; |
40 |
import org.gvsig.tools.locator.LocatorException; |
41 |
|
42 |
|
43 |
|
44 |
/**
|
45 |
* @author fdiaz
|
46 |
*
|
47 |
*/
|
48 |
public class BandsTableModel extends AbstractTableModel { |
49 |
|
50 |
/**
|
51 |
*
|
52 |
*/
|
53 |
private static final long serialVersionUID = 5993288441174158255L; |
54 |
private static final Logger LOG = LoggerFactory.getLogger(BandsTableModel.class); |
55 |
|
56 |
|
57 |
List<RasterStoreBand> rasterStoreBands;
|
58 |
|
59 |
public static final List<String> RGBColorSpace = Arrays.asList(ColorInterpretation.RED_BAND, ColorInterpretation.GREEN_BAND, ColorInterpretation.BLUE_BAND, ColorInterpretation.ALPHA_BAND); |
60 |
public static final List<String> CMYKColorSpace = Arrays.asList(ColorInterpretation.CYAN_BAND, ColorInterpretation.MAGENTA_BAND, ColorInterpretation.YELLOW_BAND, ColorInterpretation.BLACK_BAND); |
61 |
public static final List<String> HSLColorSpace = Arrays.asList(ColorInterpretation.HUE_BAND, ColorInterpretation.SATURATION_BAND, ColorInterpretation.LIGHTNESS_BAND); |
62 |
public static final List<String> YCBCRColorSpace = Arrays.asList(ColorInterpretation.YCBCR_CB_BAND, ColorInterpretation.YCBCR_CR_BAND, ColorInterpretation.YCBCR_Y_BAND); |
63 |
public static final List<List<String>> ColorSpaces = Arrays.asList(RGBColorSpace, CMYKColorSpace, HSLColorSpace, YCBCRColorSpace); |
64 |
|
65 |
/**
|
66 |
*
|
67 |
*/
|
68 |
public BandsTableModel() {
|
69 |
rasterStoreBands = new ArrayList<RasterStoreBand>(); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
public int getRowCount() { |
74 |
return rasterStoreBands.size();
|
75 |
} |
76 |
|
77 |
@Override
|
78 |
public int getColumnCount() { |
79 |
return 7; |
80 |
} |
81 |
|
82 |
@Override
|
83 |
public Object getValueAt(int rowIndex, int columnIndex) { |
84 |
RasterStoreBand rasterStoreBand = rasterStoreBands.get(rowIndex); |
85 |
switch (columnIndex) {
|
86 |
case 0: |
87 |
return rasterStoreBand.getBandColorInterpretation();
|
88 |
case 1: |
89 |
return rasterStoreBand.getNoDataNumber();
|
90 |
case 2: |
91 |
return rasterStoreBand.getDataType();
|
92 |
case 3: |
93 |
return rasterStoreBand.getStore().getName();
|
94 |
case 4: |
95 |
return rasterStoreBand.getBand();
|
96 |
case 5: |
97 |
return rasterStoreBand.getBandName();
|
98 |
case 6: |
99 |
return rasterStoreBand.getStore().getFullName();
|
100 |
} |
101 |
return null; |
102 |
} |
103 |
|
104 |
@Override
|
105 |
public String getColumnName(int column) { |
106 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
107 |
switch (column) {
|
108 |
case 0: |
109 |
return i18nManager.getTranslation("_color"); |
110 |
case 1: |
111 |
return i18nManager.getTranslation("_nodata"); |
112 |
case 2: |
113 |
return i18nManager.getTranslation("_data_type"); |
114 |
case 3: |
115 |
return i18nManager.getTranslation("_store_name"); |
116 |
case 4: |
117 |
return i18nManager.getTranslation("_band"); |
118 |
case 5: |
119 |
return i18nManager.getTranslation("_band_name"); |
120 |
case 6: |
121 |
return i18nManager.getTranslation("_store_full_name"); |
122 |
|
123 |
} |
124 |
return null; |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public boolean isCellEditable(int rowIndex, int columnIndex) { |
129 |
if(columnIndex>1){ |
130 |
return false; |
131 |
} |
132 |
return true; |
133 |
} |
134 |
|
135 |
@Override
|
136 |
public void setValueAt(Object aValue, int rowIndex, int columnIndex) { |
137 |
Object value = aValue;
|
138 |
if(columnIndex==0){ |
139 |
rasterStoreBands.get(rowIndex).setBandColorInterpretation((String)aValue);
|
140 |
String colorInterpretation = ColorInterpretation.PALETTE_BAND;
|
141 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
142 |
setUndefinedAnyOtherBand(rowIndex); |
143 |
} |
144 |
colorInterpretation = ColorInterpretation.GRAY_BAND; |
145 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
146 |
setUndefinedAnyOtherBand(rowIndex); |
147 |
} |
148 |
colorInterpretation = ColorInterpretation.RED_BAND; |
149 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
150 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
151 |
} |
152 |
colorInterpretation = ColorInterpretation.GREEN_BAND; |
153 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
154 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
155 |
} |
156 |
colorInterpretation = ColorInterpretation.BLUE_BAND; |
157 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
158 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
159 |
} |
160 |
colorInterpretation = ColorInterpretation.ALPHA_BAND; |
161 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
162 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
163 |
} |
164 |
|
165 |
colorInterpretation = ColorInterpretation.HUE_BAND; |
166 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
167 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
168 |
} |
169 |
colorInterpretation = ColorInterpretation.SATURATION_BAND; |
170 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
171 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
172 |
} |
173 |
colorInterpretation = ColorInterpretation.LIGHTNESS_BAND; |
174 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
175 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
176 |
} |
177 |
colorInterpretation = ColorInterpretation.CYAN_BAND; |
178 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
179 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
180 |
} |
181 |
colorInterpretation = ColorInterpretation.MAGENTA_BAND; |
182 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
183 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
184 |
} |
185 |
colorInterpretation = ColorInterpretation.YELLOW_BAND; |
186 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
187 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
188 |
} |
189 |
colorInterpretation = ColorInterpretation.BLACK_BAND; |
190 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
191 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
192 |
} |
193 |
colorInterpretation = ColorInterpretation.YCBCR_Y_BAND; |
194 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
195 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
196 |
} |
197 |
colorInterpretation = ColorInterpretation.YCBCR_CB_BAND; |
198 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
199 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
200 |
} |
201 |
colorInterpretation = ColorInterpretation.YCBCR_CR_BAND; |
202 |
if(((String)aValue).equalsIgnoreCase(colorInterpretation)){ |
203 |
setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation); |
204 |
} |
205 |
fireTableDataChanged(); |
206 |
} else if(columnIndex==1){ |
207 |
// Number noDataNumber = rasterStoreBands.get(rowIndex).getNoDataNumber();
|
208 |
// value = noDataNumber;
|
209 |
int dataType = rasterStoreBands.get(rowIndex).getDataType();
|
210 |
// Object noDataValue = null;
|
211 |
BufferManager bufferManager = BufferLocator.getBufferManager(); |
212 |
try {
|
213 |
value = bufferManager.coerce(dataType, aValue); |
214 |
rasterStoreBands.get(rowIndex).setNoDataNumber((Number)value);
|
215 |
} catch (LocatorException | CoercionException e) {
|
216 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
217 |
LOG.warn(i18nManager.getTranslation( |
218 |
"_it_cant_be_reconized_XvalueX_as_a_XdatatypenameX.",
|
219 |
new String[]{aValue.toString(), bufferManager.getTypeName(dataType)})); |
220 |
} |
221 |
//
|
222 |
// if(noData==null){
|
223 |
// value = bufferManager.createNoData((Number)noDataValue, null);
|
224 |
// rasterStoreBands.get(rowIndex).setNoData((NoData)value);
|
225 |
// } else {
|
226 |
// noData.setValue((Number)noDataValue);
|
227 |
// value = noData;
|
228 |
// }
|
229 |
} |
230 |
super.setValueAt(value, rowIndex, columnIndex);
|
231 |
|
232 |
} |
233 |
|
234 |
/**
|
235 |
* @param rowIndex
|
236 |
*/
|
237 |
private void setUndefinedAnyOtherBand(int rowIndex) { |
238 |
for (int i = 0; i < getRowCount(); i++) { |
239 |
if(i!=rowIndex){
|
240 |
rasterStoreBands.get(i).setBandColorInterpretation(ColorInterpretation.UNDEFINED_BAND); |
241 |
} |
242 |
} |
243 |
} |
244 |
|
245 |
/**
|
246 |
* Sets undefined the color interpretation of any other band with the same color interpretation
|
247 |
* or with a color interpretation incompatible with the color space of the colorInterpretation parameter.
|
248 |
* @param rowIndex
|
249 |
* @param colorInterpretation
|
250 |
*/
|
251 |
private void setUndefinedAnyOtherBandWithSameColorInterpretation(int rowIndex, String colorInterpretation) { |
252 |
for (int i = 0; i < getRowCount(); i++) { |
253 |
if(i!=rowIndex){
|
254 |
if(((String)getValueAt(i, 0)).equalsIgnoreCase(colorInterpretation)){ |
255 |
rasterStoreBands.get(i).setBandColorInterpretation(ColorInterpretation.UNDEFINED_BAND); |
256 |
} |
257 |
if(!areCompatible(colorInterpretation, ((String)getValueAt(i, 0)))){ |
258 |
rasterStoreBands.get(i).setBandColorInterpretation(ColorInterpretation.UNDEFINED_BAND); |
259 |
} |
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
private boolean areCompatible(String colorInterpretation, String colorInterpretation2) { |
265 |
for(List<String> colorSpace : ColorSpaces) { |
266 |
if(colorSpace.contains(colorInterpretation)){
|
267 |
if(colorSpace.contains(colorInterpretation2)){
|
268 |
return true; |
269 |
} |
270 |
} |
271 |
} |
272 |
return false; |
273 |
} |
274 |
|
275 |
/**
|
276 |
* @param rasterStoreBand
|
277 |
*/
|
278 |
public void add(RasterStoreBand rasterStoreBand) { |
279 |
rasterStoreBands.add(rasterStoreBand); |
280 |
} |
281 |
|
282 |
/**
|
283 |
* @param selectedRow
|
284 |
*/
|
285 |
public void removeElementAt(int selectedRow) { |
286 |
rasterStoreBands.remove(selectedRow); |
287 |
} |
288 |
|
289 |
public RasterStoreBand getElementAt(int selectedRow) { |
290 |
return rasterStoreBands.get(selectedRow);
|
291 |
} |
292 |
|
293 |
} |