Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / filter / mask / MaskUI.java @ 2341

History | View | Annotate | Download (12.2 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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
 */
22
package org.gvsig.raster.tools.app.basic.tool.filter.mask;
23

    
24
import java.awt.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.util.ArrayList;
29
import java.util.EventObject;
30
import java.util.Iterator;
31
import java.util.List;
32

    
33
import javax.swing.JCheckBox;
34
import javax.swing.JLabel;
35
import javax.swing.JPanel;
36
import javax.swing.event.ChangeEvent;
37
import javax.swing.event.ChangeListener;
38
import javax.swing.event.TableModelEvent;
39
import javax.swing.event.TableModelListener;
40

    
41
import org.gvsig.fmap.dal.coverage.RasterLibrary;
42
import org.gvsig.fmap.dal.coverage.RasterLocator;
43
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
44
import org.gvsig.fmap.dal.coverage.datastruct.NoData;
45
import org.gvsig.fmap.dal.coverage.datastruct.Params;
46
import org.gvsig.fmap.dal.coverage.exception.ROIException;
47
import org.gvsig.fmap.dal.coverage.grid.FilterUIListener;
48
import org.gvsig.fmap.dal.coverage.grid.RegistrableFilterListener;
49
import org.gvsig.gui.beans.datainput.DataInputContainer;
50
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
51
import org.gvsig.gui.beans.table.TableContainer;
52
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
53
import org.gvsig.i18n.Messages;
54
import org.gvsig.raster.fmap.layers.FLyrRaster;
55
import org.gvsig.raster.roi.ROI;
56
import org.gvsig.raster.swing.RasterSwingLibrary;
57
import org.gvsig.raster.tools.app.basic.RasterToolsUtil;
58
/**
59
 * Interfaz gr?fico para los filtros de mascara.
60
 * 
61
 * @author Nacho Brodin nachobrodin@gmail.com
62
 */
63
public class MaskUI extends JPanel implements RegistrableFilterListener, TableModelListener, ChangeListener, DataInputContainerListener {
64
        private static final long   serialVersionUID            = 4525736825113598035L;
65
        private TableContainer      tableContainer              = null;
66
        private FLyrRaster          layer                       = null;
67
        private ArrayList<ROI>      rois                        = null;
68
        private DataInputContainer  valueNoData                 = null;
69
        private JCheckBox           negative                    = null;
70
        private JCheckBox           transp                      = null;
71
        private JLabel              warning                     = null;
72
        private boolean             lastTransp                  = false;
73
        private boolean             lastInv                     = false;
74
        private ArrayList<FilterUIListener>          
75
                                actionCommandListeners      = new ArrayList<FilterUIListener>();
76
        protected Params            params                      = null;
77

    
78
        /**
79
         * Constructor. Inicializa los elementos gr?ficos.
80
         */
81
        public MaskUI() {
82
                init(null);
83
        }
84
        
85
        /**
86
         * Inicializa los elementos gr?ficos.
87
         */
88
        public void init(Object obj) {
89
                setLayout(new GridBagLayout());
90
                GridBagConstraints gridBagConstraints;
91
                gridBagConstraints = new GridBagConstraints();
92
                gridBagConstraints.gridx = 0;
93
                gridBagConstraints.gridy = 0;
94
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
95
                gridBagConstraints.weightx = 1.0;
96
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
97
                add(getWarning(), gridBagConstraints);
98
                
99
                gridBagConstraints = new GridBagConstraints();
100
                gridBagConstraints.gridx = 0;
101
                gridBagConstraints.gridy = 1;
102
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
103
                gridBagConstraints.weightx = 1.0;
104
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
105
                add(getInverse(), gridBagConstraints);
106
                
107
                gridBagConstraints = new GridBagConstraints();
108
                gridBagConstraints.gridx = 0;
109
                gridBagConstraints.gridy = 2;
110
                gridBagConstraints.fill = GridBagConstraints.BOTH;
111
                gridBagConstraints.weightx = 1.0;
112
                gridBagConstraints.weighty = 1.0;
113
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
114
                add(getTableContainer(), gridBagConstraints);
115
                
116
                gridBagConstraints = new GridBagConstraints();
117
                gridBagConstraints.gridx = 0;
118
                gridBagConstraints.gridy = 3;
119
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
120
                gridBagConstraints.weightx = 1.0;
121
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
122

    
123
                add(getValueNoData(), gridBagConstraints);
124
                
125
        }
126
        
127
        /**
128
         * Obtiene el checkbox que informa de si asigna valor noData a los
129
         * pixeles del interior de las ROIs o a los del exterior.
130
         * @return JCheckBox
131
         */
132
        private JCheckBox getInverse() {
133
                if(negative == null) {
134
                        negative = new JCheckBox();
135
                        negative.setText(RasterToolsUtil.getText(this, "inversa"));
136
                        negative.addChangeListener(this);
137
                }
138
                return negative;
139
        }
140
        
141
        /**
142
         * Obtiene el checkbox que informa de si se activa la generaci?n de capa
143
         * de transparencia o no.
144
         * @return JCheckBox
145
         */
146
        private JCheckBox getTransparencyActive() {
147
                if(transp == null) {
148
                        transp = new JCheckBox();
149
                        transp.setText(RasterToolsUtil.getText(this, "transparencia"));
150
                        transp.addChangeListener(this);
151
                }
152
                return transp;
153
        }
154
        
155
        /**
156
         * Gets the NoData value
157
         * @return DataInputContainer
158
         */
159
        private DataInputContainer getValueNoData() {
160
                if(valueNoData == null) {
161
                        valueNoData = new DataInputContainer();
162
                        valueNoData.setLabelText(RasterToolsUtil.getText(this, "value"));
163
                        valueNoData.setValue(RasterLibrary.defaultByteNoDataValue + "");
164
                        valueNoData.getDataInputField().addValueChangedListener(this);
165
                }
166
                return valueNoData;
167
        }
168
        
169
        /**
170
         * Obtiene el mensaje de aviso de que no hay rois en la lista. Esta etiqueta solo
171
         * es mostrada en caso en que la capa no tenga ROIs asociadas.
172
         * @return JLabel Etiqueta con el mensaje de aviso.
173
         */
174
        public JLabel getWarning() {
175
                if(warning == null) {
176
                        warning = new JLabel(RasterToolsUtil.getText(this, "rois_needed"));
177
                        warning.setVisible(false);
178
                }
179
                return warning;
180
        }
181
        
182
        /**
183
         * Obtiene el contenedor con la tabla.
184
         * @return
185
         */
186
        private TableContainer getTableContainer() {
187
                if (tableContainer == null) {
188
                        String[] columnNames = {" ", "Nombre", ""};
189
                        int[] columnWidths = {22, 334, 0};
190
                        tableContainer = new TableContainer(columnNames, columnWidths);
191
                        tableContainer.setPreferredSize(new Dimension(0, 130));
192
                        tableContainer.setModel("CheckBoxModel");
193
                        tableContainer.initialize();
194
                        tableContainer.setControlVisible(false);
195
                        tableContainer.setMoveRowsButtonsVisible(false);
196
                        tableContainer.getTable().getJTable().getColumnModel().getColumn(0).setMinWidth(22);
197
                        tableContainer.getTable().getJTable().getColumnModel().getColumn(0).setMaxWidth(22);
198
                        tableContainer.getTable().getJTable().getColumnModel().getColumn(2).setMinWidth(0);
199
                        tableContainer.getTable().getJTable().getColumnModel().getColumn(2).setMaxWidth(0);
200
                        tableContainer.getModel().addTableModelListener(this);
201
                }
202
                return tableContainer;
203
        }
204
        
205
        /**
206
         * Asigna la lista de regiones de inter?s.
207
         * @param rois Lista de ROIs
208
         */
209
        public void setRois(ArrayList<ROI> rois) {
210
                this.rois = rois;
211
        }
212
        
213
        /**
214
         * Asigna la capa.
215
         * @param layer
216
         */
217
        public void setLayer(FLyrRaster layer) {
218
                this.layer = layer;
219
                if (layer == null)
220
                        return;
221
                
222
                List<ROI> roisArray = null;
223
                try {
224
                        roisArray = ((FLyrRaster) layer).getRois();
225
                } catch (ROIException e1) {
226
                        RasterSwingLibrary.messageBoxError(Messages.getText("problems_loading_rois"), null);
227
                }
228
                
229
                if(roisArray == null || roisArray.size() == 0)
230
                        getWarning().setVisible(true);
231
                
232
                if (roisArray != null) {
233
                        for (int i = 0; i < roisArray.size(); i++) {
234
                                ROI roi = (ROI) roisArray.get(i);
235
        
236
                                Object row[] = {"", "", ""};
237
                                
238
                                boolean active = false;
239
                                
240
                                if (rois != null) {
241
                                        for (int r = 0; r < rois.size(); r++) {
242
                                                if (((ROI) rois.get(r)) == roi) {
243
                                                        active = true;
244
                                                        break;
245
                                                }
246
                                        }
247
                                }
248
                                
249
                                row[0] = new Boolean(active);
250
                                row[1] = roi.getName(); 
251
                                row[2] = new Integer(i);
252
                                try {
253
                                        getTableContainer().addRow(row);
254
                                } catch (NotInitializeException e) {
255
                                }
256
                        }
257
                }
258
                
259
                if(layer != null) {
260
                        getValueNoData().setValue(RasterLibrary.defaultByteNoDataValue + "");
261
                }
262
        }
263
        
264
        /**
265
         * Obtiene la lista de ROIs seleccionadas
266
         * @return ArrayList con la lista de ROIs
267
         */
268
        private List<ROI> getSelectedROIs() {
269
                if (layer == null)
270
                        return null;
271

    
272
                List<ROI> roisArray = null;
273
                try {
274
                        roisArray = ((FLyrRaster) layer).getRois();
275
                } catch (ROIException e1) {
276
                }
277
                List<ROI> selected = new ArrayList<ROI>();
278
                if (roisArray != null) {
279
                        for (int i = 0; i < roisArray.size(); i++) {
280
                                try {
281
                                        if (((Boolean) tableContainer.getModel().getValueAt(i, 0)).booleanValue()) {
282
                                                selected.add(roisArray.get(i));
283
                                        }
284
                                } catch (ArrayIndexOutOfBoundsException e) {
285
                                        //Entra aqu? si se han a?adido ROIs con el cuadro abierto. Pasamos de hacer nada
286
                                }
287
                        }
288
                }
289
                return selected;
290
        }
291
        
292
        /**
293
         * Sobrecargamos el m?todo getParams para que siempre devuelva
294
         * algo.
295
         */
296
        public Params getParams() {
297
                NoData noData = null;
298
                Number n = null;
299
                Double d = new Double(getValueNoData().getValue());
300
                if(layer != null) {
301
                        switch (layer.getDataStore().getDataType()[0]) {
302
                        case Buffer.TYPE_BYTE:
303
                                n = new Byte(d.byteValue());
304
                                break;
305
                        case Buffer.TYPE_SHORT:
306
                                n = new Short(d.shortValue());
307
                                break;
308
                        case Buffer.TYPE_INT:
309
                                n = new Integer(d.intValue());
310
                                break;
311
                        case Buffer.TYPE_FLOAT:
312
                                n = new Float(getValueNoData().getValue());
313
                                break;
314
                        case Buffer.TYPE_DOUBLE:
315
                                n = d;
316
                                break;
317
                        }
318
                }
319
                noData = RasterLocator.getManager().getDataStructFactory().createNoData(n, n, null);
320
                
321
                params = RasterLocator.getManager().createParams(
322
                                                        "rois",
323
                                                        getSelectedROIs(),
324
                                                        -1,
325
                                                        null);
326
                params.setParam("inverse",
327
                                new Boolean(getInverse().isSelected()),
328
                                -1,
329
                                null);
330
                try {
331
                        params.setParam("nodata",
332
                                        noData,
333
                                        -1,
334
                                        null);
335
                } catch (NumberFormatException e) {
336
                        params.setParam("nodata", 
337
                                        RasterLocator.getManager().getDataStructFactory().createDefaultNoData(1, layer.getDataStore().getDataType()[0]), 
338
                                        -1, 
339
                                        null);
340
                }
341
                return params;
342
        }
343
        
344
        public void setParams(Params params) {
345
                this.params = params;
346
        }
347

    
348
        public void tableChanged(TableModelEvent e) {
349
                callStateChanged();
350
        }
351

    
352
        /**
353
         * Cambio de estado para el check de inversa
354
         * @param e
355
         */
356
        public void stateChanged(ChangeEvent e) {
357
                if(e.getSource().equals(getTransparencyActive())) {
358
                        if(((JCheckBox)e.getSource()).isSelected() != lastTransp) {
359
                                callStateChanged();
360
                                lastTransp = ((JCheckBox)e.getSource()).isSelected();
361
                        }
362
                }
363
                if(e.getSource().equals(getInverse())) {
364
                        if(((JCheckBox)e.getSource()).isSelected() != lastInv) {
365
                                callStateChanged();
366
                                lastInv = ((JCheckBox)e.getSource()).isSelected();
367
                        }
368
                }
369
        }
370

    
371
        /**
372
         * Cambio de valor para la entrada de texto para el valor de fondo
373
         * @param e
374
         */
375
        public void actionValueChanged(EventObject e) {
376
                callStateChanged();
377
        }
378
        
379
        public void addFilterUIListener(FilterUIListener listener) {
380
                if (!actionCommandListeners.contains(listener))
381
                        actionCommandListeners.add(listener);
382
        }
383

    
384
        public void callStateChanged() {
385
                Iterator<FilterUIListener> acIterator = actionCommandListeners.iterator();
386
                while (acIterator.hasNext()) {
387
                        FilterUIListener listener = (FilterUIListener) acIterator.next();
388
                        listener.actionValuesCompleted(new EventObject(this));
389
                }
390
        }
391

    
392
        public void removeStateChangedListener(FilterUIListener listener) {
393
                actionCommandListeners.remove(listener);
394
        }
395
}