Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toolListeners / InfoListener.java @ 43979

History | View | Annotate | Download (7.76 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5 40435 jjdelcerro
 *
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 40558 jjdelcerro
 * as published by the Free Software Foundation; either version 3
9 40435 jjdelcerro
 * 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 40558 jjdelcerro
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20 40435 jjdelcerro
 *
21 40558 jjdelcerro
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 40435 jjdelcerro
 */
24
package org.gvsig.app.project.documents.view.toolListeners;
25
26
import java.awt.GridBagConstraints;
27
import java.awt.Image;
28
import java.util.HashMap;
29
import java.util.Map;
30
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33
import org.gvsig.andami.PluginServices;
34
import org.gvsig.andami.messages.NotificationManager;
35
import org.gvsig.andami.ui.mdiManager.IWindow;
36
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
37
import org.gvsig.app.ApplicationLocator;
38
import org.gvsig.app.ApplicationManager;
39
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
40
import org.gvsig.fmap.geom.primitive.Point;
41 40954 jjdelcerro
import org.gvsig.fmap.mapcontext.MapContext;
42 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
43
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
44
import org.gvsig.fmap.mapcontrol.MapControl;
45
import org.gvsig.fmap.mapcontrol.MapControlLocator;
46
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
47
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
48
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
49
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.dynobject.DynObjectSet;
52
import org.gvsig.tools.extensionpoint.ExtensionPoint;
53
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
54
import org.gvsig.tools.extensionpoint.ExtensionSingleton;
55
56
/**
57
 * Listener that looks for information at the point selected by one
58
 * click of any mouse's button, in the active layers of the associated
59
 * <code>MapControl</code>, and displays that data on a {@link FInfoDialog
60
 * FInfoDialog} dialog.
61
 *
62
 * @author -2009 Vicente Caballero Navarro
63
 * @author 2009- gvSIG Team
64
 */
65
public class InfoListener implements PointListener {
66
67
    /**
68
     * Object used to log messages for this listener.
69
     */
70
//
71
    private static final Logger logger = LoggerFactory.getLogger(InfoListener.class);
72
73
    private static final String EP_INFOTOOL_NAME = "org.gvsig.app.infotool";
74
    private static final String EP_INFOTOOL_RENDERER = "renderer";
75
76
    public static void initializeExtensionPoint() {
77
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
78
        ExtensionPoint point = manager.add(EP_INFOTOOL_NAME, "Register of data relative to infotool");
79
        point.append(
80
            EP_INFOTOOL_RENDERER,
81
            "Renderer used to show in a panel the info by point tool.",
82
            new DefaultInfoByPointRenderer()
83
        );
84
    }
85
86
    /**
87
     * The image to display when the cursor is active.
88
     */
89
    private final Image img = PluginServices.getIconTheme()
90
        .get("cursor-info-by-point").getImage();
91
92
    /**
93
     * Reference to the <code>MapControl</code> object that uses.
94
     */
95
    private MapControl mapCtrl;
96
97
    /**
98
     * Radius as tolerance around the selected point, the area will be used to
99
     * look for information.
100
     */
101
    private static int TOL = 7;
102
103
    /**
104
     * <p>
105
     * Creates a new <code>InfoListener</code> object.
106
     * </p>
107
     *
108
     * @param mc
109
     *            the <code>MapControl</code> where will be applied the changes
110
     */
111
    public InfoListener(MapControl mc) {
112
        this.mapCtrl = mc;
113
    }
114
115
    public void point(PointEvent event) throws BehaviorException {
116
        try {
117
            ApplicationManager application = ApplicationLocator.getManager();
118
            int numLayersInfoable = 0;
119
            Point point = event.getMapPoint();
120
121
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
122
            Map<String, DynObjectSet> layer2info =
123
                new HashMap<String, DynObjectSet>(sel.length);
124
125
            for (int i = 0; i < sel.length; i++) {
126
                FLayer laCapa = sel[i];
127 43979 omartinez
128 40435 jjdelcerro
                if (laCapa instanceof InfoByPoint) {
129
                    InfoByPoint layer = (InfoByPoint) laCapa;
130 43979 omartinez
                    int layerTolerance = laCapa.getDefaultTolerance();
131
                    double tolerance = mapCtrl.getViewPort().toMapDistance(layerTolerance);
132 40435 jjdelcerro
                    DynObjectSet info = layer.getInfo(point, tolerance);
133
                    layer2info.put(laCapa.getName(), info);
134
                    numLayersInfoable++;
135
                }
136
            }
137
138
            if (numLayersInfoable == 0) {
139
                return;
140
            }
141
142
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
143
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
144 40954 jjdelcerro
            if( renderer instanceof DefaultInfoByPointRenderer ) {
145
                    ((DefaultInfoByPointRenderer)renderer).setMapContext(this.mapCtrl.getMapContext());
146
            }
147 40435 jjdelcerro
            IWindow window = renderer.getPanel(layer2info);
148
149
            if (window == null) {
150
                logger.info("Error. Unable to create info panel.");
151
                return;
152
            }
153
154
            PluginServices.getMDIManager().addWindow(window, GridBagConstraints.LAST_LINE_END);
155
156
        } catch (Exception e) {
157
            NotificationManager.addError("Info by Point", e);
158
            e.printStackTrace();
159
        }
160
    }
161
162
    /**
163
     * @param window
164
     * @return whether the window is currently one the app windows
165
     */
166
    private boolean isCurrentlyAdded(IWindow iw) {
167
168
        IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
169
        for (int i=0; i<iws.length; i++) {
170
            if (iws[i] == iw) {
171
                return true;
172
            }
173
        }
174
        return false;
175
    }
176
177
    public Image getImageCursor() {
178
        return img;
179
    }
180
181
    public boolean cancelDrawing() {
182
        return false;
183
    }
184
185
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
186
        // Nothing to do
187
    }
188
189
    public interface InfoByPointRenderer {
190
191
       public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo);
192
193
    }
194
195
    public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton {
196
197
        private FInfoDialog dlgInfo = null;
198 40954 jjdelcerro
                private MapContext mapContext;
199 40435 jjdelcerro
200 40954 jjdelcerro
        public void setMapContext(MapContext mapContext) {
201
                this.mapContext = mapContext;
202
        }
203
204 40435 jjdelcerro
        public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo) {
205
            // TODO: set the writable parameter to true to activate
206
            // edition of the info by point information.
207
            LayersDynObjectSetComponent infoComponent =
208
                MapControlLocator.getMapControlManager()
209
                    .createLayersDynObjectSetComponent(layersInfo, false);
210 40954 jjdelcerro
            infoComponent.setMapContext(mapContext);
211 40435 jjdelcerro
            if (dlgInfo == null) {
212
                dlgInfo = new FInfoDialog(infoComponent);
213
            } else {
214
                dlgInfo.setInfo(infoComponent);
215
            }
216
            return dlgInfo;
217
        }
218
219
    }
220
221
222
}