Statistics
| Revision:

root / trunk / extensions / extGeoreferencing / src / org / gvsig / georeferencing / gui / listeners / GeorefMovePointListener.java @ 7304

History | View | Annotate | Download (5.2 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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.georeferencing.gui.listeners;
20

    
21
import java.awt.Component;
22
import java.awt.geom.Point2D;
23

    
24
import javax.swing.JOptionPane;
25

    
26
import org.gvsig.georeferencing.gui.dialog.GeoreferencingDialog;
27

    
28
import com.iver.andami.PluginServices;
29
import com.iver.cit.gvsig.fmap.ViewPort;
30
import com.iver.cit.gvsig.fmap.layers.FLayer;
31
import com.iver.cit.gvsig.fmap.layers.FLayers;
32
import com.iver.cit.gvsig.fmap.layers.FLyrGeoRaster;
33
import com.iver.cit.gvsig.fmap.layers.FLyrPoints;
34
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
35
import com.iver.cit.gvsig.fmap.tools.GeorefDraggListenerImpl;
36
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
37
import com.iver.cit.gvsig.project.documents.view.gui.View;
38

    
39

    
40
/**
41
 * <p>Extensi?n de la clase GeorefPointerListenerImpl de FMap para poder llamar a m?todos de
42
 * andami o de gvSIG.</p>
43
 * <p>Este listener mueve puntos desde una vista correspondientes a la capa de puntos.</p>
44
 * 
45
 * @author Nacho Brodin (brodin_ign@gva.es)
46
 */
47
public class GeorefMovePointListener extends GeorefDraggListenerImpl {
48
        
49
        private GeoreferencingDialog dialog = null;
50
        private FLyrPoints lyrPoints = null;
51
        private FLyrGeoRaster lyrGeoRaster = null;
52
        private View theView = null;
53
        private ViewPort viewPort = null;
54
        
55
        /**
56
         * Posici?n del punto a ser seleccionado. 
57
         */
58
        private int posPoint = -1;
59
        /**
60
         * Es true si el punto que se ha movido corresponde con el pixel 
61
         * de la imagen y false si corresponde con el punto de la vista
62
         */
63
        private boolean isPixel = true;
64
        
65
        /**
66
         * Constructor
67
         *
68
         * @param mapCtrl
69
         */
70
        public GeorefMovePointListener(GeoreferencingDialog dialog) {
71
                super();
72
                this.dialog = dialog;
73
        }
74

    
75
        /**
76
         * Captura de puntos sobre una capa FlyrPoint
77
         *
78
         * @param event Evento de un punto sobre la vista
79
         *
80
         * @throws BehaviorException
81
         */
82
        public void press(PointEvent event) throws BehaviorException {
83
                super.press(event);
84

    
85
                try{
86
                        theView = (View)PluginServices.getMDIManager().getActiveWindow();
87
                }catch(ClassCastException exc){
88
                        return;
89
                }
90
                viewPort = theView.getMapControl().getMapContext().getViewPort();
91
                FLayers flayers = theView.getMapControl().getMapContext().getLayers();
92
                
93
                //Obtenci?n de la capa de puntos.
94

    
95
                for(int i=0;i<flayers.getLayersCount();i++){
96
                    FLayer lyr = flayers.getLayer(i);
97
                    if(lyr instanceof FLyrGeoRaster)
98
                            lyrGeoRaster = (FLyrGeoRaster)lyr;
99
                   }
100
                lyrPoints = lyrGeoRaster.getFLyrPoints();
101
                   if(lyrGeoRaster == null || lyrPoints == null)
102
                           return;
103
                    
104
                
105
                //Hallamos el punto m?s cercano a donde hemos pinchado
106
                Point2D init = viewPort.toMapPoint(event.getPoint());
107
                double distance = Double.MAX_VALUE;
108
                
109
                for(int i=0;i<lyrPoints.length();i++){
110
                        Point2D pixel = lyrGeoRaster.img2World(lyrPoints.getPoint(i).pixelPoint);
111
                        Point2D map = lyrPoints.getPoint(i).mapPoint;
112
                        double d1 = init.distance(pixel.getX(), pixel.getY());
113
                        
114
                        if(d1 < distance){
115
                                distance = d1;
116
                                posPoint = i;
117
                                isPixel = true;
118
                        }
119
                        d1 = init.distance(map.getX(), map.getY());
120
                        if(d1 < distance){
121
                                distance = d1;
122
                                posPoint = i;
123
                                isPixel = false;
124
                        }
125
                }
126
                        
127
        }
128
        
129
        /**
130
         * Reimplementaci?n del m?todo mouseReleased de Behavior.
131
         *
132
         * @param e MouseEvent
133
         *
134
         * @throws BehaviorException Excepci?n lanzada cuando el Maptool.
135
         */
136
        public void release(PointEvent e) throws BehaviorException {
137
                
138
                if(posPoint != -1){
139
                        //Seleccionamos en el dialogo el punto que se est? moviendo
140
                        ((GeoreferencingDialog)lyrGeoRaster.getGeoDialog()).getSelectPointsPanel().changePoint(true, posPoint);
141
                        
142
                        if(isPixel){
143
                                //Comprobamos que el punto siga dentro de la imagen
144
                                Point2D pixelPoint = lyrGeoRaster.world2Img(viewPort.toMapPoint(e.getPoint()));
145
                                if(pixelPoint == null){
146
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
147
                                                        PluginServices.getText(this, "fuera_de_extent"));
148
                                        return;
149
                                }
150
                                
151
                                if(theView != null && lyrGeoRaster != null && lyrPoints != null)
152
                                        lyrGeoRaster.getFLyrPoints().getPointManager().
153
                                                        updateData(posPoint + 1,
154
                                                                                pixelPoint, 
155
                                                                                null, 
156
                                                                                lyrGeoRaster.getGeoDialog(),
157
                                                                                theView);
158
                                
159
                        }else{
160
                                 Point2D mapPoint = viewPort.toMapPoint(e.getPoint());
161
                                if(theView != null && lyrGeoRaster != null && lyrPoints != null)
162
                                        lyrGeoRaster.getFLyrPoints().getPointManager().
163
                                                        updateData(posPoint + 1,
164
                                                                                null, 
165
                                                                                mapPoint, 
166
                                                                                lyrGeoRaster.getGeoDialog(),
167
                                                                                theView);
168
                        }
169
                        
170
                }
171
        }
172
}