Statistics
| Revision:

root / org.gvsig.hyperlink.app / trunk / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / LinkListener.java @ 15

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

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

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

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

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

    
89
    private void initialize() {
90
        cur = Toolkit.getDefaultToolkit().createCustomCursor(this.getImageCursor(),
91
                    new Point(16, 16),
92
                    "Hyperlink");
93
    }
94

    
95
    /**
96
     * Creates one LinkPanel for URI provided. Gets the active layers and
97
     * invokes getLink
98
     * with the catched point and the allowed tolerance if these layers allows
99
     * HyperLinks.
100
     * getLink provides an array of URIs, this method is invoked for all active
101
     * layers.
102
     * 
103
     * @param event
104
     *            PointEvent
105
     * @throws BehaviorException
106
     * 
107
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
108
     */
109
    public void point(PointEvent event) throws BehaviorException {
110
        Point2D pReal =
111
            mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
112
        FLayer[] sel = mapCtrl.getMapContext().getLayers().getVisibles();
113

    
114
        URI[] links = null;
115
        LinkConfig linkConfig;
116
        ILinkLayerManager layerManager;
117
        ILinkActionManager actionManager;
118

    
119
        for (int layerCount = 0; layerCount < sel.length; layerCount++) {
120
            FLayer theLayer = sel[layerCount];
121
            if (!registry.hasManager(theLayer)) {
122
                continue;
123
            }
124
            try {
125
                layerManager = registry.get(theLayer);
126
                if (theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME) != null
127
                    && theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME) instanceof LayerLinkConfig) {
128
                    LayerLinkConfig layerLink =
129
                        (LayerLinkConfig) theLayer.getProperty(HyperlinkExtension.LAYERPROPERTYNAME);
130
                    if (layerLink.isEnabled() && layerLink.linkCount() > 0) { 
131
                        // there is
132
                                                                              // some
133
                                                                              // link
134
                                                                              // configured
135
                                                                              // for
136
                                                                              // this
137
                                                                              // layer
138

    
139
                        double tol = mapCtrl.getViewPort().toMapDistance(3);
140
                        for (int actionCount = 0; actionCount < layerLink.linkCount(); actionCount++) {
141
                            linkConfig = layerLink.getLink(actionCount);
142
                            links =
143
                                layerManager.getLink(pReal,
144
                                    tol,
145
                                    linkConfig.getFieldName(),
146
                                    linkConfig.getExtension());
147
                            for (int i = 0; i < links.length; i++) {
148
                                if (links[i] != null) {
149

    
150
                                    actionManager =
151
                                        (ILinkActionManager) formatManagers.get(linkConfig.getActionCode())
152
                                            .getExtension()
153
                                            .newInstance();
154
                                    if (actionManager == null) {
155
                                        logger.warn("Hyperlink error -- invalid action code: "
156
                                            + linkConfig.getActionCode());
157
                                        continue;
158
                                    }
159
                                    if (actionManager.hasPanel()) {
160
                                        ShowPanel lpanel =
161
                                            new ShowPanel(actionManager.createPanel(links[i]));
162
                                        // show the panel
163
                                        PluginServices.getMDIManager()
164
                                            .addWindow(lpanel);
165
                                    } else { // delegate in the format manager
166
                                             // to show the file
167
                                        actionManager.showDocument(links[i]);
168
                                    }
169
                                }
170
                            }
171
                        }
172
                    }
173
                }
174
            } catch (ClassNotFoundException e) {
175
                throw new BehaviorException("Hyperlink: There is no manager for this layer type",
176
                    e);
177
            } catch (InstantiationException e) {
178
                logger.error("Hyperlink error: " + e.getMessage(), e);
179
            } catch (IllegalAccessException e) {
180
                logger.error("Hyperlink error: " + e.getMessage(), e);
181
            } catch (IncompatibleLayerException e) {
182
                throw new BehaviorException("Hyperlink: There is no manager for this layer type",
183
                    e);
184
            }
185
        }
186
    }
187

    
188
    /**
189
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
190
     */
191
    public Cursor getCursor() {
192
        return cur;
193
    }
194

    
195
    /**
196
     * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
197
     */
198
    public boolean cancelDrawing() {
199
        return false;
200
    }
201

    
202
    public void pointDoubleClick(PointEvent event) throws BehaviorException {
203
        // TODO Auto-generated method stub
204

    
205
    }
206

    
207
    public Image getImageCursor() {
208
            return IconThemeHelper.getImage("cursor-view-show-hyperlink");
209
    }
210
}