svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / ColorTableListener.java @ 18962
History | View | Annotate | Download (13.4 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.colortable; |
20 |
|
21 |
import java.awt.Dimension; |
22 |
import java.awt.Graphics2D; |
23 |
import java.awt.geom.AffineTransform; |
24 |
import java.awt.geom.Dimension2D; |
25 |
import java.util.ArrayList; |
26 |
|
27 |
import javax.swing.JComponent; |
28 |
|
29 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
30 |
import org.gvsig.gui.beans.imagenavigator.IClientImageNavigator; |
31 |
import org.gvsig.raster.dataset.InvalidSetViewException; |
32 |
import org.gvsig.raster.dataset.io.RasterDriverException; |
33 |
import org.gvsig.raster.datastruct.ColorItem; |
34 |
import org.gvsig.raster.datastruct.ColorTable; |
35 |
import org.gvsig.raster.datastruct.Extent; |
36 |
import org.gvsig.raster.datastruct.ViewPortData; |
37 |
import org.gvsig.raster.grid.GridPalette; |
38 |
import org.gvsig.raster.grid.filter.FilterTypeException; |
39 |
import org.gvsig.raster.grid.filter.RasterFilter; |
40 |
import org.gvsig.raster.grid.filter.RasterFilterList; |
41 |
import org.gvsig.raster.grid.filter.RasterFilterListManager; |
42 |
import org.gvsig.raster.grid.filter.bands.ColorTableFilter; |
43 |
import org.gvsig.raster.grid.filter.bands.ColorTableListManager; |
44 |
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter; |
45 |
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter; |
46 |
import org.gvsig.raster.hierarchy.IRasterRendering; |
47 |
import org.gvsig.raster.util.PropertyEvent; |
48 |
import org.gvsig.raster.util.RasterToolsUtil; |
49 |
import org.gvsig.rastertools.colortable.data.ColorTableData; |
50 |
import org.gvsig.rastertools.colortable.data.ColorTableDataListener; |
51 |
import org.gvsig.rastertools.colortable.panels.ColorTableUIListener; |
52 |
import org.gvsig.rastertools.colortable.panels.IColorTableUI; |
53 |
import org.gvsig.rastertools.colortable.ui.ColorTablePanel; |
54 |
|
55 |
import com.iver.andami.PluginServices; |
56 |
import com.iver.cit.gvsig.fmap.ViewPort; |
57 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
58 |
/**
|
59 |
* @version 27/06/2007
|
60 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
61 |
*/
|
62 |
public class ColorTableListener implements IClientImageNavigator, ColorTableDataListener, ColorTableUIListener { |
63 |
private ColorTablePanel colorTablePanel = null; |
64 |
private FLyrRasterSE previewLayer = null; |
65 |
private ColorTableData colorTableData = null; |
66 |
private ArrayList statusList = new ArrayList(); |
67 |
private IColorTableUI lastColorTableUI = null; |
68 |
|
69 |
/**
|
70 |
* Construye un ColorTableListener
|
71 |
* @param colorTablePanel
|
72 |
*/
|
73 |
public ColorTableListener(ColorTablePanel colorTablePanel, ColorTableData colorTableData) {
|
74 |
this.colorTablePanel = colorTablePanel;
|
75 |
this.colorTableData = colorTableData;
|
76 |
getColorTableData().addValueChangedListener(this);
|
77 |
} |
78 |
|
79 |
/**
|
80 |
* Devuelve el panel de ColorTable
|
81 |
* @return
|
82 |
*/
|
83 |
private ColorTablePanel getColorTablePanel() {
|
84 |
return colorTablePanel;
|
85 |
} |
86 |
|
87 |
private ColorTableData getColorTableData() {
|
88 |
return colorTableData;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Asigna la capa raster de la vista
|
93 |
* @param fLayer
|
94 |
*/
|
95 |
public void setLayer(FLayer fLayer) { |
96 |
if (fLayer instanceof FLyrRasterSE) { |
97 |
try {
|
98 |
previewLayer = (FLyrRasterSE) fLayer.cloneLayer(); |
99 |
} catch (Exception e) { |
100 |
RasterToolsUtil.messageBoxError("preview_not_available", colorTablePanel, e);
|
101 |
} |
102 |
} |
103 |
} |
104 |
|
105 |
/**
|
106 |
* Cierra la capa abierta para previsualizaci?n
|
107 |
*/
|
108 |
public void closePreviewLayer() { |
109 |
if (previewLayer != null) { |
110 |
previewLayer.getDataSource().close(); |
111 |
previewLayer.setRemoveRasterFlag(true);
|
112 |
} |
113 |
} |
114 |
|
115 |
/*
|
116 |
* (non-Javadoc)
|
117 |
* @see org.gvsig.gui.beans.imagenavigator.IClientImageNavigator#drawImage(java.awt.Graphics2D,
|
118 |
* double, double, double, double, double, int, int)
|
119 |
*/
|
120 |
public void drawImage(Graphics2D g, double x1, double y1, double x2, double y2, double zoom, int width, int height) { |
121 |
if (previewLayer == null || !(previewLayer instanceof IRasterRendering)) |
122 |
return;
|
123 |
|
124 |
IRasterRendering rendering = ((IRasterRendering) previewLayer); |
125 |
|
126 |
if ((rendering == null) || ((x2 - x1) == 0.0) || ((y2 - y1) == 0.0)) |
127 |
return;
|
128 |
|
129 |
rendering.getRenderFilterList().pushStatus(); |
130 |
|
131 |
ViewPort vp = new ViewPort(null); |
132 |
|
133 |
Dimension2D dimension = new Dimension(width, height); |
134 |
Extent extent = new Extent(x1, y1, x2, y2);
|
135 |
|
136 |
ViewPortData vp2 = new ViewPortData(vp.getProjection(), extent, dimension);
|
137 |
vp2.setMat(new AffineTransform(zoom, 0.0, 0.0, zoom, -x1 * zoom, -y1 * zoom)); |
138 |
|
139 |
Extent extent2 = previewLayer.getBufferFactory().getDataSource().getExtent(); |
140 |
if (extent2 == null) |
141 |
return;
|
142 |
|
143 |
AffineTransform trans = g.getTransform();
|
144 |
// Calcular cuanto sobresale la imagen y rectificar ese desplazamiento
|
145 |
if (y1 > extent2.maxY()) {
|
146 |
g.translate(0.0, (-(extent2.maxY() - y1) * zoom) * 2.0); |
147 |
} |
148 |
|
149 |
try {
|
150 |
applyColorTable(rendering, true);
|
151 |
} catch (FilterTypeException e1) {
|
152 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e1); |
153 |
} |
154 |
try {
|
155 |
previewLayer.getRender().draw(g, vp2); |
156 |
} catch (RasterDriverException e) {
|
157 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_preview_render"), this, e); |
158 |
} catch (InvalidSetViewException e) {
|
159 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_preview_setview"), this, e); |
160 |
} catch (InterruptedException e) { |
161 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_preview_stop"), this, e); |
162 |
} |
163 |
|
164 |
g.setTransform(trans); |
165 |
|
166 |
rendering.getRenderFilterList().popStatus(); |
167 |
} |
168 |
|
169 |
/**
|
170 |
* Aqui se aplica el estado de las tablas de color al rendering pasado por
|
171 |
* parametro
|
172 |
* @param rendering
|
173 |
* @return
|
174 |
* @throws FilterTypeException
|
175 |
*/
|
176 |
private void applyColorTable(IRasterRendering rendering, boolean isPreview) throws FilterTypeException { |
177 |
RasterFilterList filterList = rendering.getRenderFilterList(); |
178 |
RasterFilterListManager manager = new RasterFilterListManager(filterList);
|
179 |
ColorTableListManager cManager = (ColorTableListManager) manager.getManagerByClass(ColorTableListManager.class); |
180 |
|
181 |
filterList.remove(ColorTableFilter.class); |
182 |
|
183 |
((FLyrRasterSE) getColorTablePanel().getLayer()).setLastLegend(null);
|
184 |
|
185 |
if (getColorTableData().isEnabled()) {
|
186 |
filterList.remove(LinearEnhancementFilter.class); |
187 |
filterList.remove(TailTrimFilter.class); |
188 |
GridPalette gridPalette = new GridPalette(getColorTableData().getColorTable());
|
189 |
|
190 |
// Asignamos la transparencia del render actual al filterList
|
191 |
filterList.addEnvParam("Transparency", rendering.getRender().getLastTransparency());
|
192 |
|
193 |
cManager.addColorTableFilter(gridPalette); |
194 |
|
195 |
if (!isPreview) {
|
196 |
gridPalette.compressPalette(); |
197 |
((FLyrRasterSE) getColorTablePanel().getLayer()).setLastLegend(gridPalette); |
198 |
} |
199 |
} |
200 |
for (int i = 0; i< filterList.lenght(); i++) { |
201 |
((RasterFilter) filterList.get(i)).setEnv(filterList.getEnv()); |
202 |
} |
203 |
|
204 |
rendering.setRenderFilterList(filterList); |
205 |
} |
206 |
|
207 |
/**
|
208 |
* Acciones que se ejecutaran al haber presionado el bot?n aceptar o aplicar
|
209 |
*/
|
210 |
public void accept() { |
211 |
if (getColorTablePanel().getLayer() instanceof IRasterRendering) { |
212 |
try {
|
213 |
applyColorTable(((IRasterRendering) getColorTablePanel().getLayer()), false);
|
214 |
getColorTablePanel().getLayer().getMapContext().invalidate(); |
215 |
} catch (FilterTypeException e) {
|
216 |
RasterToolsUtil.messageBoxError(PluginServices.getText(this, "error_adding_filters"), this, e); |
217 |
} |
218 |
|
219 |
} |
220 |
} |
221 |
|
222 |
public class StatusComponent { |
223 |
private JComponent object; |
224 |
private boolean enabled; |
225 |
|
226 |
public boolean isEnabled() { |
227 |
return enabled;
|
228 |
} |
229 |
|
230 |
public void setEnabled(boolean enabled) { |
231 |
this.enabled = enabled;
|
232 |
} |
233 |
|
234 |
public JComponent getObject() { |
235 |
return object;
|
236 |
} |
237 |
|
238 |
public void setObject(JComponent object) { |
239 |
this.object = object;
|
240 |
} |
241 |
} |
242 |
|
243 |
private void saveComponentsStatus(JComponent component) { |
244 |
// Guardar estado
|
245 |
StatusComponent auxStatus = new StatusComponent();
|
246 |
auxStatus.setEnabled(component.isEnabled()); |
247 |
auxStatus.setObject(component); |
248 |
statusList.add(auxStatus); |
249 |
|
250 |
for (int i = 0; i < component.getComponentCount(); i++) |
251 |
if (component.getComponent(i) instanceof JComponent) |
252 |
saveComponentsStatus((JComponent) component.getComponent(i));
|
253 |
} |
254 |
|
255 |
private void setEnabledRecursive(JComponent component, boolean enabled, int level) { |
256 |
if (enabled == true) { |
257 |
boolean auxEnabled = false; |
258 |
boolean finded = false; |
259 |
// Buscar estado de dicho componente
|
260 |
for (int i = 0; i < statusList.size(); i++) { |
261 |
StatusComponent auxStatus = (StatusComponent) statusList.get(i); |
262 |
if (auxStatus.getObject() == component) {
|
263 |
auxEnabled = auxStatus.isEnabled(); |
264 |
statusList.remove(i); |
265 |
i--; |
266 |
finded = true;
|
267 |
break;
|
268 |
} |
269 |
} |
270 |
|
271 |
// Asignar su estado
|
272 |
if (finded)
|
273 |
component.setEnabled(auxEnabled); |
274 |
} else {
|
275 |
// Si es la primera llamada, guardamos el estado de todos los componentes
|
276 |
if (level == 0) |
277 |
saveComponentsStatus(component); |
278 |
|
279 |
// Desactivar componente
|
280 |
component.setEnabled(false);
|
281 |
} |
282 |
|
283 |
for (int i = 0; i < component.getComponentCount(); i++) |
284 |
if (component.getComponent(i) instanceof JComponent) |
285 |
setEnabledRecursive((JComponent) component.getComponent(i), enabled, level + 1); |
286 |
} |
287 |
|
288 |
/**
|
289 |
* Activa/Desactiva los componentes de las pesta?as segun la pesta?a selecionada
|
290 |
* @param enabled
|
291 |
*/
|
292 |
private void setEnabledPanel(boolean enabled) { |
293 |
colorTablePanel.getGeneralPanel().setEnabledPanel(enabled); |
294 |
colorTablePanel.getTabbedPane().setEnabled(enabled); |
295 |
setEnabledRecursive(getLastColorTableUI().getPanel(), enabled, 0);
|
296 |
setEnabledRecursive(colorTablePanel.getPanelPreview(), enabled, 0);
|
297 |
setEnabledRecursive(colorTablePanel.getPanelListView(), enabled, 0);
|
298 |
} |
299 |
|
300 |
/*
|
301 |
* (non-Javadoc)
|
302 |
* @see org.gvsig.rastertools.PropertyListener#actionValueChanged(org.gvsig.rastertools.PropertyEvent)
|
303 |
*/
|
304 |
public void actionValueChanged(PropertyEvent e) { |
305 |
if (e.getName().equals("refreshPreview")) { |
306 |
colorTablePanel.getImageNavigator().updateBuffer(); |
307 |
return;
|
308 |
} |
309 |
if (e.getName().equals("interpolated")) { |
310 |
colorTablePanel.getColorTableLibraryPanel().setInterpolated(((Boolean) e.getValue()).booleanValue());
|
311 |
getColorTableData().getColorTable().setInterpolated(((Boolean) e.getValue()).booleanValue());
|
312 |
getColorTableData().refreshPreview(); |
313 |
getLastColorTableUI().setColorTable(getColorTableData().getColorTable()); |
314 |
return;
|
315 |
} |
316 |
|
317 |
if (e.getName().equals("enabled")) { |
318 |
setEnabledPanel(((Boolean) e.getValue()).booleanValue());
|
319 |
return;
|
320 |
} |
321 |
|
322 |
if (e.getName().equals("limits") || |
323 |
e.getName().equals("maxim") ||
|
324 |
e.getName().equals("minim")) {
|
325 |
if (getColorTableData().isLimitsEnabled()) {
|
326 |
double min = getColorTableData().getMinim();
|
327 |
double max = getColorTableData().getMaxim();
|
328 |
getLastColorTableUI().getColorTable().createColorTableInRange(min, max, true);
|
329 |
getLastColorTableUI().setColorTable(getLastColorTableUI().getColorTable()); |
330 |
getColorTableData().refreshPreview(); |
331 |
} else {
|
332 |
colorTablePanel.reloadPanelsFromLibraryPanel(); |
333 |
} |
334 |
return;
|
335 |
} |
336 |
} |
337 |
|
338 |
/**
|
339 |
* Recarga la tabla de elementos
|
340 |
* @param isNewSelection
|
341 |
*/
|
342 |
public void refreshItems(boolean isNewSelection) { |
343 |
if (getColorTableData().getColorTable() == null) |
344 |
return;
|
345 |
|
346 |
if (isNewSelection)
|
347 |
getColorTableData().getColorTable().removeDuplicatedValues(); |
348 |
|
349 |
getLastColorTableUI().setColorTable(getColorTableData().getColorTable()); |
350 |
} |
351 |
|
352 |
/**
|
353 |
* Pone en separaciones iguales todos los valores de la tabla seleccionada
|
354 |
*/
|
355 |
public void equidistar() { |
356 |
ColorTable colorTable = getColorTableData().getColorTable(); |
357 |
if (colorTable == null) |
358 |
return;
|
359 |
colorTable.removeDuplicatedValues(); |
360 |
ArrayList list = colorTable.getColorItems();
|
361 |
double min2 = Double.POSITIVE_INFINITY; |
362 |
double max2 = Double.NEGATIVE_INFINITY; |
363 |
for (int i = 0; i < list.size(); i++) { |
364 |
ColorItem colorItem = (ColorItem) list.get(i); |
365 |
if (min2 > colorItem.getValue())
|
366 |
min2 = colorItem.getValue(); |
367 |
if (max2 < colorItem.getValue())
|
368 |
max2 = colorItem.getValue(); |
369 |
} |
370 |
for (int i = 0; i < list.size(); i++) { |
371 |
ColorItem colorItem = (ColorItem) list.get(i); |
372 |
colorItem.setValue(min2 + (((max2 - min2) / (list.size() - 1)) * i));
|
373 |
} |
374 |
refreshItems(true);
|
375 |
getColorTableData().refreshPreview(); |
376 |
} |
377 |
|
378 |
/**
|
379 |
* @return the lastColorTableUI
|
380 |
*/
|
381 |
private IColorTableUI getLastColorTableUI() {
|
382 |
return lastColorTableUI;
|
383 |
} |
384 |
|
385 |
/**
|
386 |
* @param lastColorTableUI the lastColorTableUI to set
|
387 |
*/
|
388 |
public void setLastColorTableUI(IColorTableUI lastColorTableUI) { |
389 |
this.lastColorTableUI = lastColorTableUI;
|
390 |
} |
391 |
|
392 |
/*
|
393 |
* (non-Javadoc)
|
394 |
* @see org.gvsig.rastertools.colortable.panels.ColorTableUIListener#actionColorTableUIChanged(org.gvsig.rastertools.colortable.panels.IColorTableUI)
|
395 |
*/
|
396 |
public void actionColorTableUIChanged(IColorTableUI colorTableUI) { |
397 |
ColorTable colorTable = colorTablePanel.getColorTableLibraryPanel().getColorTableSelected(); |
398 |
colorTable.createPaletteFromColorItems(colorTableUI.getColorTable().getColorItems(), true);
|
399 |
colorTablePanel.getColorTableLibraryPanel().setColorTableSelected(colorTable); |
400 |
|
401 |
getColorTableData().setColorTable((ColorTable) colorTableUI.getColorTable().clone()); |
402 |
getColorTableData().refreshPreview(); |
403 |
} |
404 |
} |