root / trunk / extensions / extRasterTools-SE / src / org / gvsig / rastertools / cutting / ui / listener / CuttingMouseViewListener.java @ 13021
History | View | Annotate | Download (5.48 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 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.cutting.ui.listener; |
20 |
|
21 |
import java.awt.Component; |
22 |
import java.awt.geom.AffineTransform; |
23 |
import java.awt.geom.NoninvertibleTransformException; |
24 |
import java.awt.geom.Point2D; |
25 |
|
26 |
import javax.swing.JOptionPane; |
27 |
|
28 |
import org.gvsig.fmap.raster.layers.FLyrRasterSE; |
29 |
import org.gvsig.raster.util.RasterUtilities; |
30 |
import org.gvsig.rastertools.cutting.ui.CuttingPanel; |
31 |
|
32 |
import com.iver.andami.PluginServices; |
33 |
import com.iver.cit.gvsig.fmap.MapControl; |
34 |
import com.iver.cit.gvsig.fmap.tools.SaveRasterListenerImpl; |
35 |
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent; |
36 |
/**
|
37 |
* <code>CuttingMouseViewListener</code> es un listener del recorte de raster.
|
38 |
* Al seleccionar un ?rea sobre la vista debe cargar el cuadro con los datos de
|
39 |
* coordenadas pixel, coordenadas reales, ancho y alto del raster resultante,
|
40 |
* tama?o de celda.
|
41 |
*
|
42 |
* @version 19/04/2007
|
43 |
* @author BorSanZa - Borja S?nchez Zamorano (borja.sanchez@iver.es)
|
44 |
*/
|
45 |
public class CuttingMouseViewListener extends SaveRasterListenerImpl { |
46 |
private FLyrRasterSE layer = null; |
47 |
private CuttingPanel cuttingPanel;
|
48 |
private MapControl mapControl = null; |
49 |
private String currentTool = null; |
50 |
|
51 |
/**
|
52 |
* Crea un nuevo <code>CuttingMouseViewListener</code>
|
53 |
*
|
54 |
* @param mapCtrl
|
55 |
* @param cutRasterDialog
|
56 |
*/
|
57 |
public CuttingMouseViewListener(MapControl mapControl, CuttingPanel cuttingPanel) {
|
58 |
super(mapControl);
|
59 |
this.cuttingPanel = cuttingPanel;
|
60 |
this.layer = (FLyrRasterSE) cuttingPanel.getFLayer();
|
61 |
this.mapControl = mapControl;
|
62 |
currentTool = mapControl.getCurrentTool(); |
63 |
} |
64 |
|
65 |
/**
|
66 |
* Asigna la capa raster.
|
67 |
* @param flyr Capa raster
|
68 |
*/
|
69 |
public void setRasterLayer(FLyrRasterSE flyr) { |
70 |
layer = flyr; |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Realiza las acciones de selecci?n del ?rea de recorte por medio de un rectangulo
|
75 |
* sobre la vista.
|
76 |
*/
|
77 |
public void rectangle(RectangleEvent event) { |
78 |
super.rectangle(event);
|
79 |
|
80 |
AffineTransform at = ((FLyrRasterSE) layer).getAffineTransform();
|
81 |
Point2D ulPx = new Point2D.Double(rect.getMinX(), rect.getMaxY()); |
82 |
Point2D lrPx = new Point2D.Double(rect.getMaxX(), rect.getMinY()); |
83 |
Point2D urPx = new Point2D.Double(rect.getMaxX(), rect.getMaxY()); |
84 |
Point2D llPx = new Point2D.Double(rect.getMinX(), rect.getMinY()); |
85 |
try {
|
86 |
at.inverseTransform(ulPx, ulPx); |
87 |
at.inverseTransform(lrPx, lrPx); |
88 |
at.inverseTransform(urPx, urPx); |
89 |
at.inverseTransform(llPx, llPx); |
90 |
} catch (NoninvertibleTransformException e) { |
91 |
JOptionPane.showMessageDialog((Component) PluginServices.getMainFrame(), PluginServices.getText(this, "coordenadas_erroneas")); |
92 |
return;
|
93 |
} |
94 |
|
95 |
Point2D[] pointList = new Point2D[]{ulPx, lrPx, urPx, llPx}; |
96 |
Point2D dim = new Point2D.Double(layer.getPxWidth(), layer.getPxHeight()); |
97 |
|
98 |
//Comprobamos si la selecci?n est? fuera del ?rea
|
99 |
if(isOutside(ulPx, lrPx)) {
|
100 |
cuttingPanel.setCoorPixelFromDouble(0, 0, 0, 0, 0); |
101 |
cuttingPanel.setWidthText(0, 0); |
102 |
cuttingPanel.setHeightText(0, 0); |
103 |
cuttingPanel.setCellSizeText(0, 0); |
104 |
cuttingPanel.setRelWidthHeight(0);
|
105 |
cuttingPanel.setCoorRealFromDouble(0, 0, 0, 0, 0); |
106 |
return;
|
107 |
} |
108 |
|
109 |
//Ajustamos los puntos al ?rea en pixeles del raster
|
110 |
RasterUtilities.adjustToPixelSize(pointList, dim); |
111 |
|
112 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
113 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
114 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
115 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
116 |
|
117 |
//Convertimos nuevamente a coordenadas reales
|
118 |
Point2D ulWc = new Point2D.Double(); |
119 |
Point2D lrWc = new Point2D.Double(); |
120 |
at.transform(new Point2D.Double(minX, minY), ulWc); |
121 |
at.transform(new Point2D.Double(maxX + 1, maxY + 1), lrWc); |
122 |
|
123 |
cuttingPanel.setCoorRealFromDouble(ulWc.getX(), ulWc.getY(), lrWc.getX(), lrWc.getY(), 3);
|
124 |
|
125 |
cuttingPanel.setCoorPixelFromDouble(minX, minY, maxX, maxY, 3);
|
126 |
cuttingPanel.setWidthText(dim.getX(), 0);
|
127 |
cuttingPanel.setHeightText(dim.getY(), 0);
|
128 |
cuttingPanel.setCellSizeText(((maxX - minX) / dim.getX()), 4);
|
129 |
cuttingPanel.setRelWidthHeight((double) (dim.getX() / dim.getY()));
|
130 |
if (currentTool != null) |
131 |
mapControl.setTool(currentTool); |
132 |
} |
133 |
|
134 |
/**
|
135 |
* Comprueba si la selecci?n del punto est? fuera del ?rea del raster.
|
136 |
* @param ulPx Coordenada superior izquierda en pixeles
|
137 |
* @param lrPx Corrdenada inferior derecha en pixeles
|
138 |
* @return true si la selecci?n del punto est? fuera del raster y false si no lo est?
|
139 |
*/
|
140 |
private boolean isOutside(Point2D ulPx, Point2D lrPx) { |
141 |
double minX = Math.min(ulPx.getX(), lrPx.getX()); |
142 |
double minY = Math.min(ulPx.getY(), lrPx.getY()); |
143 |
double maxX = Math.max(ulPx.getX(), lrPx.getX()); |
144 |
double maxY = Math.max(ulPx.getY(), lrPx.getY()); |
145 |
if(minX >= layer.getPxWidth() || minY >= layer.getPxHeight() || maxX < 0 || maxY < 0) |
146 |
return true; |
147 |
return false; |
148 |
} |
149 |
} |