gvsig-raster / org.gvsig.raster.tools / branches / org.gvsig.raster.tools_dataaccess_refactoring / org.gvsig.raster.tools.app.basic / src / main / java / org / gvsig / raster / tools / app / basic / tool / properties / control / EnhancedControl.java @ 2337
History | View | Annotate | Download (16.1 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.properties.control; |
23 |
|
24 |
import java.awt.event.ActionEvent; |
25 |
import java.awt.event.ActionListener; |
26 |
import java.util.ArrayList; |
27 |
import java.util.List; |
28 |
|
29 |
import javax.swing.JCheckBox; |
30 |
|
31 |
import org.gvsig.andami.PluginServices; |
32 |
import org.gvsig.fmap.dal.coverage.datastruct.Params; |
33 |
import org.gvsig.fmap.dal.coverage.exception.FilterAddException; |
34 |
import org.gvsig.fmap.dal.coverage.exception.FilterManagerException; |
35 |
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException; |
36 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilter; |
37 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList; |
38 |
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager; |
39 |
import org.gvsig.fmap.dal.coverage.grid.filter.LinearStretchParams; |
40 |
import org.gvsig.fmap.dal.coverage.store.props.Statistics; |
41 |
import org.gvsig.gui.beans.panelGroup.AbstractPanelGroup; |
42 |
import org.gvsig.gui.beans.slidertext.listeners.SliderEvent; |
43 |
import org.gvsig.gui.beans.slidertext.listeners.SliderListener; |
44 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
45 |
import org.gvsig.raster.mainplugin.properties.RasterPropertiesTocMenuEntry; |
46 |
import org.gvsig.raster.swing.RasterSwingLibrary; |
47 |
import org.gvsig.raster.tools.app.basic.RasterExtension; |
48 |
import org.gvsig.raster.tools.app.basic.raster.process.IProcessActions; |
49 |
import org.gvsig.raster.tools.app.basic.raster.process.StatisticsProcess; |
50 |
import org.gvsig.raster.tools.app.basic.tool.properties.panel.EnhancedBrightnessContrastPanel; |
51 |
import org.gvsig.raster.tools.app.basic.tool.properties.panel.EnhancedPanel; |
52 |
import org.gvsig.raster.tools.app.basic.tool.properties.panel.EnhancedWithTrimPanel; |
53 |
|
54 |
|
55 |
/**
|
56 |
* Clase que hace de interfaz entre los objetos que contienen la informaci?n de
|
57 |
* realce y el panel.
|
58 |
*
|
59 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
60 |
*/
|
61 |
public class EnhancedControl implements IProcessActions { |
62 |
private EnhancedPanel tPanel = null; |
63 |
private RasterFilterList filterList = null; |
64 |
private EnhancedWithTrimPanel ePanel = null; |
65 |
private EnhancedBrightnessContrastPanel bcPanel = null; |
66 |
private FLyrRaster lyr = null; |
67 |
private AbstractPanelGroup panelGroup = null; |
68 |
private Statistics stats = null; |
69 |
private int[] renderBands = new int[] { 0, 1, 2 }; |
70 |
|
71 |
|
72 |
/**
|
73 |
* Manejador de eventos de los slider de brillo y contraste.
|
74 |
* @author Nacho Brodin (nachobrodin@gmail.com)
|
75 |
*/
|
76 |
class BrightnessContrastListener implements ActionListener, SliderListener { |
77 |
JCheckBox active = null; |
78 |
/**
|
79 |
* Constructor. Registra los listener
|
80 |
* @param panel
|
81 |
*/
|
82 |
public BrightnessContrastListener(EnhancedBrightnessContrastPanel panel) {
|
83 |
panel.addBrightnessValueChangedListener(this);
|
84 |
panel.addContrastValueChangedListener(this);
|
85 |
active = panel.getActive(); |
86 |
active.addActionListener(this);
|
87 |
} |
88 |
|
89 |
public void actionPerformed(ActionEvent e) { |
90 |
if (!RasterExtension.autoRefreshView)
|
91 |
return;
|
92 |
|
93 |
if (e.getSource() == active)
|
94 |
onlyApply(); |
95 |
} |
96 |
|
97 |
public void actionValueChanged(SliderEvent e) { |
98 |
if (!RasterExtension.autoRefreshView)
|
99 |
return;
|
100 |
|
101 |
onlyApply(); |
102 |
} |
103 |
|
104 |
public void actionValueDragged(SliderEvent e) { |
105 |
} |
106 |
} |
107 |
|
108 |
/**
|
109 |
* Manejador de eventos del panel EnhancedWithTrim.
|
110 |
*
|
111 |
* @author BorSanZa - Borja Sanchez Zamorano
|
112 |
*/
|
113 |
class EnhancedWithTrimListener implements ActionListener, SliderListener { |
114 |
JCheckBox active = null; |
115 |
/**
|
116 |
* Constructor. Registra los listener
|
117 |
* @param panel
|
118 |
*/
|
119 |
public EnhancedWithTrimListener(EnhancedWithTrimPanel panel) {
|
120 |
active = panel.getActive(); |
121 |
active.addActionListener(this);
|
122 |
panel.getRemoveCheck().addActionListener(this);
|
123 |
panel.getTrimCheck().addActionListener(this);
|
124 |
panel.getTrimSlider().addValueChangedListener(this);
|
125 |
} |
126 |
|
127 |
public void actionPerformed(ActionEvent e) { |
128 |
if (!RasterExtension.autoRefreshView)
|
129 |
return;
|
130 |
|
131 |
onlyApply(); |
132 |
} |
133 |
|
134 |
public void actionValueChanged(SliderEvent e) { |
135 |
if (!RasterExtension.autoRefreshView)
|
136 |
return;
|
137 |
|
138 |
onlyApply(); |
139 |
} |
140 |
|
141 |
public void actionValueDragged(SliderEvent e) { |
142 |
} |
143 |
} |
144 |
|
145 |
/**
|
146 |
* Constructor
|
147 |
* @param tp
|
148 |
*/
|
149 |
public EnhancedControl(AbstractPanelGroup panelGroup, EnhancedPanel tp, FLyrRaster lyr, RasterFilterList rfl) {
|
150 |
this.panelGroup = panelGroup;
|
151 |
this.tPanel = tp;
|
152 |
this.filterList = rfl;
|
153 |
this.lyr = lyr;
|
154 |
this.bcPanel = tPanel.getBrightnessContrastPanel();
|
155 |
this.ePanel = tPanel.getEnhancedWithTrimPanel();
|
156 |
new BrightnessContrastListener(bcPanel);
|
157 |
new EnhancedWithTrimListener(ePanel);
|
158 |
|
159 |
try {
|
160 |
saveStatus(); |
161 |
} catch (FilterManagerException e) {
|
162 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_saving_filters"), this, e); |
163 |
} catch (FilterTypeException e) {
|
164 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_saving_filters"), this, e); |
165 |
} |
166 |
|
167 |
setValuesFromFilterToPanel(); |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Carga los valores del panel desde el filtro
|
172 |
*/
|
173 |
private void setValuesFromFilterToPanel() { |
174 |
// BRILLO
|
175 |
RasterFilter bFilter = filterList.getByName("brightness");
|
176 |
if (bFilter != null){ |
177 |
int incr = ((Integer)bFilter.getParam("incrBrillo")).intValue(); |
178 |
bcPanel.setBrightnessValue((double) incr);
|
179 |
}else
|
180 |
bcPanel.setBrightnessValue(0);
|
181 |
|
182 |
// CONTRASTE
|
183 |
RasterFilter cFilter = filterList.getByName("contrast");
|
184 |
if (cFilter != null){ |
185 |
int incr = ((Integer)cFilter.getParam("incrContraste")).intValue(); |
186 |
bcPanel.setContrastValue((double) incr);
|
187 |
} |
188 |
else
|
189 |
bcPanel.setContrastValue(0);
|
190 |
|
191 |
if (bFilter != null || cFilter != null) |
192 |
bcPanel.setControlEnabled(true);
|
193 |
else
|
194 |
bcPanel.setControlEnabled(false);
|
195 |
|
196 |
// REALCE LINEAL
|
197 |
RasterFilter eFilter = filterList.getByName("enhanced_stretch");
|
198 |
if (eFilter != null) { |
199 |
ePanel.setControlEnabled(true);
|
200 |
|
201 |
// Comprueba si esta activo eliminar extremos
|
202 |
boolean removeEnds = false; |
203 |
if (eFilter.getParam("remove") != null) |
204 |
removeEnds = ((Boolean) eFilter.getParam("remove")).booleanValue(); |
205 |
ePanel.setRemoveEndsActive(removeEnds); |
206 |
|
207 |
// Comprueba si hay recorte de colas
|
208 |
LinearStretchParams stretchs = (LinearStretchParams) eFilter.getParam("stretchs");
|
209 |
double[] tailTrimList; |
210 |
if (stretchs != null) |
211 |
tailTrimList = stretchs.getTailTrimList(); |
212 |
else
|
213 |
tailTrimList = new double[0]; |
214 |
double median = 0; |
215 |
double nValues = tailTrimList.length;
|
216 |
for (int i = 0; i < tailTrimList.length; i++) |
217 |
median += tailTrimList[i]; |
218 |
double tailTrim = new Double(nValues > 0 ? median / nValues : median).doubleValue(); |
219 |
|
220 |
if (tailTrim != 0) { |
221 |
ePanel.setTailTrimCheckActive(true);
|
222 |
ePanel.setTailTrimValue(tailTrim * 100);
|
223 |
} else {
|
224 |
ePanel.setTailTrimCheckActive(false);
|
225 |
ePanel.setTailTrimValue(0);
|
226 |
} |
227 |
} else {
|
228 |
ePanel.setControlEnabled(false);
|
229 |
ePanel.setRemoveEndsActive(false);
|
230 |
ePanel.setTailTrimCheckActive(false);
|
231 |
ePanel.setTailTrimValue(0);
|
232 |
} |
233 |
} |
234 |
|
235 |
/**
|
236 |
* Carga los valores del filtro desde el panel
|
237 |
* @throws FilterTypeException
|
238 |
* @throws FilterAddException
|
239 |
*/
|
240 |
private void setValuesFromPanelToFilter() throws FilterTypeException, FilterAddException { |
241 |
|
242 |
// REALCE
|
243 |
if (ePanel.getActive().isSelected()) {
|
244 |
if(lyr != null) { |
245 |
stats = lyr.getDataStore().getStatistics(); |
246 |
renderBands = lyr.getRender().getRenderColorInterpretation().buildRenderBands(); |
247 |
// En este caso siempre es necesario el m?ximo y m?nimo
|
248 |
try {
|
249 |
if(!stats.isCalculated() && lyr instanceof FLyrRaster) |
250 |
StatisticsProcess.launcher(lyr, this);
|
251 |
else {
|
252 |
RasterFilterListManager enhancementManager = filterList.getManagerByID("EnhancementStretch");
|
253 |
Params params = filterList.createEmptyFilterParams(); |
254 |
params.setParam("stats", stats);
|
255 |
params.setParam("remove", ePanel.isRemoveEndsSelected());
|
256 |
params.setParam("renderBands", renderBands);
|
257 |
params.setParam("rgb", lyr.isRGB());
|
258 |
|
259 |
if (ePanel.isTailTrimCheckSelected()) {
|
260 |
params.setParam("tailtrim", (double) (ePanel.getTrimValue() / 100D)); |
261 |
} else {
|
262 |
params.setParam("tailtrim", 0.0); |
263 |
} |
264 |
|
265 |
enhancementManager.addFilter(params); |
266 |
} |
267 |
} catch (FilterManagerException e) {
|
268 |
throw new FilterAddException("No se han podido calcular estadisticas. Error al a?adir realce;" + e.getMessage()); |
269 |
} |
270 |
} |
271 |
} else {
|
272 |
filterList.remove("enhanced_stretch");
|
273 |
filterList.remove("tailTrim");
|
274 |
} |
275 |
|
276 |
// BRILLO Y CONTRASTE
|
277 |
try {
|
278 |
RasterFilterListManager bcManager = filterList.getManagerByID("BrightnessContrast");
|
279 |
|
280 |
if (bcPanel.getActive().isSelected() && ((int) bcPanel.getBrightnessValue() != 0)) { |
281 |
Params params = filterList.createEmptyFilterParams(); |
282 |
params.setParam("Brightness", new Integer((int) bcPanel.getBrightnessValue())); |
283 |
filterList.add(bcManager.createFilter(params)); |
284 |
} else
|
285 |
filterList.remove("brightness");
|
286 |
|
287 |
if (bcPanel.getActive().isSelected() && ((int) bcPanel.getContrastValue() != 0)) { |
288 |
Params params = filterList.createEmptyFilterParams(); |
289 |
params.setParam("Contrast", new Integer((int) bcPanel.getContrastValue())); |
290 |
filterList.add(bcManager.createFilter(params)); |
291 |
} else
|
292 |
filterList.remove("contrast");
|
293 |
} catch (FilterManagerException e) {
|
294 |
throw new FilterAddException("No se han podido calcular estadisticas. Error al a?adir realce;" + e.getMessage()); |
295 |
} |
296 |
|
297 |
endActionsForFilterSettings(); |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Acciones realizadas al final la aplicaci?n de filtros
|
302 |
* @throws FilterTypeException
|
303 |
*/
|
304 |
@SuppressWarnings("unchecked") |
305 |
private void endActionsForFilterSettings() throws FilterTypeException { |
306 |
ArrayList listOrder = (ArrayList) panelGroup.getProperties().get("filterOrder"); |
307 |
List<RasterFilter> listCopy = filterList.getStatusCloned();
|
308 |
int cont = 0; |
309 |
for (int i = 0; i < listOrder.size(); i++) { |
310 |
int pos = hasFilter(listCopy, ((RasterFilter) listOrder.get(i)).getName());
|
311 |
if (pos != -1) { |
312 |
// Esta pero en posicion equivocada
|
313 |
if (pos != cont) {
|
314 |
RasterFilter copy = listCopy.remove(pos); |
315 |
listCopy.add(cont, copy); |
316 |
} |
317 |
cont++; |
318 |
} |
319 |
} |
320 |
filterList.setStatus(listCopy); |
321 |
filterList.controlTypes(); |
322 |
|
323 |
// Redibujamos
|
324 |
if (lyr != null) |
325 |
lyr.getMapContext().invalidate(); |
326 |
} |
327 |
|
328 |
/**
|
329 |
* Acciones a ejecutar cuando se acepta
|
330 |
*/
|
331 |
public void accept() { |
332 |
try {
|
333 |
setValuesFromPanelToFilter(); |
334 |
} catch (FilterTypeException e) {
|
335 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e); |
336 |
} catch (FilterAddException e) {
|
337 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e); |
338 |
} |
339 |
} |
340 |
|
341 |
/**
|
342 |
* Acciones a ejecutar cuando se aplica
|
343 |
*/
|
344 |
public void apply() { |
345 |
onlyApply(); |
346 |
try {
|
347 |
saveStatus(); |
348 |
} catch (FilterManagerException e) {
|
349 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_saving_filters"), this, e); |
350 |
} catch (FilterTypeException e) {
|
351 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_saving_filters"), this, e); |
352 |
} |
353 |
} |
354 |
|
355 |
/**
|
356 |
* Acciones a ejecutar cuando se aplica
|
357 |
*/
|
358 |
public void onlyApply() { |
359 |
if (RasterPropertiesTocMenuEntry.enableEvents)
|
360 |
try {
|
361 |
setValuesFromPanelToFilter(); |
362 |
} catch (FilterTypeException e) {
|
363 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e); |
364 |
} catch (FilterAddException e) {
|
365 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e); |
366 |
} |
367 |
} |
368 |
|
369 |
/**
|
370 |
* Acciones a ejecutar cuando se cancela
|
371 |
*/
|
372 |
public void cancel() { |
373 |
restoreStatus(); |
374 |
} |
375 |
|
376 |
/**
|
377 |
* Consulta si el filtro especificado en el par?metro name est? dentro
|
378 |
* de la lista filter.
|
379 |
* @param filter Lista donde se consulta
|
380 |
* @param name Nombre a comprobar si est? en la lista
|
381 |
* @return true si est? en la lista y false si no est?.
|
382 |
*/
|
383 |
private int hasFilter(List<RasterFilter> filter, String name) { |
384 |
for (int i = 0; i < filter.size(); i++) { |
385 |
if (((RasterFilter) filter.get(i)).getName().equals(name))
|
386 |
return i;
|
387 |
} |
388 |
return -1; |
389 |
} |
390 |
|
391 |
@SuppressWarnings("unchecked") |
392 |
public void saveStatus() throws FilterManagerException, FilterTypeException { |
393 |
panelGroup.getProperties().put("filterStatus", filterList.getStatusCloned());
|
394 |
|
395 |
List<RasterFilter> filterOrder = filterList.getStatusCloned();
|
396 |
int posEnhanced = hasFilter(filterOrder, "enhanced_stretch"); |
397 |
int posTailTrim = hasFilter(filterOrder, "tailTrim"); |
398 |
|
399 |
RasterFilterListManager statFilterManager = filterList.getManagerByID("Statistics");
|
400 |
Params params = filterList.createEmptyFilterParams(); |
401 |
params.setParam("stats", null); |
402 |
params.setParam("tail", new Double(0.0)); |
403 |
params.setParam("samples", new Double(0.0)); |
404 |
params.setParam("remove", new Boolean(false)); |
405 |
RasterFilter filter = statFilterManager.createFilter(params); |
406 |
|
407 |
// Si tiene realce comprobamos el tailtrim
|
408 |
if (posEnhanced != -1) { |
409 |
// Si no tiene el tailTrim, insertamos uno antes del realce
|
410 |
if (posTailTrim == -1) { |
411 |
filterOrder.add(posEnhanced, filter); |
412 |
} |
413 |
} else {
|
414 |
// Si existe un tailTrim lo borramos pq no tiene realce
|
415 |
if (posTailTrim != -1) |
416 |
filterOrder.remove(posTailTrim); |
417 |
// Insertamos primero el tailtrim y luego el realce para conservar un orden logico
|
418 |
filterOrder.add(0, filter);
|
419 |
RasterFilterListManager eManager = filterList.getManagerByID("EnhancementStretch");
|
420 |
params = filterList.createEmptyFilterParams(); |
421 |
params.setParam("remove", new Boolean(false)); |
422 |
eManager.createFilter(params); |
423 |
} |
424 |
|
425 |
RasterFilterListManager bcManager = filterList.getManagerByID("BrightnessContrast");
|
426 |
|
427 |
// Si no tiene brillo, lo insertamos
|
428 |
if (hasFilter(filterOrder, "brightness") == -1) { |
429 |
params = filterList.createEmptyFilterParams(); |
430 |
params.setParam("Brightness", new Integer(0)); |
431 |
filterOrder.add(bcManager.createFilter(params)); |
432 |
} |
433 |
|
434 |
// Si no tiene el contraste, lo insertamos
|
435 |
if (hasFilter(filterOrder, "contrast") == -1) { |
436 |
params = filterList.createEmptyFilterParams(); |
437 |
params.setParam("Contrast", new Integer(0)); |
438 |
filterOrder.add(bcManager.createFilter(params)); |
439 |
} |
440 |
|
441 |
panelGroup.getProperties().put("filterOrder", filterOrder);
|
442 |
} |
443 |
|
444 |
@SuppressWarnings("unchecked") |
445 |
public void restoreStatus() { |
446 |
filterList.setStatus((ArrayList) panelGroup.getProperties().get("filterStatus")); |
447 |
|
448 |
if (lyr != null) |
449 |
lyr.getMapContext().invalidate(); |
450 |
} |
451 |
|
452 |
public void end(Object param) { |
453 |
Statistics st = null;
|
454 |
if(param instanceof FLyrRaster && ((FLyrRaster)param).getDataStore() != null) |
455 |
st = ((FLyrRaster)param).getDataStore().getStatistics(); |
456 |
Statistics statistics = (st == null) ? stats : st;
|
457 |
|
458 |
try {
|
459 |
RasterFilterListManager enhancementManager = filterList.getManagerByID("EnhancementStretch");
|
460 |
Params params = filterList.createEmptyFilterParams(); |
461 |
params.setParam("stats", statistics);
|
462 |
params.setParam("remove", ePanel.isRemoveEndsSelected());
|
463 |
params.setParam("renderBands", renderBands);
|
464 |
params.setParam("rgb", lyr.isRGB());
|
465 |
|
466 |
if (ePanel.isTailTrimCheckSelected()) {
|
467 |
params.setParam("tailtrim", (double) (ePanel.getTrimValue() / 100D)); |
468 |
} else {
|
469 |
filterList.remove("tailTrim");
|
470 |
params.setParam("tailtrim", 0.0); |
471 |
} |
472 |
|
473 |
enhancementManager.addFilter(params); |
474 |
endActionsForFilterSettings(); |
475 |
} catch (FilterManagerException e) {
|
476 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_stats"), this, e); |
477 |
} catch (FilterTypeException e) {
|
478 |
RasterSwingLibrary.messageBoxError(PluginServices.getText(this, "error_adding_stats"), this, e); |
479 |
} |
480 |
} |
481 |
|
482 |
public void interrupted() { |
483 |
} |
484 |
} |