Statistics
| Revision:

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

History | View | Annotate | Download (9.95 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.MouseEvent;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51

    
52
import javax.swing.JDialog;
53
import javax.swing.JOptionPane;
54
import javax.swing.JPanel;
55
import javax.swing.JLabel;
56
import javax.swing.JTextField;
57
import javax.swing.JButton;
58

    
59
import org.cresques.cts.IProjection;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.andami.ui.mdiManager.View;
63
import com.iver.andami.ui.mdiManager.ViewInfo;
64
import com.iver.cit.gvsig.fmap.FMap;
65
import com.iver.cit.gvsig.fmap.MapControl;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
68
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
69
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
70
import com.iver.cit.gvsig.fmap.layers.GraphicLayer;
71
import com.iver.cit.gvsig.fmap.rendering.FGraphic;
72
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
73
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
74
import com.iver.cit.gvsig.gui.Panels.ColorChooserPanel;
75
import com.iver.cit.gvsig.gui.toolListeners.InfoListener;
76
import com.iver.gvsig.centerviewpoint.CenterViewToPointExtension;
77

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

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

    
148
    /**
149
     * This method initializes this
150
     * 
151
     * @return void
152
     */
153
    private void initialize() {
154
        labelY = new JLabel();
155
        labelY.setBounds(10, 35, 28, 20);
156
        labelY.setText(secondCoordinate + ":");
157
        labelX = new JLabel();
158
        labelX.setBounds(10, 10, 28, 20);
159
        labelX.setText(firstCoordinate + ":");
160
        this.setLayout(null);
161
        this.setSize(212, 122);
162
        this.add(labelX, null);
163
        this.add(getTextX(), null);
164
        this.add(labelY, null);
165
        this.add(getTextY(), null);
166
        this.add(getColorPanel());
167
        this.add(getButtonSearch(), null);
168
    }
169

    
170
    /**
171
     * This method initializes textX        
172
     *         
173
     * @return javax.swing.JTextField        
174
     */    
175
    private JTextField getTextX() {
176
            if (textX == null) {
177
                    textX = new JTextField();
178
                    textX.setBounds(40, 10, 132, 20);
179
                    textX.addActionListener(new java.awt.event.ActionListener() { 
180
                            public void actionPerformed(java.awt.event.ActionEvent e) {    
181
                                    textX.transferFocus();
182
                            }
183
                    });
184
            }
185
            return textX;
186
    }
187

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

    
206
    /**
207
     * This method initializes buttonSearch        
208
     *         
209
     * @return javax.swing.JButton        
210
     */    
211
    private JButton getButtonSearch() {
212
            if (buttonSearch == null) {
213
                    buttonSearch = new JButton();
214
                    buttonSearch.setBounds(88, 91, 84, 20);
215
                    buttonSearch.addActionListener(new java.awt.event.ActionListener() { 
216
                            public void actionPerformed(java.awt.event.ActionEvent e) {    
217
                                    try{
218
                                    zoomToCoordinates();
219
                                    }catch (Exception e1) {
220
                                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"Formato de n?mero incorrecto."));
221
                                            return;
222
                                    }
223
                                    // y sale.
224
                    if (PluginServices.getMainFrame() == null)
225
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
226
                    else
227
                        PluginServices.getMDIManager().closeView(InputCoordinatesPanel.this);
228
                    
229
                    openInfo();
230
                    drawPoint(((ColorChooserPanel)getColorPanel()).getColor());
231
                            }
232
                    });
233
                    buttonSearch.setText(PluginServices.getText(this,"Aceptar"));
234
            }
235
            return buttonSearch;
236
    }
237

    
238
    /* (non-Javadoc)
239
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
240
     */
241
    public ViewInfo getViewInfo() {
242
        // TODO Auto-generated method stub
243
        if (viewInfo == null) {
244
            viewInfo=new ViewInfo(ViewInfo.MODALDIALOG);
245
            viewInfo.setTitle(PluginServices.getText(this,"Centrar_la_Vista_sobre_un_punto"));
246
        }
247
        return viewInfo;
248
    }
249
    private void openInfo(){
250
            InfoListener infoListener=new InfoListener(mapControl);
251
            MouseEvent e=new MouseEvent((Component)(((CenterViewToPointExtension)PluginServices.getExtension(CenterViewToPointExtension.class)).getView()),MouseEvent.BUTTON1,MouseEvent.ACTION_EVENT_MASK,MouseEvent.MOUSE_CLICKED,500,400,1,true);
252
            Point2D centerPixels=mapControl.getViewPort().fromMapPoint(center.getX(),center.getY());
253
            PointEvent pe=new PointEvent(centerPixels,e);
254
            try {
255
                        infoListener.point(pe);
256
                } catch (BehaviorException e1) {
257
                        // TODO Auto-generated catch block
258
                        e1.printStackTrace();
259
                }
260
                if (mapControl.getMapContext().getLayers().getActives().length==0){
261
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"No hay ning?n tema seleccionado.")+" \n"+
262
                                        PluginServices.getText(this,"Debe seleccionar los temas de los que quiera obtener informaci?n."));
263
                }
264
    }
265
    private void drawPoint(Color color){
266
            CenterViewToPointExtension.COLOR=color;
267
            lyr.clearAllGraphics();
268
            FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT,color);           
269
        int idSymbol = lyr.addSymbol(theSymbol);
270
        IGeometry geom = ShapeFactory.createPoint2D(center.getX(),center.getY());
271
        FGraphic theGraphic = new FGraphic(geom, idSymbol);
272
        lyr.addGraphic(theGraphic);
273
        mapControl.drawGraphics();
274

    
275
    }
276
   
277

    
278
        /**
279
         * This method initializes jPanel        
280
         *         
281
         * @return javax.swing.JPanel        
282
         */
283
        private JPanel getColorPanel() {
284
                if (colorPanel==null){
285
                         colorPanel=new ColorChooserPanel();
286
                         colorPanel.setAlpha(250);
287
                         colorPanel.setColor(CenterViewToPointExtension.COLOR);
288
                         colorPanel.setBounds(new java.awt.Rectangle(40,59,98,24));
289
                }
290
                         return colorPanel;
291
        }
292
}  //  @jve:decl-index=0:visual-constraint="103,18"