01: package net.refractions.udig.project.ui.internal.actions;
02:
03: import java.io.File;
04: import java.io.IOException;
05: import java.net.MalformedURLException;
06: import java.net.URL;
07:
08: import net.refractions.udig.project.internal.Layer;
09: import net.refractions.udig.project.internal.Map;
10: import net.refractions.udig.project.ui.internal.Messages;
11: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
12: import net.refractions.udig.style.sld.SLDContent;
13: import net.refractions.udig.ui.IDropAction;
14: import net.refractions.udig.ui.ViewerDropLocation;
15:
16: import org.eclipse.core.runtime.IProgressMonitor;
17: import org.geotools.styling.Style;
18:
19: public class SLDDropAction extends IDropAction {
20:
21: private Style style;
22:
23: @Override
24: public boolean accept() {
25:
26: if (getViewerLocation() == ViewerDropLocation.NONE)
27: return false;
28: URL url = null;
29: // make sure we can turn the object into an sld
30: try {
31: if (getData() instanceof URL) {
32: url = (URL) getData();
33: } else if (getData() instanceof File) {
34: url = ((File) getData()).toURL();
35: } else if (getData() instanceof String) {
36: try {
37: url = new URL((String) getData());
38: } catch (MalformedURLException e) {
39: // try attaching a file protocol
40: url = new URL("file:///" + (String) getData()); //$NON-NLS-1$
41: }
42: if (url != null) {
43: if (!url.getFile().toLowerCase().endsWith(".sld") || url.getQuery() != null) //$NON-NLS-1$
44: url = null;
45: }
46:
47: }
48: } catch (MalformedURLException e) {
49: String msg = Messages.SLDDropAction_badSldUrl;
50: ProjectUIPlugin.log(msg, e);
51: }
52:
53: if (url == null)
54: return false;
55: try {
56: style = SLDContent.parse(url);
57: } catch (Throwable e) {
58: return false;
59: }
60:
61: return style != null
62: && (getDestination() instanceof Layer || getDestination() instanceof Map);
63: }
64:
65: @Override
66: public void perform(IProgressMonitor monitor) {
67:
68: if (!accept())
69: throw new IllegalStateException(
70: "Data is not acceptable for this action! Programatic Error!!!"); //$NON-NLS-1$
71: // grab the actual target
72: Object target = getDestination();
73: if (target instanceof Layer) {
74: dropOnLayer(monitor, (Layer) target);
75: } else {
76: if (target instanceof Map) {
77: dropOnLayer(monitor, (Layer) ((Map) target)
78: .getEditManagerInternal().getSelectedLayer());
79: }
80: }
81:
82: }
83:
84: private void dropOnLayer(IProgressMonitor monitor, Layer target) {
85: Layer layer = (Layer) target;
86: // parse the sld object
87:
88: try {
89: SLDContent.apply(layer, style, monitor);
90: layer.refresh(null);
91:
92: } catch (IOException e) {
93: String msg = Messages.SLDDropAction_sldParseError;
94: ProjectUIPlugin.log(msg, e);
95: }
96: }
97:
98: }
|