Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / InfoListener.java @ 38605

History | View | Annotate | Download (7.53 KB)

1 14821 jmvivo
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2 7771 caballero
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23 14821 jmvivo
 *   Av. Blasco Ib��ez, 50
24 7771 caballero
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41 29596 jpiera
package org.gvsig.app.project.documents.view.toolListeners;
42 7771 caballero
43 38605 jldominguez
import java.awt.GridBagConstraints;
44 7771 caballero
import java.awt.Image;
45 33429 cordinyana
import java.util.HashMap;
46
import java.util.Map;
47 7771 caballero
48 38562 jldominguez
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50
51 29596 jpiera
import org.gvsig.andami.PluginServices;
52
import org.gvsig.andami.messages.NotificationManager;
53 35485 jjdelcerro
import org.gvsig.andami.ui.mdiManager.IWindow;
54 38605 jldominguez
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
55 35485 jjdelcerro
import org.gvsig.app.ApplicationLocator;
56
import org.gvsig.app.ApplicationManager;
57 29596 jpiera
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
58 35485 jjdelcerro
import org.gvsig.fmap.geom.primitive.Point;
59 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
61
import org.gvsig.fmap.mapcontrol.MapControl;
62 33429 cordinyana
import org.gvsig.fmap.mapcontrol.MapControlLocator;
63
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
64 20994 jmvivo
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
65
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
66
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
67 35485 jjdelcerro
import org.gvsig.tools.ToolsLocator;
68 33429 cordinyana
import org.gvsig.tools.dynobject.DynObjectSet;
69 35485 jjdelcerro
import org.gvsig.tools.extensionpoint.ExtensionPoint;
70
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
71
import org.gvsig.tools.extensionpoint.ExtensionSingleton;
72 7771 caballero
73
/**
74 33429 cordinyana
 * Listener that looks for information at the point selected by one
75
 * click of any mouse's button, in the active layers of the associated
76
 * <code>MapControl</code>, and displays that data on a {@link FInfoDialog
77
 * FInfoDialog} dialog.
78
 *
79
 * @author -2009 Vicente Caballero Navarro
80
 * @author 2009- gvSIG Team
81 7771 caballero
 */
82
public class InfoListener implements PointListener {
83
84 33429 cordinyana
    /**
85
     * Object used to log messages for this listener.
86
     */
87 38562 jldominguez
//
88
    private static final Logger logger = LoggerFactory.getLogger(InfoListener.class);
89 7771 caballero
90 35485 jjdelcerro
    private static final String EP_INFOTOOL_NAME = "org.gvsig.app.infotool";
91
    private static final String EP_INFOTOOL_RENDERER = "renderer";
92
93
    public static void initializeExtensionPoint() {
94
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
95
        ExtensionPoint point = manager.add(EP_INFOTOOL_NAME, "Register of data relative to infotool");
96
        point.append(
97
            EP_INFOTOOL_RENDERER,
98
            "Renderer used to show in a panel the info by point tool.",
99
            new DefaultInfoByPointRenderer()
100
        );
101
    }
102
103 33429 cordinyana
    /**
104
     * The image to display when the cursor is active.
105
     */
106
    private final Image img = PluginServices.getIconTheme()
107 38564 jjdelcerro
        .get("cursor-info-by-point").getImage();
108 7771 caballero
109 33429 cordinyana
    /**
110
     * Reference to the <code>MapControl</code> object that uses.
111
     */
112
    private MapControl mapCtrl;
113 20098 jmvivo
114 33429 cordinyana
    /**
115
     * Radius as tolerance around the selected point, the area will be used to
116
     * look for information.
117
     */
118
    private static int TOL = 7;
119 7771 caballero
120 33429 cordinyana
    /**
121
     * <p>
122
     * Creates a new <code>InfoListener</code> object.
123
     * </p>
124
     *
125
     * @param mc
126
     *            the <code>MapControl</code> where will be applied the changes
127
     */
128
    public InfoListener(MapControl mc) {
129
        this.mapCtrl = mc;
130
    }
131 7771 caballero
132 33429 cordinyana
    public void point(PointEvent event) throws BehaviorException {
133 33657 cordinyana
        try {
134 35485 jjdelcerro
            ApplicationManager application = ApplicationLocator.getManager();
135 33657 cordinyana
            int numLayersInfoable = 0;
136 35485 jjdelcerro
            Point point = event.getMapPoint();
137
            double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
138 7771 caballero
139 33657 cordinyana
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
140
            Map<String, DynObjectSet> layer2info =
141
                new HashMap<String, DynObjectSet>(sel.length);
142 7771 caballero
143 33657 cordinyana
            for (int i = 0; i < sel.length; i++) {
144
                FLayer laCapa = sel[i];
145
                if (laCapa instanceof InfoByPoint) {
146
                    InfoByPoint layer = (InfoByPoint) laCapa;
147 35485 jjdelcerro
                    DynObjectSet info = layer.getInfo(point, tolerance);
148 33429 cordinyana
                    layer2info.put(laCapa.getName(), info);
149 7771 caballero
                    numLayersInfoable++;
150 33429 cordinyana
                }
151
            }
152 38562 jldominguez
153
            if (numLayersInfoable == 0) {
154
                return;
155
            }
156 7771 caballero
157 35485 jjdelcerro
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
158
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
159
160 38605 jldominguez
            IWindow window = renderer.getPanel(layer2info);
161
162 38562 jldominguez
            if (window == null) {
163
                logger.info("Error. Unable to create info panel.");
164
                return;
165
            }
166 7771 caballero
167 38605 jldominguez
            PluginServices.getMDIManager().addWindow(window, GridBagConstraints.LAST_LINE_END);
168
169 33657 cordinyana
        } catch (Exception e) {
170
            NotificationManager.addError("Info by Point", e);
171
            e.printStackTrace();
172 33429 cordinyana
        }
173
    }
174 7771 caballero
175 38605 jldominguez
    /**
176
     * @param window
177
     * @return whether the window is currently one the app windows
178
     */
179
    private boolean isCurrentlyAdded(IWindow iw) {
180
181
        IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
182
        for (int i=0; i<iws.length; i++) {
183
            if (iws[i] == iw) {
184
                return true;
185
            }
186
        }
187
        return false;
188
    }
189
190 33429 cordinyana
    public Image getImageCursor() {
191
        return img;
192
    }
193 7771 caballero
194 33429 cordinyana
    public boolean cancelDrawing() {
195
        return false;
196
    }
197 7771 caballero
198 33429 cordinyana
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
199
        // Nothing to do
200
    }
201 35485 jjdelcerro
202
    public interface InfoByPointRenderer {
203 38562 jldominguez
204 38605 jldominguez
       public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo);
205 38562 jldominguez
206 35485 jjdelcerro
    }
207
208
    public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton {
209 38562 jldominguez
210
        private FInfoDialog dlgInfo = null;
211
212 38605 jldominguez
        public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo) {
213 35485 jjdelcerro
            // TODO: set the writable parameter to true to activate
214
            // edition of the info by point information.
215
            LayersDynObjectSetComponent infoComponent =
216
                MapControlLocator.getMapControlManager()
217
                    .createLayersDynObjectSetComponent(layersInfo, false);
218 38562 jldominguez
219
            if (dlgInfo == null) {
220
                dlgInfo = new FInfoDialog(infoComponent);
221
            } else {
222
                dlgInfo.setInfo(infoComponent);
223
            }
224 38605 jldominguez
            return dlgInfo;
225 35485 jjdelcerro
        }
226
227
    }
228
229
230 33429 cordinyana
}