Statistics
| Revision:

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

History | View | Annotate | Download (7.44 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
import java.awt.Image;
44 33429 cordinyana
import java.util.HashMap;
45
import java.util.Map;
46 7771 caballero
47
import javax.swing.JDialog;
48 35485 jjdelcerro
import javax.swing.JPanel;
49 7771 caballero
50 38562 jldominguez
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52
53 29596 jpiera
import org.gvsig.andami.PluginServices;
54
import org.gvsig.andami.messages.NotificationManager;
55 35485 jjdelcerro
import org.gvsig.andami.ui.mdiManager.IWindow;
56
import org.gvsig.app.ApplicationLocator;
57
import org.gvsig.app.ApplicationManager;
58 29596 jpiera
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
59 35485 jjdelcerro
import org.gvsig.fmap.geom.primitive.Point;
60 20994 jmvivo
import org.gvsig.fmap.mapcontext.layers.FLayer;
61
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
62
import org.gvsig.fmap.mapcontrol.MapControl;
63 33429 cordinyana
import org.gvsig.fmap.mapcontrol.MapControlLocator;
64
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
65 20994 jmvivo
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
66
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
67
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
68 35485 jjdelcerro
import org.gvsig.tools.ToolsLocator;
69 33429 cordinyana
import org.gvsig.tools.dynobject.DynObjectSet;
70 35485 jjdelcerro
import org.gvsig.tools.extensionpoint.ExtensionPoint;
71
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
72
import org.gvsig.tools.extensionpoint.ExtensionSingleton;
73 7771 caballero
74
/**
75 33429 cordinyana
 * Listener that looks for information at the point selected by one
76
 * click of any mouse's button, in the active layers of the associated
77
 * <code>MapControl</code>, and displays that data on a {@link FInfoDialog
78
 * FInfoDialog} dialog.
79
 *
80
 * @author -2009 Vicente Caballero Navarro
81
 * @author 2009- gvSIG Team
82 7771 caballero
 */
83
public class InfoListener implements PointListener {
84
85 33429 cordinyana
    /**
86
     * Object used to log messages for this listener.
87
     */
88 38562 jldominguez
//
89
    private static final Logger logger = LoggerFactory.getLogger(InfoListener.class);
90 7771 caballero
91 35485 jjdelcerro
    private static final String EP_INFOTOOL_NAME = "org.gvsig.app.infotool";
92
    private static final String EP_INFOTOOL_RENDERER = "renderer";
93
94
    public static void initializeExtensionPoint() {
95
        ExtensionPointManager manager = ToolsLocator.getExtensionPointManager();
96
        ExtensionPoint point = manager.add(EP_INFOTOOL_NAME, "Register of data relative to infotool");
97
        point.append(
98
            EP_INFOTOOL_RENDERER,
99
            "Renderer used to show in a panel the info by point tool.",
100
            new DefaultInfoByPointRenderer()
101
        );
102
    }
103
104 33429 cordinyana
    /**
105
     * The image to display when the cursor is active.
106
     */
107
    private final Image img = PluginServices.getIconTheme()
108 38564 jjdelcerro
        .get("cursor-info-by-point").getImage();
109 7771 caballero
110 33429 cordinyana
    /**
111
     * Reference to the <code>MapControl</code> object that uses.
112
     */
113
    private MapControl mapCtrl;
114 20098 jmvivo
115 33429 cordinyana
    /**
116
     * Radius as tolerance around the selected point, the area will be used to
117
     * look for information.
118
     */
119
    private static int TOL = 7;
120 7771 caballero
121 33429 cordinyana
    /**
122
     * <p>
123
     * Creates a new <code>InfoListener</code> object.
124
     * </p>
125
     *
126
     * @param mc
127
     *            the <code>MapControl</code> where will be applied the changes
128
     */
129
    public InfoListener(MapControl mc) {
130
        this.mapCtrl = mc;
131
    }
132 7771 caballero
133 33429 cordinyana
    public void point(PointEvent event) throws BehaviorException {
134 33657 cordinyana
        try {
135 35485 jjdelcerro
            ApplicationManager application = ApplicationLocator.getManager();
136 33657 cordinyana
            int numLayersInfoable = 0;
137 35485 jjdelcerro
            Point point = event.getMapPoint();
138
            double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
139 7771 caballero
140 33657 cordinyana
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
141
            Map<String, DynObjectSet> layer2info =
142
                new HashMap<String, DynObjectSet>(sel.length);
143 7771 caballero
144 33657 cordinyana
            for (int i = 0; i < sel.length; i++) {
145
                FLayer laCapa = sel[i];
146
                if (laCapa instanceof InfoByPoint) {
147
                    InfoByPoint layer = (InfoByPoint) laCapa;
148 35485 jjdelcerro
                    DynObjectSet info = layer.getInfo(point, tolerance);
149 33429 cordinyana
                    layer2info.put(laCapa.getName(), info);
150 7771 caballero
                    numLayersInfoable++;
151 33429 cordinyana
                }
152
            }
153 38562 jldominguez
154
            if (numLayersInfoable == 0) {
155
                return;
156
            }
157 7771 caballero
158 35485 jjdelcerro
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
159
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
160
161 38562 jldominguez
            IWindow window = renderer.getPanel();
162
            boolean first_time = (window == null);
163
            renderer.resetPanel(layer2info);
164
165
            window = renderer.getPanel();
166
            if (window == null) {
167
                logger.info("Error. Unable to create info panel.");
168
                return;
169
            }
170
171
172
            if (first_time) {
173
                PluginServices.getMDIManager().addCentredWindow(window);
174
            } else {
175
                // perhaps bring to front
176
            }
177
178 7771 caballero
179 33657 cordinyana
        } catch (Exception e) {
180
            NotificationManager.addError("Info by Point", e);
181
            e.printStackTrace();
182 33429 cordinyana
        }
183
    }
184 7771 caballero
185 33429 cordinyana
    public Image getImageCursor() {
186
        return img;
187
    }
188 7771 caballero
189 33429 cordinyana
    public boolean cancelDrawing() {
190
        return false;
191
    }
192 7771 caballero
193 33429 cordinyana
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
194
        // Nothing to do
195
    }
196 35485 jjdelcerro
197
    public interface InfoByPointRenderer {
198 38562 jldominguez
199
       public FInfoDialog getPanel();
200
       public void resetPanel(Map<String, DynObjectSet> layersInfo);
201
202 35485 jjdelcerro
    }
203
204
    public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton {
205 38562 jldominguez
206
        private FInfoDialog dlgInfo = null;
207
208
        public FInfoDialog getPanel() {
209
            return dlgInfo;
210
        }
211
212
213
        public void resetPanel(Map<String, DynObjectSet> layersInfo) {
214 35485 jjdelcerro
            // TODO: set the writable parameter to true to activate
215
            // edition of the info by point information.
216
            LayersDynObjectSetComponent infoComponent =
217
                MapControlLocator.getMapControlManager()
218
                    .createLayersDynObjectSetComponent(layersInfo, false);
219 38562 jldominguez
220
            if (dlgInfo == null) {
221
                dlgInfo = new FInfoDialog(infoComponent);
222
            } else {
223
                dlgInfo.setInfo(infoComponent);
224
            }
225 35485 jjdelcerro
        }
226
227
    }
228
229
230 33429 cordinyana
}