Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / panels / TabInterpolated.java @ 13777

History | View | Annotate | Download (16.7 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.awt.event.KeyEvent;
30
import java.awt.event.KeyListener;
31
import java.util.ArrayList;
32

    
33
import javax.swing.BorderFactory;
34
import javax.swing.JCheckBox;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37
import javax.swing.JTextField;
38

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

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

    
62
        private double             min                = Double.POSITIVE_INFINITY;
63
        private double             max                = Double.NEGATIVE_INFINITY;
64
        private ColorTablePanel    colorTablePanel    = null;
65

    
66
        //[start]Variables UI
67
        private ColorSliderEdition colorSliderEdition = null;
68
        private ColorButton        colorButton        = null;
69
        private JTextField         jTextClassName     = null;
70
        private JPanel             jPanelLimits       = null;
71
        private JCheckBox          jCheckBoxLimits    = null;
72
        private JLabel             jLabelColor        = null;
73
        private JLabel             jLabelClassName    = null;
74
        private JLabel             jLabelMinimum      = null;
75
        private JLabel             jLabelMaximum      = null;
76
        private JTextField         jTextFieldMinimum  = null;
77

    
78
        private JTextField         jTextFieldMaximum  = null;
79
        //[end]
80

    
81
        /**
82
         * Constructor de un TabInterpolated
83
         * @param colorTablePanel
84
         */
85
        public TabInterpolated(ColorTablePanel colorTablePanel) {
86
                this.colorTablePanel = colorTablePanel;
87
                initialize();
88
        }
89

    
90
        //[start] Code UI
91
        /**
92
         * Creaci?n de todos los componentes del interfaz
93
         */
94
        public void initialize() {
95
                setLayout(new GridBagLayout());
96

    
97
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
98
                gridBagConstraints.gridwidth = 2;
99
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
100
                gridBagConstraints.weightx = 1.0;
101
                gridBagConstraints.insets = new Insets(5, 5, 2, 5);
102
                add(getColorSliderEdition(), gridBagConstraints);
103

    
104
                gridBagConstraints = new GridBagConstraints();
105
                gridBagConstraints.gridx = 0;
106
                gridBagConstraints.gridy = 1;
107
                gridBagConstraints.anchor = GridBagConstraints.EAST;
108
                gridBagConstraints.weightx = 0.5;
109
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
110
                add(getJLabelColor(), gridBagConstraints);
111

    
112
                gridBagConstraints = new GridBagConstraints();
113
                gridBagConstraints.gridx = 1;
114
                gridBagConstraints.gridy = 1;
115
                gridBagConstraints.anchor = GridBagConstraints.WEST;
116
                gridBagConstraints.weightx = 0.5;
117
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
118
                add(getColorButton(), gridBagConstraints);
119
                
120
                gridBagConstraints = new GridBagConstraints();
121
                gridBagConstraints.gridx = 0;
122
                gridBagConstraints.gridy = 2;
123
                gridBagConstraints.anchor = GridBagConstraints.EAST;
124
                gridBagConstraints.insets = new Insets(2, 5, 2, 2);
125
                add(getJLabelClassName(), gridBagConstraints);
126
                
127
                gridBagConstraints = new GridBagConstraints();
128
                gridBagConstraints.gridx = 1;
129
                gridBagConstraints.gridy = 2;
130
                gridBagConstraints.anchor = GridBagConstraints.WEST;
131
                gridBagConstraints.weightx = 1.0;
132
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
133
                gridBagConstraints.insets = new Insets(2, 2, 2, 5);
134
                add(getJTextClassName(), gridBagConstraints);
135
                
136
                gridBagConstraints = new GridBagConstraints();
137
                gridBagConstraints.gridx = 0;
138
                gridBagConstraints.gridy = 3;
139
                gridBagConstraints.anchor = GridBagConstraints.CENTER;
140
                gridBagConstraints.gridwidth = 2;
141
                gridBagConstraints.weightx = 1.0;
142
                gridBagConstraints.insets = new Insets(2, 5, 5, 5);
143
                add(getJPanelLimits(), gridBagConstraints);
144
        }
145

    
146
        private JLabel getJLabelMinimum() {
147
                if (jLabelMinimum == null) {
148
                        jLabelMinimum = new JLabel();
149
                        jLabelMinimum.setText(PluginServices.getText(this, "minimo") + ": ");
150
                        jLabelMinimum.setEnabled(false);
151
                }
152
                return jLabelMinimum;
153
        }
154

    
155
        private JLabel getJLabelMaximum() {
156
                if (jLabelMaximum == null) {
157
                        jLabelMaximum = new JLabel();
158
                        jLabelMaximum.setText(PluginServices.getText(this, "maximo")+ ": ");
159
                        jLabelMaximum.setEnabled(false);
160
                }
161
                return jLabelMaximum;
162
        }
163

    
164
        private JLabel getJLabelColor() {
165
                if (jLabelColor == null) {
166
                        jLabelColor = new JLabel(PluginServices.getText(this, "tabinterpolated_color") + ": ");
167
                        jLabelColor.setEnabled(false);
168
                }
169
                return jLabelColor;
170
        }
171
        
172
        private JLabel getJLabelClassName() {
173
                if (jLabelClassName == null) {
174
                        jLabelClassName = new JLabel(PluginServices.getText(this, "nombre") + ": ");
175
                        jLabelClassName.setEnabled(false);
176
                }
177
                return jLabelClassName;
178
        }
179

    
180
        private JTextField getJTextFieldMinimum() {
181
                if (jTextFieldMinimum == null) {
182
                        jTextFieldMinimum = new JTextField();
183
                        jTextFieldMinimum.setText("0");
184
                        jTextFieldMinimum.setEnabled(false);
185
                        jTextFieldMinimum.addFocusListener(this);
186
                }
187
                return jTextFieldMinimum;
188
        }
189

    
190
        private JTextField getJTextFieldMaximum() {
191
                if (jTextFieldMaximum == null) {
192
                        jTextFieldMaximum = new JTextField();
193
                        jTextFieldMaximum.setText("255");
194
                        jTextFieldMaximum.setEnabled(false);
195
                        jTextFieldMaximum.addFocusListener(this);
196
                }
197
                return jTextFieldMaximum;
198
        }
199

    
200
        private JCheckBox getJCheckBoxLimits() {
201
                if (jCheckBoxLimits == null) {
202
                        jCheckBoxLimits = new JCheckBox();
203
                        jCheckBoxLimits.setText(PluginServices.getText(this, "ajustar_limites"));
204
                        jCheckBoxLimits.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
205
                        jCheckBoxLimits.setMargin(new Insets(0, 0, 0, 0));
206
                        jCheckBoxLimits.addActionListener(this);
207
                }
208
                return jCheckBoxLimits;
209
        }
210

    
211
        private JPanel getJPanelLimits() {
212
                if (jPanelLimits == null) {
213
                        java.awt.GridBagConstraints gridBagConstraints;
214

    
215
                        jPanelLimits = new JPanel();
216
                        jPanelLimits.setLayout(new GridBagLayout());
217

    
218
                        jPanelLimits.setBorder(BorderFactory.createTitledBorder(PluginServices.getText(this, "limites")));
219
                        gridBagConstraints = new GridBagConstraints();
220
                        gridBagConstraints.gridwidth = 2;
221
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
222
                        gridBagConstraints.insets = new Insets(5, 5, 2, 5);
223
                        jPanelLimits.add(getJCheckBoxLimits(), gridBagConstraints);
224

    
225
                        gridBagConstraints = new GridBagConstraints();
226
                        gridBagConstraints.gridx = 0;
227
                        gridBagConstraints.gridy = 1;
228
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
229
                        gridBagConstraints.insets = new Insets(2, 5, 2, 2);
230
                        jPanelLimits.add(getJLabelMinimum(), gridBagConstraints);
231

    
232
                        gridBagConstraints = new GridBagConstraints();
233
                        gridBagConstraints.gridx = 0;
234
                        gridBagConstraints.gridy = 2;
235
                        gridBagConstraints.anchor = GridBagConstraints.EAST;
236
                        gridBagConstraints.insets = new Insets(2, 5, 5, 2);
237
                        gridBagConstraints.weightx = 0.5;
238
                        jPanelLimits.add(getJLabelMaximum(), gridBagConstraints);
239

    
240
                        gridBagConstraints = new GridBagConstraints();
241
                        gridBagConstraints.gridx = 1;
242
                        gridBagConstraints.gridy = 1;
243
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
244
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
245
                        gridBagConstraints.weightx = 0.5;
246
                        gridBagConstraints.insets = new Insets(2, 2, 2, 5);
247
                        jPanelLimits.add(getJTextFieldMinimum(), gridBagConstraints);
248

    
249
                        gridBagConstraints = new GridBagConstraints();
250
                        gridBagConstraints.gridx = 1;
251
                        gridBagConstraints.gridy = 2;
252
                        gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
253
                        gridBagConstraints.anchor = GridBagConstraints.WEST;
254
                        gridBagConstraints.weightx = 1.0;
255
                        gridBagConstraints.insets = new Insets(2, 2, 5, 5);
256
                        jPanelLimits.add(getJTextFieldMaximum(), gridBagConstraints);
257
                }
258
                return jPanelLimits;
259
        }
260

    
261
        public ColorSliderEdition getColorSliderEdition() {
262
                if (colorSliderEdition == null) {
263
                        colorSliderEdition = new ColorSliderEdition();
264
                        colorSliderEdition.addValueChangedListener(this);
265
                        colorSliderEdition.addSelectionChangedListener(this);
266
                }
267
                return colorSliderEdition;
268
        }
269

    
270
        private ColorButton getColorButton() {
271
                if (colorButton == null) {
272
                        colorButton = new ColorButton();
273
                        colorButton.addValueChangedListener(this);
274
                }
275
                return colorButton;
276
        }
277

    
278
        private JTextField getJTextClassName() {
279
                if (jTextClassName == null) {
280
                        jTextClassName = new JTextField();
281
                        jTextClassName.setEnabled(false);
282
                        jTextClassName.addKeyListener(this);
283
                }
284
                return jTextClassName;
285
        }
286
        //[end]
287

    
288
        /**
289
         * Define si el componente ColorSliderEdition tiene interpolaci?n o no.
290
         * @param value
291
         */
292
        public void setInterpolated(boolean value) {
293
                getColorSliderEdition().setInterpolated(value);
294
        }
295

    
296
        /*
297
         * (non-Javadoc)
298
         * @see org.gvsig.gui.beans.colorslideredition.ColorSliderListener#actionSelectionChanged(org.gvsig.gui.beans.colorslideredition.ColorSliderEvent)
299
         */
300
        public void actionSelectionChanged(ColorSliderEvent e) {
301
                ItemColorSlider itemColorSlider = getColorSliderEdition().getSelectedItem();
302
                boolean enabled = false;
303
                if ((itemColorSlider != null) && (itemColorSlider.getSelected() == 1)) {
304
                        getColorButton().setColor(itemColorSlider.getColor());
305
                        getJTextClassName().setText(itemColorSlider.getName());
306
                        enabled = true;
307
                }
308
                setControlsEnabled(enabled);
309
        }
310

    
311
        /**
312
         * Acci?n que se ejecuta cuando cambia el color el usuario. Este metodo
313
         * actualiza el color del ItemColorSlider segun la seleccion del usuario.
314
         */
315
        private void callValueColorChanged() {
316
                ItemColorSlider itemColorSlider = getColorSliderEdition().getSelectedItem();
317
                if (itemColorSlider != null) {
318
                        itemColorSlider.setColor(getColorButton().getColor());
319
                        itemColorSlider.setName(getJTextClassName().getText());
320
                        getColorSliderEdition().repaint();
321
                }
322
        }
323

    
324
        /*
325
         * (non-Javadoc)
326
         * @see org.gvsig.gui.beans.colorbutton.ColorButtonListener#actionValueDragged(org.gvsig.gui.beans.colorbutton.ColorButtonEvent)
327
         */
328
        public void actionValueDragged(ColorButtonEvent e) {
329
                callValueColorChanged();
330
                // refreshItems();
331
        }
332

    
333
        /**
334
         * Convierte el slider de la paleta en un array de objetos para poder crear
335
         * con ?l el objeto Palette
336
         * @return
337
         */
338
        public ArrayList getPalette() {
339
                double min2 = min;
340
                double max2 = max;
341

    
342
                if (getJCheckBoxLimits().isSelected()) {
343
                        try {
344
                                min2 = Double.parseDouble(getJTextFieldMinimum().getText());
345
                        } catch (NumberFormatException exception) {
346
                                getJTextFieldMinimum().setText(Double.toString(min));
347
                        }
348
                        try {
349
                                max2 = Double.parseDouble(getJTextFieldMaximum().getText());
350
                        } catch (NumberFormatException exception) {
351
                                getJTextFieldMaximum().setText(Double.toString(max));
352
                        }
353
                }
354
                
355
                if (min2 > max2) {
356
                        double aux = min2;
357
                        min2 = max2;
358
                        max2 = aux;
359
                }
360

    
361
                ArrayList arrayList = new ArrayList();
362
                ArrayList items = getColorSliderEdition().getItemsShowed();
363

    
364
                // A?adir el minimo
365
                ItemColorSlider item;
366
                if (items.size() > 0)
367
                        item = (ItemColorSlider) items.get(0);
368
                else
369
                        item = new ItemColorSlider(0, Color.black);
370
                ColorItem colorItem = new ColorItem();
371
                colorItem.setColor(item.getColor());
372
                colorItem.setInterpolated(50);
373
                colorItem.setNameClass("");
374
                colorItem.setValue(min2);
375
                arrayList.add(colorItem);
376

    
377
                for (int i = 0; i < items.size(); i++) {
378
                        item = (ItemColorSlider) items.get(i);
379
                        colorItem = new ColorItem();
380
                        colorItem.setColor(item.getColor());
381
                        colorItem.setInterpolated((int) item.getInterpolated());
382
                        colorItem.setNameClass(item.getName());
383

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

    
386
                        arrayList.add(colorItem);
387
                }
388

    
389
                // A?adir el maximo
390
                if (items.size() > 0)
391
                        item = (ItemColorSlider) items.get(items.size() - 1);
392
                else
393
                        item = new ItemColorSlider(255, Color.white);
394
                colorItem = new ColorItem();
395
                colorItem.setColor(item.getColor());
396
                colorItem.setInterpolated(50);
397
                colorItem.setNameClass("");
398
                colorItem.setValue(max2);
399
                arrayList.add(colorItem);
400

    
401

    
402
                return arrayList;
403
        }        
404
        
405
        /**
406
         * Refresco de los items del panel de color
407
         * @param colorItems
408
         */
409
        public void refreshItems() {
410
                ListViewItem  listViewItem = colorTablePanel.getListViewComponent().getLastSelectedItem();
411
                if (listViewItem != null)
412
                        ((ColorTableIconPainter) listViewItem.getIcon()).setColorItems(getPalette(), colorTablePanel.getCheckBoxInterpolated().isSelected(), false);
413
                colorTablePanel.tabChanged();
414
        }
415
        
416
        /*
417
         * (non-Javadoc)
418
         * @see org.gvsig.gui.beans.colorbutton.ColorButtonListener#actionValueChanged(org.gvsig.gui.beans.colorbutton.ColorButtonEvent)
419
         */
420
        public void actionValueChanged(ColorButtonEvent e) {
421
                callValueColorChanged();
422
                refreshItems();
423
        }
424

    
425
        /*
426
         * (non-Javadoc)
427
         * @see org.gvsig.gui.beans.colorslideredition.ColorSliderListener#actionValueChanged(org.gvsig.gui.beans.colorslideredition.ColorSliderEvent)
428
         */
429
        public void actionValueChanged(ColorSliderEvent e) {
430
                refreshItems();
431
        }
432

    
433
        public void initialState() {
434
                getColorSliderEdition().repaint();
435
                setControlsEnabled(false);
436
        }
437
        
438
        public void setControlsEnabled(boolean value) {
439
                getColorButton().setEnabled(value);
440
                getJLabelColor().setEnabled(value);
441
                getJTextClassName().setEnabled(value);
442
                getJLabelClassName().setEnabled(value);
443
                if (value == false)
444
                        getJTextClassName().setText("");
445
        }
446

    
447
        /**
448
         * Carga inicial de los items del panel de color
449
         * @param colorItems
450
         */
451
        public void reloadItems(ArrayList colorItems) {
452
                min = Double.POSITIVE_INFINITY;
453
                max = Double.NEGATIVE_INFINITY;
454
                for (int i = 0; i < colorItems.size(); i++) {
455
                        ColorItem c1 = (ColorItem) colorItems.get(i);
456
                        if (c1.getValue() > max)
457
                                max = c1.getValue();
458
                        if (c1.getValue() < min)
459
                                min = c1.getValue();
460
                }
461

    
462
                clearTable();
463
                for (int i = 0; i < colorItems.size(); i++) {
464
                        ColorItem c1 = (ColorItem) colorItems.get(i);
465
                        double percent = (((c1.getValue() - min) * 100) / (max - min));
466

    
467
                        ItemColorSlider aux = new ItemColorSlider(percent, c1.getColor());
468
                        aux.setInterpolated(c1.getInterpolated());
469
                        aux.setName(c1.getNameClass());
470

    
471
                        getColorSliderEdition().addItem(aux, false);
472
                }
473
                initialState();
474
                if (min > max) {
475
                        min = 0;
476
                        max = 255;
477
                }
478
                setInterpolated(colorTablePanel.getCheckBoxInterpolated().isSelected());
479
                refreshItems();
480
        }
481

    
482
        /**
483
         * Borra todas las filas de la tabla.
484
         */
485
        public void clearTable() {
486
                getColorSliderEdition().removeAllItems();
487
        }
488

    
489
        /*
490
         * (non-Javadoc)
491
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
492
         */
493
        public void actionPerformed(ActionEvent e) {
494
                if (e.getSource() == getJCheckBoxLimits()) {
495
                        getJTextFieldMaximum().setEnabled(getJCheckBoxLimits().isSelected());
496
                        getJTextFieldMinimum().setEnabled(getJCheckBoxLimits().isSelected());
497
                        getJLabelMaximum().setEnabled(getJCheckBoxLimits().isSelected());
498
                        getJLabelMinimum().setEnabled(getJCheckBoxLimits().isSelected());
499
                        callValueColorChanged();
500
                        refreshItems();
501
                }
502
        }
503

    
504
        /*
505
         * (non-Javadoc)
506
         * @see java.awt.event.FocusListener#focusLost(java.awt.event.FocusEvent)
507
         */
508
        public void focusLost(FocusEvent e) {
509
                callValueColorChanged();
510
                refreshItems();
511
  }
512

    
513
        /*
514
         * (non-Javadoc)
515
         * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
516
         */
517
        public void keyReleased(KeyEvent e) {
518
                if (e.getSource() == getJTextClassName()) {
519
                        callValueColorChanged();
520
                        refreshItems();
521
                }
522
  }
523

    
524
        public void actionValueDragged(ColorSliderEvent e) {}
525
        public void focusGained(FocusEvent e) {}
526
        public void keyPressed(KeyEvent e) {}
527
        public void keyTyped(KeyEvent e) {}
528
}