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 / HyperlinkExtension.java @ 20

History | View | Annotate | Download (9.81 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
package org.gvsig.hyperlink.app.extension;
23

    
24
import java.util.Map;
25

    
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28
import org.gvsig.andami.IconThemeHelper;
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.plugins.Extension;
31
import org.gvsig.andami.ui.mdiManager.IWindow;
32
import org.gvsig.app.project.documents.view.ViewDocument;
33
import org.gvsig.app.project.documents.view.gui.IView;
34
import org.gvsig.app.project.documents.view.legend.gui.ThemeManagerWindow;
35
import org.gvsig.fmap.mapcontext.MapContext;
36
import org.gvsig.fmap.mapcontext.layers.FLayer;
37
import org.gvsig.fmap.mapcontext.layers.FLayers;
38
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
39
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
40
import org.gvsig.fmap.mapcontrol.MapControl;
41
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
42
import org.gvsig.hyperlink.app.extension.actions.ImgFormat;
43
import org.gvsig.hyperlink.app.extension.actions.PdfFormat;
44
import org.gvsig.hyperlink.app.extension.actions.SvgFormat;
45
import org.gvsig.hyperlink.app.extension.actions.TxtFormat;
46
import org.gvsig.hyperlink.app.extension.config.LayerLinkConfig;
47
import org.gvsig.hyperlink.app.extension.config.LinkConfig;
48
import org.gvsig.hyperlink.app.extension.config.gui.ConfigTab;
49
import org.gvsig.hyperlink.app.extension.layers.ManagerRegistry;
50
import org.gvsig.hyperlink.app.extension.layers.VectLayerManager;
51
import org.gvsig.tools.ToolsLocator;
52
import org.gvsig.tools.extensionpoint.ExtensionPoint;
53
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
54
import org.gvsig.tools.library.LibraryException;
55
import org.gvsig.tools.util.Caller;
56
import org.gvsig.tools.util.impl.DefaultCaller;
57

    
58
/**
59
 * Andami extension to show Hyperlink in the application.
60
 * 
61
 * @author gvSIG Team
62
 * @version $Id$
63
 */
64
public class HyperlinkExtension extends Extension {
65

    
66
    private static final Logger LOG = LoggerFactory
67
        .getLogger(HyperlinkExtension.class);
68
    ManagerRegistry layerManager;
69
    public static final String LAYERPROPERTYNAME = "org.gvsig.hyperlink.config";
70
    public static final String TOOLNAME = "org.gvsig.hyperlink.tool";
71
    public static final String ACTIONSEXTENSIONPOINT = "HyperLinkAction";
72

    
73
    private static final int LEGACY_IMAGE_TYPE = 0;
74
    private static final int LEGACY_HTML_TYPE = 1;
75
    private static final int LEGACY_PDF_TYPE = 2;
76
    private static final int LEGACY_SVG_TYPE = 3;
77

    
78
    /**
79
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
80
     */
81
    public void execute(String s) {
82
        IView view = (IView) PluginServices.getMDIManager().getActiveWindow();
83
        MapControl mapCtrl = view.getMapControl();
84
        LOG.debug("Comand : " + s);
85

    
86
        if (s.compareTo("view-show-hyperlink") == 0) {
87
            initTool(view);
88
            mapCtrl.setTool(TOOLNAME);
89
        }
90
    }
91

    
92
    /**
93
     * Inits the tool for the provided view.
94
     * 
95
     * @param view
96
     */
97
    public void initTool(IView view) {
98
        MapControl mapCtrl = view.getMapControl();
99
        if (view.getMapControl().getNamesMapTools().get(TOOLNAME) == null) {
100
            mapCtrl.addBehavior(TOOLNAME,
101
                new PointBehavior(new LinkListener(mapCtrl, layerManager)));
102
            loadLegacyConfig(view);
103
        }
104
    }
105

    
106
    public void loadLegacyConfig(IView view) {
107
        LayersIterator iterator =
108
            new LayersIterator(view.getMapControl().getMapContext().getLayers());
109
        while (iterator.hasNext()) {
110
            FLayer layer = iterator.nextLayer();
111
            loadLegacyConfig(layer);
112
        }
113
    }
114

    
115
    /**
116
     * Returns a LayerLinkConfig object if an old-style or new-style hyperlink
117
     * was found and configured,
118
     * or null otherwise.
119
     * 
120
     * @param layer
121
     */
122
    public LayerLinkConfig loadLegacyConfig(FLayer layer) {
123
        LayerLinkConfig layerConfig =
124
            (LayerLinkConfig) layer.getProperty(LAYERPROPERTYNAME);
125
        if (layerConfig != null) { // don't apply compatibility if the layer
126
                                   // already has new 1.9.0 configuration
127
            return layerConfig;
128
        }
129
        Object fName = layer.getProperty("legacy.hyperlink.selectedField");
130
        if (fName != null && fName instanceof String) {
131
            Map properties = layer.getExtendedProperties();
132
            properties.remove("legacy.hyperlink.selectedField");
133
            // remove it from layer to don't keep legacy properties for ever
134
            // in the project
135

    
136
            String fieldName = (String) fName;
137
            Object extObj = layer.getProperty("legacy.hyperlink.extension");
138
            String extension = null;
139
            if (extObj != null && extObj instanceof String) {
140
                properties.remove("legacy.hyperlink.extension");
141
                // remove it from layer to don't keep legacy properties for ever
142
                // in the project
143
                extension = (String) extObj;
144
            }
145
            Object typeObj = layer.getProperty("legacy.hyperlink.type");
146
            int type = -1;
147
            if (typeObj != null && typeObj instanceof Integer) {
148
                properties.remove("legacy.hyperlink.type"); 
149
                // remove it from layer to don't keep legacy properties for ever
150
                // in the project
151
                type = ((Integer) typeObj).intValue();
152
            }
153
            LayerLinkConfig config = new LayerLinkConfig();
154
            config.setEnabled(true);
155
            config.addLink(getLegacyActionCode(type), fieldName, extension);
156
            layer.setProperty(LAYERPROPERTYNAME, config);
157
            return config;
158
        }
159
        return null;
160
    }
161

    
162
    private String getLegacyActionCode(int type) {
163
        switch (type) {
164
        case LEGACY_IMAGE_TYPE:
165
            return ImgFormat.actionCode;
166
        case LEGACY_PDF_TYPE:
167
            return PdfFormat.actionCode;
168
        case LEGACY_SVG_TYPE:
169
            return SvgFormat.actionCode;
170
        case LEGACY_HTML_TYPE:
171
        default:
172
            return TxtFormat.actionCode;
173
        }
174
    }
175

    
176
    /**
177
     * @see com.iver.mdiApp.plugins.IExtension#isVisible()
178
     */
179
    public boolean isVisible() {
180
        IWindow f = PluginServices.getMDIManager().getActiveWindow();
181

    
182
        if (f == null) {
183
            return false;
184
        }
185

    
186
        if (f instanceof IView) {
187

    
188
            MapContext mapa = ((IView) f).getViewDocument().getMapContext();
189

    
190
            return mapa.getLayers().getLayersCount() > 0;
191
        } else {
192
            return false;
193
        }
194
    }
195

    
196
    /**
197
     * @see com.iver.andami.plugins.IExtension#isEnabled()
198
     */
199
    public boolean isEnabled() {
200
        IWindow window = PluginServices.getMDIManager().getActiveWindow();
201

    
202
        if (window == null) {
203
            return false;
204
        }
205

    
206
        if (window instanceof IView) {
207
            IView view = (IView) window;
208
            ViewDocument viewDocument = view.getViewDocument();
209

    
210
            if (viewDocument != null
211
                && viewDocument.getMapContext() != null
212
                && viewDocument.getMapContext().getLayers().getVisibles().length > 0) {
213
                    FLayers lyrs = viewDocument.getMapContext().getLayers();
214
                    FLayer[] lyrsSelected = lyrs.getActives();
215
                    for (int i = 0; i < lyrsSelected.length; i++) {
216
                            Object obj = lyrsSelected[i].getProperty(HyperlinkExtension.LAYERPROPERTYNAME);
217
                                        if(obj != null)
218
                                                return true;
219
                                }
220
            } 
221
        }
222
        return false;
223
    }
224

    
225
    public void postInitialize() {
226
        Caller caller = new DefaultCaller();
227

    
228
        caller.add(new LinkConfig.RegisterPersistence());
229
        caller.add(new LayerLinkConfig.RegisterPersistence());
230
        if( !caller.call() ) {
231
            throw new LibraryException(HyperlinkExtension.class, caller.getExceptions());
232
        }
233

    
234
        registerLayers();
235
        registerActions();
236
        registerConfigPanel();
237
    }
238

    
239
    private void registerLayers() {
240
        layerManager = new ManagerRegistry();
241
        layerManager.put(FLyrVect.class, VectLayerManager.class);
242
    }
243

    
244
    public ManagerRegistry getLayerManager() {
245
        return layerManager;
246
    }
247

    
248
    private void registerActions() {
249
        ExtensionPointManager epm = ToolsLocator.getExtensionPointManager();
250
        ExtensionPoint ep =
251
            epm.add(ACTIONSEXTENSIONPOINT,
252
                "Registers Actions that are able to manage specific format types.");
253

    
254
        ep.append(TxtFormat.actionCode, "", TxtFormat.class);
255
        ep.append(ImgFormat.actionCode, "", ImgFormat.class);
256
        ep.append(PdfFormat.actionCode, "", PdfFormat.class);
257
        ep.append(SvgFormat.actionCode, "", SvgFormat.class);
258
    }
259

    
260
    private void registerConfigPanel() {
261
        // pages
262
        ThemeManagerWindow.addPage(ConfigTab.class);
263

    
264
        ThemeManagerWindow.setTabEnabledForLayer(ConfigTab.class,
265
            FLyrVect.class,
266
            true);
267
    }
268

    
269
    public void initialize() {
270
        IconThemeHelper.registerIcon("action",
271
            "view-show-hyperlink", this);
272
        IconThemeHelper.registerIcon("cursor",
273
            "cursor-view-show-hyperlink", this);
274
            
275
    }
276

    
277
}