Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.centerviewpoint.app / org.gvsig.centerviewpoint.app.mainplugin / src / main / java / org / gvsig / centerviewpoint / gui / InputCoordinatesPanel.java @ 40469

History | View | Annotate | Download (11.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.centerviewpoint.gui;
25

    
26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.MouseEvent;
31
import java.awt.geom.Point2D;
32
import java.util.prefs.Preferences;
33

    
34
import javax.swing.JDialog;
35
import javax.swing.JLabel;
36
import javax.swing.JOptionPane;
37
import javax.swing.JPanel;
38
import javax.swing.JTextField;
39

    
40
import org.cresques.cts.IProjection;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
import org.gvsig.andami.PluginServices;
45
import org.gvsig.andami.ui.mdiManager.IWindow;
46
import org.gvsig.andami.ui.mdiManager.WindowInfo;
47
import org.gvsig.app.gui.panels.ColorChooserPanel;
48
import org.gvsig.app.project.documents.view.toolListeners.InfoListener;
49
import org.gvsig.centerviewpoint.CenterViewToPointExtension;
50
import org.gvsig.fmap.geom.Geometry;
51
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
52
import org.gvsig.fmap.geom.Geometry.TYPES;
53
import org.gvsig.fmap.geom.GeometryLocator;
54
import org.gvsig.fmap.geom.GeometryManager;
55
import org.gvsig.fmap.geom.primitive.Envelope;
56
import org.gvsig.fmap.geom.primitive.Point;
57
import org.gvsig.fmap.mapcontext.MapContext;
58
import org.gvsig.fmap.mapcontext.MapContextLocator;
59
import org.gvsig.fmap.mapcontext.MapContextManager;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
61
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
62
import org.gvsig.fmap.mapcontrol.MapControl;
63
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
64
import org.gvsig.gui.beans.AcceptCancelPanel;
65

    
66

    
67
/**
68
 * The InputCoordinatesPanel class creates a JPanel where the
69
 * user can input the coordinates of the point of reference
70
 * for center the View.
71
 *
72
 * @author jmorell
73
 */
74
public class InputCoordinatesPanel extends JPanel implements IWindow {
75
    private static final long serialVersionUID = 1L;
76
    private JLabel labelX = null;
77
    private JTextField textX = null;
78
    private JLabel labelY = null;
79
    private JTextField textY = null;
80
    private MapControl mapControl;
81
    private WindowInfo viewInfo = null;
82
    private String firstCoordinate;
83
    private String secondCoordinate;
84
    private Point2D center;
85
    private GraphicLayer lyr;
86
    private ColorChooserPanel colorPanel;
87
        private AcceptCancelPanel okCancelPanel = null;
88
        
89
        MapContextManager mapContextManager = MapContextLocator
90
                        .getMapContextManager();
91
        
92
    private static final Logger LOG = LoggerFactory
93
        .getLogger(InputCoordinatesPanel.class);
94

    
95
        /**
96
     * This is the default constructor
97
     */
98
    public InputCoordinatesPanel(MapContext mapContext) {
99
        super();
100
        this.mapControl = new MapControl();
101
        mapControl.setMapContext(mapContext);
102
        lyr=mapControl.getMapContext().getGraphicsLayer();
103
        initializeCoordinates();
104
        initialize();
105
    }
106

    
107
    /**
108
     * Sets the proper text for the first and second coordinate labels,
109
     * depending on the kind of selected projection.
110
     *
111
     */
112
    private void initializeCoordinates() {
113
        IProjection proj = mapControl.getProjection();
114
        if (proj.isProjected()) {
115
            firstCoordinate = "X";
116
            secondCoordinate = "Y";
117
        } else {
118
            firstCoordinate = "Lon";
119
            secondCoordinate = "Lat";
120
        }
121
    }
122

    
123
    /**
124
     * Move the view's extent so that the specified point gets
125
     * centered.
126
     *
127
     * @throws Exception
128
     */
129
    private void zoomToCoordinates() throws Exception {
130
       try{
131
            Envelope oldExtent = mapControl.getViewPort().getAdjustedExtent();
132
        double oldCenterX = oldExtent.getCenter(0);
133
        double oldCenterY = oldExtent.getCenter(1);
134
        double centerX = (new Double((String)textX.getText())).doubleValue();
135
        double centerY = (new Double((String)textY.getText())).doubleValue();
136
        center=new Point2D.Double(centerX,centerY);
137
        double movX = centerX-oldCenterX;
138
        double movY = centerY-oldCenterY;
139
        double upperLeftCornerX = oldExtent.getMinimum(0)+movX;
140
        double upperLeftCornerY = oldExtent.getMinimum(1)+movY;
141
        double maxX = oldExtent.getMaximum(0);
142
        double maxY = oldExtent.getMaximum(1);
143
        Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(upperLeftCornerX, upperLeftCornerY, maxX, maxY, SUBTYPES.GEOM2D);
144
        mapControl.getViewPort().setEnvelope(extent);
145
       }catch (NumberFormatException e) {
146
               throw new Exception();
147
       }
148

    
149
    }
150

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

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

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

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

    
243
    /**
244
     * Draws the selected point on the view.
245
     *
246
     * @param color
247
     */
248
    private void drawPoint(Color color){
249
            CenterViewToPointExtension.COLOR=color;
250
            lyr.clearAllGraphics();
251
                ISymbol theSymbol =
252
                                mapContextManager.getSymbolManager().createSymbol(
253
                                Geometry.TYPES.POINT, color);
254
        int idSymbol = lyr.addSymbol(theSymbol);
255
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
256
//        org.gvsig.fmap.geom.Geometry geom = geomManager.create(center.getX(),center.getY());
257
        Point geom = null;
258
                try {
259
                        geom = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
260
                        geom.setX(center.getX());
261
                        geom.setY(center.getY());
262
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
263
            LOG.error("Error creating a point geometry", e);
264
                }
265
//        FGraphic theGraphic = new FGraphic(geom, idSymbol);
266
//        lyr.addGraphic(theGraphic);
267
        lyr.addGraphic(geom, idSymbol);
268
        mapControl.drawGraphics();
269

    
270
    }
271

    
272

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

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

    
327
        /**
328
         * Close the window.
329
         *
330
         */
331
        private void closeThis() {
332
                PluginServices.getMDIManager().closeWindow(this);
333

    
334
        }
335

    
336
        public Object getWindowProfile() {
337
                return WindowInfo.DIALOG_PROFILE;
338
        }
339

    
340

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