root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / enhanced / ui / GraphicsPanel.java @ 20947
History | View | Annotate | Download (9.47 KB)
1 | 19135 | nbrodin | /* 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 | 19288 | nbrodin | import java.awt.Color; |
22 | 19489 | bsanchez | import java.awt.Dimension; |
23 | 19173 | nbrodin | import java.awt.GridBagConstraints; |
24 | import java.awt.GridBagLayout; |
||
25 | import java.awt.Insets; |
||
26 | 19595 | bsanchez | import java.awt.event.KeyEvent; |
27 | import java.awt.event.KeyListener; |
||
28 | 19173 | nbrodin | import java.text.NumberFormat; |
29 | |||
30 | 19628 | bsanchez | import javax.swing.JCheckBox; |
31 | 19173 | nbrodin | import javax.swing.JFormattedTextField; |
32 | import javax.swing.JLabel; |
||
33 | 19135 | nbrodin | import javax.swing.JPanel; |
34 | 19173 | nbrodin | import javax.swing.text.DefaultFormatterFactory; |
35 | import javax.swing.text.NumberFormatter; |
||
36 | 19135 | nbrodin | |
37 | 19277 | nbrodin | import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
38 | 19135 | nbrodin | import org.gvsig.raster.beans.previewbase.IUserPanelInterface; |
39 | 20078 | nbrodin | import org.gvsig.raster.dataset.IBuffer; |
40 | 19277 | nbrodin | import org.gvsig.raster.datastruct.Histogram; |
41 | import org.gvsig.raster.datastruct.HistogramException; |
||
42 | 19173 | nbrodin | import org.gvsig.raster.util.RasterToolsUtil; |
43 | import org.gvsig.rastertools.enhanced.graphics.InputHistogram; |
||
44 | import org.gvsig.rastertools.enhanced.graphics.OutputHistogram; |
||
45 | 19135 | nbrodin | |
46 | /**
|
||
47 | 19205 | nbrodin | * Panel que contiene las gr?ficas para la modificaci?n del realce.
|
48 | *
|
||
49 | 19135 | nbrodin | * 19/02/2008
|
50 | * @author Nacho Brodin nachobrodin@gmail.com
|
||
51 | */
|
||
52 | 19595 | bsanchez | public class GraphicsPanel extends JPanel implements IUserPanelInterface, KeyListener { |
53 | 20057 | bsanchez | private static final long serialVersionUID = 1L; |
54 | private InputHistogram input = null; |
||
55 | private OutputHistogram output = null; |
||
56 | private JPanel controls = null; |
||
57 | 19135 | nbrodin | |
58 | 20057 | bsanchez | private JLabel llevels = null; |
59 | private JFormattedTextField tlevels = null; |
||
60 | private JCheckBox cbRGB = null; |
||
61 | 19277 | nbrodin | |
62 | 20057 | bsanchez | private Histogram hist = null; |
63 | private Histogram histRGB = null; |
||
64 | private int[] renderBands = null; |
||
65 | |||
66 | private double[] minList = null; |
||
67 | private double[] maxList = null; |
||
68 | |||
69 | 19173 | nbrodin | /**
|
70 | * Crea una instancia del panel GraphicsPanel
|
||
71 | */
|
||
72 | 19277 | nbrodin | public GraphicsPanel(FLyrRasterSE lyr) {
|
73 | try {
|
||
74 | hist = lyr.getDataSource().getHistogram(); |
||
75 | 19898 | bsanchez | histRGB = Histogram.convertHistogramToRGB(hist); |
76 | 19362 | nbrodin | renderBands = lyr.getRender().getRenderBands(); |
77 | 19288 | nbrodin | minList = lyr.getDataSource().getStatistics().getMin(); |
78 | maxList = lyr.getDataSource().getStatistics().getMax(); |
||
79 | 19898 | bsanchez | if (minList == null) |
80 | minList = new double[] { 0 }; |
||
81 | if (maxList == null) |
||
82 | maxList = new double[] { 255 }; |
||
83 | 19277 | nbrodin | } catch (HistogramException e) {
|
84 | hist = null;
|
||
85 | } catch (InterruptedException e) { |
||
86 | hist = null;
|
||
87 | } |
||
88 | 19978 | bsanchez | |
89 | 19173 | nbrodin | init(); |
90 | 19978 | bsanchez | |
91 | 20078 | nbrodin | if (lyr.getDataType()[0] == IBuffer.TYPE_BYTE) |
92 | 19978 | bsanchez | getRGB().setSelected(true);
|
93 | else
|
||
94 | getRGB().setEnabled(false);
|
||
95 | 19173 | nbrodin | } |
96 | |||
97 | /**
|
||
98 | * Inicializaci?n de los controles gr?ficos.
|
||
99 | */
|
||
100 | private void init() { |
||
101 | setLayout(new GridBagLayout()); |
||
102 | 19205 | nbrodin | 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 | 19457 | bsanchez | GridBagConstraints gridBagConstraints = null; |
104 | |||
105 | 19489 | bsanchez | JLabel label1 = new JLabel(RasterToolsUtil.getText(null, "input_hist")); |
106 | 19457 | bsanchez | gridBagConstraints = new GridBagConstraints(); |
107 | 19489 | bsanchez | 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 | 19457 | bsanchez | 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 | 19489 | bsanchez | |
132 | 19457 | bsanchez | gridBagConstraints = new GridBagConstraints(); |
133 | gridBagConstraints.gridx = 1;
|
||
134 | gridBagConstraints.gridy = 1;
|
||
135 | 19489 | bsanchez | gridBagConstraints.fill = GridBagConstraints.VERTICAL;
|
136 | 19457 | bsanchez | 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 | 19489 | bsanchez | add(getOutputHistogram(), gridBagConstraints); |
147 | 19457 | bsanchez | |
148 | 19280 | nbrodin | setLevelsEnabled(false);
|
149 | 19173 | nbrodin | } |
150 | |||
151 | /**
|
||
152 | 19455 | nbrodin | * Obtiene el histograma de entrada
|
153 | * @return Histogram
|
||
154 | */
|
||
155 | public Histogram getHistogram() {
|
||
156 | return hist;
|
||
157 | } |
||
158 | |||
159 | /**
|
||
160 | 19173 | nbrodin | * Obtiene el gr?fico con el histograma de entrada
|
161 | * @return InputHistogram
|
||
162 | */
|
||
163 | public InputHistogram getInputHistogram() {
|
||
164 | if(input == null) { |
||
165 | 19362 | nbrodin | input = new InputHistogram(hist, renderBands, minList, maxList);
|
166 | 19898 | bsanchez | input.setHistogram(hist, minList, maxList); |
167 | 19173 | nbrodin | } |
168 | return input;
|
||
169 | } |
||
170 | |||
171 | /**
|
||
172 | 19898 | bsanchez | * 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 | 19173 | nbrodin | * Obtiene el gr?fico con el histograma de salida
|
190 | * @return OutputHistogram
|
||
191 | */
|
||
192 | public OutputHistogram getOutputHistogram() {
|
||
193 | 19457 | bsanchez | if (output == null) { |
194 | 19362 | nbrodin | output = new OutputHistogram(hist, renderBands, minList, maxList);
|
195 | 19173 | nbrodin | } |
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 | 19595 | bsanchez | controls.add(llevels, gbc); |
215 | 19173 | nbrodin | gbc.gridy = 1;
|
216 | 19628 | bsanchez | controls.add(getLevels(), gbc); |
217 | |||
218 | gbc.gridy = 2;
|
||
219 | controls.add(getRGB(), gbc); |
||
220 | 19173 | nbrodin | } |
221 | return controls;
|
||
222 | } |
||
223 | |||
224 | 19280 | nbrodin | /**
|
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 | 19628 | bsanchez | getLevels().setEnabled(enabled); |
231 | 19288 | nbrodin | if(enabled)
|
232 | 19628 | bsanchez | getLevels().setBackground(Color.WHITE);
|
233 | 19288 | nbrodin | else
|
234 | 19628 | bsanchez | getLevels().setBackground(getControls().getBackground()); |
235 | 19280 | nbrodin | } |
236 | |||
237 | 19560 | nbrodin | /**
|
238 | * Obtiene el n?mero de niveles.
|
||
239 | * @return
|
||
240 | */
|
||
241 | public JFormattedTextField getLevels() { |
||
242 | 19628 | bsanchez | 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 | 19560 | nbrodin | return tlevels;
|
252 | } |
||
253 | |||
254 | 19628 | bsanchez | /**
|
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 | 19173 | nbrodin | /*
|
266 | * (non-Javadoc)
|
||
267 | * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getPanel()
|
||
268 | */
|
||
269 | 19135 | nbrodin | public JPanel getPanel() { |
270 | return this; |
||
271 | } |
||
272 | |||
273 | 19173 | nbrodin | /*
|
274 | * (non-Javadoc)
|
||
275 | * @see org.gvsig.raster.beans.previewbase.IUserPanelInterface#getTitle()
|
||
276 | */
|
||
277 | 19135 | nbrodin | public String getTitle() { |
278 | return ""; |
||
279 | } |
||
280 | |||
281 | 19296 | nbrodin | /**
|
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 | 19595 | bsanchez | |
289 | /*
|
||
290 | * (non-Javadoc)
|
||
291 | * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
|
||
292 | */
|
||
293 | public void keyPressed(KeyEvent e) { |
||
294 | 19628 | bsanchez | Long lValue = (Long) getLevels().getValue(); |
295 | 19595 | bsanchez | 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 | 19628 | bsanchez | if (!getLevels().getValue().equals(new Long(value))) { |
317 | getLevels().setValue(new Long(value)); |
||
318 | getLevels().postActionEvent(); |
||
319 | 19595 | bsanchez | } |
320 | } |
||
321 | } |
||
322 | |||
323 | public void keyReleased(KeyEvent e) {} |
||
324 | public void keyTyped(KeyEvent e) {} |
||
325 | 19457 | bsanchez | } |