Statistics
| Revision:

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

History | View | Annotate | Download (11.2 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.core.v02.FConstant;
75
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
76
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
77
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
78
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
79
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
80
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
81
import com.iver.cit.gvsig.project.documents.view.toolListeners.InfoListener;
82
import com.iver.gvsig.centerviewpoint.CenterViewToPointExtension;
83

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

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

    
163
    }
164

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

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

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

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

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

    
272
    }
273

    
274

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

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

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

    
336
        }
337

    
338

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