Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / enhanced / graphics / HistogramGraphicBase.java @ 19296

History | View | Annotate | Download (8.93 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.enhanced.graphics;
20

    
21
import java.awt.Color;
22
import java.awt.Dimension;
23
import java.awt.event.ComponentEvent;
24
import java.awt.event.ComponentListener;
25
import java.util.ArrayList;
26

    
27
import javax.swing.JFormattedTextField;
28
import javax.swing.JPanel;
29
import javax.swing.JSlider;
30

    
31
import org.gvsig.raster.beans.canvas.GCanvas;
32
import org.gvsig.raster.beans.canvas.layers.Border;
33
import org.gvsig.raster.beans.canvas.layers.GraphicHistogram;
34
import org.gvsig.raster.dataset.properties.DatasetColorInterpretation;
35
import org.gvsig.raster.datastruct.Histogram;
36

    
37
/**
38
 * Clase base para los gr?ficos de histogramas de entrada y salida.
39
 * 
40
 * 20/02/2008
41
 * @author Nacho Brodin nachobrodin@gmail.com
42
 */
43
public abstract class HistogramGraphicBase extends JPanel implements ComponentListener {
44
        /**
45
         * Constantes que identifican un histograma con una banda
46
         */
47
        public final static int                    RED             = 0;
48
        public final static int                    GREEN           = 1;
49
        public final static int                    BLUE            = 2;
50
        public final static int                    GRAY            = 3;
51
        
52
        /**
53
         * Constantes que identifican el tipo Standard/Cumulative
54
         */
55
        public final static int                    STANDARD        = 0;
56
        public final static int                    CUMULATIVE      = 1;
57
        
58
        protected GCanvas                          canvas          = null;
59
        protected JPanel                           north           = null;
60
        protected JPanel                           south           = null;
61
        
62
        protected Color                            borderColor     = Color.WHITE;
63
        protected Color                            histogramColor  = Color.RED;
64
        protected JSlider                          slider          = null;
65
        protected JFormattedTextField              minValue        = null;
66
        protected JFormattedTextField              maxValue        = null;
67
        
68
        protected double[]                         histogramRed    = null;
69
        protected double[]                         histogramGreen  = null;
70
        protected double[]                         histogramBlue   = null;
71
        protected double[]                         histogramGray   = null;
72
        protected double[]                         histogramDrawed = new double[]{0, 0, 3, 4, 5, 8, 7, 18, 45, 36, 21, 36, 12, 23, 23, 40, 17, 10, 5, 1, 0, 0, 0};
73
        protected Histogram                        hist            = null;
74
        protected DatasetColorInterpretation       ci              = null;
75
        protected GraphicHistogram                 gHist           = null;
76
        
77
        private double[]                           minList         = null;
78
        private double[]                           maxList         = null;
79
        
80
        public HistogramGraphicBase(Histogram hist, DatasetColorInterpretation ci, double[] minList, double[] maxList) {
81
                this.hist = hist;
82
                this.ci = ci;
83
                this.minList = minList;
84
                this.maxList = maxList;
85
        }
86
        
87
        /**
88
         * Obtiene el campo de texto con el valor m?ximo
89
         * @return DataInputField
90
         */
91
        public JFormattedTextField getMaxValue() {
92
                if(maxValue == null) {
93
                        maxValue = new JFormattedTextField(String.valueOf(maxList[0]));
94
                        maxValue.setPreferredSize(new Dimension(54, 20));
95
                }
96
                return maxValue;
97
        }
98

    
99
        /**
100
         * Obtiene el campo de texto con el valor m?nimo
101
         * @return DataInputField
102
         */
103
        public JFormattedTextField getMinValue() {
104
                if(minValue == null) {
105
                        minValue = new JFormattedTextField(String.valueOf(minList[0]));
106
                        minValue.setPreferredSize(new Dimension(54, 20));
107
                }
108
                return minValue;
109
        }
110

    
111
        /**
112
         * Obtiene el slider 
113
         * @return JSlider
114
         */
115
        public JSlider getSlider() {
116
                if(slider == null) {
117
                        slider = new JSlider();
118
                }
119
                return slider;
120
        }
121
        
122
        /**
123
         * Asigna el histograma marcado con la interpretaci?n de color indicada.
124
         * @param hist Histograma a asignar
125
         * @param colorInterp Band a la que corresponde el histograma
126
         */
127
        public void setHistogram(double[] hist, int colorInterp) {
128
                switch (colorInterp) {
129
                case RED: histogramRed = hist; break;
130
                case GREEN: histogramGreen = hist; break;
131
                case BLUE: histogramBlue = hist; break;
132
                case GRAY: histogramGray = hist; break;
133
                }
134
                if(histogramDrawed == null)
135
                        histogramDrawed = hist;
136
        }
137
        
138
        /**
139
         * Asigna el histograma dibujado 
140
         * @param colorInterp
141
         */
142
        public void setHistogramDrawed(int colorInterp) {
143
                switch (colorInterp) {
144
                case RED: histogramDrawed = histogramRed; break;
145
                case GREEN: histogramDrawed = histogramGreen; break;
146
                case BLUE: histogramDrawed = histogramBlue; break;
147
                case GRAY: histogramDrawed = histogramGray; break;
148
                }
149
                if(gHist != null)
150
                        gHist.setHistogramDrawed(histogramDrawed);
151
                if(canvas != null)
152
                        canvas.repaint();
153
        }
154
        
155
        /**
156
         * Asigna el tipo de histograma Standard/Cumulative
157
         * @param type Tipo de histograma. El valor est? definido en las constantes de esta clase
158
         */
159
        public void setHistogramType(int type) {
160
                //TODO: FUNCIONALIDAD: Sin implementar el tipo de histograma
161
                switch (type) {
162
                case STANDARD:  break;
163
                case CUMULATIVE: break;
164
                }
165
        }
166
        
167
        /**
168
         * Obtiene el borde del gr?fico si lo tiene
169
         * return borde del gr?fico o null si no lo tiene
170
         */
171
        public Border getGraphicBorder() {
172
                for (int i = 0; i < canvas.getDrawableElements().size(); i++) {
173
                        if(canvas.getDrawableElements().get(i) instanceof Border)
174
                                return (Border)canvas.getDrawableElements().get(i);
175
                }
176
                return null;
177
        }
178
        
179
        /*
180
         * (non-Javadoc)
181
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
182
         */
183
        public void componentHidden(ComponentEvent e) {
184
        }
185

    
186
        /*
187
         * (non-Javadoc)
188
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
189
         */
190
        public void componentMoved(ComponentEvent e) {
191
        }
192

    
193
        /*
194
         * (non-Javadoc)
195
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
196
         */
197
        public void componentResized(ComponentEvent e) {
198
                if(canvas != null) {
199
                        getGraphicBorder().assignVisibleSize();
200
                        canvas.execFirstDrawActions();
201
                        canvas.repaint();
202
                }
203
        }
204
        
205
        /**
206
         * Asigna el tipo
207
         * @param type
208
         */
209
        public void setType(int type) {
210
                ArrayList elements = getCanvas().getDrawableElements();
211
                for (int i = 0; i < elements.size(); i++) {
212
                        if(elements.get(i) instanceof GraphicHistogram)
213
                                ((GraphicHistogram)elements.get(i)).setType(type);
214
                }
215
        }
216

    
217
        /*
218
         * (non-Javadoc)
219
         * @see java.awt.event.ComponentListener#componentShown(java.awt.event.ComponentEvent)
220
         */
221
        public void componentShown(ComponentEvent e) {
222
        }
223
        
224
        /**
225
         * Crea la lista de objetos dibujables y la guarda en un array
226
         */
227
        protected void init() {
228
                this.addComponentListener(this);
229
                
230
                int nbands = hist.getNumBands();
231
                if(nbands == 3 || nbands == 4) {
232
                        long[][] table = hist.getTable();
233
                        int[] undef = ci.getBands(DatasetColorInterpretation.UNDEF_BAND);
234
                        int rPos = ci.getBand(DatasetColorInterpretation.RED_BAND);
235
                        int gPos = ci.getBand(DatasetColorInterpretation.GREEN_BAND);
236
                        int bPos = ci.getBand(DatasetColorInterpretation.BLUE_BAND);
237
                        if(rPos == -1) {
238
                                if(undef != null) 
239
                                        rPos = undef[0];
240
                                else
241
                                        rPos = 0;
242
                        }
243
                        if(gPos == -1) {
244
                                if(undef != null && undef.length >= 2) 
245
                                        gPos = undef[1];
246
                                else
247
                                        gPos = 1;
248
                        }
249
                        if(bPos == -1) {
250
                                if(undef != null && undef.length >= 3) 
251
                                        bPos = undef[2];
252
                                else
253
                                        gPos = 2;
254
                        }
255
                        
256
                        histogramRed = new double[table[rPos].length];
257
                        histogramGreen = new double[table[gPos].length];
258
                        histogramBlue = new double[table[bPos].length];
259
                        for (int i = 0; i < histogramRed.length; i++) { 
260
                                histogramRed[i] = (double)table[rPos][i];
261
                                histogramGreen[i] = (double)table[gPos][i];
262
                                histogramBlue[i] = (double)table[bPos][i];
263
                        }
264
                        setHistogramDrawed(BLUE);
265
                }
266
                if(nbands == 1) {
267
                        long[][] table = hist.getTable();
268
                        histogramGray = new double[table[0].length];
269
                        for (int i = 0; i < histogramGray.length; i++) 
270
                                histogramGray[i] = (double)table[0][i];
271
                        setHistogramDrawed(GRAY);
272
                }
273
                
274
        }
275
                 
276
        /**
277
         * Obtiene el panel con los combos de selecci?n de gr?fico y bandas
278
         * @return JPanel
279
         */
280
        public abstract JPanel getNorthPanel();
281
        
282
        /**
283
         * Obtiene el panel con las entradas de texto para los valores
284
         * m?ximo y m?nimo
285
         * @return JPanel
286
         */
287
        public abstract JPanel getSouthPanel();
288
        
289
        /**
290
         * Obtiene el lienzo donde se dibujan las gr?ficas
291
         * @return GCanvas
292
         */
293
        public abstract GCanvas getCanvas();
294
        
295
}