Statistics
| Revision:

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

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.app.extension.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.Histogram;
46
import org.gvsig.raster.app.extension.raster.bean.previewbase.IUserPanelInterface;
47
import org.gvsig.raster.app.extension.tool.enhanced.graphics.InputHistogram;
48
import org.gvsig.raster.app.extension.tool.enhanced.graphics.OutputHistogram;
49
import org.gvsig.raster.fmap.layers.FLyrRaster;
50
import org.gvsig.raster.util.RasterToolsUtil;
51
import org.gvsig.tools.locator.LocatorException;
52
/**
53
 * Panel que contiene las gr?ficas para la modificaci?n del realce.
54
 * 
55
 * 19/02/2008
56
 * @author Nacho Brodin nachobrodin@gmail.com
57
 */
58
public class GraphicsPanel extends JPanel implements IUserPanelInterface, KeyListener {
59
        private static final long   serialVersionUID = 1L;
60
        private InputHistogram      input            = null;
61
        private OutputHistogram     output           = null;
62
        private JPanel              controls         = null;
63

    
64
        private JLabel              llevels          = null;
65
        private JFormattedTextField tlevels          = null;
66
        private JCheckBox           cbRGB            = null;
67

    
68
        private Histogram           hist             = null;
69
        private BufferHistogram     histRGB          = null;
70

    
71
        private double[]            minList          = null;
72
        private double[]            maxList          = null;
73
        private FLyrRaster          lyr              = null;
74
        
75

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

    
99
                init();
100

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

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

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

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

    
275
                        tlevels = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleDisplayFormat), new NumberFormatter(doubleEditFormat)));
276

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

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

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

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

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

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

    
351
        public void keyReleased(KeyEvent e) {}
352
        public void keyTyped(KeyEvent e) {}
353
}