Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / raster / filter / mask / MaskUI.java @ 21690

History | View | Annotate | Download (9.75 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 */
19
package org.gvsig.raster.filter.mask;
20

    
21
import java.awt.Dimension;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.util.ArrayList;
26
import java.util.EventObject;
27

    
28
import javax.swing.JCheckBox;
29
import javax.swing.JLabel;
30
import javax.swing.event.ChangeEvent;
31
import javax.swing.event.ChangeListener;
32
import javax.swing.event.TableModelEvent;
33
import javax.swing.event.TableModelListener;
34

    
35
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
36
import org.gvsig.gui.beans.datainput.DataInputContainer;
37
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
38
import org.gvsig.gui.beans.table.TableContainer;
39
import org.gvsig.gui.beans.table.exceptions.NotInitializeException;
40
import org.gvsig.raster.RasterLibrary;
41
import org.gvsig.raster.dataset.Params;
42
import org.gvsig.raster.grid.filter.RegistrableFilterListener;
43
import org.gvsig.raster.grid.roi.ROI;
44
import org.gvsig.raster.util.RasterToolsUtil;
45

    
46
import com.iver.cit.gvsig.fmap.layers.FLayer;
47
/**
48
 * Interfaz gr?fico para los filtros de mascara.
49
 * 
50
 * 14/03/2008
51
 * @author Nacho Brodin nachobrodin@gmail.com
52
 */
53
public class MaskUI extends RegistrableFilterListener implements TableModelListener, ChangeListener, DataInputContainerListener {
54
        private static final long   serialVersionUID  = 4525736825113598035L;
55
        private TableContainer      tableContainer    = null;
56
        private FLayer              layer             = null;
57
        private ArrayList           rois              = null;
58
        private DataInputContainer  valueNoData       = null;
59
        private JCheckBox           negative          = null;
60
        private JCheckBox           transp            = null;
61
        private JLabel              warning           = null;
62
        private boolean             lastTransp        = false;
63
        private boolean             lastInv           = false;
64

    
65
        /**
66
         * Constructor. Inicializa los elementos gr?ficos.
67
         */
68
        public MaskUI() {
69
                initialize();
70
        }
71
        
72
        /**
73
         * Inicializa los elementos gr?ficos.
74
         */
75
        private void initialize() {
76
                setLayout(new GridBagLayout());
77
                GridBagConstraints gridBagConstraints;
78
                gridBagConstraints = new GridBagConstraints();
79
                gridBagConstraints.gridx = 0;
80
                gridBagConstraints.gridy = 0;
81
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
82
                gridBagConstraints.weightx = 1.0;
83
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
84
                add(getWarning(), gridBagConstraints);
85
                
86
                gridBagConstraints = new GridBagConstraints();
87
                gridBagConstraints.gridx = 0;
88
                gridBagConstraints.gridy = 1;
89
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
90
                gridBagConstraints.weightx = 1.0;
91
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
92
                add(getInverse(), gridBagConstraints);
93
                
94
                gridBagConstraints = new GridBagConstraints();
95
                gridBagConstraints.gridx = 0;
96
                gridBagConstraints.gridy = 2;
97
                gridBagConstraints.fill = GridBagConstraints.BOTH;
98
                gridBagConstraints.weightx = 1.0;
99
                gridBagConstraints.weighty = 1.0;
100
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
101
                add(getTableContainer(), gridBagConstraints);
102
                
103
                gridBagConstraints = new GridBagConstraints();
104
                gridBagConstraints.gridx = 0;
105
                gridBagConstraints.gridy = 3;
106
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
107
                gridBagConstraints.weightx = 1.0;
108
                gridBagConstraints.insets = new Insets(0, 0, 2, 0);
109

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

    
249
                ArrayList roisArray = ((FLyrRasterSE) layer).getRois();
250
                ArrayList selected = new ArrayList();
251
                if (roisArray != null) {
252
                        for (int i = 0; i < roisArray.size(); i++) {
253
                                if (((Boolean) tableContainer.getModel().getValueAt(i, 0)).booleanValue()) {
254
                                        selected.add(roisArray.get(i));
255
                                }
256
                        }
257
                }
258
                return selected;
259
        }
260
        
261
        /**
262
         * Sobrecargamos el m?todo getParams para que siempre devuelva
263
         * algo.
264
         */
265
        public Params getParams() {
266
                params = new Params();
267
                params.setParam("rois",
268
                                getSelectedROIs(),
269
                                -1,
270
                                null);
271
                params.setParam("inverse",
272
                                new Boolean(getInverse().isSelected()),
273
                                -1,
274
                                null);
275
                try {
276
                        params.setParam("nodata",
277
                                        Double.valueOf(getValueNoData().getValue()),
278
                                        -1,
279
                                        null);
280
                } catch (NumberFormatException e) {
281
                        params.setParam("nodata", Double.valueOf(-99999), -1, null);
282
                }
283
                return params;
284
        }
285

    
286
        /*
287
         * (non-Javadoc)
288
         * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
289
         */
290
        public void tableChanged(TableModelEvent e) {
291
                callStateChanged();
292
        }
293

    
294
        /**
295
         * Cambio de estado para el check de inversa
296
         * @param e
297
         */
298
        public void stateChanged(ChangeEvent e) {
299
                if(e.getSource().equals(getTransparencyActive())) {
300
                        if(((JCheckBox)e.getSource()).isSelected() != lastTransp) {
301
                                callStateChanged();
302
                                lastTransp = ((JCheckBox)e.getSource()).isSelected();
303
                        }
304
                }
305
                if(e.getSource().equals(getInverse())) {
306
                        if(((JCheckBox)e.getSource()).isSelected() != lastInv) {
307
                                callStateChanged();
308
                                lastInv = ((JCheckBox)e.getSource()).isSelected();
309
                        }
310
                }
311
        }
312

    
313
        /**
314
         * Cambio de valor para la entrada de texto para el valor de fondo
315
         * @param e
316
         */
317
        public void actionValueChanged(EventObject e) {
318
                callStateChanged();
319
        }
320
}