Statistics
| Revision:

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

History | View | Annotate | Download (10.7 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
    private void initializeCoordinates() {
118
        IProjection proj = mapControl.getProjection();
119
        String projName = proj.getAbrev();
120
        if (projName.equals("EPSG:23030") || projName.equals("EPSG:27492") || projName.equals("EPSG:42101") || projName.equals("EPSG:42304")) {
121
            firstCoordinate = "X";
122
            secondCoordinate = "Y";
123
        } else if (projName.equals("EPSG:4230") || projName.equals("EPSG:4326")) {
124
            firstCoordinate = "Lon";
125
            secondCoordinate = "Lat";
126
        } else {
127
            System.out.println("Proyecci?n: " + projName);
128
            System.out.println("Proyecci?n no soportada.");
129
        }
130
    }
131

    
132
    private void zoomToCoordinates() throws Exception {
133
       try{
134
            Rectangle2D oldExtent = mapControl.getViewPort().getAdjustedExtent();
135
        double oldCenterX = oldExtent.getCenterX();
136
        double oldCenterY = oldExtent.getCenterY();
137
        double centerX = (new Double((String)textX.getText())).doubleValue();
138
        double centerY = (new Double((String)textY.getText())).doubleValue();
139
        center=new Point2D.Double(centerX,centerY);
140
        double movX = centerX-oldCenterX;
141
        double movY = centerY-oldCenterY;
142
        double upperLeftCornerX = oldExtent.getMinX()+movX;
143
        double upperLeftCornerY = oldExtent.getMinY()+movY;
144
        double width = oldExtent.getWidth();
145
        double height = oldExtent.getHeight();
146
        Rectangle2D extent = new Rectangle2D.Double(upperLeftCornerX, upperLeftCornerY, width, height);
147
        mapControl.getViewPort().setExtent(extent);
148
       }catch (NumberFormatException e) {
149
               throw new Exception();
150
       }
151

    
152
    }
153

    
154
    /**
155
     * This method initializes this
156
     *
157
     * @return void
158
     */
159
    private void initialize() {
160
        labelY = new JLabel();
161
        labelY.setBounds(10, 35, 28, 20);
162
        labelY.setText(secondCoordinate + ":");
163
        labelX = new JLabel();
164
        labelX.setBounds(10, 10, 28, 20);
165
        labelX.setText(firstCoordinate + ":");
166
        this.setLayout(null);
167
        this.setSize(307, 100);
168
        this.add(labelX, null);
169
        this.add(getTextX(), null);
170
        this.add(labelY, null);
171
        this.add(getTextY(), null);
172
        this.add(getColorPanel());
173
        this.add(getOkCancelPanel(), null);
174
    }
175

    
176
    /**
177
     * This method initializes textX
178
     *
179
     * @return javax.swing.JTextField
180
     */
181
    private JTextField getTextX() {
182
            if (textX == null) {
183
                    textX = new JTextField();
184
                    textX.setBounds(40, 10, 260, 20);
185
                    textX.addActionListener(new java.awt.event.ActionListener() {
186
                            public void actionPerformed(java.awt.event.ActionEvent e) {
187
                                    textX.transferFocus();
188
                            }
189
                    });
190
            }
191
            return textX;
192
    }
193

    
194
    /**
195
     * This method initializes textY
196
     *
197
     * @return javax.swing.JTextField
198
     */
199
    private JTextField getTextY() {
200
            if (textY == null) {
201
                    textY = new JTextField();
202
                    textY.setBounds(40, 35, 260, 20);
203
                    textY.addActionListener(new java.awt.event.ActionListener() {
204
                            public void actionPerformed(java.awt.event.ActionEvent e) {
205
                                    textY.transferFocus();
206
                            }
207
                    });
208
            }
209
            return textY;
210
    }
211

    
212
    /* (non-Javadoc)
213
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
214
     */
215
    public WindowInfo getWindowInfo() {
216
        // TODO Auto-generated method stub
217
        if (viewInfo == null) {
218
            viewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
219
            viewInfo.setTitle(PluginServices.getText(this,"Centrar_la_Vista_sobre_un_punto"));
220
            viewInfo.setWidth(this.getWidth()+8);
221
            viewInfo.setHeight(this.getHeight());
222
        }
223
        return viewInfo;
224
    }
225
    private void openInfo(){
226
            InfoListener infoListener=new InfoListener(mapControl);
227
            MouseEvent e=new MouseEvent((Component)(((CenterViewToPointExtension)PluginServices.getExtension(CenterViewToPointExtension.class)).getView()),MouseEvent.BUTTON1,MouseEvent.ACTION_EVENT_MASK,MouseEvent.MOUSE_CLICKED,500,400,1,true);
228
            Point2D centerPixels=mapControl.getViewPort().fromMapPoint(center.getX(),center.getY());
229
            PointEvent pe=new PointEvent(centerPixels,e);
230
            try {
231
                        infoListener.point(pe);
232
                } catch (BehaviorException e1) {
233
                        // TODO Auto-generated catch block
234
                        e1.printStackTrace();
235
                }
236
                if (mapControl.getMapContext().getLayers().getActives().length==0){
237
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_ninguna_capa_seleccionada")+" \n"+
238
                                        PluginServices.getText(this,"debe_seleccionar_las_capas_de_las_que_quiera_obtener_informacion"));
239
                }
240
    }
241
    private void drawPoint(Color color){
242
            CenterViewToPointExtension.COLOR=color;
243
            lyr.clearAllGraphics();
244
            ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(FShape.POINT,color);
245
        int idSymbol = lyr.addSymbol(theSymbol);
246
        IGeometry geom = ShapeFactory.createPoint2D(center.getX(),center.getY());
247
        FGraphic theGraphic = new FGraphic(geom, idSymbol);
248
        lyr.addGraphic(theGraphic);
249
        mapControl.drawGraphics();
250

    
251
    }
252

    
253

    
254
        /**
255
         * This method initializes jPanel
256
         *
257
         * @return javax.swing.JPanel
258
         */
259
        private JPanel getColorPanel() {
260
                if (colorPanel==null){
261
                         colorPanel=new ColorChooserPanel();
262
                         colorPanel.setAlpha(250);
263
                         colorPanel.setColor(CenterViewToPointExtension.COLOR);
264
                         colorPanel.setBounds(new java.awt.Rectangle(40,59,123,24));
265
                }
266
                         return colorPanel;
267
        }
268

    
269
        /**
270
         * This method initializes okCancelPanel
271
         *
272
         * @return javax.swing.JPanel
273
         */
274
        private AcceptCancelPanel getOkCancelPanel() {
275
                if (okCancelPanel == null) {
276
                        ActionListener okAction, cancelAction;
277
                        okAction = new java.awt.event.ActionListener() {
278
                            public void actionPerformed(java.awt.event.ActionEvent e) {
279
                                    try{
280
                                    zoomToCoordinates();
281
                                    }catch (Exception e1) {
282
                                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"formato_de_numero_incorrecto"));
283
                                            return;
284
                                    }
285
                                    // y sale.
286
                    if (PluginServices.getMainFrame() == null)
287
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
288
                    else
289
                        PluginServices.getMDIManager().closeWindow(InputCoordinatesPanel.this);
290
                    Preferences prefs = Preferences.userRoot().node( "gvsig.centerViewToPoint" );
291
                    if( prefs.get("showInfo", "True").equalsIgnoreCase("True")){
292
                            openInfo();
293
                    }
294
                    drawPoint(((ColorChooserPanel)getColorPanel()).getColor());
295
                            }
296
                    };
297
                    cancelAction = new ActionListener() {
298
                                public void actionPerformed(ActionEvent e) {
299
                                        closeThis();
300
                                }
301
                    };
302
                        okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
303
                        okCancelPanel.setBounds(new java.awt.Rectangle(40, 88, 260, 30));
304
                }
305
                return okCancelPanel;
306
        }
307

    
308
        private void closeThis() {
309
                PluginServices.getMDIManager().closeWindow(this);
310

    
311
        }
312

    
313

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