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 / colortable / ui / ColorTableGlobalPanel.java @ 2351

History | View | Annotate | Download (13.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.colortable.ui;
23

    
24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
26
import java.awt.Insets;
27
import java.awt.event.ActionEvent;
28
import java.awt.event.ActionListener;
29
import java.beans.PropertyChangeEvent;
30
import java.beans.PropertyChangeListener;
31
import java.text.NumberFormat;
32

    
33
import javax.swing.JButton;
34
import javax.swing.JCheckBox;
35
import javax.swing.JFormattedTextField;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.text.DefaultFormatterFactory;
39
import javax.swing.text.NumberFormatter;
40

    
41
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
42
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
43
import org.gvsig.raster.fmap.layers.FLyrRaster;
44
import org.gvsig.raster.tools.app.basic.raster.process.IProcessActions;
45
import org.gvsig.raster.tools.app.basic.raster.process.RasterProcess;
46
import org.gvsig.raster.tools.app.basic.raster.process.StatisticsProcess;
47
import org.gvsig.raster.tools.app.basic.tool.colortable.data.ColorTableData;
48
import org.gvsig.raster.util.BasePanel;
49
/**
50
 * Panel que contiene las opciones sobre los maximos y minimos de la tabla de color,
51
 * checkbox de limites, si esta interpolado o si esta activa la tabla de color 
52
 * 
53
 * @author BorSanZa - Borja S?nchez Zamorano 
54
 */
55
public class ColorTableGlobalPanel extends BasePanel implements ActionListener, PropertyChangeListener, IProcessActions {
56
        private static final long   serialVersionUID     = 1L;
57
        private JCheckBox           checkBoxInterpolated = null;
58
        private JCheckBox           checkBoxEnabled      = null;
59
        private ColorTableData      colorTableData       = null;
60

    
61
        private JCheckBox           checkBoxLimits       = null;
62
        private JFormattedTextField textFieldMinim       = null;
63
        private JFormattedTextField textFieldMaxim       = null;
64
        private JLabel              labelMinim           = null;
65
        private JLabel              labelMaxim           = null;
66
        private JButton             buttonStatistics     = null;
67

    
68
        private NumberFormat        doubleDisplayFormat  = null;
69
        private NumberFormat        doubleEditFormat     = null;
70
        private FLyrRaster          fLayer               = null;
71
        
72
        /**
73
         *Inicializa componentes gr?ficos y traduce
74
         */
75
        public ColorTableGlobalPanel() {
76
                init();
77
                translate();
78
        }
79
        
80
        public ColorTableGlobalPanel(ColorTableData colorTableData) {
81
                doubleDisplayFormat = NumberFormat.getNumberInstance();
82
                doubleDisplayFormat.setMinimumFractionDigits(0);
83
                doubleEditFormat = NumberFormat.getNumberInstance();
84

    
85
                this.colorTableData = colorTableData;
86
                init();
87
        }
88
        
89
        protected void init() {
90
                GridBagConstraints gridBagConstraints;
91

    
92
                setLayout(new GridBagLayout());
93

    
94
                gridBagConstraints = new GridBagConstraints();
95
                gridBagConstraints.gridx = 4;
96
                gridBagConstraints.gridy = 0;
97
                gridBagConstraints.insets = new Insets(5, 2, 2, 5);
98
                add(getButtonStatistics(), gridBagConstraints);
99

    
100
                gridBagConstraints = new GridBagConstraints();
101
                gridBagConstraints.gridx = 1;
102
                gridBagConstraints.gridy = 0;
103
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
104
                gridBagConstraints.weightx = 1.0;
105
                gridBagConstraints.insets = new Insets(5, 2, 2, 2);
106
                add(getTextFieldMinim(), gridBagConstraints);
107

    
108
                gridBagConstraints = new GridBagConstraints();
109
                gridBagConstraints.gridx = 3;
110
                gridBagConstraints.gridy = 0;
111
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
112
                gridBagConstraints.weightx = 1.0;
113
                gridBagConstraints.insets = new Insets(5, 2, 2, 2);
114
                add(getTextFieldMaxim(), gridBagConstraints);
115

    
116
                gridBagConstraints = new GridBagConstraints();
117
                gridBagConstraints.gridx = 0;
118
                gridBagConstraints.gridy = 0;
119
                gridBagConstraints.insets = new Insets(5, 5, 2, 2);
120
                add(getLabelMinim(), gridBagConstraints);
121

    
122
                gridBagConstraints = new GridBagConstraints();
123
                gridBagConstraints.gridx = 2;
124
                gridBagConstraints.gridy = 0;
125
                gridBagConstraints.insets = new Insets(5, 2, 2, 2);
126
                add(getLabelMaxim(), gridBagConstraints);
127

    
128
                JPanel panel = new JPanel();
129
                panel.setLayout(new GridBagLayout());
130
                
131
                gridBagConstraints = new GridBagConstraints();
132
                gridBagConstraints.gridx = 0;
133
                gridBagConstraints.gridy = 0;
134
                gridBagConstraints.weightx = 1.0;
135
                gridBagConstraints.insets = new Insets(2, 5, 5, 2);
136
                panel.add(getCheckBoxEnabled(), gridBagConstraints);
137
                
138
                gridBagConstraints = new GridBagConstraints();
139
                gridBagConstraints.gridx = 1;
140
                gridBagConstraints.gridy = 0;
141
                gridBagConstraints.weightx = 1.0;
142
                gridBagConstraints.insets = new Insets(2, 2, 5, 5);
143
                panel.add(getCheckBoxInterpolated(), gridBagConstraints);
144
                
145
                gridBagConstraints = new GridBagConstraints();
146
                gridBagConstraints.gridx = 2;
147
                gridBagConstraints.gridy = 0;
148
                gridBagConstraints.weightx = 1.0;
149
                gridBagConstraints.insets = new Insets(2, 2, 5, 5);
150
                panel.add(getCheckBoxLimits(), gridBagConstraints);
151
                
152
                gridBagConstraints = new GridBagConstraints();
153
                gridBagConstraints.gridx = 0;
154
                gridBagConstraints.gridy = 1;
155
                gridBagConstraints.gridwidth = 5;
156
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
157
                gridBagConstraints.insets = new java.awt.Insets(2, 5, 5, 5);
158
                add(panel, gridBagConstraints);
159
        }
160
        
161
        protected void translate() {
162
        }
163

    
164
        /**
165
         * Obtiene el jCBInterpolated
166
         * @return
167
         */
168
        private JCheckBox getCheckBoxInterpolated() {
169
                if (checkBoxInterpolated == null) {
170
                        checkBoxInterpolated = new JCheckBox(getText(this, "interpolado"));
171
                        checkBoxInterpolated.setSelected(true);
172
                        checkBoxInterpolated.addActionListener(this);
173
                }
174
                return checkBoxInterpolated;
175
        }
176

    
177
        /**
178
         * Obtiene el jCBInterpolated
179
         * @return
180
         */
181
        private JButton getButtonStatistics() {
182
                if (buttonStatistics == null) {
183
                        buttonStatistics = new JButton(getText(this, "recalc_stats"));
184
                        buttonStatistics.addActionListener(this);
185
                }
186
                return buttonStatistics;
187
        }
188

    
189
        /**
190
         * Obtiene el jCBInterpolated
191
         * @return
192
         */
193
        public JCheckBox getCheckBoxEnabled() {
194
                if (checkBoxEnabled == null) {
195
                        checkBoxEnabled = new JCheckBox(getText(this, "activar_tablas_color"));
196
                        checkBoxEnabled.setSelected(true);
197
                        checkBoxEnabled.addActionListener(this);
198
                }
199
                return checkBoxEnabled;
200
        }
201
        
202
        /**
203
         * Activa el control de interpolacion
204
         * @param b
205
         */
206
        public void setCheckBoxInterpolated(boolean b) {
207
                getCheckBoxInterpolated().setSelected(b);
208
                colorTableData.setInterpolated(b);
209
        }
210
        
211
        /**
212
         * Activa el uso de tablas de color para el panel
213
         * @param b
214
         */
215
        public void setCheckBoxEnabled(boolean b) {
216
                getCheckBoxEnabled().setSelected(b);
217
                colorTableData.setEnabled(b);
218
        }
219
        
220
        private boolean isInterpolated() {
221
                return getCheckBoxInterpolated().isSelected();
222
        }
223
        
224
        private boolean isCheckEnabled() {
225
                return getCheckBoxEnabled().isSelected();
226
        }
227
        
228
        /**
229
         * Activa/Desactiva los componentes de las pesta?as segun la pesta?a selecionada
230
         * @param enabled
231
         */
232
        public void setEnabledPanel(boolean enabled) {
233
                getLabelMinim().setEnabled(enabled);
234
                getTextFieldMinim().setEnabled(enabled);
235
                getLabelMaxim().setEnabled(enabled);
236
                getTextFieldMaxim().setEnabled(enabled);
237
                getCheckBoxLimits().setEnabled(enabled);
238
                getCheckBoxInterpolated().setEnabled(enabled);
239
                getButtonStatistics().setEnabled(enabled);
240
                if (enabled)
241
                        setLimitsEnabled(getCheckBoxLimits().isSelected());
242
        }
243

    
244
        public void actionPerformed(ActionEvent e) {
245
                if (e.getSource() == getCheckBoxInterpolated()) {
246
                        colorTableData.setInterpolated(isInterpolated());
247
                        return;
248
                }
249

    
250
                if (e.getSource() == getCheckBoxEnabled()) {
251
                        colorTableData.setEnabled(isCheckEnabled());
252
                        return;
253
                }
254

    
255
                if (e.getSource() == getCheckBoxLimits()) {
256
                        setLimitsEnabled(getCheckBoxLimits().isSelected());
257
                        return;
258
                }
259
                
260
                if ((e.getSource() == getButtonStatistics()) &&
261
                                (fLayer != null)) {
262
                        RasterProcess process = new StatisticsProcess();
263
                        process.setActions(this);
264
                        process.addParam("layer", fLayer);
265
                        process.addParam("force", new Boolean(false));
266
                        process.start();
267
                        return;
268
                }
269
        }
270
        
271
        /**
272
         * Indica si los limites estaran activos para la tabla de color activa
273
         * @param enabled
274
         */
275
        public void setLimitsEnabled(boolean enabled) {
276
                colorTableData.setLimitsEnabled(enabled);
277
                getCheckBoxLimits().setSelected(enabled);
278
                getLabelMaxim().setEnabled(enabled);
279
                getLabelMinim().setEnabled(enabled);
280
                getTextFieldMinim().setEnabled(enabled);
281
                getTextFieldMaxim().setEnabled(enabled);
282
                getButtonStatistics().setEnabled(enabled);
283
        }
284

    
285
        /**
286
         * @return the checkBoxLimits
287
         */
288
        private JCheckBox getCheckBoxLimits() {
289
                if (checkBoxLimits == null) {
290
                        checkBoxLimits = new JCheckBox();
291
                        checkBoxLimits.setText(getText(this, "ajustar_limites"));
292
                        checkBoxLimits.addActionListener(this);
293
                }
294
                return checkBoxLimits;
295
        }
296

    
297
        /**
298
         * @return the textFieldMinim
299
         */
300
        private JFormattedTextField getTextFieldMinim() {
301
                if (textFieldMinim == null) {
302
                        textFieldMinim = new JFormattedTextField(new DefaultFormatterFactory(
303
                                        new NumberFormatter(doubleDisplayFormat),
304
                                        new NumberFormatter(doubleDisplayFormat),
305
                                        new NumberFormatter(doubleEditFormat)));
306
                        textFieldMinim.setText("0");
307
                        textFieldMinim.setEnabled(false);
308
                        textFieldMinim.addPropertyChangeListener("value", this);
309
                }
310
                return textFieldMinim;
311
        }
312

    
313
        /**
314
         * @return the textFieldMaxim
315
         */
316
        private JFormattedTextField getTextFieldMaxim() {
317
                if (textFieldMaxim == null) {
318
                        textFieldMaxim = new JFormattedTextField(new DefaultFormatterFactory(
319
                                        new NumberFormatter(doubleDisplayFormat),
320
                                        new NumberFormatter(doubleDisplayFormat),
321
                                        new NumberFormatter(doubleEditFormat)));
322
                        textFieldMaxim.setText("255");
323
                        textFieldMaxim.setEnabled(false);
324
                        textFieldMaxim.addPropertyChangeListener("value", this);
325
                }
326
                return textFieldMaxim;
327
        }
328

    
329
        /**
330
         * @return the labelMinim
331
         */
332
        private JLabel getLabelMinim() {
333
                if (labelMinim == null) {
334
                        labelMinim = new JLabel();
335
                        labelMinim.setText(getText(this, "minimo") + ":");
336
                        labelMinim.setEnabled(false);
337
                }
338
                return labelMinim;
339
        }
340

    
341
        /**
342
         * @return the labelMaxim
343
         */
344
        private JLabel getLabelMaxim() {
345
                if (labelMaxim == null) {
346
                        labelMaxim = new JLabel();
347
                        labelMaxim.setText(getText(this, "maximo") + ":");
348
                        labelMaxim.setEnabled(false);
349
                }
350
                return labelMaxim;
351
        }
352
        
353
        private void setLimitMaxim(double limit) {
354
                getTextFieldMaxim().setValue(Double.valueOf(limit));
355
                colorTableData.setMaxim(limit);
356
        }
357
        
358
        private void setLimitMinim(double limit) {
359
                getTextFieldMinim().setValue(Double.valueOf(limit));
360
                colorTableData.setMinim(limit);
361
        }
362

    
363
        /*
364
         * (non-Javadoc)
365
         * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
366
         */
367
        public void propertyChange(PropertyChangeEvent evt) {
368
                if (evt.getSource() == getTextFieldMaxim()) {
369
                        double max = ((Number) getTextFieldMaxim().getValue()).doubleValue();
370
                        colorTableData.setMaxim(max);
371
                        return;
372
                }
373
                
374
                if (evt.getSource() == getTextFieldMinim()) {
375
                        double min = ((Number) getTextFieldMinim().getValue()).doubleValue();
376
                        colorTableData.setMinim(min);
377
                        return;
378
                }
379
        }
380
        
381
        /**
382
         * Establece los rangos de minimo y maximo obtenidos de las estadisticas
383
         * de la capa.
384
         */
385
        private void putStatistics() {
386
                Statistics statistics = ((FLyrRaster) fLayer).getDataStore().getStatistics();
387
                if (statistics.isCalculated()) {
388
                        // En caso de que al hacer la conversion a byte sea igual al valor double
389
                        // podremos pensar que estamos en un rango RGB y por lo tanto definir
390
                        // el limite segun los valores RGB, en caso contrario, no deberiamos usar
391
                        // el valor RGB para mayor fiabilidad
392
                        if (        ((FLyrRaster) fLayer).getDataStore().getDataType()[0] == Buffer.TYPE_BYTE &&
393
                                        ((byte) statistics.getMinimun() == (double) statistics.getMinimun()) &&
394
                                        ((byte) statistics.getMaximun() == (double) statistics.getMaximun())) {
395
                                setLimitMaxim(statistics.getMaximunByteUnsigned());
396
                                setLimitMinim(statistics.getMinimunByteUnsigned());
397
                        } else {
398
                                setLimitMaxim(statistics.getMaximun());
399
                                setLimitMinim(statistics.getMinimun());
400
                        }
401
                }
402
        }
403

    
404
        /**
405
         * Establece que capa se va a usar en el panel para aplicarle la tabla de
406
         * color
407
         * @param fLayer
408
         */
409
        public void setLayer(FLyrRaster fLayer) {
410
                this.fLayer = fLayer;
411
                putStatistics();
412
        }
413

    
414
        public void end(Object param) {
415
                putStatistics();
416
        }
417

    
418
        public void interrupted() {}
419

    
420
}