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 @ 44050

History | View | Annotate | Download (7.61 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
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 3
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
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
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
import org.gvsig.fmap.mapcontext.MapContext;
42
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
     * <p>
99
     * Creates a new <code>InfoListener</code> object.
100
     * </p>
101
     * 
102
     * @param mc
103
     *            the <code>MapControl</code> where will be applied the changes
104
     */
105
    public InfoListener(MapControl mc) {
106
        this.mapCtrl = mc;
107
    }
108

    
109
    public void point(PointEvent event) throws BehaviorException {
110
        try {
111
            ApplicationManager application = ApplicationLocator.getManager();
112
            int numLayersInfoable = 0;
113
            Point point = event.getMapPoint();
114

    
115
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
116
            Map<String, DynObjectSet> layer2info =
117
                new HashMap<String, DynObjectSet>(sel.length);
118

    
119
            for (int i = 0; i < sel.length; i++) {
120
                FLayer laCapa = sel[i];
121
                
122
                if (laCapa instanceof InfoByPoint) {
123
                    InfoByPoint layer = (InfoByPoint) laCapa;
124
                    int layerTolerance = laCapa.getDefaultTolerance();
125
                    double tolerance = mapCtrl.getViewPort().toMapDistance(layerTolerance);
126
                    DynObjectSet info = layer.getInfo(point, tolerance);
127
                    layer2info.put(laCapa.getName(), info);
128
                    numLayersInfoable++;
129
                }
130
            }
131
            
132
            if (numLayersInfoable == 0) {
133
                return;
134
            }
135

    
136
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
137
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
138
            if( renderer instanceof DefaultInfoByPointRenderer ) {
139
                    ((DefaultInfoByPointRenderer)renderer).setMapContext(this.mapCtrl.getMapContext());
140
            }
141
            IWindow window = renderer.getPanel(layer2info);
142

    
143
            if (window == null) {
144
                logger.info("Error. Unable to create info panel.");
145
                return;
146
            }
147

    
148
            PluginServices.getMDIManager().addWindow(window, GridBagConstraints.LAST_LINE_END);
149

    
150
        } catch (Exception e) {
151
            NotificationManager.addError("Info by Point", e);
152
            e.printStackTrace();
153
        }
154
    }
155

    
156
    /**
157
     * @param window
158
     * @return whether the window is currently one the app windows
159
     */
160
    private boolean isCurrentlyAdded(IWindow iw) {
161
        
162
        IWindow[] iws = PluginServices.getMDIManager().getAllWindows();
163
        for (int i=0; i<iws.length; i++) {
164
            if (iws[i] == iw) {
165
                return true;
166
            }
167
        }
168
        return false;
169
    }
170

    
171
    public Image getImageCursor() {
172
        return img;
173
    }
174

    
175
    public boolean cancelDrawing() {
176
        return false;
177
    }
178

    
179
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
180
        // Nothing to do
181
    }
182
    
183
    public interface InfoByPointRenderer {
184
        
185
       public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo);
186
       
187
    }
188
    
189
    public static class DefaultInfoByPointRenderer implements InfoByPointRenderer, ExtensionSingleton {
190
        
191
        private FInfoDialog dlgInfo = null;
192
                private MapContext mapContext;
193
        
194
        public void setMapContext(MapContext mapContext) {
195
                this.mapContext = mapContext;
196
        }
197
        
198
        public SingletonWindow getPanel(Map<String, DynObjectSet> layersInfo) {
199
            // TODO: set the writable parameter to true to activate
200
            // edition of the info by point information.
201
            LayersDynObjectSetComponent infoComponent =
202
                MapControlLocator.getMapControlManager()
203
                    .createLayersDynObjectSetComponent(layersInfo, false);
204
            infoComponent.setMapContext(mapContext);
205
            if (dlgInfo == null) {
206
                dlgInfo = new FInfoDialog(infoComponent);
207
            } else {
208
                dlgInfo.setInfo(infoComponent);
209
            }
210
            return dlgInfo;
211
        }
212
       
213
    }
214
    
215
    
216
}