Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / panels / TabInterpolated.java @ 13396

History | View | Annotate | Download (14.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2007 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.rastertools.colortable.panels;
20

    
21
import java.awt.Color;
22
import java.awt.GridBagConstraints;
23
import java.awt.GridBagLayout;
24
import java.awt.Insets;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.awt.event.FocusEvent;
28
import java.awt.event.FocusListener;
29
import java.util.ArrayList;
30

    
31
import javax.swing.BorderFactory;
32
import javax.swing.JCheckBox;
33
import javax.swing.JLabel;
34
import javax.swing.JPanel;
35
import javax.swing.JTextField;
36

    
37
import org.gvsig.gui.beans.colorbutton.ColorButton;
38
import org.gvsig.gui.beans.colorbutton.ColorButtonEvent;
39
import org.gvsig.gui.beans.colorbutton.ColorButtonListener;
40
import org.gvsig.gui.beans.colorslideredition.ColorSliderEdition;
41
import org.gvsig.gui.beans.colorslideredition.ColorSliderEvent;
42
import org.gvsig.gui.beans.colorslideredition.ColorSliderListener;
43
import org.gvsig.gui.beans.colorslideredition.ItemColorSlider;
44
import org.gvsig.raster.datastruct.ColorItem;
45
import org.gvsig.rastertools.colortable.ui.ColorTableIconPainter;
46
import org.gvsig.rastertools.colortable.ui.ColorTablePanel;
47

    
48
import com.iver.andami.PluginServices;
49
/**
50
 * Pesta?a para las interpolaciones de la tabla de color, aqui se tratar?n
51
 * los gradientes de color, etc...
52
 *
53
 * @version 27/06/2007
54
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
55
 */
56
public class TabInterpolated extends JPanel implements ColorSliderListener, ColorButtonListener, ActionListener, FocusListener {
57
        private static final long  serialVersionUID   = -5208861410196059899L;
58

    
59
        private double             min                = Double.POSITIVE_INFINITY;
60
        private double             max                = Double.NEGATIVE_INFINITY;
61
        private ColorTablePanel    colorTablePanel    = null;
62

    
63
        //[start]Variables UI
64
        private ColorSliderEdition colorSliderEdition = null;
65
        private ColorButton        colorButton        = null;
66
        private JLabel             jLabelColor        = null;
67
        private JPanel             jPanelLimits       = null;
68
        private JCheckBox          jCheckBoxLimits    = null;
69
        private JLabel             jLabelMinimum      = null;
70
        private JLabel             jLabelMaximum      = null;
71
        private JTextField         jTextFieldMinimum  = null;
72

    
73
        private JTextField         jTextFieldMaximum  = null;
74
        //[end]
75

    
76
        /**
77
         * Constructor de un TabInterpolated
78
         * @param colorTablePanel
79
         */
80
        public TabInterpolated(ColorTablePanel colorTablePanel) {
81
                this.colorTablePanel = colorTablePanel;
82
                initialize();
83
        }
84

    
85
        //[start] Code UI
86

    
87
        /**
88
         * Creaci?n de todos los componentes del interfaz
89
         */
90
        public void initialize() {
91
                setLayout(new GridBagLayout());
92

    
93
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
94
                gridBagConstraints.gridwidth = 2;
95
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
96
                gridBagConstraints.weightx = 1.0;
97
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
98

    
99
                add(getColorSliderEdition(), gridBagConstraints);
100

    
101
                gridBagConstraints = new GridBagConstraints();
102
                gridBagConstraints.gridx = 1;
103
                gridBagConstraints.gridy = 1;
104
                gridBagConstraints.anchor = GridBagConstraints.WEST;
105
                gridBagConstraints.weightx = 0.5;
106
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
107
                add(getColorButton(), gridBagConstraints);
108

    
109
                jLabelColor = new JLabel(PluginServices.getText(this, "tabinterpolated_color") + ": ");
110
                jLabelColor.setEnabled(false);
111
                gridBagConstraints = new GridBagConstraints();
112
                gridBagConstraints.gridx = 0;
113
                gridBagConstraints.gridy = 1;
114
                gridBagConstraints.anchor = GridBagConstraints.EAST;
115
                gridBagConstraints.weightx = 0.5;
116
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
117
                add(jLabelColor, gridBagConstraints);
118

    
119
                gridBagConstraints = new GridBagConstraints();
120
                gridBagConstraints.gridx = 0;
121
                gridBagConstraints.gridy = 2;
122
                gridBagConstraints.anchor = GridBagConstraints.CENTER;
123
                gridBagConstraints.gridwidth = 2;
124
                gridBagConstraints.weightx = 1.0;
125
                gridBagConstraints.insets = new Insets(2, 5, 5, 5);
126
                add(getJPanelLimits(), gridBagConstraints);
127
        }
128

    
129
        private JLabel getJLabelMinimum() {
130
                if (jLabelMinimum == null) {
131
                        jLabelMinimum = new JLabel();
132
                        jLabelMinimum.setText(PluginServices.getText(this, "minimo") + ": ");
133
                        jLabelMinimum.setEnabled(false);
134
                }
135
                return jLabelMinimum;
136
        }
137

    
138
        private JLabel getJLabelMaximum() {
139
                if (jLabelMaximum == null) {
140
                        jLabelMaximum = new JLabel();
141
                        jLabelMaximum.setText(PluginServices.getText(this, "maximo")+ ": ");
142
                        jLabelMaximum.setEnabled(false);
143
                }
144
                return jLabelMaximum;
145
        }
146

    
147
        private JTextField getJTextFieldMinimum() {
148
                if (jTextFieldMinimum == null) {
149
                        jTextFieldMinimum = new JTextField();
150
                        jTextFieldMinimum.setText("0");
151
                        jTextFieldMinimum.setEnabled(false);
152
                        jTextFieldMinimum.addFocusListener(this);
153
                }
154
                return jTextFieldMinimum;
155
        }
156

    
157
        private JTextField getJTextFieldMaximum() {
158
                if (jTextFieldMaximum == null) {
159
                        jTextFieldMaximum = new JTextField();
160
                        jTextFieldMaximum.setText("255");
161
                        jTextFieldMaximum.setEnabled(false);
162
                        jTextFieldMaximum.addFocusListener(this);
163
                }
164
                return jTextFieldMaximum;
165
        }
166

    
167
        private JCheckBox getJCheckBoxLimits() {
168
                if (jCheckBoxLimits == null) {
169
                        jCheckBoxLimits = new JCheckBox();
170
                        jCheckBoxLimits.setText(PluginServices.getText(this, "ajustar_limites"));
171
                        jCheckBoxLimits.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
172
                        jCheckBoxLimits.setMargin(new Insets(0, 0, 0, 0));
173
                        jCheckBoxLimits.addActionListener(this);
174
                }
175
                return jCheckBoxLimits;
176
        }
177

    
178
        private JPanel getJPanelLimits() {
179
                if (jPanelLimits == null) {
180
                        java.awt.GridBagConstraints gridBagConstraints;
181

    
182
                        jPanelLimits = new JPanel();
183
                        jPanelLimits.setLayout(new GridBagLayout());
184

    
185
                        jPanelLimits.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "limites")));
186
                        gridBagConstraints = new GridBagConstraints();
187
                        gridBagConstraints.gridwidth = 2;
188
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
189
                        gridBagConstraints.insets = new Insets(5, 5, 2, 5);
190
                        jPanelLimits.add(getJCheckBoxLimits(), gridBagConstraints);
191

    
192
                        gridBagConstraints = new GridBagConstraints();
193
                        gridBagConstraints.gridx = 0;
194
                        gridBagConstraints.gridy = 1;
195
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
196
                        gridBagConstraints.insets = new Insets(2, 5, 2, 2);
197
                        jPanelLimits.add(getJLabelMinimum(), gridBagConstraints);
198

    
199
                        gridBagConstraints = new GridBagConstraints();
200
                        gridBagConstraints.gridx = 0;
201
                        gridBagConstraints.gridy = 2;
202
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
203
                        gridBagConstraints.insets = new Insets(2, 5, 5, 2);
204
                        jPanelLimits.add(getJLabelMaximum(), gridBagConstraints);
205

    
206
                        gridBagConstraints = new GridBagConstraints();
207
                        gridBagConstraints.gridx = 1;
208
                        gridBagConstraints.gridy = 1;
209
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
210
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
211
                        gridBagConstraints.weightx = 1.0;
212
                        gridBagConstraints.insets = new Insets(2, 2, 2, 5);
213
                        jPanelLimits.add(getJTextFieldMinimum(), gridBagConstraints);
214

    
215
                        gridBagConstraints = new GridBagConstraints();
216
                        gridBagConstraints.gridx = 1;
217
                        gridBagConstraints.gridy = 2;
218
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
219
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
220
                        gridBagConstraints.weightx = 1.0;
221
                        gridBagConstraints.insets = new Insets(2, 2, 5, 5);
222
                        jPanelLimits.add(getJTextFieldMaximum(), gridBagConstraints);
223
                }
224
                return jPanelLimits;
225
        }
226

    
227
        public ColorSliderEdition getColorSliderEdition() {
228
                if (colorSliderEdition == null) {
229
                        colorSliderEdition = new ColorSliderEdition();
230
                        colorSliderEdition.addValueChangedListener(this);
231
                        colorSliderEdition.addSelectionChangedListener(this);
232
                }
233
                return colorSliderEdition;
234
        }
235

    
236
        private ColorButton getColorButton() {
237
                if (colorButton == null) {
238
                        colorButton = new ColorButton();
239
                        colorButton.addValueChangedListener(this);
240
                }
241
                return colorButton;
242
        }
243
        //[end]
244

    
245
        /**
246
         * Define si el componente ColorSliderEdition tiene interpolaci?n o no.
247
         * @param value
248
         */
249
        public void setInterpolated(boolean value) {
250
                getColorSliderEdition().setInterpolated(value);
251
        }
252

    
253
        /*
254
         * (non-Javadoc)
255
         * @see org.gvsig.gui.beans.colorslideredition.ColorSliderListener#actionSelectionChanged(org.gvsig.gui.beans.colorslideredition.ColorSliderEvent)
256
         */
257
        public void actionSelectionChanged(ColorSliderEvent e) {
258
                ItemColorSlider itemColorSlider = getColorSliderEdition().getSelectedItem();
259
                if ((itemColorSlider != null) && (itemColorSlider.getSelected() == 1)) {
260
                        getColorButton().setColor(itemColorSlider.getColor());
261
                        getColorButton().setEnabled(true);
262
                        jLabelColor.setEnabled(true);
263
                } else {
264
                        getColorButton().setEnabled(false);
265
                        jLabelColor.setEnabled(false);
266
                }
267
        }
268

    
269
        /**
270
         * Acci?n que se ejecuta cuando cambia el color el usuario. Este metodo
271
         * actualiza el color del ItemColorSlider segun la seleccion del usuario.
272
         */
273
        private void callValueColorChanged() {
274
                ItemColorSlider itemColorSlider = getColorSliderEdition().getSelectedItem();
275
                if (itemColorSlider != null) {
276
                        itemColorSlider.setColor(getColorButton().getColor());
277
                        getColorSliderEdition().repaint();
278
                }
279
        }
280

    
281
        /*
282
         * (non-Javadoc)
283
         * @see org.gvsig.gui.beans.colorbutton.ColorButtonListener#actionValueDragged(org.gvsig.gui.beans.colorbutton.ColorButtonEvent)
284
         */
285
        public void actionValueDragged(ColorButtonEvent e) {
286
                callValueColorChanged();
287
                // refreshItems();
288
        }
289

    
290
        /**
291
         * Convierte el slider de la paleta en un array de objetos para poder crear
292
         * con ?l el objeto Palette
293
         * @return
294
         */
295
        public ArrayList getPalette() {
296
                double min2 = min;
297
                double max2 = max;
298

    
299
                if (getJCheckBoxLimits().isSelected()) {
300
                        min2 = Double.parseDouble(getJTextFieldMinimum().getText());
301
                        max2 = Double.parseDouble(getJTextFieldMaximum().getText());
302
                }
303

    
304

    
305
                ArrayList arrayList = new ArrayList();
306
                ArrayList items = getColorSliderEdition().getItemsShowed();
307

    
308
                // A?adir el minimo
309
                ItemColorSlider item;
310
                if (items.size() > 0)
311
                        item = (ItemColorSlider) items.get(0);
312
                else
313
                        item = new ItemColorSlider(0, Color.black);
314
                ColorItem colorItem = new ColorItem();
315
                colorItem.setColor(item.getColor());
316
                colorItem.setInterpolated(50);
317
                colorItem.setNameClass("");
318
                colorItem.setValue(min2);
319
                arrayList.add(colorItem);
320

    
321
                for (int i = 0; i < items.size(); i++) {
322
                        item = (ItemColorSlider) items.get(i);
323
                        colorItem = new ColorItem();
324
                        colorItem.setColor(item.getColor());
325
                        colorItem.setInterpolated((int) item.getInterpolated());
326
                        colorItem.setNameClass(item.getName());
327

    
328
                        colorItem.setValue(min2 + ((item.getValue() * (max2 - min2)) / 100.0d));
329

    
330
                        arrayList.add(colorItem);
331
                }
332

    
333
                // A?adir el maximo
334
                if (items.size() > 0)
335
                        item = (ItemColorSlider) items.get(items.size() - 1);
336
                else
337
                        item = new ItemColorSlider(255, Color.white);
338
                colorItem = new ColorItem();
339
                colorItem.setColor(item.getColor());
340
                colorItem.setInterpolated(50);
341
                colorItem.setNameClass("");
342
                colorItem.setValue(max2);
343
                arrayList.add(colorItem);
344

    
345

    
346
                return arrayList;
347
        }        
348
        
349
        /**
350
         * Refresco de los items del panel de color
351
         * @param colorItems
352
         */
353
        public void refreshItems() {
354
                ((ColorTableIconPainter) colorTablePanel.getListViewComponent().getSelectedValue().getIcon()).setColorItems(getPalette(), colorTablePanel.getCheckBoxInterpolated().isSelected(), false);
355
                colorTablePanel.tabChanged();
356
        }
357
        
358
        /*
359
         * (non-Javadoc)
360
         * @see org.gvsig.gui.beans.colorbutton.ColorButtonListener#actionValueChanged(org.gvsig.gui.beans.colorbutton.ColorButtonEvent)
361
         */
362
        public void actionValueChanged(ColorButtonEvent e) {
363
                callValueColorChanged();
364
                refreshItems();
365
        }
366

    
367
        /*
368
         * (non-Javadoc)
369
         * @see org.gvsig.gui.beans.colorslideredition.ColorSliderListener#actionValueChanged(org.gvsig.gui.beans.colorslideredition.ColorSliderEvent)
370
         */
371
        public void actionValueChanged(ColorSliderEvent e) {
372
                refreshItems();
373
        }
374

    
375
        /*
376
         * (non-Javadoc)
377
         * @see org.gvsig.gui.beans.colorslideredition.ColorSliderListener#actionValueDragged(org.gvsig.gui.beans.colorslideredition.ColorSliderEvent)
378
         */
379
        public void actionValueDragged(ColorSliderEvent e) {
380
                // refreshItems();
381
        }
382

    
383
        public void initialState() {
384
                getColorSliderEdition().repaint();
385
                getColorButton().setEnabled(false);
386
        }
387

    
388
        /**
389
         * Carga inicial de los items del panel de color
390
         * @param colorItems
391
         */
392
        public void reloadItems(ArrayList colorItems) {
393
                min = Double.POSITIVE_INFINITY;
394
                max = Double.NEGATIVE_INFINITY;
395
                for (int i = 0; i < colorItems.size(); i++) {
396
                        ColorItem c1 = (ColorItem) colorItems.get(i);
397
                        if (c1.getValue() > max)
398
                                max = c1.getValue();
399
                        if (c1.getValue() < min)
400
                                min = c1.getValue();
401
                }
402

    
403
                clearTable();
404
                for (int i = 0; i < colorItems.size(); i++) {
405
                        ColorItem c1 = (ColorItem) colorItems.get(i);
406
                        double percent = (((c1.getValue() - min) * 100) / (max - min));
407

    
408
                        ItemColorSlider aux = new ItemColorSlider(percent, c1.getColor());
409
                        aux.setInterpolated(c1.getInterpolated());
410
                        aux.setName(c1.getNameClass());
411

    
412
                        getColorSliderEdition().addItem(aux, false);
413
                }
414
                initialState();
415
                if (min > max) {
416
                        min = 0;
417
                        max = 255;
418
                }
419
                setInterpolated(colorTablePanel.getCheckBoxInterpolated().isSelected());
420
                refreshItems();
421
        }
422

    
423
        /**
424
         * Borra todas las filas de la tabla.
425
         */
426
        public void clearTable() {
427
                getColorSliderEdition().removeAllItems();
428
        }
429

    
430
        /*
431
         * (non-Javadoc)
432
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
433
         */
434
        public void actionPerformed(ActionEvent e) {
435
                if (e.getSource() == getJCheckBoxLimits()) {
436
                        getJTextFieldMaximum().setEnabled(getJCheckBoxLimits().isSelected());
437
                        getJTextFieldMinimum().setEnabled(getJCheckBoxLimits().isSelected());
438
                        getJLabelMaximum().setEnabled(getJCheckBoxLimits().isSelected());
439
                        getJLabelMinimum().setEnabled(getJCheckBoxLimits().isSelected());
440
                        callValueColorChanged();
441
                        refreshItems();
442
                }
443
        }
444

    
445
        /*
446
         * (non-Javadoc)
447
         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
448
         */
449
        public void focusLost(FocusEvent e) {
450
                callValueColorChanged();
451
                refreshItems();
452
  }
453
        
454
        public void focusGained(FocusEvent e) {}
455
}