Statistics
| Revision:

root / trunk / extensions / extCenterViewToPoint / src / com / iver / gvsig / centerviewtopoint / gui / InputCoordinatesPanel.java @ 13881

History | View | Annotate | Download (11.1 KB)

1
/*
2
 * Created on 08-nov-2005
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
package com.iver.gvsig.centerviewtopoint.gui;
45

    
46
import java.awt.Color;
47
import java.awt.Component;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.MouseEvent;
51
import java.awt.geom.Point2D;
52
import java.awt.geom.Rectangle2D;
53
import java.util.prefs.Preferences;
54

    
55
import javax.swing.JDialog;
56
import javax.swing.JLabel;
57
import javax.swing.JOptionPane;
58
import javax.swing.JPanel;
59
import javax.swing.JTextField;
60

    
61
import org.cresques.cts.IProjection;
62
import org.gvsig.gui.beans.AcceptCancelPanel;
63

    
64
import com.iver.andami.PluginServices;
65
import com.iver.andami.ui.mdiManager.IWindow;
66
import com.iver.andami.ui.mdiManager.WindowInfo;
67
import com.iver.cit.gvsig.fmap.MapContext;
68
import com.iver.cit.gvsig.fmap.MapControl;
69
import com.iver.cit.gvsig.fmap.core.FShape;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
72
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
73
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
74
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
75
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
76
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
77
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
78
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
79
import com.iver.cit.gvsig.project.documents.view.toolListeners.InfoListener;
80
import com.iver.gvsig.centerviewpoint.CenterViewToPointExtension;
81

    
82
/**
83
 * The InputCoordinatesPanel class creates a JPanel where the
84
 * user can input the coordinates of the point of reference
85
 * for center the View.
86
 *
87
 * @author jmorell
88
 */
89
public class InputCoordinatesPanel extends JPanel implements IWindow {
90
    private static final long serialVersionUID = 1L;
91
    private JLabel labelX = null;
92
    private JTextField textX = null;
93
    private JLabel labelY = null;
94
    private JTextField textY = null;
95
    private MapControl mapControl;
96
    private WindowInfo viewInfo = null;
97
    private String firstCoordinate;
98
    private String secondCoordinate;
99
    private Point2D center;
100
    private GraphicLayer lyr;
101
    private ColorChooserPanel colorPanel;
102
        private AcceptCancelPanel okCancelPanel = null;
103
        /**
104
     * This is the default constructor
105
     */
106
    public InputCoordinatesPanel(MapContext mapContext) {
107
        super();
108
        this.mapControl = new MapControl();
109
        mapControl.setMapContext(mapContext);
110
        lyr=mapControl.getMapContext().getGraphicsLayer();
111
        initializeCoordinates();
112
        initialize();
113
    }
114
    
115
    /**
116
     * Sets the proper text for the first and second coordinate labels,
117
     * depending on the kind of selected projection.
118
     *
119
     */
120
    private void initializeCoordinates() {
121
        IProjection proj = mapControl.getProjection();
122
        String projName = proj.getAbrev();
123
        if (projName.equals("EPSG:23030") || projName.equals("EPSG:27492") || projName.equals("EPSG:42101") || projName.equals("EPSG:42304")) {
124
            firstCoordinate = "X";
125
            secondCoordinate = "Y";
126
        } else if (projName.equals("EPSG:4230") || projName.equals("EPSG:4326")) {
127
            firstCoordinate = "Lon";
128
            secondCoordinate = "Lat";
129
        } else {
130
            System.out.println("Proyecci?n: " + projName);
131
            System.out.println("Proyecci?n no soportada.");
132
        }
133
    }
134

    
135
    /**
136
     * Move the view's extent so that the specified point gets
137
     * centered.
138
     * 
139
     * @throws Exception
140
     */
141
    private void zoomToCoordinates() throws Exception {
142
       try{
143
            Rectangle2D oldExtent = mapControl.getViewPort().getAdjustedExtent();
144
        double oldCenterX = oldExtent.getCenterX();
145
        double oldCenterY = oldExtent.getCenterY();
146
        double centerX = (new Double((String)textX.getText())).doubleValue();
147
        double centerY = (new Double((String)textY.getText())).doubleValue();
148
        center=new Point2D.Double(centerX,centerY);
149
        double movX = centerX-oldCenterX;
150
        double movY = centerY-oldCenterY;
151
        double upperLeftCornerX = oldExtent.getMinX()+movX;
152
        double upperLeftCornerY = oldExtent.getMinY()+movY;
153
        double width = oldExtent.getWidth();
154
        double height = oldExtent.getHeight();
155
        Rectangle2D extent = new Rectangle2D.Double(upperLeftCornerX, upperLeftCornerY, width, height);
156
        mapControl.getViewPort().setExtent(extent);
157
       }catch (NumberFormatException e) {
158
               throw new Exception();
159
       }
160

    
161
    }
162

    
163
    /**
164
     * This method initializes this
165
     *
166
     * @return void
167
     */
168
    private void initialize() {
169
        labelY = new JLabel();
170
        labelY.setBounds(10, 35, 28, 20);
171
        labelY.setText(secondCoordinate + ":");
172
        labelX = new JLabel();
173
        labelX.setBounds(10, 10, 28, 20);
174
        labelX.setText(firstCoordinate + ":");
175
        this.setLayout(null);
176
        this.setSize(307, 100);
177
        this.add(labelX, null);
178
        this.add(getTextX(), null);
179
        this.add(labelY, null);
180
        this.add(getTextY(), null);
181
        this.add(getColorPanel());
182
        this.add(getOkCancelPanel(), null);
183
    }
184

    
185
    /**
186
     * This method initializes textX
187
     *
188
     * @return javax.swing.JTextField
189
     */
190
    private JTextField getTextX() {
191
            if (textX == null) {
192
                    textX = new JTextField();
193
                    textX.setBounds(40, 10, 260, 20);
194
                    textX.addActionListener(new java.awt.event.ActionListener() {
195
                            public void actionPerformed(java.awt.event.ActionEvent e) {
196
                                    textX.transferFocus();
197
                            }
198
                    });
199
            }
200
            return textX;
201
    }
202

    
203
    /**
204
     * This method initializes textY
205
     *
206
     * @return javax.swing.JTextField
207
     */
208
    private JTextField getTextY() {
209
            if (textY == null) {
210
                    textY = new JTextField();
211
                    textY.setBounds(40, 35, 260, 20);
212
                    textY.addActionListener(new java.awt.event.ActionListener() {
213
                            public void actionPerformed(java.awt.event.ActionEvent e) {
214
                                    textY.transferFocus();
215
                            }
216
                    });
217
            }
218
            return textY;
219
    }
220

    
221
    /* (non-Javadoc)
222
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
223
     */
224
    public WindowInfo getWindowInfo() {
225
        // TODO Auto-generated method stub
226
        if (viewInfo == null) {
227
            viewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
228
            viewInfo.setTitle(PluginServices.getText(this,"Centrar_la_Vista_sobre_un_punto"));
229
            viewInfo.setWidth(this.getWidth()+8);
230
            viewInfo.setHeight(this.getHeight());
231
        }
232
        return viewInfo;
233
    }
234
    /**
235
     * Opens the infoByPoint dialog for the selected point.
236
     *
237
     */
238
    private void openInfo(){
239
            InfoListener infoListener=new InfoListener(mapControl);
240
            MouseEvent e=new MouseEvent((Component)(((CenterViewToPointExtension)PluginServices.getExtension(CenterViewToPointExtension.class)).getView()),MouseEvent.BUTTON1,MouseEvent.ACTION_EVENT_MASK,MouseEvent.MOUSE_CLICKED,500,400,1,true);
241
            Point2D centerPixels=mapControl.getViewPort().fromMapPoint(center.getX(),center.getY());
242
            PointEvent pe=new PointEvent(centerPixels,e);
243
            try {
244
                        infoListener.point(pe);
245
                } catch (BehaviorException e1) {
246
                        // TODO Auto-generated catch block
247
                        e1.printStackTrace();
248
                }
249
                if (mapControl.getMapContext().getLayers().getActives().length==0){
250
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_ninguna_capa_seleccionada")+" \n"+
251
                                        PluginServices.getText(this,"debe_seleccionar_las_capas_de_las_que_quiera_obtener_informacion"));
252
                }
253
    }
254

    
255
    /**
256
     * Draws the selected point on the view.
257
     * 
258
     * @param color
259
     */
260
    private void drawPoint(Color color){
261
            CenterViewToPointExtension.COLOR=color;
262
            lyr.clearAllGraphics();
263
            ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(FShape.POINT,color);
264
        int idSymbol = lyr.addSymbol(theSymbol);
265
        IGeometry geom = ShapeFactory.createPoint2D(center.getX(),center.getY());
266
        FGraphic theGraphic = new FGraphic(geom, idSymbol);
267
        lyr.addGraphic(theGraphic);
268
        mapControl.drawGraphics();
269

    
270
    }
271

    
272

    
273
        /**
274
         * This method initializes jPanel
275
         *
276
         * @return javax.swing.JPanel
277
         */
278
        private JPanel getColorPanel() {
279
                if (colorPanel==null){
280
                         colorPanel=new ColorChooserPanel();
281
                         colorPanel.setAlpha(250);
282
                         colorPanel.setColor(CenterViewToPointExtension.COLOR);
283
                         colorPanel.setBounds(new java.awt.Rectangle(40,59,123,24));
284
                }
285
                         return colorPanel;
286
        }
287

    
288
        /**
289
         * This method initializes okCancelPanel
290
         *
291
         * @return javax.swing.JPanel
292
         */
293
        private AcceptCancelPanel getOkCancelPanel() {
294
                if (okCancelPanel == null) {
295
                        ActionListener okAction, cancelAction;
296
                        okAction = new java.awt.event.ActionListener() {
297
                            public void actionPerformed(java.awt.event.ActionEvent e) {
298
                                    try{
299
                                    zoomToCoordinates();
300
                                    }catch (Exception e1) {
301
                                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"formato_de_numero_incorrecto"));
302
                                            return;
303
                                    }
304
                                    // y sale.
305
                    if (PluginServices.getMainFrame() == null)
306
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
307
                    else
308
                        PluginServices.getMDIManager().closeWindow(InputCoordinatesPanel.this);
309
                    Preferences prefs = Preferences.userRoot().node( "gvsig.centerViewToPoint" );
310
                    if( prefs.get("showInfo", "True").equalsIgnoreCase("True")){
311
                            openInfo();
312
                    }
313
                    drawPoint(((ColorChooserPanel)getColorPanel()).getColor());
314
                            }
315
                    };
316
                    cancelAction = new ActionListener() {
317
                                public void actionPerformed(ActionEvent e) {
318
                                        closeThis();
319
                                }
320
                    };
321
                        okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
322
                        okCancelPanel.setBounds(new java.awt.Rectangle(40, 88, 260, 30));
323
                }
324
                return okCancelPanel;
325
        }
326

    
327
        /**
328
         * Close the window.
329
         *
330
         */
331
        private void closeThis() {
332
                PluginServices.getMDIManager().closeWindow(this);
333

    
334
        }
335

    
336

    
337
}  //  @jve:decl-index=0:visual-constraint="103,18"