Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / colortable / ColorTableListener.java @ 13633

History | View | Annotate | Download (5.3 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

    
26
import org.gvsig.fmap.raster.layers.FLyrRasterSE;
27
import org.gvsig.gui.beans.imagenavigator.IClientImageNavigator;
28
import org.gvsig.raster.dataset.InvalidSetViewException;
29
import org.gvsig.raster.datastruct.Extent;
30
import org.gvsig.raster.datastruct.ViewPortData;
31
import org.gvsig.raster.grid.GridPalette;
32
import org.gvsig.raster.grid.filter.RasterFilterList;
33
import org.gvsig.raster.grid.filter.RasterFilterListManager;
34
import org.gvsig.raster.grid.filter.bands.ColorTableFilter;
35
import org.gvsig.raster.grid.filter.bands.ColorTableListManager;
36
import org.gvsig.raster.grid.filter.enhancement.LinearEnhancementFilter;
37
import org.gvsig.raster.grid.filter.statistics.TailTrimFilter;
38
import org.gvsig.raster.grid.render.Rendering;
39
import org.gvsig.rastertools.colortable.ui.ColorTablePanel;
40

    
41
import com.iver.andami.messages.NotificationManager;
42
import com.iver.cit.gvsig.fmap.ViewPort;
43

    
44
/**
45
 * @version 27/06/2007
46
 * @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
47
 */
48
public class ColorTableListener implements IClientImageNavigator {
49
        private ColorTablePanel colorTablePanel = null;
50

    
51
        /**
52
   * Construye un ColorTableListener
53
   * @param colorTablePanel
54
   */
55
        public ColorTableListener(ColorTablePanel colorTablePanel) {
56
                this.colorTablePanel = colorTablePanel;
57
        }
58

    
59
        /*
60
   * (non-Javadoc)
61
   * @see org.gvsig.gui.beans.imagenavigator.IClientImageNavigator#drawImage(java.awt.Graphics2D,
62
   *      double, double, double, double, double, int, int)
63
   */
64
        public void drawImage(Graphics2D g, double x1, double y1, double x2, double y2, double zoom, int width, int height) {
65
                if (getColorTablePanel().getLayer() == null)
66
                        return;
67

    
68
                Rendering rendering = ((FLyrRasterSE) getColorTablePanel().getLayer()).getRender();
69

    
70
                if ((rendering == null) || ((x2 - x1) == 0.0) || ((y2 - y1) == 0.0))
71
                        return;
72

    
73
                rendering.getFilterList().pushStatus();
74

    
75
                ViewPort vp = new ViewPort(null);
76

    
77
                Dimension2D dimension = new Dimension(width, height);
78
                Extent extent = new Extent(x1, y1, x2, y2);
79

    
80
                ViewPortData vp2 = new ViewPortData(vp.getProjection(), extent, dimension);
81
                vp2.setMat(new AffineTransform(zoom, 0.0, 0.0, zoom, -x1 * zoom, -y1 * zoom));
82

    
83
                Extent extent2 = ((FLyrRasterSE) colorTablePanel.getLayer()).getBufferFactory().getExtent();
84

    
85
                AffineTransform trans = g.getTransform();
86
                // Calcular cuanto sobresale la imagen y rectificar ese desplazamiento
87
                if (y1 > extent2.maxY()) {
88
                        g.translate(0.0, (-(extent2.maxY() - y1) * zoom) * 2.0);
89
                }
90

    
91
                applyColorTable(rendering, true);
92
                try {
93
                        rendering.draw(g, vp2);
94
                } catch (ArrayIndexOutOfBoundsException e) {
95
                        NotificationManager.addError("Error en el renderizado de la mini-imagen del panel de tabla de color.", e);
96
                } catch (InvalidSetViewException e) {
97
                        NotificationManager.addError("Error asignando la vista en el renderizado de la mini-imagen del panel de tabla de color.", e);
98
                } catch (InterruptedException e) {
99
                        NotificationManager.addWarning("Dibujado de la preview detenido por el usuario.", e);
100
                }
101

    
102
                g.setTransform(trans);
103

    
104
                rendering.getFilterList().popStatus();
105
        }
106

    
107
        /**
108
   * Aqui se aplica el estado de las tablas de color al rendering pasado por
109
   * parametro
110
   * @param rendering
111
   * @return
112
   */
113
        public void applyColorTable(Rendering rendering, boolean isPreview) {
114
                RasterFilterList filterList = rendering.getFilterList();
115
                RasterFilterListManager manager = new RasterFilterListManager(filterList);
116
                ColorTableListManager cManager = (ColorTableListManager) manager.getManagerByClass(ColorTableListManager.class);
117

    
118
                filterList.remove(ColorTableFilter.class);
119

    
120
                if (getColorTablePanel().getCheckBoxEnabled().isSelected()) {
121
                        filterList.remove(LinearEnhancementFilter.class);
122
                        filterList.remove(TailTrimFilter.class);
123
                        GridPalette gridPalette = colorTablePanel.getGridPalette();
124
                        cManager.addColorTableFilter(gridPalette);
125
                        if (!isPreview)
126
                                ((FLyrRasterSE) getColorTablePanel().getLayer()).setLastLegend(gridPalette);
127
                }
128
        }
129

    
130
        /**
131
   * Acciones que se ejecutaran al haber presionado el bot?n aceptar o aplicar
132
   */
133
        public void accept() {
134
                Rendering rendering = ((FLyrRasterSE) getColorTablePanel().getLayer()).getRender();
135
                applyColorTable(rendering, false);
136
                getColorTablePanel().getLayer().getMapContext().invalidate();
137
        }
138

    
139
        /**
140
   * Devuelve el panel de ColorTable
141
   * @return
142
   */
143
        public ColorTablePanel getColorTablePanel() {
144
                return colorTablePanel;
145
        }
146
}