Statistics
| Revision:

root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / enhanced / ui / GraphicsPanel.java @ 19978

History | View | Annotate | Download (9.53 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.ui;
20

    
21
import java.awt.Color;
22
import java.awt.Dimension;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.Insets;
26
import java.awt.event.KeyEvent;
27
import java.awt.event.KeyListener;
28
import java.text.NumberFormat;
29

    
30
import javax.swing.JCheckBox;
31
import javax.swing.JFormattedTextField;
32
import javax.swing.JLabel;
33
import javax.swing.JPanel;
34
import javax.swing.text.DefaultFormatterFactory;
35
import javax.swing.text.NumberFormatter;
36

    
37
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
38
import org.gvsig.raster.beans.previewbase.IUserPanelInterface;
39
import org.gvsig.raster.datastruct.Histogram;
40
import org.gvsig.raster.datastruct.HistogramException;
41
import org.gvsig.raster.util.RasterToolsUtil;
42
import org.gvsig.rastertools.enhanced.graphics.InputHistogram;
43
import org.gvsig.rastertools.enhanced.graphics.OutputHistogram;
44

    
45
/**
46
 * Panel que contiene las gr?ficas para la modificaci?n del realce.
47
 * 
48
 * 19/02/2008
49
 * @author Nacho Brodin nachobrodin@gmail.com
50
 */
51
public class GraphicsPanel extends JPanel implements IUserPanelInterface, KeyListener {
52
        private static final long            serialVersionUID   = 1L;
53
        private InputHistogram               input              = null;
54
        private OutputHistogram              output             = null;
55
        private JPanel                       controls           = null;
56
        
57
        private JLabel                       llevels            = null;
58
        private JFormattedTextField          tlevels            = null;
59
        private JCheckBox                    cbRGB              = null;
60

    
61
        private Histogram                    hist               = null;
62
        private Histogram                    histRGB            = null;
63
        private int[]                        renderBands        = null;
64
        
65
        private double[]                     minList            = null;
66
        private double[]                     maxList            = null;
67

    
68
        /**
69
         * Crea una instancia del panel GraphicsPanel
70
         *
71
         */
72
        public GraphicsPanel(FLyrRasterSE lyr) {
73
                try {
74
                        hist = lyr.getDataSource().getHistogram();
75
                        histRGB = Histogram.convertHistogramToRGB(hist);
76
                        renderBands = lyr.getRender().getRenderBands();
77
                        minList = lyr.getDataSource().getStatistics().getMin();
78
                        maxList = lyr.getDataSource().getStatistics().getMax();
79
                        if (minList == null)
80
                                minList = new double[] { 0 };
81
                        if (maxList == null)
82
                                maxList = new double[] { 255 };
83
                } catch (HistogramException e) {
84
                        hist = null;
85
                } catch (InterruptedException e) {
86
                        hist = null;
87
                }
88

    
89
                init();
90

    
91
                if (lyr.isRGB())
92
                        getRGB().setSelected(true);
93
                else
94
                        getRGB().setEnabled(false);
95
        }
96
        
97
        /**
98
         * Inicializaci?n de los controles gr?ficos.
99
         */
100
        private void init() {
101
                setLayout(new GridBagLayout());
102
                setBorder(javax.swing.BorderFactory.createTitledBorder(null, RasterToolsUtil.getText(this, "histograms"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
103
                GridBagConstraints gridBagConstraints = null;
104

    
105
                JLabel label1 = new JLabel(RasterToolsUtil.getText(null, "input_hist"));
106
                gridBagConstraints = new GridBagConstraints();
107
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
108
                gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
109
                label1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
110
                label1.setPreferredSize(new Dimension(150, 14));
111
                add(label1, gridBagConstraints);
112
                
113
                JLabel label2 = new JLabel(RasterToolsUtil.getText(null, "output_hist"));
114
                gridBagConstraints = new GridBagConstraints();
115
                gridBagConstraints.gridx = 2;
116
                gridBagConstraints.gridy = 0;
117
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
118
                gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
119
                label2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
120
                label2.setPreferredSize(new Dimension(150, 14));
121
                add(label2, gridBagConstraints);
122

    
123
                gridBagConstraints = new GridBagConstraints();
124
                gridBagConstraints.gridx = 0;
125
                gridBagConstraints.gridy = 1;
126
                gridBagConstraints.fill = GridBagConstraints.BOTH;
127
                gridBagConstraints.weightx = 1.0;
128
                gridBagConstraints.weighty = 1.0;
129
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
130
                add(getInputHistogram(), gridBagConstraints);
131

    
132
                gridBagConstraints = new GridBagConstraints();
133
                gridBagConstraints.gridx = 1;
134
                gridBagConstraints.gridy = 1;
135
                gridBagConstraints.fill = GridBagConstraints.VERTICAL;
136
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
137
                add(getControls(), gridBagConstraints);
138
                
139
                gridBagConstraints = new GridBagConstraints();
140
                gridBagConstraints.gridx = 2;
141
                gridBagConstraints.gridy = 1;
142
                gridBagConstraints.fill = GridBagConstraints.BOTH;
143
                gridBagConstraints.weightx = 1.0;
144
                gridBagConstraints.weighty = 1.0;
145
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
146
                add(getOutputHistogram(), gridBagConstraints);                
147
                
148
                setLevelsEnabled(false);
149
        }
150
        
151
        /**
152
         * Obtiene el histograma de entrada
153
         * @return Histogram
154
         */
155
        public Histogram getHistogram() {
156
                return hist;
157
        }
158
        
159
        /**
160
         * Obtiene el gr?fico con el histograma de entrada
161
         * @return InputHistogram
162
         */
163
        public InputHistogram getInputHistogram() {
164
                if(input == null) {
165
                        input = new InputHistogram(hist, renderBands, minList, maxList);
166
                        input.setHistogram(hist, minList, maxList);
167
                }
168
                return input;
169
        }
170
        
171
        /**
172
         * Actualiza el histograma a visualizar por si se ha cambiado de RGB a normal.
173
         */
174
        public void updateHistogram() {
175
                if (getRGB().isSelected()) {
176
                        double[] min = new double[minList.length];
177
                        double[] max = new double[maxList.length];
178
                        for (int i = 0; i < minList.length; i++) {
179
                                min[i] = 0.0;
180
                                max[i] = 255.0;
181
                        }
182
                        input.setHistogram(histRGB, min, max);
183
                } else {
184
                        input.setHistogram(hist, minList, maxList);
185
                }
186
        }
187
        
188
        /**
189
         * Obtiene el gr?fico con el histograma de salida
190
         * @return OutputHistogram
191
         */
192
        public OutputHistogram getOutputHistogram() {
193
                if (output == null) {
194
                        output = new OutputHistogram(hist, renderBands, minList, maxList);
195
                }
196
                return output;
197
        }
198
        
199
        /**
200
         * Obtiene el panel con los controles
201
         * @return JPanel
202
         */
203
        public JPanel getControls() {
204
                if(controls == null) {
205
                        controls = new JPanel();
206
                        controls.setLayout(new GridBagLayout());
207
                        
208
                        llevels = new JLabel(RasterToolsUtil.getText(null, "levels"));
209
                        
210
                        GridBagConstraints gbc = new GridBagConstraints();
211
                        gbc.weightx = 1;
212
                        gbc.insets = new Insets(0, 5, 0, 5);
213
                        gbc.fill = GridBagConstraints.BOTH;
214
                        controls.add(llevels, gbc);
215
                        gbc.gridy = 1;
216
                        controls.add(getLevels(), gbc);
217
                        
218
                        gbc.gridy = 2;
219
                        controls.add(getRGB(), gbc);
220
                }
221
                return controls;
222
        }
223
        
224
        /**
225
         * Activa o desactiva el control de niveles de posterizaci?n en level-slice
226
         * de recorte.
227
         * @param enabled
228
         */
229
        public void setLevelsEnabled(boolean enabled) {
230
                getLevels().setEnabled(enabled);
231
                if(enabled)
232
                        getLevels().setBackground(Color.WHITE);
233
                else
234
                        getLevels().setBackground(getControls().getBackground());
235
        }
236
        
237
        /**
238
         * Obtiene el n?mero de niveles.
239
         * @return
240
         */
241
        public JFormattedTextField getLevels() {
242
                if (tlevels == null) {
243
                        NumberFormat doubleDisplayFormat = NumberFormat.getNumberInstance();
244
                        doubleDisplayFormat.setMinimumFractionDigits(0);
245
                        NumberFormat doubleEditFormat = NumberFormat.getNumberInstance();
246

    
247
                        tlevels = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleEditFormat)));
248

    
249
                        tlevels.addKeyListener(this);
250
                }
251
                return tlevels;
252
        }
253
        
254
        /**
255
         * Obtiene el checkbox que indica si se va a tratar la imagen como un checkbox
256
         * @return
257
         */
258
        public JCheckBox getRGB() {
259
                if (cbRGB == null) {
260
                        cbRGB = new JCheckBox("RGB");
261
                }
262
                return cbRGB;
263
        }
264
        
265
        /*
266
         * (non-Javadoc)
267
         * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getPanel()
268
         */
269
        public JPanel getPanel() {
270
                return this;
271
        }
272

    
273
        /*
274
         * (non-Javadoc)
275
         * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getTitle()
276
         */
277
        public String getTitle() {
278
                return "";
279
        }
280

    
281
        /**
282
         * Asigna el listener para gestionar el evento de movimiento de gr?ficos
283
         * @param listener
284
         */
285
        public void setListener(EnhancedListener listener) {
286
                getInputHistogram().setListener(listener);
287
        }
288

    
289
        /*
290
         * (non-Javadoc)
291
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
292
         */
293
        public void keyPressed(KeyEvent e) {
294
                Long lValue = (Long) getLevels().getValue();
295
                int value = lValue.intValue();
296

    
297
                boolean change = false;
298
                switch (e.getKeyCode()) {
299
                        case KeyEvent.VK_UP:
300
                                value++;
301
                                change = true;
302
                                break;
303
                        case KeyEvent.VK_DOWN:
304
                                value--;
305
                                change = true;
306
                                break;
307
                }
308

    
309
                if (change) {
310
                        if (value > 30)
311
                                value = 30;
312
        
313
                        if (value < 2)
314
                                value = 2;
315
        
316
                        if (!getLevels().getValue().equals(new Long(value))) {
317
                                getLevels().setValue(new Long(value));
318
                                getLevels().postActionEvent();
319
                        }
320
                }
321
        }
322

    
323
        public void keyReleased(KeyEvent e) {}
324
        public void keyTyped(KeyEvent e) {}
325
}