Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCenterViewToPoint / src / com / iver / gvsig / centerviewtopoint / gui / InputCoordinatesPanel.java @ 27475

History | View | Annotate | Download (11.9 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.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.fmap.dal.feature.exception.CreateGeometryException;
63
import org.gvsig.fmap.geom.Geometry;
64
import org.gvsig.fmap.geom.GeometryLocator;
65
import org.gvsig.fmap.geom.GeometryManager;
66
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
67
import org.gvsig.fmap.geom.Geometry.TYPES;
68
import org.gvsig.fmap.geom.primitive.Envelope;
69
import org.gvsig.fmap.geom.primitive.Point;
70
import org.gvsig.fmap.mapcontext.MapContext;
71
import org.gvsig.fmap.mapcontext.layers.GraphicLayer;
72
import org.gvsig.fmap.mapcontext.rendering.legend.FGraphic;
73
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbologyFactory;
74
import org.gvsig.fmap.mapcontrol.MapControl;
75
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
76
import org.gvsig.gui.beans.AcceptCancelPanel;
77

    
78
import com.iver.andami.PluginServices;
79
import com.iver.andami.ui.mdiManager.IWindow;
80
import com.iver.andami.ui.mdiManager.WindowInfo;
81
import com.iver.cit.gvsig.gui.panels.ColorChooserPanel;
82
import com.iver.cit.gvsig.project.documents.view.toolListeners.InfoListener;
83
import com.iver.gvsig.centerviewpoint.CenterViewToPointExtension;
84

    
85
/**
86
 * The InputCoordinatesPanel class creates a JPanel where the
87
 * user can input the coordinates of the point of reference
88
 * for center the View.
89
 *
90
 * @author jmorell
91
 */
92
public class InputCoordinatesPanel extends JPanel implements IWindow {
93
    private static final long serialVersionUID = 1L;
94
    private JLabel labelX = null;
95
    private JTextField textX = null;
96
    private JLabel labelY = null;
97
    private JTextField textY = null;
98
    private MapControl mapControl;
99
    private WindowInfo viewInfo = null;
100
    private String firstCoordinate;
101
    private String secondCoordinate;
102
    private Point2D center;
103
    private GraphicLayer lyr;
104
    private ColorChooserPanel colorPanel;
105
        private AcceptCancelPanel okCancelPanel = null;
106
        protected static Logger logger = Logger.getLogger(InputCoordinatesPanel.class
107
                        .getName());
108
        /**
109
     * This is the default constructor
110
     */
111
    public InputCoordinatesPanel(MapContext mapContext) {
112
        super();
113
        this.mapControl = new MapControl();
114
        mapControl.setMapContext(mapContext);
115
        lyr=mapControl.getMapContext().getGraphicsLayer();
116
        initializeCoordinates();
117
        initialize();
118
    }
119

    
120
    /**
121
     * Sets the proper text for the first and second coordinate labels,
122
     * depending on the kind of selected projection.
123
     *
124
     */
125
    private void initializeCoordinates() {
126
        IProjection proj = mapControl.getProjection();
127
        if (proj.isProjected()) {
128
            firstCoordinate = "X";
129
            secondCoordinate = "Y";
130
        } else {
131
            firstCoordinate = "Lon";
132
            secondCoordinate = "Lat";
133
        }
134
    }
135

    
136
    /**
137
     * Move the view's extent so that the specified point gets
138
     * centered.
139
     *
140
     * @throws Exception
141
     */
142
    private void zoomToCoordinates() throws Exception {
143
       try{
144
            Envelope oldExtent = mapControl.getViewPort().getAdjustedExtent();
145
        double oldCenterX = oldExtent.getCenter(0);
146
        double oldCenterY = oldExtent.getCenter(1);
147
        double centerX = (new Double((String)textX.getText())).doubleValue();
148
        double centerY = (new Double((String)textY.getText())).doubleValue();
149
        center=new Point2D.Double(centerX,centerY);
150
        double movX = centerX-oldCenterX;
151
        double movY = centerY-oldCenterY;
152
        double upperLeftCornerX = oldExtent.getMinimum(0)+movX;
153
        double upperLeftCornerY = oldExtent.getMinimum(1)+movY;
154
        double maxX = oldExtent.getMaximum(0);
155
        double maxY = oldExtent.getMaximum(1);
156
        Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(upperLeftCornerX, upperLeftCornerY, maxX, maxY, SUBTYPES.GEOM2D);
157
        mapControl.getViewPort().setEnvelope(extent);
158
       }catch (NumberFormatException e) {
159
               throw new Exception();
160
       }
161

    
162
    }
163

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

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

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

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

    
256
    /**
257
     * Draws the selected point on the view.
258
     *
259
     * @param color
260
     */
261
    private void drawPoint(Color color){
262
            CenterViewToPointExtension.COLOR=color;
263
            lyr.clearAllGraphics();
264
            org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol theSymbol = SymbologyFactory.createDefaultSymbolByShapeType(Geometry.TYPES.POINT,color);
265
        int idSymbol = lyr.addSymbol(theSymbol);
266
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
267
//        org.gvsig.fmap.geom.Geometry geom = geomManager.create(center.getX(),center.getY());
268
        Point geom = null;
269
                try {
270
                        geom = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
271
                        geom.setX(center.getX());
272
                        geom.setY(center.getY());
273
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
274
                        logger.error(new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
275
                }
276
        FGraphic theGraphic = new FGraphic(geom, idSymbol);
277
        lyr.addGraphic(theGraphic);
278
        mapControl.drawGraphics();
279

    
280
    }
281

    
282

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

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

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

    
344
        }
345

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

    
350

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