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

History | View | Annotate | Download (12.6 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 org.gvsig.andami.PluginServices;
27
import org.gvsig.andami.ui.mdiManager.IWindow;
28
import org.gvsig.andami.ui.mdiManager.WindowInfo;
29
import org.gvsig.app.gui.panels.ColorChooserPanel;
30
import org.gvsig.app.project.documents.view.toolListeners.InfoListener;
31
import org.gvsig.centerviewpoint.CenterViewToPointExtension;
32
import org.gvsig.fmap.geom.*;
33
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
34
import org.gvsig.fmap.geom.Geometry.TYPES;
35
import org.gvsig.fmap.geom.primitive.Envelope;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.mapcontext.*;
38
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer;
39
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
40
import org.gvsig.fmap.mapcontrol.MapControl;
41
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
42
import org.gvsig.gui.beans.AcceptCancelPanel;
43
import org.gvsig.tools.ToolsLocator;
44
import org.gvsig.tools.i18n.I18nManager;
45

    
46
import org.cresques.cts.IProjection;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50
import javax.swing.*;
51
import javax.swing.border.EmptyBorder;
52
import javax.swing.border.TitledBorder;
53

    
54
import java.awt.*;
55
import java.awt.event.*;
56
import java.awt.geom.Point2D;
57
import java.util.prefs.Preferences;
58

    
59

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

    
89
        /**
90
     * This is the default constructor
91
     */
92
    public InputCoordinatesPanel(MapContext mapContext) {
93
        super();
94
        this.mapControl = new MapControl();
95
        mapControl.setMapContext(mapContext);
96
        lyr=mapControl.getMapContext().getGraphicsLayer();
97
        initializeCoordinates();
98
        initialize();
99
    }
100

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

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

    
147
    }
148

    
149
    /**
150
     * This method initializes this
151
     *
152
     * @return void
153
     */
154
    private void initialize() {
155
        
156
        I18nManager i18nManager = ToolsLocator.getI18nManager();
157
        
158
        this.setLayout(new BorderLayout(0,5));
159
        this.setBorder(new EmptyBorder(new Insets(5,5,5,5)));
160
        this.setSize(250, 150);
161
        
162
        labelX = new JLabel();
163
        labelX.setText(firstCoordinate + ":");
164

    
165
        labelY = new JLabel();
166
        labelY.setText(secondCoordinate + ":");
167
        
168
        GridBagLayout gbl = new GridBagLayout();
169
        JPanel centerPanel = new JPanel(gbl);
170
        GridBagConstraints c = new GridBagConstraints();
171
        
172
        c.fill = GridBagConstraints.BOTH;
173
        c.anchor = GridBagConstraints.LINE_START;
174
        c.insets = new Insets(3,5,5,5);
175
        
176
        // JLabels
177
        c.weightx = 0;
178
        c.gridx = 0;
179
        
180
        c.gridy = 0;
181
        centerPanel.add(labelX, c);
182
        
183
        c.gridy = 1;
184
        centerPanel.add(labelY, c);
185
        
186
        c.gridwidth = 3;
187
        c.weightx = 1;
188
        c.gridx = 1;
189
        
190
        c.gridy = 0;
191
        centerPanel.add(getTextX(), c);
192
        
193
        c.gridy = 1;
194
        centerPanel.add(getTextY(), c);
195

    
196
        // Color chooser    
197
        JPanel centerPanel2 = new JPanel(new BorderLayout());
198
        centerPanel2.add(getColorPanel(), BorderLayout.CENTER);
199
        
200
        // First TitledBorder
201
        TitledBorder tb1 = BorderFactory.createTitledBorder(new TitledBorder(i18nManager.getTranslation("coordinates")));
202
        centerPanel.setBorder(tb1);   
203
        this.add(centerPanel, BorderLayout.NORTH);
204
        
205
        // Second TitledBorder
206
        TitledBorder tb2 = BorderFactory.createTitledBorder(new TitledBorder(i18nManager.getTranslation("point_color")));
207
        centerPanel2.setBorder(tb2);
208
        this.add(centerPanel2, BorderLayout.CENTER);
209
        
210
        // Buttons Panel
211
        this.add(getOkCancelPanel(), BorderLayout.SOUTH);
212
    }
213

    
214
    /**
215
     * This method initializes textX
216
     *
217
     * @return javax.swing.JTextField
218
     */
219
    private JTextField getTextX() {
220
            if (textX == null) {
221
                    textX = new JTextField();
222
                    textX.addActionListener(new java.awt.event.ActionListener() {
223
                            public void actionPerformed(java.awt.event.ActionEvent e) {
224
                                    textX.transferFocus();
225
                            }
226
                    });
227
            }
228
            return textX;
229
    }
230

    
231
    /**
232
     * This method initializes textY
233
     *
234
     * @return javax.swing.JTextField
235
     */
236
    private JTextField getTextY() {
237
            if (textY == null) {
238
                    textY = new JTextField();
239
                    textY.addActionListener(new java.awt.event.ActionListener() {
240
                            public void actionPerformed(java.awt.event.ActionEvent e) {
241
                                    textY.transferFocus();
242
                            }
243
                    });
244
            }
245
            return textY;
246
    }
247

    
248
    /* (non-Javadoc)
249
     * @see com.iver.andami.ui.mdiManager.View#getViewInfo()
250
     */
251
    public WindowInfo getWindowInfo() {
252
        // TODO Auto-generated method stub
253
        if (viewInfo == null) {
254
            viewInfo=new WindowInfo(WindowInfo.MODALDIALOG);
255
            viewInfo.setTitle(PluginServices.getText(this,"Centrar_la_Vista_sobre_un_punto"));
256
            viewInfo.setWidth(this.getWidth());
257
            viewInfo.setHeight(this.getHeight());
258
        }
259
        return viewInfo;
260
    }
261
    /**
262
     * Opens the infoByPoint dialog for the selected point.
263
     *
264
     */
265
    private void openInfo(){
266
            InfoListener infoListener=new InfoListener(mapControl);
267
            MouseEvent e=new MouseEvent((Component)(((CenterViewToPointExtension)PluginServices.getExtension(CenterViewToPointExtension.class)).getView()),MouseEvent.BUTTON1,MouseEvent.ACTION_EVENT_MASK,MouseEvent.MOUSE_CLICKED,500,400,1,true);
268
            Point2D centerPixels=mapControl.getViewPort().fromMapPoint(center.getX(),center.getY());
269
            PointEvent pe=new PointEvent(centerPixels,e,mapControl);
270
            try {
271
                        infoListener.point(pe);
272
                } catch (org.gvsig.fmap.mapcontrol.tools.BehaviorException e1) {
273
                        // TODO Auto-generated catch block
274
                        e1.printStackTrace();
275
                }
276
                if (mapControl.getMapContext().getLayers().getActives().length==0){
277
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"no_hay_ninguna_capa_seleccionada")+" \n"+
278
                                        PluginServices.getText(this,"debe_seleccionar_las_capas_de_las_que_quiera_obtener_informacion"));
279
                }
280
    }
281

    
282
    /**
283
     * Draws the selected point on the view.
284
     *
285
     * @param color
286
     */
287
    private void drawPoint(Color color){
288
            CenterViewToPointExtension.COLOR=color;
289
            lyr.clearAllGraphics();
290
                ISymbol theSymbol =
291
                                mapContextManager.getSymbolManager().createSymbol(
292
                                Geometry.TYPES.POINT, color);
293
        int idSymbol = lyr.addSymbol(theSymbol);
294
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
295
//        org.gvsig.fmap.geom.Geometry geom = geomManager.create(center.getX(),center.getY());
296
        Point geom = null;
297
                try {
298
                        geom = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
299
                        geom.setX(center.getX());
300
                        geom.setY(center.getY());
301
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
302
            LOG.error("Error creating a point geometry", e);
303
                }
304
//        FGraphic theGraphic = new FGraphic(geom, idSymbol);
305
//        lyr.addGraphic(theGraphic);
306
        lyr.addGraphic(geom, idSymbol);
307
        mapControl.drawGraphics();
308

    
309
    }
310

    
311

    
312
        /**
313
         * This method initializes jPanel
314
         *
315
         * @return javax.swing.JPanel
316
         */
317
        private JPanel getColorPanel() {
318
                if (colorPanel==null){
319
                         colorPanel=new ColorChooserPanel();
320
                         colorPanel.setAlpha(250);
321
                         colorPanel.setColor(CenterViewToPointExtension.COLOR);
322
                }
323
                         return colorPanel;
324
        }
325

    
326
        /**
327
         * This method initializes okCancelPanel
328
         *
329
         * @return javax.swing.JPanel
330
         */
331
        private AcceptCancelPanel getOkCancelPanel() {
332
                if (okCancelPanel == null) {
333
                        ActionListener okAction, cancelAction;
334
                        okAction = new java.awt.event.ActionListener() {
335
                            public void actionPerformed(java.awt.event.ActionEvent e) {
336
                                    try{
337
                                    zoomToCoordinates();
338
                                    }catch (Exception e1) {
339
                                            JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"formato_de_numero_incorrecto"));
340
                                            return;
341
                                    }
342
                                    // y sale.
343
                    if (PluginServices.getMainFrame() == null)
344
                        ((JDialog) (getParent().getParent().getParent().getParent())).dispose();
345
                    else
346
                        PluginServices.getMDIManager().closeWindow(InputCoordinatesPanel.this);
347
                    Preferences prefs = Preferences.userRoot().node( "gvsig.centerViewToPoint" );
348
                    if( prefs.get("showInfo", "True").equalsIgnoreCase("True")){
349
                            openInfo();
350
                    }
351
                    drawPoint(((ColorChooserPanel)getColorPanel()).getColor());
352
                            }
353
                    };
354
                    cancelAction = new ActionListener() {
355
                                public void actionPerformed(ActionEvent e) {
356
                                        closeThis();
357
                                }
358
                    };
359
                    
360
                        okCancelPanel = new AcceptCancelPanel(okAction, cancelAction);
361
                }
362
                return okCancelPanel;
363
        }
364

    
365
        /**
366
         * Close the window.
367
         *
368
         */
369
        private void closeThis() {
370
                PluginServices.getMDIManager().closeWindow(this);
371

    
372
        }
373

    
374
        public Object getWindowProfile() {
375
                return WindowInfo.DIALOG_PROFILE;
376
        }
377

    
378

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