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 @ 41225

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().getAdjustedEnvelope();
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
        
140
        double minx = oldExtent.getMinimum(0) + movX;
141
        double miny = oldExtent.getMinimum(1) + movY;
142
        double maxX = oldExtent.getMaximum(0) + movX;
143
        double maxY = oldExtent.getMaximum(1) + movY;
144
        Envelope extent = GeometryLocator.getGeometryManager().createEnvelope(
145
            minx, miny,
146
            maxX, maxY,
147
            SUBTYPES.GEOM2D);
148
        mapControl.getViewPort().setEnvelope(extent);
149
       }catch (NumberFormatException e) {
150
               throw new Exception();
151
       }
152

    
153
    }
154

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

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

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

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

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

    
274
    }
275

    
276

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

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

    
331
        /**
332
         * Close the window.
333
         *
334
         */
335
        private void closeThis() {
336
                PluginServices.getMDIManager().closeWindow(this);
337

    
338
        }
339

    
340
        public Object getWindowProfile() {
341
                return WindowInfo.DIALOG_PROFILE;
342
        }
343

    
344

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