Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / raster / bean / stretchselector / StretchSelectorPanel.java @ 2480

History | View | Annotate | Download (11.6 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.raster.bean.stretchselector;
23

    
24
import java.awt.Dimension;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.util.EventObject;
31
import java.util.Observable;
32
import java.util.Observer;
33

    
34
import javax.swing.BorderFactory;
35
import javax.swing.ButtonGroup;
36
import javax.swing.JLabel;
37
import javax.swing.JPanel;
38
import javax.swing.JRadioButton;
39

    
40
import org.gvsig.andami.PluginServices;
41
import org.gvsig.gui.beans.datainput.DataInputContainerListener;
42
import org.gvsig.gui.beans.datainput.DataInputField;
43
import org.gvsig.raster.util.BasePanel;
44
import org.gvsig.raster.util.GenericBasePanel;
45

    
46

    
47
/**
48
 * Clase que contiene el panel con los elementos para a?adir una serie
49
 * de intervalos. La selecci?n de intervalos puede ser seleccionando el n?mero de
50
 * estos para una distancia definida entre un m?ximo y un m?nimo. En este caso se
51
 * calcular? autom?ticamente la distancia entre los tramos. Tambi?n puede ser calculada
52
 * a partir de una distancia dada. En este caso calcular? autom?ticamenet el n?mero de 
53
 * tramos para la longitud total.
54
 * 
55
 * El controlador est? metido dentro del mismo panel por lo que tendr? tambi?n una referencia
56
 * al modelo de datos.
57
 * 
58
 * 06/08/2008
59
 * @author Nacho Brodin nachobrodin@gmail.com
60
 */
61
public class StretchSelectorPanel extends BasePanel implements ActionListener, DataInputContainerListener, Observer {
62
        private static final long    serialVersionUID     = 1638303379491319120L;
63

    
64
        private boolean              selectedCount        = false;
65
        private ButtonGroup          buttonGroup1         = null;
66

    
67
        private JLabel               jLabelMinim          = null;
68
        private JLabel               jLabelMaxim          = null;
69
        private BasePanel            jPanel1              = null;
70
        private JRadioButton         jRadioIntervalSize   = null;
71
        private JRadioButton         jRadioIntervalNumber = null;
72

    
73
        private DataInputField       jTextMinim           = null;
74
        private DataInputField       jTextMaxim           = null;
75
        private DataInputField       jTextValue           = null;
76
        private StretchSelectorData  data                 = null;
77
                
78
        /**
79
         * Constructor. Llama al inicializador de elementos gr?ficos
80
         */
81
        public StretchSelectorPanel() {
82
                init();
83
                translate();
84
        }
85
        
86
        /**
87
         * Constructor. Llama al inicializador de elementos gr?ficos
88
         */
89
        public StretchSelectorPanel(StretchSelectorData data) {
90
                this.data = data;
91
                this.data.addObserver(this);
92
                init();
93
                translate();
94
        }
95
        
96
        protected void translate() {
97
                jLabelMinim.setText(getText(this, "minimo") + ":");
98
                jLabelMaxim.setText(getText(this, "maximo") + ":");
99
                getIntervalSize().setText(getText(this, "tamano_intervalo"));
100
                getIntervalNumber().setText(getText(this, "n_intervalos"));
101
        }
102
        
103
        /**
104
         * Inicializa los elementos gr?ficos
105
         */
106
        protected void init() {
107
                GridBagConstraints gridBagConstraints;
108

    
109
                setLayout(new GridBagLayout());
110
                jPanel1 = new GenericBasePanel();
111
                jPanel1.setLayout(new GridBagLayout());
112
                jPanel1.setBorder(BorderFactory.createTitledBorder(""));
113

    
114
                int y = 1;
115

    
116
            jLabelMinim = new JLabel();
117
                gridBagConstraints = new GridBagConstraints();
118
                gridBagConstraints.gridx = 0;
119
                gridBagConstraints.gridy = y;
120
                gridBagConstraints.anchor = GridBagConstraints.EAST;
121
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
122
                add(jLabelMinim, gridBagConstraints);
123

    
124
                gridBagConstraints = new GridBagConstraints();
125
                gridBagConstraints.gridx = 1;
126
                gridBagConstraints.gridy = y;
127
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
128
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
129
                add(getMinimText(), gridBagConstraints);
130

    
131
                y++;
132
            jLabelMaxim = new JLabel();
133
                gridBagConstraints = new GridBagConstraints();
134
                gridBagConstraints.gridx = 0;
135
                gridBagConstraints.gridy = y;
136
                gridBagConstraints.anchor = GridBagConstraints.EAST;
137
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
138
                add(jLabelMaxim, gridBagConstraints);
139

    
140
                gridBagConstraints = new GridBagConstraints();
141
                gridBagConstraints.gridx = 1;
142
                gridBagConstraints.gridy = y;
143
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
144
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
145
                add(getMaximunText(), gridBagConstraints);
146

    
147
                y++;
148
                gridBagConstraints = new GridBagConstraints();
149
                gridBagConstraints.gridx = 0;
150
                gridBagConstraints.gridy = y;
151
                gridBagConstraints.gridwidth = 2;
152
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
153
                gridBagConstraints.weightx = 1.0;
154
                gridBagConstraints.insets = new Insets(2, 5, 0, 5);
155
                add(jPanel1, gridBagConstraints);
156

    
157
                // Panel vacio
158
                y++;
159
                gridBagConstraints = new GridBagConstraints();
160
                gridBagConstraints.gridx = 0;
161
                gridBagConstraints.gridy = y;
162
                gridBagConstraints.gridwidth = 2;
163
                gridBagConstraints.fill = GridBagConstraints.BOTH;
164
                gridBagConstraints.weightx = 1.0;
165
                gridBagConstraints.weighty = 1.0;
166
                JPanel panel2 = new JPanel();
167
                panel2.setPreferredSize(new Dimension(0, 0));
168
                add(panel2, gridBagConstraints);
169

    
170
                gridBagConstraints = new GridBagConstraints();
171
                gridBagConstraints.anchor = GridBagConstraints.WEST;
172
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
173
                jPanel1.add(getIntervalSize(), gridBagConstraints);
174

    
175
                gridBagConstraints = new GridBagConstraints();
176
                gridBagConstraints.gridx = 0;
177
                gridBagConstraints.gridy = 1;
178
                gridBagConstraints.anchor = GridBagConstraints.WEST;
179
                gridBagConstraints.insets = new Insets(2, 5, 2, 5);
180
                jPanel1.add(getIntervalNumber(), gridBagConstraints);
181

    
182
                gridBagConstraints = new GridBagConstraints();
183
                gridBagConstraints.gridx = 0;
184
                gridBagConstraints.gridy = 2;
185
                gridBagConstraints.weightx = 1.0;
186
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
187
                gridBagConstraints.insets = new Insets(2, 5, 5, 5);
188
                jPanel1.add(getIntervalInputField(), gridBagConstraints);
189

    
190
                buttonGroup1 = new ButtonGroup();
191
                buttonGroup1.add(getIntervalSize());
192
                buttonGroup1.add(getIntervalNumber());
193
        }
194
        
195
        /**
196
         * Obtiene el modelo de datos o crea uno si no encuentra ninguno
197
         * @return StretchSelectorData
198
         */
199
        public StretchSelectorData getData() {
200
                if(data == null) {
201
                        data = new StretchSelectorData();
202
                        data.addObserver(this);
203
                }
204
                return data;
205
        }
206
        
207
        /**
208
         * Asigna el modelo de datos y a?ade el panel como observador
209
         * @param data
210
         */
211
        public void setData(StretchSelectorData data) {
212
                this.data = data;
213
                data.addObserver(this);
214
        }
215
        
216
        /**
217
         * Obtiene el componente con la entrada de texto para el valor del m?nimo
218
         * @return DataInputField
219
         */
220
        private DataInputField getIntervalInputField() {
221
                if(jTextValue == null) {
222
                        jTextValue = new DataInputField();
223
                        jTextValue.setValue(Double.valueOf(255).toString());
224
                        jTextValue.addValueChangedListener(this);
225
                }
226
                return jTextValue;                        
227
        }
228
        
229
        /**
230
         * Obtiene el componente con la entrada de texto para el valor del m?nimo
231
         * @return DataInputField
232
         */
233
        private DataInputField getMinimText() {
234
                if(jTextMinim == null) {
235
                        jTextMinim = new DataInputField();
236
                        jTextMinim.setValue(Double.valueOf("0").toString());
237
                        jTextMinim.addValueChangedListener(this);
238
                }
239
                return jTextMinim;                        
240
        }
241
        
242
        /**
243
         * Obtiene el componente con la entrada de texto para el valor del m?nimo
244
         * @return DataInputField
245
         */
246
        private DataInputField getMaximunText() {
247
                if(jTextMaxim == null) {
248
                        jTextMaxim = new DataInputField();
249
                        jTextMaxim.setValue(Double.valueOf("255").toString());
250
                        jTextMaxim.addValueChangedListener(this);
251
                }
252
                return jTextMaxim;                        
253
        }
254
        
255
        /**
256
         * Obtiene el componente bot?n de selecci?n de creaci?n de tramos por valor del intervalo
257
         * @return JRadioButton
258
         */
259
        public JRadioButton getIntervalSize() {
260
                if(jRadioIntervalSize == null) {
261
                        jRadioIntervalSize = new JRadioButton(PluginServices.getText(this, "tamano_intervalo"), true);
262
                        jRadioIntervalSize.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
263
                        jRadioIntervalSize.setMargin(new Insets(0, 0, 0, 0));
264
                        jRadioIntervalSize.addActionListener(this);
265
                }
266
                return jRadioIntervalSize;                        
267
        }
268
        
269
        /**
270
         * Obtiene el componente bot?n de selecci?n de creaci?n de tramos por n?mero de intervalos
271
         * @return JRadioButton
272
         */
273
        public JRadioButton getIntervalNumber() {
274
                if(jRadioIntervalNumber == null) {
275
                        jRadioIntervalNumber = new JRadioButton(PluginServices.getText(this, "n_intervalos"));
276
                        jRadioIntervalNumber.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
277
                        jRadioIntervalNumber.setMargin(new Insets(0, 0, 0, 0));
278
                        jRadioIntervalNumber.addActionListener(this);
279
                }
280
                return jRadioIntervalNumber;                        
281
        }
282
                
283
        /************************************
284
         *           FUNCIONALIDAD
285
         ************************************/
286

    
287
        /**
288
         * Actualiza el modelo de datos
289
         */
290
        private void updateData() {
291
                if (getIntervalNumber().isSelected()) {
292
                        if (!selectedCount) {
293
                                getData().setIntervalSize(getIntervalInputField().getValue());
294
                                selectedCount = true;
295
                        }
296
                } else {
297
                        if (selectedCount) {
298
                                getData().setNumber(getIntervalInputField().getValue());
299
                                selectedCount = false;
300
                        }
301
                }
302
                getData().setMin(getMinimText().getValue());
303
                getData().setMax(getMaximunText().getValue());        
304
        }
305
        
306
        public void actionPerformed(ActionEvent e) {
307
                if(!isEnableValueChangedEvent())
308
                        return;
309
                updateData();
310
        }
311
        
312
        public void actionValueChanged(EventObject e) {
313
                if(!isEnableValueChangedEvent())
314
                        return;
315
                getInterval();
316
        }
317

    
318
        /**
319
         * Devuelve un intervalo valido
320
         * @return
321
         */
322
        public double getInterval() {
323
                getData().setMin(getMinimText().getValue());
324
                getData().setMax(getMaximunText().getValue());        
325
                if (getIntervalNumber().isSelected())
326
                        getData().setNumber(getIntervalInputField().getValue());
327
                else
328
                        getData().setIntervalSize(getIntervalInputField().getValue());
329
                double value = Double.parseDouble(getIntervalInputField().getValue());
330
                double value2 = value;
331
                if (getIntervalSize().isSelected()) {
332
                        if (value > Math.abs(getData().getMaximum() - getData().getMinimum()))
333
                                value = Math.abs(getData().getMaximum() - getData().getMinimum());
334
                        if (value <= 0)
335
                                value = 1;
336
                } else {
337
                        if (value < 1)
338
                                value = 1;
339
                }
340
                value = Math.abs(value);
341
                
342
                if (value != value2) {
343
                        if (getIntervalNumber().isSelected())
344
                                getData().setNumber(Double.valueOf(value).toString());
345
                        else
346
                                getData().setIntervalSize(Double.valueOf(value).toString());
347
                }
348
                return value;
349
        }
350
        
351
        /**
352
         * Actualiza los valores de los paneles cuando los datos de ClippingData varian
353
         * @param o 
354
         * @param arg
355
         */
356
        public void update(Observable o, Object arg) {
357
                if(!(o instanceof StretchSelectorData))
358
                        return;
359
                StretchSelectorData data = (StretchSelectorData)o;
360
                
361
                setEnableValueChangedEvent(false);
362
                if(getIntervalNumber().isSelected())
363
                        getIntervalInputField().setValue(data.getNumber());
364
                if(getIntervalSize().isSelected())
365
                        getIntervalInputField().setValue(data.getIntervalSize());
366
                getMinimText().setValue(data.getMin());
367
                getMaximunText().setValue(data.getMax());
368
                setEnableValueChangedEvent(true);
369
        }
370
}