Statistics
| Revision:

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

History | View | Annotate | Download (10.8 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
        if (proj.isProjected()) {
123
            firstCoordinate = "X";
124
            secondCoordinate = "Y";
125
        } else {
126
            firstCoordinate = "Lon";
127
            secondCoordinate = "Lat";
128
        }
129
    }
130

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

    
157
    }
158

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

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

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

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

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

    
266
    }
267

    
268

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

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

    
323
        /**
324
         * Close the window.
325
         *
326
         */
327
        private void closeThis() {
328
                PluginServices.getMDIManager().closeWindow(this);
329

    
330
        }
331

    
332
        public Object getWindowProfile() {
333
                return WindowInfo.DIALOG_PROFILE;
334
        }
335

    
336

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