Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2020 / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / LinkListener.java @ 33915

History | View | Annotate | Download (9.05 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
package org.gvsig.hyperlink.app.extension;
24

    
25
import java.awt.Cursor;
26
import java.awt.Image;
27
import java.awt.Point;
28
import java.awt.Toolkit;
29
import java.awt.geom.Point2D;
30
import java.net.URI;
31
import java.net.URL;
32

    
33
import javax.swing.ImageIcon;
34

    
35
import org.gvsig.andami.PluginServices;
36
import org.gvsig.app.project.documents.view.toolListeners.InfoListener;
37
import org.gvsig.fmap.mapcontext.layers.FLayer;
38
import org.gvsig.fmap.mapcontrol.MapControl;
39
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
40
import org.gvsig.fmap.mapcontrol.tools.Events.PointEvent;
41
import org.gvsig.fmap.mapcontrol.tools.Listeners.PointListener;
42
import org.gvsig.hyperlink.app.extension.config.LayerLinkConfig;
43
import org.gvsig.hyperlink.app.extension.config.LinkConfig;
44
import org.gvsig.hyperlink.app.extension.layers.ILinkLayerManager;
45
import org.gvsig.hyperlink.app.extension.layers.IncompatibleLayerException;
46
import org.gvsig.hyperlink.app.extension.layers.ManagerRegistry;
47
import org.gvsig.tools.ToolsLocator;
48
import org.gvsig.tools.extensionpoint.ExtensionPoint;
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
/**
53
 * Listener that implements PointListener to show the information of the
54
 * HyperLink of the layer,
55
 * if the actual layer allows it. When the user selects the tool and make one
56
 * clic with
57
 * the mouse throws an event to create Panels with the information of the
58
 * associated HyperLink
59
 * to the layer. Creates one panel for URI provided, to load the information.
60
 * 
61
 * @author Vicente Caballero Navarro
62
 * @author Cesar Martinez Izqueirdo
63
 * 
64
 */
65
public class LinkListener implements PointListener {
66

    
67
    private static Logger logger = LoggerFactory.getLogger(InfoListener.class);
68
    private Cursor cur = null;
69
    private MapControl mapCtrl;
70
    private ManagerRegistry registry;
71
    private Image imageCursor = null;
72
    public static final int TYPELINKIMAGE = 0;
73
    public static final int TYPELINKTEXT = 1;
74
    public ExtensionPoint formatManagers;
75

    
76
    /**
77
     * Creates a new LinkListener
78
     * 
79
     * @param mc
80
     *            mapControl
81
     */
82
    public LinkListener(MapControl mc, ManagerRegistry registry) {
83
        this.mapCtrl = mc;
84
        this.registry = registry;
85
        formatManagers =
86
            ToolsLocator.getExtensionPointManager()
87
                .get(HyperlinkExtension.ACTIONSEXTENSIONPOINT);
88
        initialize();
89
    }
90

    
91
    private void initialize() {
92
        URL url =
93
            PluginServices.getPluginServices(this)
94
                .getClassLoader()
95
                .getResource("images/LinkCursor.gif");
96
        if (url != null) {
97
            ImageIcon img = new ImageIcon(url);
98
            imageCursor = img.getImage();
99
            cur =
100
                Toolkit.getDefaultToolkit().createCustomCursor(imageCursor,
101
                    new Point(16, 16),
102
                    "Hyperlink");
103
        } else {
104
            PluginServices.getLogger()
105
                .error("Icon not found: images/LinkCursor.gif");
106
        }
107
    }
108

    
109
    /**
110
     * Creates one LinkPanel for URI provided. Gets the active layers and
111
     * invokes getLink
112
     * with the catched point and the allowed tolerance if these layers allows
113
     * HyperLinks.
114
     * getLink provides an array of URIs, this method is invoked for all active
115
     * layers.
116
     * 
117
     * @param event
118
     *            PointEvent
119
     * @throws BehaviorException
120
     * 
121
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
122
     */
123
    public void point(PointEvent event) throws BehaviorException {
124
        Point2D pReal =
125
            mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
126
        FLayer[] sel = mapCtrl.getMapContext().getLayers().getVisibles();
127

    
128
        URI[] links = null;
129
        LinkConfig linkConfig;
130
        ILinkLayerManager layerManager;
131
        ILinkActionManager actionManager;
132

    
133
        for (int layerCount = 0; layerCount < sel.length; layerCount++) {
134
            FLayer theLayer = sel[layerCount];
135
            if (!registry.hasManager(theLayer)) {
136
                continue;
137
            }
138
            try {
139
                layerManager = registry.get(theLayer);
140
                if (theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME) != null
141
                    && theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME) instanceof LayerLinkConfig) {
142
                    LayerLinkConfig layerLink =
143
                        (LayerLinkConfig) theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME);
144
                    if (layerLink.isEnabled() && layerLink.linkCount() > 0) { 
145
                        // there is
146
                                                                              // some
147
                                                                              // link
148
                                                                              // configured
149
                                                                              // for
150
                                                                              // this
151
                                                                              // layer
152

    
153
                        double tol = mapCtrl.getViewPort().toMapDistance(3);
154
                        for (int actionCount = 0; actionCount < layerLink.linkCount(); actionCount++) {
155
                            linkConfig = layerLink.getLink(actionCount);
156
                            links =
157
                                layerManager.getLink(pReal,
158
                                    tol,
159
                                    linkConfig.getFieldName(),
160
                                    linkConfig.getExtension());
161
                            for (int i = 0; i < links.length; i++) {
162
                                if (links[i] != null) {
163

    
164
                                    actionManager =
165
                                        (ILinkActionManager) formatManagers.get(linkConfig.getActionCode())
166
                                            .getExtension()
167
                                            .newInstance();
168
                                    if (actionManager == null) {
169
                                        logger.warn("Hyperlink error -- invalid action code: "
170
                                            + linkConfig.getActionCode());
171
                                        continue;
172
                                    }
173
                                    if (actionManager.hasPanel()) {
174
                                        ShowPanel lpanel =
175
                                            new ShowPanel(actionManager.createPanel(links[i]));
176
                                        // show the panel
177
                                        PluginServices.getMDIManager()
178
                                            .addWindow(lpanel);
179
                                    } else { // delegate in the format manager
180
                                             // to show the file
181
                                        actionManager.showDocument(links[i]);
182
                                    }
183
                                }
184
                            }
185
                        }
186
                    }
187
                }
188
            } catch (ClassNotFoundException e) {
189
                throw new BehaviorException("Hyperlink: There is no manager for this layer type",
190
                    e);
191
            } catch (InstantiationException e) {
192
                logger.error("Hyperlink error: " + e.getMessage(), e);
193
            } catch (IllegalAccessException e) {
194
                logger.error("Hyperlink error: " + e.getMessage(), e);
195
            } catch (IncompatibleLayerException e) {
196
                throw new BehaviorException("Hyperlink: There is no manager for this layer type",
197
                    e);
198
            }
199
        }
200
    }
201

    
202
    /**
203
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
204
     */
205
    public Cursor getCursor() {
206
        return cur;
207
    }
208

    
209
    /**
210
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
211
     */
212
    public boolean cancelDrawing() {
213
        return false;
214
    }
215

    
216
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
217
        // TODO Auto-generated method stub
218

    
219
    }
220

    
221
    public Image getImageCursor() {
222
        return imageCursor;
223
    }
224
}