Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.swing / org.gvsig.raster.swing.buffer / org.gvsig.raster.swing.buffer.impl / src / main / java / org / gvsig / raster / swing / buffer / impl / DefaultSelectableBandsTableModel.java @ 6703

History | View | Annotate | Download (7.76 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.raster.swing.buffer.impl;
24

    
25
import java.util.ArrayList;
26
import java.util.Collections;
27
import java.util.List;
28

    
29
import javax.swing.table.AbstractTableModel;
30

    
31
import org.gvsig.raster.lib.buffer.api.BandInfo;
32
import org.gvsig.raster.swing.buffer.SelectableBandsTableModel;
33
import org.gvsig.tools.ToolsLocator;
34
import org.gvsig.tools.i18n.I18nManager;
35

    
36

    
37
/**
38
 * @author fdiaz
39
 *
40
 */
41
public class DefaultSelectableBandsTableModel extends AbstractTableModel implements SelectableBandsTableModel{
42

    
43
    /**
44
     *
45
     *
46
     */
47
    private static final long serialVersionUID = -6739781555636046945L;
48
    private List<SelectableBand> bands;
49

    
50
    private static final int COLUMNS = 4;
51

    
52
    private static final int COLUMN_SELECTED = 0;
53
    private static final int COLUMN_NUMBER = 1;
54
    private static final int COLUMN_BAND_NAME = 2;
55
    private static final int COLUMN_BAND_DESCRIPTION = 3;
56

    
57
    /**
58
     * @param bandsInfo
59
     */
60
    public DefaultSelectableBandsTableModel(BandInfo[] bandsInfo) {
61
        bands = new ArrayList<DefaultSelectableBandsTableModel.SelectableBand>();
62
        for (int i=0; i<bandsInfo.length; i++) {
63
            SelectableBand selectableBand = new SelectableBand(i, bandsInfo[i], true);
64
            bands.add(selectableBand);
65
        }
66
    }
67

    
68
    /* (non-Javadoc)
69
     * @see javax.swing.table.TableModel#getRowCount()
70
     */
71
    @Override
72
    public int getRowCount() {
73
        return bands.size();
74
    }
75

    
76
    /* (non-Javadoc)
77
     * @see javax.swing.table.TableModel#getColumnCount()
78
     */
79
    @Override
80
    public int getColumnCount() {
81
        return COLUMNS;
82
    }
83

    
84
    /* (non-Javadoc)
85
     * @see javax.swing.table.TableModel#getValueAt(int, int)
86
     */
87
    @Override
88
    public Object getValueAt(int rowIndex, int columnIndex) {
89
        switch (columnIndex) {
90
        case COLUMN_SELECTED:
91
            return bands.get(rowIndex).isSelected();
92
        case COLUMN_NUMBER:
93
            return bands.get(rowIndex).getNumber();
94
        case COLUMN_BAND_NAME:
95
            return bands.get(rowIndex).getBandInfo().getName();
96
        case COLUMN_BAND_DESCRIPTION:
97
            return bands.get(rowIndex).getBandInfo().getDescription();
98
        }
99
        return null;
100
    }
101

    
102
    @Override
103
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
104

    
105
        if (rowIndex < bands.size()) {
106
            if (columnIndex < COLUMNS) {
107
                switch (columnIndex) {
108
                case COLUMN_SELECTED:
109
                    if (aValue == null) {
110
                        bands.get(rowIndex).setSelected(false);
111
                        break;
112
                    }
113
                    if (!(aValue instanceof Boolean)) {
114
                        throw new IllegalArgumentException("The object '" + aValue.toString() + "' must be a Boolean.");
115
                    }
116
                    bands.get(rowIndex).setSelected((Boolean) aValue);
117
                    this.fireTableDataChanged();
118
                    break;
119
                default:
120
                    break;
121
                }
122

    
123
            }
124
        }
125
    }
126

    
127
    @Override
128
    public String getColumnName(int column) {
129
        I18nManager i18nManager = ToolsLocator.getI18nManager();
130
        switch (column) {
131
        case COLUMN_SELECTED:
132
            return i18nManager.getTranslation("_selected");
133
        case COLUMN_NUMBER:
134
            return i18nManager.getTranslation("_number");
135
        case COLUMN_BAND_NAME:
136
            return i18nManager.getTranslation("_band_name");
137
        case COLUMN_BAND_DESCRIPTION:
138
            return i18nManager.getTranslation("_band_description");
139
        }
140
        return null;
141
    }
142

    
143
    public boolean isCellEditable(int row, int col) {
144
        if (col == COLUMN_SELECTED) {
145
            return true;
146
        }
147
        return false;
148
    }
149

    
150
    @Override
151
    public Class<?> getColumnClass(int col) {
152
        if (col == COLUMN_SELECTED) {
153
            return Boolean.class;
154
        }
155
        return super.getColumnClass(col);
156
    }
157

    
158

    
159
    /**
160
     * @param rowIndex
161
     */
162
    public void up(int rowIndex){
163
        if(rowIndex>0){
164
            SelectableBand aux = bands.get(rowIndex);
165
            bands.set(rowIndex, bands.get(rowIndex-1));
166
            bands.set(rowIndex-1, aux);
167
            this.fireTableDataChanged();
168
        }
169
    }
170

    
171
    /**
172
     * @param rowIndex
173
     */
174
    public void down(int rowIndex){
175
        if(rowIndex<(bands.size()-1)){
176
            SelectableBand aux = bands.get(rowIndex);
177
            bands.set(rowIndex, bands.get(rowIndex+1));
178
            bands.set(rowIndex+1, aux);
179
            this.fireTableDataChanged();
180
        }
181
    }
182

    
183
    public List<Integer> getSelectedBands() {
184
        List<Integer> selectedBands = new ArrayList<Integer>();
185
        for (SelectableBand selBand : this.bands) {
186
            if(selBand.isSelected()){
187
                selectedBands.add(selBand.getNumber());
188
            }
189
        }
190
        return Collections.unmodifiableList(selectedBands);
191
    }
192

    
193
    public void selectBand(int band) {
194
        this.bands.get(band).setSelected(true);
195
        this.fireTableDataChanged();
196
    }
197

    
198
    public void unselectBand(int band) {
199
        this.bands.get(band).setSelected(false);
200
        this.fireTableDataChanged();
201
    }
202

    
203
    public void selectAllBands() {
204
        for (SelectableBand selBand : this.bands) {
205
            selBand.setSelected(true);
206
            this.fireTableDataChanged();
207
        }
208
    }
209

    
210
    public void unselectAllBands() {
211
        for (SelectableBand selBand : this.bands) {
212
            selBand.setSelected(false);
213
            this.fireTableDataChanged();
214
        }
215
    }
216

    
217
    /**
218
     * @author fdiaz
219
     *
220
     */
221
    public static class SelectableBand {
222

    
223
        private boolean selected;
224
        private int number;
225
        private BandInfo bandInfo;
226

    
227
        /**
228
         * @param number
229
         * @param bandInfo
230
         * @param selected
231
         */
232
        public SelectableBand(int number, BandInfo bandInfo, boolean selected) {
233

    
234
            this.setNumber(number);
235
            this.setBandInfo(bandInfo);
236
            this.setSelected(selected);
237
        }
238

    
239
        /**
240
         * @return the number
241
         */
242
        public int getNumber() {
243
            return number;
244
        }
245

    
246
        /**
247
         * @param number the number to set
248
         */
249
        public void setNumber(int number) {
250
            this.number = number;
251
        }
252

    
253
        /**
254
         * @return the selected
255
         */
256
        public boolean isSelected() {
257
            return selected;
258
        }
259

    
260
        /**
261
         * @param selected the selected to set
262
         */
263
        public void setSelected(boolean selected) {
264
            this.selected = selected;
265
        }
266

    
267
        /**
268
         * @return the bandDescriptor
269
         */
270
        public BandInfo getBandInfo() {
271
            return bandInfo;
272
        }
273

    
274
        /**
275
         * @param bandInfo the bandInfo to set
276
         */
277
        public void setBandInfo(BandInfo bandInfo) {
278
            this.bandInfo = bandInfo;
279
        }
280
    }
281

    
282
}