Statistics
| Revision:

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

History | View | Annotate | Download (8.74 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
                Color color = Color.white;
144
                switch (colorInterp) {
145
                        case RED:
146
                                histogramDrawed = histogramRed;
147
                                color = Color.red;
148
                                break;
149
                        case GREEN:
150
                                histogramDrawed = histogramGreen;
151
                                color = Color.green;
152
                                break;
153
                        case BLUE:
154
                                histogramDrawed = histogramBlue;
155
                                color = Color.blue;
156
                                break;
157
                        case GRAY:
158
                                histogramDrawed = histogramGray;
159
                                color = Color.gray;
160
                                break;
161
                }
162

    
163
                if (gHist == null)
164
                        return;
165

    
166
                gHist.setColor(color);
167
                gHist.setHistogramDrawed(histogramDrawed);
168
        }
169
        
170
        /**
171
         * Asigna el tipo de histograma Standard/Cumulative
172
         * @param type Tipo de histograma. El valor est? definido en las constantes de esta clase
173
         */
174
        public void setHistogramType(int type) {
175
                //TODO: FUNCIONALIDAD: Sin implementar el tipo de histograma
176
                switch (type) {
177
                case STANDARD:  break;
178
                case CUMULATIVE: break;
179
                }
180
        }
181
        
182
        /**
183
         * Obtiene el borde del gr?fico si lo tiene
184
         * return borde del gr?fico o null si no lo tiene
185
         */
186
        public Border getGraphicBorder() {
187
                for (int i = 0; i < canvas.getDrawableElements().size(); i++) {
188
                        if(canvas.getDrawableElements().get(i) instanceof Border)
189
                                return (Border)canvas.getDrawableElements().get(i);
190
                }
191
                return null;
192
        }
193
        
194
        /*
195
         * (non-Javadoc)
196
         * @see java.awt.event.ComponentListener#componentHidden(java.awt.event.ComponentEvent)
197
         */
198
        public void componentHidden(ComponentEvent e) {
199
        }
200

    
201
        /*
202
         * (non-Javadoc)
203
         * @see java.awt.event.ComponentListener#componentMoved(java.awt.event.ComponentEvent)
204
         */
205
        public void componentMoved(ComponentEvent e) {
206
        }
207

    
208
        /*
209
         * (non-Javadoc)
210
         * @see java.awt.event.ComponentListener#componentResized(java.awt.event.ComponentEvent)
211
         */
212
        public void componentResized(ComponentEvent e) {
213
                if(canvas != null) {
214
                        getGraphicBorder().assignVisibleSize();
215
                        canvas.execFirstDrawActions();
216
                        canvas.repaint();
217
                }
218
        }
219
        
220
        /**
221
         * Asigna el tipo
222
         * @param type
223
         */
224
        public void setType(int type) {
225
                ArrayList elements = getCanvas().getDrawableElements();
226
                for (int i = 0; i < elements.size(); i++) {
227
                        if(elements.get(i) instanceof GraphicHistogram)
228
                                ((GraphicHistogram)elements.get(i)).setType(type);
229
                }
230
        }
231

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