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
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
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
 *   Av. Blasco Ib��ez, 50
24
 *   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
package org.gvsig.app.project.documents.view.toolListeners;
42

    
43
import java.awt.GridBagConstraints;
44
import java.awt.Image;
45
import java.util.HashMap;
46
import java.util.Map;
47

    
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import org.gvsig.andami.PluginServices;
52
import org.gvsig.andami.messages.NotificationManager;
53
import org.gvsig.andami.ui.mdiManager.IWindow;
54
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
55
import org.gvsig.app.ApplicationLocator;
56
import org.gvsig.app.ApplicationManager;
57
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
58
import org.gvsig.fmap.geom.primitive.Point;
59
import org.gvsig.fmap.mapcontext.layers.FLayer;
60
import org.gvsig.fmap.mapcontext.layers.operations.InfoByPoint;
61
import org.gvsig.fmap.mapcontrol.MapControl;
62
import org.gvsig.fmap.mapcontrol.MapControlLocator;
63
import org.gvsig.fmap.mapcontrol.swing.dynobject.LayersDynObjectSetComponent;
64
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
import org.gvsig.tools.ToolsLocator;
68
import org.gvsig.tools.dynobject.DynObjectSet;
69
import org.gvsig.tools.extensionpoint.ExtensionPoint;
70
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
71
import org.gvsig.tools.extensionpoint.ExtensionSingleton;
72

    
73
/**
74
 * 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
 */
82
public class InfoListener implements PointListener {
83

    
84
    /**
85
     * Object used to log messages for this listener.
86
     */
87
//    
88
    private static final Logger logger = LoggerFactory.getLogger(InfoListener.class);
89

    
90
    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
    /**
104
     * The image to display when the cursor is active.
105
     */
106
    private final Image img = PluginServices.getIconTheme()
107
        .get("cursor-info-by-point").getImage();
108

    
109
    /**
110
     * Reference to the <code>MapControl</code> object that uses.
111
     */
112
    private MapControl mapCtrl;
113

    
114
    /**
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

    
120
    /**
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

    
132
    public void point(PointEvent event) throws BehaviorException {
133
        try {
134
            ApplicationManager application = ApplicationLocator.getManager();
135
            int numLayersInfoable = 0;
136
            Point point = event.getMapPoint();
137
            double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
138

    
139
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
140
            Map<String, DynObjectSet> layer2info =
141
                new HashMap<String, DynObjectSet>(sel.length);
142

    
143
            for (int i = 0; i < sel.length; i++) {
144
                FLayer laCapa = sel[i];
145
                if (laCapa instanceof InfoByPoint) {
146
                    InfoByPoint layer = (InfoByPoint) laCapa;
147
                    DynObjectSet info = layer.getInfo(point, tolerance);
148
                    layer2info.put(laCapa.getName(), info);
149
                    numLayersInfoable++;
150
                }
151
            }
152
            
153
            if (numLayersInfoable == 0) {
154
                return;
155
            }
156

    
157
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
158
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
159
            
160
            IWindow window = renderer.getPanel(layer2info);
161

    
162
            if (window == null) {
163
                logger.info("Error. Unable to create info panel.");
164
                return;
165
            }
166

    
167
            PluginServices.getMDIManager().addWindow(window, GridBagConstraints.LAST_LINE_END);
168

    
169
        } catch (Exception e) {
170
            NotificationManager.addError("Info by Point", e);
171
            e.printStackTrace();
172
        }
173
    }
174

    
175
    /**
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
    public Image getImageCursor() {
191
        return img;
192
    }
193

    
194
    public boolean cancelDrawing() {
195
        return false;
196
    }
197

    
198
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
199
        // Nothing to do
200
    }
201
    
202
    public interface InfoByPointRenderer {
203
        
204
       public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo);
205
       
206
    }
207
    
208
    public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton {
209
        
210
        private FInfoDialog dlgInfo = null;
211
        
212
        public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo) {
213
            // 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
            
219
            if (dlgInfo == null) {
220
                dlgInfo = new FInfoDialog(infoComponent);
221
            } else {
222
                dlgInfo.setInfo(infoComponent);
223
            }
224
            return dlgInfo;
225
        }
226
       
227
    }
228
    
229
    
230
}