Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / 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 / bands / BandsTableModel.java @ 44831

History | View | Annotate | Download (12.4 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.ColorInterpretation;
37
import org.gvsig.tools.ToolsLocator;
38
import org.gvsig.tools.dataTypes.CoercionException;
39
import org.gvsig.tools.dispose.DisposeUtils;
40
import org.gvsig.tools.i18n.I18nManager;
41
import org.gvsig.tools.locator.LocatorException;
42

    
43

    
44

    
45
/**
46
 * @author fdiaz
47
 *
48
 */
49
public class BandsTableModel extends AbstractTableModel {
50

    
51
    /**
52
     *
53
     */
54
    private static final long serialVersionUID = 5993288441174158255L;
55
    private static final Logger LOG = LoggerFactory.getLogger(BandsTableModel.class);
56

    
57

    
58
    List<RasterStoreBand> rasterStoreBands;
59

    
60
    public static final List<String> RGBColorSpace = Arrays.asList(ColorInterpretation.RED_BAND, ColorInterpretation.GREEN_BAND, ColorInterpretation.BLUE_BAND, ColorInterpretation.ALPHA_BAND);
61
    public static final List<String> CMYKColorSpace = Arrays.asList(ColorInterpretation.CYAN_BAND, ColorInterpretation.MAGENTA_BAND, ColorInterpretation.YELLOW_BAND, ColorInterpretation.BLACK_BAND, ColorInterpretation.ALPHA_BAND);
62
    public static final List<String> HSLColorSpace = Arrays.asList(ColorInterpretation.HUE_BAND, ColorInterpretation.SATURATION_BAND, ColorInterpretation.LIGHTNESS_BAND, ColorInterpretation.ALPHA_BAND);
63
    public static final List<String> YCBCRColorSpace = Arrays.asList(ColorInterpretation.YCBCR_CB_BAND, ColorInterpretation.YCBCR_CR_BAND, ColorInterpretation.YCBCR_Y_BAND, ColorInterpretation.ALPHA_BAND);
64
    public static final List<List<String>> ColorSpaces = Arrays.asList(RGBColorSpace, CMYKColorSpace, HSLColorSpace, YCBCRColorSpace);
65
    public static final List<String> ColorSpaceNames = Arrays.asList(ColorInterpretation.RGB, ColorInterpretation.CMYK, ColorInterpretation.HSL, ColorInterpretation.YCBCR);
66

    
67
    public static final int COLUMN_COLOR = 0;
68
    public static final int COLUMN_NODATA = 1;
69
    public static final int COLUMN_DATA_TYPE = 2;
70
    public static final int COLUMN_STORE_NAME = 3;
71
    public static final int COLUMN_BAND = 4;
72
    public static final int COLUMN_BAND_NAME = 5;
73
    public static final int COLUMN_STORE_FULL_NAME = 6;
74

    
75

    
76
    /**
77
     *
78
     */
79
    public BandsTableModel() {
80
        rasterStoreBands = new ArrayList<RasterStoreBand>();
81
    }
82

    
83
    @Override
84
    public int getRowCount() {
85
        return rasterStoreBands.size();
86
    }
87

    
88
    @Override
89
    public int getColumnCount() {
90
        return 7;
91
    }
92

    
93
    @Override
94
    public Object getValueAt(int rowIndex, int columnIndex) {
95
        RasterStoreBand rasterStoreBand = rasterStoreBands.get(rowIndex);
96
        switch (columnIndex) {
97
        case COLUMN_COLOR:
98
            return rasterStoreBand.getBandColorInterpretation();
99
        case COLUMN_NODATA:
100
            return rasterStoreBand.getNoDataNumber();
101
        case COLUMN_DATA_TYPE:
102
            return rasterStoreBand.getDataType();
103
        case COLUMN_STORE_NAME:
104
            return rasterStoreBand.getStore().getName();
105
        case COLUMN_BAND:
106
            return rasterStoreBand.getBand();
107
        case COLUMN_BAND_NAME:
108
            return rasterStoreBand.getBandName();
109
        case COLUMN_STORE_FULL_NAME:
110
            return rasterStoreBand.getStore().getFullName();
111
        }
112
        return null;
113
    }
114

    
115
    @Override
116
    public String getColumnName(int column) {
117
        I18nManager i18nManager = ToolsLocator.getI18nManager();
118
        switch (column) {
119
        case COLUMN_COLOR:
120
            return i18nManager.getTranslation("_color");
121
        case COLUMN_NODATA:
122
            return i18nManager.getTranslation("_nodata");
123
        case COLUMN_DATA_TYPE:
124
            return i18nManager.getTranslation("_data_type");
125
        case COLUMN_STORE_NAME:
126
            return i18nManager.getTranslation("_store_name");
127
        case COLUMN_BAND:
128
            return i18nManager.getTranslation("_band");
129
        case COLUMN_BAND_NAME:
130
            return i18nManager.getTranslation("_band_name");
131
        case COLUMN_STORE_FULL_NAME:
132
            return i18nManager.getTranslation("_store_full_name");
133

    
134
        }
135
        return null;
136
    }
137

    
138
    @Override
139
    public boolean isCellEditable(int rowIndex, int columnIndex) {
140
        if(columnIndex>1){
141
            return false;
142
        }
143
        return true;
144
    }
145

    
146
    @Override
147
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
148
        Object value = aValue;
149
        if(columnIndex==0){
150
            rasterStoreBands.get(rowIndex).setBandColorInterpretation((String)aValue);
151
            String colorInterpretation = ColorInterpretation.PALETTE_BAND;
152
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
153
                setUndefinedAnyOtherBand(rowIndex);
154
            }
155
            colorInterpretation = ColorInterpretation.GRAY_BAND;
156
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
157
                setUndefinedAnyOtherBand(rowIndex);
158
            }
159
            colorInterpretation = ColorInterpretation.RED_BAND;
160
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
161
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
162
            }
163
            colorInterpretation = ColorInterpretation.GREEN_BAND;
164
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
165
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
166
            }
167
            colorInterpretation = ColorInterpretation.BLUE_BAND;
168
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
169
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
170
            }
171
            colorInterpretation = ColorInterpretation.ALPHA_BAND;
172
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
173
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
174
            }
175

    
176
            colorInterpretation = ColorInterpretation.HUE_BAND;
177
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
178
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
179
            }
180
            colorInterpretation = ColorInterpretation.SATURATION_BAND;
181
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
182
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
183
            }
184
            colorInterpretation = ColorInterpretation.LIGHTNESS_BAND;
185
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
186
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
187
            }
188
            colorInterpretation = ColorInterpretation.CYAN_BAND;
189
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
190
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
191
            }
192
            colorInterpretation = ColorInterpretation.MAGENTA_BAND;
193
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
194
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
195
            }
196
            colorInterpretation = ColorInterpretation.YELLOW_BAND;
197
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
198
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
199
            }
200
            colorInterpretation = ColorInterpretation.BLACK_BAND;
201
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
202
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
203
            }
204
            colorInterpretation = ColorInterpretation.YCBCR_Y_BAND;
205
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
206
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
207
            }
208
            colorInterpretation = ColorInterpretation.YCBCR_CB_BAND;
209
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
210
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
211
            }
212
            colorInterpretation = ColorInterpretation.YCBCR_CR_BAND;
213
            if(((String)aValue).equalsIgnoreCase(colorInterpretation)){
214
                setUndefinedAnyOtherBandWithSameColorInterpretation(rowIndex, colorInterpretation);
215
            }
216
            fireTableDataChanged();
217
        } else if(columnIndex==1){
218
            int dataType = rasterStoreBands.get(rowIndex).getDataType();
219
            BufferManager bufferManager = BufferLocator.getBufferManager();
220
            try {
221
                value = bufferManager.coerce(dataType, aValue);
222
                rasterStoreBands.get(rowIndex).setNoDataNumber((Number)value);
223
            } catch (LocatorException | CoercionException e) {
224
                I18nManager i18nManager = ToolsLocator.getI18nManager();
225
                LOG.warn(i18nManager.getTranslation(
226
                    "_it_cant_be_reconized_XvalueX_as_a_XdatatypenameX.",
227
                    new String[]{aValue.toString(), bufferManager.getTypeName(dataType)}));
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
        DisposeUtils.bind(rasterStoreBand);
281
    }
282

    
283
    /**
284
     * @param selectedRow
285
     */
286
    public void removeElementAt(int selectedRow) {
287
        RasterStoreBand rasterStoreBand = rasterStoreBands.get(selectedRow);
288
        rasterStoreBands.remove(selectedRow);
289
        DisposeUtils.disposeQuietly(rasterStoreBand);
290

    
291
    }
292

    
293
    public RasterStoreBand getElementAt(int selectedRow) {
294
        return rasterStoreBands.get(selectedRow);
295
    }
296

    
297
}