001: package net.refractions.udig.project.internal.commands;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.List;
006:
007: import net.refractions.udig.catalog.IGeoResource;
008: import net.refractions.udig.project.command.AbstractCommand;
009: import net.refractions.udig.project.command.UndoableMapCommand;
010: import net.refractions.udig.project.internal.Layer;
011: import net.refractions.udig.project.internal.LayerFactory;
012: import net.refractions.udig.project.internal.Map;
013: import net.refractions.udig.project.internal.Messages;
014: import net.refractions.udig.project.internal.ProjectPlugin;
015: import net.refractions.udig.project.internal.Trace;
016:
017: import org.eclipse.core.runtime.IProgressMonitor;
018:
019: public class AddLayersCommand extends AbstractCommand implements
020: UndoableMapCommand {
021:
022: Collection<Object> resources;
023:
024: List<Layer> layers;
025:
026: private int index;
027:
028: private Layer selection;
029:
030: /**
031: * Creates a the command from a set of
032: *
033: * @see Layer or
034: * @see IGeoResource objects.
035: * @param resources A list containing a combination of layers or resources.
036: */
037: @SuppressWarnings("unchecked")
038: public AddLayersCommand(Collection resources) {
039: this (resources, -1);
040:
041: }
042:
043: /**
044: * Creates a the command from a set of
045: *
046: * @see Layer or
047: * @see IGeoResource objects.
048: * @param resources A list containing a combination of layers or resources.
049: */
050: @SuppressWarnings("unchecked")
051: public AddLayersCommand(Collection resources, int i) {
052: this .resources = resources;
053: index = i;
054: }
055:
056: public void run(IProgressMonitor monitor) throws Exception {
057: Map map = getMap();
058: if (layers == null) {
059: layers = new ArrayList<Layer>();
060: LayerFactory layerFactory = map.getLayerFactory();
061:
062: selection = (Layer) map.getEditManager().getSelectedLayer();
063:
064: for (Object o : resources) {
065: Layer layer = null;
066:
067: if (o instanceof IGeoResource) {
068:
069: // ensure that the service is part of the catalog so that the find method in layer
070: // turn into layer
071: layer = layerFactory.createLayer((IGeoResource) o);
072: }
073: if (o instanceof Layer) {
074: // leave as is
075: layer = (Layer) o;
076: }
077:
078: if (layer != null) {
079: layers.add(layer);
080: }
081:
082: }
083: }
084: if (!layers.isEmpty()) {
085: if (index < 0) {
086: index = map.getLayersInternal().size();
087: }
088: trace();
089:
090: map.getLayersInternal().addAll(index, layers);
091: map.getEditManagerInternal()
092: .setSelectedLayer(layers.get(0));
093: }
094:
095: }
096:
097: private void trace() {
098: if (ProjectPlugin.isDebugging(Trace.COMMANDS)) {
099: List<String> ids = new ArrayList<String>();
100: for (Layer layer : layers) {
101: ids.add(layer.getID().toString());
102: }
103: ProjectPlugin
104: .trace(
105: getClass(),
106: "Adding " + layers.size() + " layers to map:" + getMap().getName() + ". IDs=" + ids, null); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
107: }
108: }
109:
110: public String getName() {
111: return Messages.AddLayersCommand_name;
112: }
113:
114: public void rollback(IProgressMonitor monitor) throws Exception {
115: getMap().getLayersInternal().removeAll(layers);
116: getMap().getEditManagerInternal().setSelectedLayer(selection);
117: layers.clear();
118: }
119:
120: public List<Layer> getLayers() {
121: return layers;
122: }
123:
124: }
|