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

History | View | Annotate | Download (9.71 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
import org.gvsig.andami.PluginServices;
26
import org.gvsig.andami.plugins.Extension;
27
import org.gvsig.andami.ui.mdiManager.IWindow;
28
import org.gvsig.app.project.documents.view.ViewDocument;
29
import org.gvsig.app.project.documents.view.gui.IView;
30
import org.gvsig.app.project.documents.view.legend.gui.ThemeManagerWindow;
31
import org.gvsig.fmap.mapcontext.MapContext;
32
import org.gvsig.fmap.mapcontext.layers.FLayer;
33
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
34
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
35
import org.gvsig.fmap.mapcontrol.MapControl;
36
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
37
import org.gvsig.hyperlink.app.extension.actions.ImgFormat;
38
import org.gvsig.hyperlink.app.extension.actions.LoadRasterLayer;
39
import org.gvsig.hyperlink.app.extension.actions.LoadVectorLayer;
40
import org.gvsig.hyperlink.app.extension.actions.PdfFormat;
41
import org.gvsig.hyperlink.app.extension.actions.SvgFormat;
42
import org.gvsig.hyperlink.app.extension.actions.TxtFormat;
43
import org.gvsig.hyperlink.app.extension.config.LayerLinkConfig;
44
import org.gvsig.hyperlink.app.extension.config.LinkConfig;
45
import org.gvsig.hyperlink.app.extension.config.gui.ConfigTab;
46
import org.gvsig.hyperlink.app.extension.layers.ManagerRegistry;
47
import org.gvsig.hyperlink.app.extension.layers.VectLayerManager;
48
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.fill.impl.SimpleFillSymbol;
49
import org.gvsig.symbology.impl.SymbologyDefaultImplLibrary;
50
import org.gvsig.tools.ToolsLocator;
51
import org.gvsig.tools.extensionpoint.ExtensionPoint;
52
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
53
import org.gvsig.tools.library.LibraryException;
54
import org.gvsig.tools.util.Caller;
55
import org.gvsig.tools.util.impl.DefaultCaller;
56
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
58

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

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

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

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

    
87
        if (s.compareTo("HYPERLINK") == 0) {
88
            initTool(view);
89
            mapCtrl.setTool(TOOLNAME);
90
        }
91
    }
92

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

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

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

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

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

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

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

    
187
        if (f instanceof IView) {
188

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

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

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

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

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

    
211
            if (viewDocument != null
212
                && viewDocument.getMapContext() != null
213
                && viewDocument.getMapContext().getLayers().getVisibles().length > 0)
214
                return true;
215
            else
216
                return false;
217

    
218
        }
219
        return true;
220
    }
221

    
222
    public void postInitialize() {
223
        Caller caller = new DefaultCaller();
224

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

    
231
        registerLayers();
232
        registerActions();
233
        registerConfigPanel();
234
    }
235

    
236
    private void registerLayers() {
237
        layerManager = new ManagerRegistry();
238
        layerManager.put(FLyrVect.class, VectLayerManager.class);
239
    }
240

    
241
    public ManagerRegistry getLayerManager() {
242
        return layerManager;
243
    }
244

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

    
251
        ep.append(TxtFormat.actionCode, "", TxtFormat.class);
252
        ep.append(ImgFormat.actionCode, "", ImgFormat.class);
253
        ep.append(PdfFormat.actionCode, "", PdfFormat.class);
254
        ep.append(LoadRasterLayer.actionCode, "", LoadRasterLayer.class);
255
        ep.append(LoadVectorLayer.actionCode, "", LoadVectorLayer.class);
256
        ep.append(SvgFormat.actionCode, "", SvgFormat.class);
257
    }
258

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

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

    
268
    public void initialize() {
269
        // Do nothing
270
        
271
    }
272

    
273
}