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

History | View | Annotate | Download (10.1 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.ExternTxtFormat;
43
import org.gvsig.hyperlink.app.extension.actions.FolderFormat;
44
import org.gvsig.hyperlink.app.extension.actions.ImgFormat;
45
import org.gvsig.hyperlink.app.extension.actions.PdfFormat;
46
import org.gvsig.hyperlink.app.extension.actions.SvgFormat;
47
import org.gvsig.hyperlink.app.extension.actions.TxtFormat;
48
import org.gvsig.hyperlink.app.extension.config.LayerLinkConfig;
49
import org.gvsig.hyperlink.app.extension.config.LinkConfig;
50
import org.gvsig.hyperlink.app.extension.config.gui.ConfigTab;
51
import org.gvsig.hyperlink.app.extension.layers.ManagerRegistry;
52
import org.gvsig.hyperlink.app.extension.layers.VectLayerManager;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.extensionpoint.ExtensionPoint;
55
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
56
import org.gvsig.tools.library.LibraryException;
57
import org.gvsig.tools.util.Caller;
58
import org.gvsig.tools.util.impl.DefaultCaller;
59

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

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

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

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

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

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

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

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

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

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

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

    
184
        if (f == null) {
185
            return false;
186
        }
187

    
188
        if (f instanceof IView) {
189

    
190
            MapContext mapa = ((IView) f).getViewDocument().getMapContext();
191

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

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

    
204
        if (window == null) {
205
            return false;
206
        }
207

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

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

    
227
    public void postInitialize() {
228
        Caller caller = new DefaultCaller();
229

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

    
236
        registerLayers();
237
        registerActions();
238
        registerConfigPanel();
239
    }
240

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

    
246
    public ManagerRegistry getLayerManager() {
247
        return layerManager;
248
    }
249

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

    
256
        ep.append(TxtFormat.actionCode, "", TxtFormat.class);
257
        ep.append(ExternTxtFormat.actionCode, "", ExternTxtFormat.class);
258
        ep.append(ImgFormat.actionCode, "", ImgFormat.class);
259
        ep.append(PdfFormat.actionCode, "", PdfFormat.class);
260
        ep.append(SvgFormat.actionCode, "", SvgFormat.class);
261
        ep.append(FolderFormat.actionCode, "", FolderFormat.class);
262

    
263
    }
264

    
265
    private void registerConfigPanel() {
266
        // pages
267
        ThemeManagerWindow.addPage(ConfigTab.class);
268

    
269
        ThemeManagerWindow.setTabEnabledForLayer(ConfigTab.class,
270
            FLyrVect.class,
271
            true);
272
    }
273

    
274
    public void initialize() {
275
        IconThemeHelper.registerIcon("action",
276
            "view-show-hyperlink", this);
277
        IconThemeHelper.registerIcon("cursor",
278
            "cursor-view-show-hyperlink", this);
279

    
280
    }
281

    
282
}