Statistics
| Revision:

root / trunk / extensions / extTopology / src / com / iver / cit / gvsig / referencing / MappedPositionPanel.java @ 23163

History | View | Annotate | Download (10.5 KB)

1
/*
2
 * Created on 10-abr-2006
3
 *
4
 * gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
/* CVS MESSAGES:
45
 *
46
 * $Id: 
47
 * $Log: 
48
 */
49
package com.iver.cit.gvsig.referencing;
50

    
51
import java.awt.event.ActionEvent;
52
import java.awt.event.ActionListener;
53
import java.awt.geom.Rectangle2D;
54

    
55
import javax.swing.JButton;
56
import javax.swing.JComponent;
57
import javax.swing.JInternalFrame;
58
import javax.swing.JLabel;
59
import javax.swing.JTextField;
60

    
61
import org.geotools.referencefork.referencing.operation.builder.MappedPosition;
62
import org.gvsig.fmap.core.FGeometryUtil;
63
import org.gvsig.fmap.tools.VectorListenerImpl;
64
import org.gvsig.fmap.tools.behavior.VectorBehavior;
65
import org.gvsig.referencing.DisactivableMappedPosition;
66
import org.gvsig.referencing.MappedPositionContainer;
67
import org.gvsig.referencing.ReferencingUtil;
68
import org.gvsig.topology.ui.util.BoxLayoutPanel;
69
import org.gvsig.topology.ui.util.GUIUtil;
70
import org.opengis.spatialschema.geometry.DirectPosition;
71

    
72

    
73
import com.iver.andami.PluginServices;
74
import com.iver.andami.ui.mdiManager.IWindow;
75
import com.iver.cit.gvsig.fmap.MapContext;
76
import com.iver.cit.gvsig.fmap.MapControl;
77
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
78
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
79
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
80
import com.iver.cit.gvsig.fmap.tools.Behavior.MouseMovementBehavior;
81
import com.iver.cit.gvsig.fmap.tools.Events.MoveEvent;
82
import com.iver.cit.gvsig.project.documents.view.toolListeners.StatusBarListener;
83
import com.vividsolutions.jts.geom.Envelope;
84

    
85
/**
86
 * Panel to digitize MappedPosition in mapcontrol.
87
 * 
88
 * MappedPosition is a 2D euclidean vector, useful to represent correspondency between equivalent
89
 * points.
90
 * 
91
 * @author Alvaro Zabala
92
 * 
93
 * 
94
 * TODO Puede ser mas intuitivo usar una tabla, en la que se muestre x0,y0-x1,y1, y que tenga una serie de botones
95
 *(borrar, redigitalizar en el mapa, a?adir, etc) que se seleccionen en funcion de lo que haya seleccionado en la tabla
96
 *(borrar solo si hay un registro seleccionado, a?adir siempre habilitado)
97
 *
98
 */
99
public class MappedPositionPanel extends BoxLayoutPanel {
100
        /**
101
         * Serial version id
102
         */
103
        private static final long serialVersionUID = 2689973568535047698L;
104
        
105
        /**
106
         * Identifier of this mapped position
107
         */
108
        int mappedPositionIdx;
109
        
110
        
111
//        private List<MappedPosition> linksList;
112
        /**
113
         * List of mapped positions digitized in an adjust session
114
         */
115
        private MappedPositionContainer linksList;
116
        
117
        /**
118
         * Reference to the active view when user opened referencing dialog
119
         * (when digitizing, referencing dialog is not visible, so user could active
120
         * another view, causing problems.
121
         */
122
        private MapControl currentView;
123
        
124
        /**
125
         * mapped position associated to this panel
126
         */
127
        private MappedPosition mappedPosition;
128

    
129
        
130
        String title = PluginServices.getText(null, "Mapped_Position");
131
        String xLbl = PluginServices.getText(null, "X=");
132
        String yLbl = PluginServices.getText(null, "Y=");
133
        
134
        JTextField originPtTextFieldX;
135
        JTextField originPtTextFieldY;
136
        
137
        JTextField destPtTextFieldX;
138
        JTextField destPtTextFieldY;
139
        
140
        JButton digitizeButton;
141
        
142
        
143
        /**
144
         * Constructor
145
         * @param 
146
         * 
147
         */
148
        public MappedPositionPanel(MappedPositionContainer linksList, MapControl currentView) {
149
                super();
150
                this.linksList = linksList;
151
                this.mappedPositionIdx = linksList.getCount();
152
                this.currentView = currentView;
153
                
154
                initialize();
155
        }
156
        
157
        private void initialize(){
158
                this.addRow(new JComponent[] { 
159
                                new JLabel(title+" "+ mappedPositionIdx) , getDigitizeButton()});
160
                
161
                this.addRow(new JComponent[]{new JLabel(xLbl), 
162
                                                                        getOriginXTF(), 
163
                                                                        new JLabel(" ; "+yLbl), 
164
                                                                        getOriginYTF()});
165
                
166
                this.addRow(new JComponent[]{new JLabel(xLbl), 
167
                                getDestXTF(), 
168
                                new JLabel(" ; "+yLbl), 
169
                                getDestYTF()/*, getDigitizeButton()*/});
170
        }
171
        
172
        public void setMappedPosition(MappedPosition mappedPosition){
173
                this.mappedPosition = mappedPosition;
174
                
175
                DirectPosition source = mappedPosition.getSource();
176
                DirectPosition dest = mappedPosition.getTarget();
177
                
178
                this.originPtTextFieldX.setText(source.getCoordinates()[0]+"");
179
                this.originPtTextFieldY.setText(source.getCoordinates()[1]+"");
180
                
181
                this.destPtTextFieldX.setText(dest.getCoordinates()[0]+"");
182
                this.destPtTextFieldY.setText(dest.getCoordinates()[1]+"");
183
                
184
        }
185
        
186
        public MappedPosition getMappedPosition() throws IllegalArgumentException {
187
                try {
188
                        double x0 = Double.parseDouble(getOriginXTF().getText());
189
                        double y0 = Double.parseDouble(getOriginYTF().getText());
190
                        
191
                        double x1 = Double.parseDouble(getDestXTF().getText());
192
                        double y1 = Double.parseDouble(getDestYTF().getText());
193
                          
194
                        ReferencingUtil ref = ReferencingUtil.getInstance();
195
                        
196
                        DirectPosition a = ref.create(new double[]{x0, y0} , null);
197
                        DirectPosition b = ref.create(new double[]{x1, y1} , null);
198
                        
199
                        
200
                        DisactivableMappedPosition p = new DisactivableMappedPosition(a,b);
201
                        return p;
202
                } catch (NumberFormatException e) {
203
                        e.printStackTrace();
204
                        throw new IllegalArgumentException("Valor no numerico", e);
205
                }
206
        }
207
        
208
        public void setEditable(boolean editable){
209
                originPtTextFieldX.setEditable(editable);
210
                originPtTextFieldY.setEditable(editable);
211
                destPtTextFieldX.setEditable(editable);
212
                destPtTextFieldY.setEditable(editable);
213
        }
214
        
215
        private JTextField getOriginXTF(){
216
                if(originPtTextFieldX == null){
217
                        originPtTextFieldX = new JTextField(20);
218
                        originPtTextFieldX.setEditable(false);
219
                }
220
                return originPtTextFieldX;
221
        }
222
        
223
        private JTextField getOriginYTF(){
224
                if(originPtTextFieldY == null){
225
                        originPtTextFieldY = new JTextField(20);
226
                        originPtTextFieldY.setEditable(false);
227
                }
228
                return originPtTextFieldY;
229
        }
230
        
231
        private JTextField getDestXTF(){
232
                if(destPtTextFieldX == null){
233
                        destPtTextFieldX = new JTextField(20);
234
                        destPtTextFieldX.setEditable(false);
235
                }
236
                return destPtTextFieldX;
237
        }
238
        
239
        private JTextField getDestYTF(){
240
                if(destPtTextFieldY == null){
241
                        destPtTextFieldY = new JTextField(20);
242
                        destPtTextFieldY.setEditable(false);
243
                }
244
                return destPtTextFieldY;
245
        }
246
        
247
        private JButton getDigitizeButton(){
248
                if(digitizeButton == null){
249
                        digitizeButton = new JButton(PluginServices.getText(this, " -> "));
250
                        digitizeButton.addActionListener(new ActionListener(){
251
                                public void actionPerformed(ActionEvent arg0) {
252
                                        try {
253
//                                                MDIManager mdiManager = PluginServices.getMDIManager(); 
254
//                                                if (mdiManager != null && mdiManager.getActiveWindow() != null && mdiManager.getActiveWindow() instanceof View) {
255
//                                                        View view = (View) mdiManager.getActiveWindow();
256
//                                                        final MapControl mapCtrl = view.getMapControl();
257
                                                        final MapControl mapCtrl = currentView;
258
                                                        if (mapCtrl != null) {//PROBLEMA. EL PRIMER VECTOR DE ERROR SE CONSTRUYE CUANDO EL MAPCONTROL ES NULL ARREGLAR!!!!!!
259
                                                                String sTool = "digitizeVectorError";
260
                                                                StatusBarListener sbl = new StatusBarListener(mapCtrl);        
261
                                                                
262
                                                                final JComponent thisContainer = (JComponent) GUIUtil.getInstance().getParentOfType(MappedPositionPanel.this, IWindow.class);
263
                                                                
264
                                                                VectorListenerImpl vl = new VectorListenerImpl(mapCtrl, linksList){
265
                                                                        public void vector(MoveEvent event) throws BehaviorException {
266
                                                                                super.vector(event);
267
                                                                                
268
                                                                                MappedPosition lastMappedPosition = 
269
                                                                                        linksList.getMappedPosition(linksList.getCount() - 1);
270
                                                                                
271
                                                                                MappedPositionPanel.this.setMappedPosition(lastMappedPosition);
272
                                                                                
273
                                                                                if(thisContainer != null){
274
                                                                                        if(!GUIUtil.getInstance().getParentOfType(thisContainer, JInternalFrame.class).isVisible()){
275
                                                                                                GUIUtil.getInstance().getParentOfType(thisContainer, JInternalFrame.class).setVisible(true);
276
                                                                                        }
277
                                                                                                
278
                                                                                }//thisContainer
279
                                                                                mapCtrl.setPrevTool();
280
                                                                                
281
                                                                                if(!linksList.existsLinksLyr()){
282
                                                                                        FLyrVect linkLyr = linksList.getLinkLyr(currentView.getProjection());
283
                                                                                        MapContext mapContext = currentView.getMapContext();
284
                                                                                        mapContext.beginAtomicEvent();
285
                                                                                        mapContext.getLayers().addLayer(linkLyr);
286
                                                                                        mapContext.endAtomicEvent();
287
                                                                                }
288
                                                                                mapCtrl.commandRepaint();
289
                                                                                
290
                                                                                
291
                
292
                                                                        }
293
                                                                };
294
                                                                
295
                                                                mapCtrl.addMapTool(sTool, new Behavior[] {
296
                                                                                new VectorBehavior(vl, null),
297
                                                                                new MouseMovementBehavior(sbl) });
298
                                                                mapCtrl.setTool(sTool);
299

    
300
                                                                if(mappedPosition != null){
301
                                                                        DirectPosition source = mappedPosition.getSource();
302
                                                                        double[] sourceCoords = source.getCoordinates();
303
                                                                        DirectPosition dest = mappedPosition.getTarget();
304
                                                                        double[] destCoords = dest.getCoordinates();
305
                                                                        
306
                                                                        Envelope envelope = new Envelope(sourceCoords[0], 
307
                                                                                                                                           destCoords[0], 
308
                                                                                                                                           sourceCoords[1], 
309
                                                                                                                                           destCoords[1]);
310
                                                                        
311
                                                                        Rectangle2D rect = FGeometryUtil.envelopeToRectangle2D(envelope);
312
                                                                        mapCtrl.getMapContext().zoomToExtent(rect);
313
                                                                }
314
                                                                
315
                                                                
316
                                                                if (thisContainer != null) {
317
                                                                        GUIUtil.getInstance().getParentOfType(thisContainer, JInternalFrame.class).setVisible(false);
318
                                                                }
319

    
320
                                                        }// if mapCtrl != null
321
//                                                }// if f!=null
322
                                        } catch (RuntimeException e) {
323
                                                e.printStackTrace();
324
                                        }
325
                                }});
326
                }
327
                return digitizeButton;
328
        }
329
        
330
}