Revision 127

View differences:

org.gvsig.educa.batovi/trunk/org.gvsig.educa.batovi.mapviewer/org.gvsig.educa.batovi.mapviewer/src/main/java/org/gvsig/educa/batovi/mapviewer/tools/InfoListener.java
1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22
package org.gvsig.educa.batovi.mapviewer.tools;
23

  
24
import java.awt.Dimension;
25
import java.awt.Image;
26
import java.awt.Toolkit;
27
import java.util.HashMap;
28
import java.util.Map;
29

  
30
import javax.swing.ImageIcon;
31
import javax.swing.JDialog;
32
import javax.swing.JOptionPane;
33

  
34
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.fmap.mapcontext.layers.FLayer;
36
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
37
import org.gvsig.fmap.mapcontrol.MapControl;
38
import org.gvsig.fmap.mapcontrol.MapControlLocator;
39
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
40
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
41
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
42
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
43
import org.gvsig.tools.dynobject.DynObjectSet;
44

  
45
/**
46
 * @author gvSIG Team
47
 * @version $Id$
48
 * 
49
 */
50
public class InfoListener implements PointListener {
51

  
52
    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory
53
        .getLogger(InfoListener.class);
54

  
55
    /**
56
     * Reference to the <code>MapControl</code> object that uses.
57
     */
58
    private final MapControl mapCtrl;
59

  
60
    private final Dimension windowSize;
61

  
62
    // FIXME
63
    private final Image info = new ImageIcon(MapControl.class.getClassLoader()
64
        .getResource("org/gvsig/fmap/mapcontrol/images/InfoCursor.gif"))
65
        .getImage();
66
    /**
67
     * Radius as tolerance around the selected point, the area will be used to
68
     * look for information.
69
     */
70
    private static int TOL = 7;
71

  
72
    /**
73
     * <p>
74
     * Creates a new <code>InfoListener</code> object.
75
     * </p>
76
     * 
77
     * @param mc
78
     *            the <code>MapControl</code> where will be applied the changes
79
     */
80
    public InfoListener(MapControl mc) {
81
        this.mapCtrl = mc;
82
        // Gets screen size
83
        Toolkit toolkit = Toolkit.getDefaultToolkit();
84
        Dimension sSize = toolkit.getScreenSize();
85

  
86
        // Subtract a 10%
87
        windowSize =
88
            new Dimension(sSize.width - (sSize.width / 10), sSize.height
89
                - (sSize.height / 10));
90

  
91
    }
92

  
93
    public Image getImageCursor() {
94
        return info;
95
    }
96

  
97
    public boolean cancelDrawing() {
98
        // TODO Auto-generated method stub
99
        return false;
100
    }
101

  
102
    public void point(PointEvent event) throws BehaviorException {
103
        try {
104
            int numLayersInfoable = 0;
105
            Point point = event.getMapPoint();
106
            double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
107

  
108
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
109
            if (sel.length == 0) {
110
                JOptionPane.showMessageDialog(null,
111
                    "Select a layer to get info", "Information tool",
112
                    JOptionPane.INFORMATION_MESSAGE);
113
            }
114
            Map<String, DynObjectSet> layer2info =
115
                new HashMap<String, DynObjectSet>(sel.length);
116

  
117
            for (int i = 0; i < sel.length; i++) {
118
                FLayer laCapa = sel[i];
119
                if (laCapa instanceof InfoByPoint) {
120
                    InfoByPoint layer = (InfoByPoint) laCapa;
121
                    DynObjectSet info = layer.getInfo(point, tolerance);
122
                    layer2info.put(laCapa.getName(), info);
123
                    numLayersInfoable++;
124
                }
125
            }
126

  
127
            if (numLayersInfoable > 0) {
128
                LayersDynObjectSetComponent infoComponent =
129
                    MapControlLocator.getMapControlManager()
130
                        .createLayersDynObjectSetComponent(layer2info, false);
131
                JDialog dialog = new JDialog();
132
                dialog.getContentPane().add(infoComponent.asJComponent());
133
                dialog.setModal(true);
134
                dialog.pack();
135
                dialog.setSize(windowSize);
136
                dialog.setVisible(true);
137
            }
138
        } catch (Exception ex) {
139
            LOG.error("Exception in infoByPoint", ex);
140
        }
141

  
142
    }
143

  
144
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
145
        // TODO Auto-generated method stub
146

  
147
    }
148

  
149
}
org.gvsig.educa.batovi/trunk/org.gvsig.educa.batovi.mapviewer/org.gvsig.educa.batovi.mapviewer/src/main/java/org/gvsig/educa/batovi/mapviewer/MapToolsRegistrant.java
22 22
package org.gvsig.educa.batovi.mapviewer;
23 23

  
24 24
import org.gvsig.educa.batovi.mapviewer.tools.AreaListener;
25
import org.gvsig.educa.batovi.mapviewer.tools.InfoListener;
25 26
import org.gvsig.educa.batovi.mapviewer.tools.MeasureListener;
26 27
import org.gvsig.educa.batovi.mapviewer.tools.StatusBarBehavior;
27 28
import org.gvsig.educa.thematicmap.swing.viewer.MapControlToolRegistrant;
......
52 53

  
53 54
    public static final String TOOL_AREA_ID = "area";
54 55

  
56
    public static final String TOOL_INFO_ID = "info";
57

  
55 58
    private final StatusBarBehavior statusBarBehavior = new StatusBarBehavior();
56 59
    private final MouseWheelBehavior mouseWheelBehavior =
57 60
        new MouseWheelBehavior();
......
62 65

  
63 66
    public void registerTools(MapControl mapControl) {
64 67
        // Adds tools to mapControl
65
        // mouseWhell
68
        // mouseWhell and statusBar
66 69
        mapControl.addCombinedBehavior(compoundBehavior);
70

  
67 71
        // zoom
68 72
        mapControl.addBehavior(TOOL_ZOOM_ID, new Behavior[] {
69 73
            new RectangleBehavior(new ZoomInListenerImpl(mapControl)),
......
80 84
        mapControl.addBehavior(TOOL_AREA_ID, new PolylineBehavior(
81 85
            new AreaListener(mapControl, statusBar)));
82 86

  
87
        // info
88
        mapControl.addBehavior(TOOL_INFO_ID, new PointBehavior(
89
            new InfoListener(mapControl)));
83 90
        mapControl.setTool(TOOL_PAN_ID);
84 91
    }
85 92

  
org.gvsig.educa.batovi/trunk/org.gvsig.educa.batovi.mapviewer/org.gvsig.educa.batovi.mapviewer/src/main/java/org/gvsig/educa/batovi/mapviewer/Main.java
35 35
import javax.swing.JButton;
36 36
import javax.swing.JFileChooser;
37 37
import javax.swing.JFrame;
38
import javax.swing.JMenu;
39
import javax.swing.JMenuBar;
40
import javax.swing.JMenuItem;
41 38
import javax.swing.JToolBar;
42 39
import javax.swing.SwingUtilities;
43 40
import javax.swing.UIManager;
......
121 118

  
122 119
    private AbstractAction setAreaToolAction;
123 120

  
121
    private AbstractAction setInfoToolAction;
122

  
124 123
    public static void main(String args[]) {
125 124
        new DefaultLibrariesInitializer().fullInitialize();
126 125

  
......
251 250
        setMeasuerToolAction = new AbstractAction("Tool measure") {
252 251

  
253 252
            public void actionPerformed(ActionEvent e) {
254
                setMeasuerToolAction();
253
                setMeasuerTool();
255 254
            }
256 255
        };
257 256
        setMeasuerToolAction.setEnabled(false);
......
259 258
        setAreaToolAction = new AbstractAction("Tool area") {
260 259

  
261 260
            public void actionPerformed(ActionEvent e) {
262
                setAreaToolAction();
261
                setAreaTool();
263 262
            }
264 263
        };
265 264
        setAreaToolAction.setEnabled(false);
266 265

  
266
        setInfoToolAction = new AbstractAction("Tool Info") {
267

  
268
            public void actionPerformed(ActionEvent e) {
269
                setInfoTool();
270
            }
271
        };
272
        setInfoToolAction.setEnabled(false);
273

  
267 274
        closeThematicMapAction = new AbstractAction("Close Map") {
268 275

  
269 276
            public void actionPerformed(ActionEvent e) {
......
335 342
    }
336 343

  
337 344
    /**
338
     * Creates the menu bar
339
     */
340
    private void createMenu() {
341
        // Create the menu bar.
342
        JMenuBar menuBar = new JMenuBar();
343

  
344
        // Build the menu.
345
        JMenu menuFile = new JMenu("File");
346
        menuFile.add(new JMenuItem(setThematiMapFolderAction));
347
        menuFile.addSeparator();
348
        menuFile.add(new JMenuItem(showOpenThematicMapDialogAction));
349
        menuFile.add(new JMenuItem(closeThematicMapAction));
350
        menuFile.addSeparator();
351
        menuFile.add(new JMenuItem(createCompilationAction));
352
        menuFile.add(new JMenuItem(createCompilationFromMapAction));
353
        menuFile.addSeparator();
354
        menuFile.add(new JMenuItem(addInstallUrl));
355
        menuFile.add(new JMenuItem(installThematicMaps));
356
        menuFile.addSeparator();
357
        menuFile.add(new JMenuItem(exitAction));
358

  
359
        JMenu menuViewer = new JMenu("Viewer");
360
        menuViewer.add(setPanToolAction);
361
        menuViewer.add(setZoomToolAction);
362
        menuViewer.addSeparator();
363
        menuViewer.add(zoomAllAction);
364

  
365
        menuBar.add(menuFile);
366
        menuBar.add(menuViewer);
367

  
368
        mainFrame.setJMenuBar(menuBar);
369
    }
370

  
371
    /**
372 345
     * Creates the tools bar
373 346
     */
374 347
    private void createToolBar() {
......
385 358
        toolBar.add(usabManager.createJButton(setZoomToolAction));
386 359
        toolBar.add(usabManager.createJButton(setMeasuerToolAction));
387 360
        toolBar.add(usabManager.createJButton(setAreaToolAction));
361
        toolBar.add(usabManager.createJButton(setInfoToolAction));
388 362

  
389 363
        toolBar.addSeparator(sepSize);
390 364
        toolBar.add(usabManager.createJButton(zoomAllAction));
......
401 375
     * Creates the tools bar
402 376
     */
403 377
    private void createStatusBar() {
404
        // TODO look at gvSIG status bar
405
        Dimension sepSize = new Dimension(20, 5);
406 378
        statusBar = new StatusBar();
407 379
        mapToolsRegistrant.setStatusBar(statusBar);
408 380
        mainFrame.add(statusBar, BorderLayout.PAGE_END);
......
532 504
        setPanToolAction.setEnabled(curViewer != null);
533 505
        setMeasuerToolAction.setEnabled(curViewer != null);
534 506
        setAreaToolAction.setEnabled(curViewer != null);
507
        setInfoToolAction.setEnabled(curViewer != null);
535 508
        showHideTocAction.setEnabled(curViewer != null);
536 509
        closeThematicMapAction.setEnabled(curViewer != null);
537 510
        zoomAllAction.setEnabled(curViewer != null);
......
569 542
    }
570 543

  
571 544
    /**
545
     * Sets the pan tool
546
     */
547
    public void setInfoTool() {
548
        if (curViewer != null) {
549
            curViewer.setTool(MapToolsRegistrant.TOOL_INFO_ID);
550
            statusBar.setInfo("");
551
        }
552
    }
553

  
554
    /**
572 555
     * Zoom to all thematic map contents
573 556
     */
574 557
    public void zoomAll() {
......
601 584
        System.exit(0);
602 585
    }
603 586

  
604
    public void setMeasuerToolAction() {
587
    public void setMeasuerTool() {
605 588
        if (curViewer == null) {
606 589
            return;
607 590
        }
......
609 592
        statusBar.setInfo("");
610 593
    }
611 594

  
612
    public void setAreaToolAction() {
595
    public void setAreaTool() {
613 596
        if (curViewer == null) {
614 597
            return;
615 598
        }

Also available in: Unified diff