Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCenterViewToPoint / src / org / gvsig / centerviewpoint / gui / InputCoordinatesPanel.java @ 29626

History | View | Annotate | Download (11.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 org.gvsig.centerviewpoint.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.util.prefs.Preferences;
53

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

    
60
import org.apache.log4j.Logger;
61
import org.cresques.cts.IProjection;
62
import org.gvsig.andami.PluginServices;
63
import org.gvsig.andami.ui.mdiManager.IWindow;
64
import org.gvsig.andami.ui.mdiManager.WindowInfo;
65
import org.gvsig.app.gui.panels.ColorChooserPanel;
66
import org.gvsig.app.project.documents.view.toolListeners.InfoListener;
67
import org.gvsig.centerviewpoint.CenterViewToPointExtension;
68
import org.gvsig.fmap.geom.Geometry;
69
import org.gvsig.fmap.geom.GeometryLocator;
70
import org.gvsig.fmap.geom.GeometryManager;
71
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
72
import org.gvsig.fmap.geom.Geometry.TYPES;
73
import org.gvsig.fmap.geom.primitive.Envelope;
74
import org.gvsig.fmap.geom.primitive.Point;
75
import org.gvsig.fmap.mapcontext.MapContext;
76
import org.gvsig.fmap.mapcontext.layers.GraphicLayer;
77
import org.gvsig.fmap.mapcontext.rendering.legend.FGraphic;
78
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
79
import org.gvsig.fmap.mapcontrol.MapControl;
80
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
81
import org.gvsig.gui.beans.AcceptCancelPanel;
82

    
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
        protected static Logger logger = Logger.getLogger(InputCoordinatesPanel.class
106
                        .getName());
107
        /**
108
     * This is the default constructor
109
     */
110
    public InputCoordinatesPanel(MapContext mapContext) {
111
        super();
112
        this.mapControl = new MapControl();
113
        mapControl.setMapContext(mapContext);
114
        lyr=mapControl.getMapContext().getGraphicsLayer();
115
        initializeCoordinates();
116
        initialize();
117
    }
118

    
119
    /**
120
     * Sets the proper text for the first and second coordinate labels,
121
     * depending on the kind of selected projection.
122
     *
123
     */
124
    private void initializeCoordinates() {
125
        IProjection proj = mapControl.getProjection();
126
        if (proj.isProjected()) {
127
            firstCoordinate = "X";
128
            secondCoordinate = "Y";
129
        } else {
130
            firstCoordinate = "Lon";
131
            secondCoordinate = "Lat";
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
            Envelope oldExtent = mapControl.getViewPort().getAdjustedExtent();
144
        double oldCenterX = oldExtent.getCenter(0);
145
        double oldCenterY = oldExtent.getCenter(1);
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.getMinimum(0)+movX;
152
        double upperLeftCornerY = oldExtent.getMinimum(1)+movY;
153
        double maxX = oldExtent.getMaximum(0);
154
        double maxY = oldExtent.getMaximum(1);
155
        Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(upperLeftCornerX, upperLeftCornerY, maxX, maxY, SUBTYPES.GEOM2D);
156
        mapControl.getViewPort().setEnvelope(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 (org.gvsig.fmap.mapcontrol.tools.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
            org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(Geometry.TYPES.POINT,color);
264
        int idSymbol = lyr.addSymbol(theSymbol);
265
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
266
//        org.gvsig.fmap.geom.Geometry geom = geomManager.create(center.getX(),center.getY());
267
        Point geom = null;
268
                try {
269
                        geom = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
270
                        geom.setX(center.getX());
271
                        geom.setY(center.getY());
272
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
273
                        logger.error(e);
274
                }
275
        FGraphic theGraphic = new FGraphic(geom, idSymbol);
276
        lyr.addGraphic(theGraphic);
277
        mapControl.drawGraphics();
278

    
279
    }
280

    
281

    
282
        /**
283
         * This method initializes jPanel
284
         *
285
         * @return javax.swing.JPanel
286
         */
287
        private JPanel getColorPanel() {
288
                if (colorPanel==null){
289
                         colorPanel=new ColorChooserPanel();
290
                         colorPanel.setAlpha(250);
291
                         colorPanel.setColor(CenterViewToPointExtension.COLOR);
292
                         colorPanel.setBounds(new java.awt.Rectangle(40,59,123,24));
293
                }
294
                         return colorPanel;
295
        }
296

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

    
336
        /**
337
         * Close the window.
338
         *
339
         */
340
        private void closeThis() {
341
                PluginServices.getMDIManager().closeWindow(this);
342

    
343
        }
344

    
345
        public Object getWindowProfile() {
346
                return WindowInfo.DIALOG_PROFILE;
347
        }
348

    
349

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