01: /**
02: *
03: */package net.refractions.udig.project.internal.commands.edit;
04:
05: import java.net.URL;
06: import java.util.List;
07:
08: import net.refractions.udig.catalog.CatalogPlugin;
09: import net.refractions.udig.catalog.IGeoResource;
10: import net.refractions.udig.catalog.IResolve;
11: import net.refractions.udig.project.command.AbstractCommand;
12: import net.refractions.udig.project.command.UndoableMapCommand;
13: import net.refractions.udig.project.internal.Layer;
14: import net.refractions.udig.project.internal.Messages;
15:
16: import org.eclipse.core.runtime.IProgressMonitor;
17:
18: /**
19: * Creates a layer and adds it to the executing map. The resources must already exist
20: * in the catalog.
21: *
22: * @author jones
23: *
24: */
25: public class CreateLayerCommand extends AbstractCommand implements
26: UndoableMapCommand {
27:
28: IGeoResource resource;
29: private URL id;
30: private Layer layer;
31:
32: /**
33: * Creates a layer from the resource.
34: *
35: * @param resource a resource to create the layer from.
36: */
37: public CreateLayerCommand(IGeoResource resource) {
38: this .resource = resource;
39: id = resource.getIdentifier();
40: }
41:
42: /**
43: * Create a layer from a IGeoResource identifier url.
44: * @param id2 Must identify a IGeoResource.
45: */
46: public CreateLayerCommand(URL id2) {
47: assert id2.getRef() != null
48: && id2.getRef().trim().length() != 0;
49: this .id = id2;
50: }
51:
52: /* (non-Javadoc)
53: * @see net.refractions.udig.project.command.Command#run(org.eclipse.core.runtime.IProgressMonitor)
54: */
55: public void run(IProgressMonitor monitor) throws Exception {
56: if (resource == null) {
57: List<IResolve> resources = CatalogPlugin.getDefault()
58: .getLocalCatalog().find(id, monitor);
59: if (resources.size() == 0)
60: throw new Exception(Messages.CreateLayerCommand_badID
61: + id + Messages.CreateLayerCommand_badID2);
62:
63: resource = (IGeoResource) resources.get(0);
64: }
65: this .layer = getMap().getLayerFactory().createLayer(resource);
66: getMap().getLayersInternal().add(this .layer);
67: }
68:
69: /* (non-Javadoc)
70: * @see net.refractions.udig.project.command.Command#getName()
71: */
72: public String getName() {
73: return Messages.CreateLayerCommand_name + id;
74: }
75:
76: public void rollback(IProgressMonitor monitor) throws Exception {
77: if (this .layer == null)
78: throw new IllegalStateException(
79: Messages.CreateLayerCommand_illegalRollback
80: + Messages.CreateLayerCommand_illegalRollback2);
81: getMap().getLayersInternal().remove(layer);
82: }
83:
84: }
|