Statistics
| Revision:

gvsig-raster / org.gvsig.raster.tools / trunk / org.gvsig.raster.tools / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / enhanced / ui / GraphicsPanel.java @ 2585

History | View | Annotate | Download (10.8 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 */
22
package org.gvsig.raster.tools.app.basic.tool.enhanced.ui;
23

    
24
import java.awt.Color;
25
import java.awt.Dimension;
26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.Insets;
29
import java.awt.event.KeyEvent;
30
import java.awt.event.KeyListener;
31
import java.text.NumberFormat;
32

    
33
import javax.swing.JCheckBox;
34
import javax.swing.JFormattedTextField;
35
import javax.swing.JLabel;
36
import javax.swing.JPanel;
37
import javax.swing.text.DefaultFormatterFactory;
38
import javax.swing.text.NumberFormatter;
39

    
40
import org.gvsig.fmap.dal.coverage.RasterLocator;
41
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
42
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram;
43
import org.gvsig.fmap.dal.coverage.exception.HistogramException;
44
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
45
import org.gvsig.fmap.dal.coverage.store.props.HistogramComputer;
46
import org.gvsig.i18n.Messages;
47
import org.gvsig.raster.fmap.layers.FLyrRaster;
48
import org.gvsig.raster.swing.preview.PreviewBasePanelTab;
49
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.InputHistogram;
50
import org.gvsig.raster.tools.app.basic.tool.enhanced.graphics.OutputHistogram;
51
import org.gvsig.tools.locator.LocatorException;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54
/**
55
 * Panel que contiene las gr?ficas para la modificaci?n del realce.
56
 * 
57
 * 19/02/2008
58
 * @author Nacho Brodin nachobrodin@gmail.com
59
 */
60
public class GraphicsPanel extends JPanel implements PreviewBasePanelTab, KeyListener {
61
        private static final long   serialVersionUID = 1L;
62
        private Logger              log              = LoggerFactory.getLogger(GraphicsPanel.class);
63
        private InputHistogram      input            = null;
64
        private OutputHistogram     output           = null;
65
        private JPanel              controls         = null;
66

    
67
        private JLabel              llevels          = null;
68
        private JFormattedTextField tlevels          = null;
69
        private JCheckBox           cbRGB            = null;
70

    
71
        private HistogramComputer           hist             = null;
72
        private BufferHistogram     histRGB          = null;
73

    
74
        private double[]            minList          = null;
75
        private double[]            maxList          = null;
76
        private FLyrRaster          lyr              = null;
77
        
78

    
79
        /**
80
         * Crea una instancia del panel GraphicsPanel
81
         */
82
        public GraphicsPanel(FLyrRaster lyr) {
83
                try {
84
                        this.lyr = lyr;
85
                        hist = lyr.getDataStore().getHistogramComputer();
86
                        histRGB = RasterLocator.getManager().getRasterUtils().convertHistogramToRGB(hist.getBufferHistogram());
87
                        minList = lyr.getDataStore().getStatistics().getMin();
88
                        maxList = lyr.getDataStore().getStatistics().getMax();
89
                        if (minList == null)
90
                                minList = new double[] { 0 };
91
                        if (maxList == null)
92
                                maxList = new double[] { 255 };
93
                } catch (HistogramException e) {
94
                        hist = null;
95
                } catch (LocatorException e) {
96
                        
97
                } catch (ProcessInterruptedException e) {
98
                        e.printStackTrace();
99
                }
100

    
101
                init();
102

    
103
                if (lyr.getDataStore().getDataType()[0] == Buffer.TYPE_BYTE)
104
                        getRGB().setSelected(true);
105
                else
106
                        getRGB().setEnabled(false);
107
        }
108
        
109
        /**
110
         * Inicializaci?n de los controles gr?ficos.
111
         */
112
        private void init() {
113
                setLayout(new GridBagLayout());
114
                setBorder(javax.swing.BorderFactory.createTitledBorder(null, Messages.getText("histograms"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
115
                GridBagConstraints gridBagConstraints = null;
116

    
117
                JLabel label1 = new JLabel(Messages.getText("input_hist"));
118
                gridBagConstraints = new GridBagConstraints();
119
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
120
                gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
121
                label1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
122
                label1.setPreferredSize(new Dimension(150, 14));
123
                add(label1, gridBagConstraints);
124
                
125
                JLabel label2 = new JLabel(Messages.getText("output_hist"));
126
                gridBagConstraints = new GridBagConstraints();
127
                gridBagConstraints.gridx = 2;
128
                gridBagConstraints.gridy = 0;
129
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
130
                gridBagConstraints.anchor = java.awt.GridBagConstraints.CENTER;
131
                label2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
132
                label2.setPreferredSize(new Dimension(150, 14));
133
                add(label2, gridBagConstraints);
134

    
135
                gridBagConstraints = new GridBagConstraints();
136
                gridBagConstraints.gridx = 0;
137
                gridBagConstraints.gridy = 1;
138
                gridBagConstraints.fill = GridBagConstraints.BOTH;
139
                gridBagConstraints.weightx = 1.0;
140
                gridBagConstraints.weighty = 1.0;
141
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
142
                add(getInputHistogram(), gridBagConstraints);
143

    
144
                gridBagConstraints = new GridBagConstraints();
145
                gridBagConstraints.gridx = 1;
146
                gridBagConstraints.gridy = 1;
147
                gridBagConstraints.fill = GridBagConstraints.VERTICAL;
148
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
149
                add(getControls(), gridBagConstraints);
150
                
151
                gridBagConstraints = new GridBagConstraints();
152
                gridBagConstraints.gridx = 2;
153
                gridBagConstraints.gridy = 1;
154
                gridBagConstraints.fill = GridBagConstraints.BOTH;
155
                gridBagConstraints.weightx = 1.0;
156
                gridBagConstraints.weighty = 1.0;
157
                gridBagConstraints.insets = new Insets(4, 4, 4, 4);
158
                add(getOutputHistogram(), gridBagConstraints);                
159
                
160
                setLevelsEnabled(false);
161
        }
162
        
163
        /**
164
         * Obtiene el histograma de entrada
165
         * @return Histogram
166
         */
167
        public HistogramComputer getHistogram() {
168
                return hist;
169
        }
170
        
171
        /**
172
         * Obtiene el gr?fico con el histograma de entrada
173
         * @return InputHistogram
174
         */
175
        public InputHistogram getInputHistogram() {
176
                if (input == null) {
177
                        try {
178
                                input = new InputHistogram(hist.getBufferHistogram(), lyr, minList, maxList);
179
                                input.setHistogram(hist.getBufferHistogram(), minList, maxList);
180
                        } catch (ProcessInterruptedException e) {
181
                                log.debug("Proceso de calculo de histograma interrumpido", this, e);
182
                        } catch (HistogramException e) {
183
                                log.debug("Excepci?n en el c?lculo del histograma", this, e);
184
                        }
185
                }
186
                return input;
187
        }
188
        
189
        /**
190
         * Actualiza el histograma a visualizar por si se ha cambiado de RGB a normal.
191
         */
192
        public void updateHistogram() {
193
                if (getRGB().isSelected()) {
194
                        double[] min = new double[minList.length];
195
                        double[] max = new double[maxList.length];
196
                        for (int i = 0; i < minList.length; i++) {
197
                                min[i] = 0.0;
198
                                max[i] = 255.0;
199
                        }
200
                        input.setHistogram(histRGB, min, max);
201
                } else {
202
                        try {
203
                                input.setHistogram(hist.getBufferHistogram(), minList, maxList);
204
                        } catch (ProcessInterruptedException e) {
205
                                log.debug("Proceso de calculo de histograma interrumpido", this, e);
206
                        } catch (HistogramException e) {
207
                                log.debug("Excepci?n en el c?lculo del histograma", this, e);
208
                        }
209
                }
210
        }
211
        
212
        /**
213
         * Obtiene el gr?fico con el histograma de salida
214
         * @return OutputHistogram
215
         */
216
        public OutputHistogram getOutputHistogram() {
217
                if (output == null) {
218
                        try {
219
                                output = new OutputHistogram(hist.getBufferHistogram(), lyr, minList, maxList);
220
                        } catch (ProcessInterruptedException e) {
221
                                log.debug("Proceso de calculo de histograma interrumpido", this, e);
222
                        } catch (HistogramException e) {
223
                                log.debug("Excepci?n en el c?lculo del histograma", this, e);
224
                        }
225
                }
226
                return output;
227
        }
228
        
229
        /**
230
         * Obtiene el panel con los controles
231
         * @return JPanel
232
         */
233
        public JPanel getControls() {
234
                if(controls == null) {
235
                        controls = new JPanel();
236
                        controls.setLayout(new GridBagLayout());
237
                        
238
                        llevels = new JLabel(Messages.getText("levels"));
239
                        
240
                        GridBagConstraints gbc = new GridBagConstraints();
241
                        gbc.weightx = 1;
242
                        gbc.insets = new Insets(0, 5, 0, 5);
243
                        gbc.fill = GridBagConstraints.BOTH;
244
                        controls.add(llevels, gbc);
245
                        gbc.gridy = 1;
246
                        controls.add(getLevels(), gbc);
247
                        
248
                        gbc.gridy = 2;
249
                        controls.add(getRGB(), gbc);
250
                }
251
                return controls;
252
        }
253
        
254
        /**
255
         * Activa o desactiva el control de niveles de posterizaci?n en level-slice
256
         * de recorte.
257
         * @param enabled
258
         */
259
        public void setLevelsEnabled(boolean enabled) {
260
                getLevels().setEnabled(enabled);
261
                if(enabled)
262
                        getLevels().setBackground(Color.WHITE);
263
                else
264
                        getLevels().setBackground(getControls().getBackground());
265
        }
266
        
267
        /**
268
         * Obtiene el n?mero de niveles.
269
         * @return
270
         */
271
        public JFormattedTextField getLevels() {
272
                if (tlevels == null) {
273
                        NumberFormat doubleDisplayFormat = NumberFormat.getNumberInstance();
274
                        doubleDisplayFormat.setMinimumFractionDigits(0);
275
                        NumberFormat doubleEditFormat = NumberFormat.getNumberInstance();
276

    
277
                        tlevels = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleEditFormat)));
278

    
279
                        tlevels.addKeyListener(this);
280
                }
281
                return tlevels;
282
        }
283
        
284
        /**
285
         * Obtiene el checkbox que indica si se va a tratar la imagen como un checkbox
286
         * @return
287
         */
288
        public JCheckBox getRGB() {
289
                if (cbRGB == null) {
290
                        cbRGB = new JCheckBox("RGB");
291
                }
292
                return cbRGB;
293
        }
294
        
295
        /*
296
         * (non-Javadoc)
297
         * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getPanel()
298
         */
299
        public JPanel getPanel() {
300
                return this;
301
        }
302

    
303
        /*
304
         * (non-Javadoc)
305
         * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getTitle()
306
         */
307
        public String getTitle() {
308
                return "";
309
        }
310

    
311
        /**
312
         * Asigna el listener para gestionar el evento de movimiento de gr?ficos
313
         * @param listener
314
         */
315
        public void setListener(EnhancedListener listener) {
316
                getInputHistogram().setListener(listener);
317
        }
318

    
319
        /*
320
         * (non-Javadoc)
321
         * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
322
         */
323
        public void keyPressed(KeyEvent e) {
324
                Long lValue = (Long) getLevels().getValue();
325
                int value = lValue.intValue();
326

    
327
                boolean change = false;
328
                switch (e.getKeyCode()) {
329
                        case KeyEvent.VK_UP:
330
                                value++;
331
                                change = true;
332
                                break;
333
                        case KeyEvent.VK_DOWN:
334
                                value--;
335
                                change = true;
336
                                break;
337
                }
338

    
339
                if (change) {
340
                        if (value > 30)
341
                                value = 30;
342
        
343
                        if (value < 2)
344
                                value = 2;
345
        
346
                        if (!getLevels().getValue().equals(new Long(value))) {
347
                                getLevels().setValue(new Long(value));
348
                                getLevels().postActionEvent();
349
                        }
350
                }
351
        }
352

    
353
        public void keyReleased(KeyEvent e) {}
354
        public void keyTyped(KeyEvent e) {}
355
}