svn-gvsig-desktop / tags / v2_0_0_Build_2056 / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / HyperlinkExtension.java @ 39035
History | View | Annotate | Download (9.9 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 |
|
29 |
import org.gvsig.andami.IconThemeHelper; |
30 |
import org.gvsig.andami.PluginServices; |
31 |
import org.gvsig.andami.plugins.Extension; |
32 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
33 |
import org.gvsig.app.project.documents.view.ViewDocument; |
34 |
import org.gvsig.app.project.documents.view.gui.IView; |
35 |
import org.gvsig.app.project.documents.view.legend.gui.ThemeManagerWindow; |
36 |
import org.gvsig.fmap.mapcontext.MapContext; |
37 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
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.LoadRasterLayer; |
44 |
import org.gvsig.hyperlink.app.extension.actions.LoadVectorLayer; |
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 |
return true; |
216 |
else
|
217 |
return false; |
218 |
|
219 |
} |
220 |
return true; |
221 |
} |
222 |
|
223 |
public void postInitialize() { |
224 |
Caller caller = new DefaultCaller();
|
225 |
|
226 |
caller.add(new LinkConfig.RegisterPersistence());
|
227 |
caller.add(new LayerLinkConfig.RegisterPersistence());
|
228 |
if( !caller.call() ) {
|
229 |
throw new LibraryException(HyperlinkExtension.class, caller.getExceptions()); |
230 |
} |
231 |
|
232 |
registerLayers(); |
233 |
registerActions(); |
234 |
registerConfigPanel(); |
235 |
} |
236 |
|
237 |
private void registerLayers() { |
238 |
layerManager = new ManagerRegistry();
|
239 |
layerManager.put(FLyrVect.class, VectLayerManager.class); |
240 |
} |
241 |
|
242 |
public ManagerRegistry getLayerManager() {
|
243 |
return layerManager;
|
244 |
} |
245 |
|
246 |
private void registerActions() { |
247 |
ExtensionPointManager epm = ToolsLocator.getExtensionPointManager(); |
248 |
ExtensionPoint ep = |
249 |
epm.add(ACTIONSEXTENSIONPOINT, |
250 |
"Registers Actions that are able to manage specific format types.");
|
251 |
|
252 |
ep.append(TxtFormat.actionCode, "", TxtFormat.class);
|
253 |
ep.append(ImgFormat.actionCode, "", ImgFormat.class);
|
254 |
ep.append(PdfFormat.actionCode, "", PdfFormat.class);
|
255 |
ep.append(LoadVectorLayer.actionCode, "", LoadVectorLayer.class);
|
256 |
ep.append(SvgFormat.actionCode, "", SvgFormat.class);
|
257 |
try {
|
258 |
ep.append(LoadRasterLayer.actionCode, "", LoadRasterLayer.class);
|
259 |
} catch (Throwable e) { |
260 |
LOG.error("Could not load raster layer hyperlink support", e);
|
261 |
} |
262 |
} |
263 |
|
264 |
private void registerConfigPanel() { |
265 |
// pages
|
266 |
ThemeManagerWindow.addPage(ConfigTab.class); |
267 |
|
268 |
ThemeManagerWindow.setTabEnabledForLayer(ConfigTab.class, |
269 |
FLyrVect.class, |
270 |
true);
|
271 |
} |
272 |
|
273 |
public void initialize() { |
274 |
IconThemeHelper.registerIcon("action",
|
275 |
"view-show-hyperlink", this); |
276 |
IconThemeHelper.registerIcon("cursor",
|
277 |
"cursor-view-show-hyperlink", this); |
278 |
|
279 |
} |
280 |
|
281 |
} |