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

History | View | Annotate | Download (7.27 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

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.andami.messages.NotificationManager;
36
import org.gvsig.andami.ui.mdiManager.IWindow;
37
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
38
import org.gvsig.app.ApplicationLocator;
39
import org.gvsig.app.ApplicationManager;
40
import org.gvsig.app.project.documents.view.info.gui.FInfoDialog;
41
import org.gvsig.fmap.geom.primitive.Point;
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
     * 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
            double tolerance = mapCtrl.getViewPort().toMapDistance(TOL);
121

    
122
            FLayer[] sel = mapCtrl.getMapContext().getLayers().getActives();
123
            Map<String, DynObjectSet> layer2info =
124
                new HashMap<String, DynObjectSet>(sel.length);
125

    
126
            for (int i = 0; i < sel.length; i++) {
127
                FLayer laCapa = sel[i];
128
                if (laCapa instanceof InfoByPoint) {
129
                    InfoByPoint layer = (InfoByPoint) laCapa;
130
                    DynObjectSet info = layer.getInfo(point, tolerance);
131
                    layer2info.put(laCapa.getName(), info);
132
                    numLayersInfoable++;
133
                }
134
            }
135
            
136
            if (numLayersInfoable == 0) {
137
                return;
138
            }
139

    
140
            ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
141
            InfoByPointRenderer renderer = (InfoByPointRenderer)extensionPoints.get(EP_INFOTOOL_NAME).create(EP_INFOTOOL_RENDERER);
142
            
143
            IWindow window = renderer.getPanel(layer2info);
144

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

    
150
            PluginServices.getMDIManager().addWindow(window, GridBagConstraints.LAST_LINE_END);
151

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

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

    
173
    public Image getImageCursor() {
174
        return img;
175
    }
176

    
177
    public boolean cancelDrawing() {
178
        return false;
179
    }
180

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