001: /*
002: * uDig - User Friendly Desktop Internet GIS client http://udig.refractions.net (C) 2004,
003: * Refractions Research Inc. This library is free software; you can redistribute it and/or modify it
004: * under the terms of the GNU Lesser General Public License as published by the Free Software
005: * Foundation; version 2.1 of the License. This library is distributed in the hope that it will be
006: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
007: * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
008: */
009: package net.refractions.udig.project.internal.commands;
010:
011: import java.text.MessageFormat;
012: import java.util.Collection;
013: import java.util.Collections;
014: import java.util.List;
015:
016: import net.refractions.udig.project.ILayer;
017: import net.refractions.udig.project.command.AbstractCommand;
018: import net.refractions.udig.project.command.UndoableMapCommand;
019: import net.refractions.udig.project.internal.EditManager;
020: import net.refractions.udig.project.internal.Layer;
021: import net.refractions.udig.project.internal.Map;
022: import net.refractions.udig.project.internal.Messages;
023:
024: import org.eclipse.core.runtime.IProgressMonitor;
025:
026: /**
027: * Deletes a layer.
028: *
029: * @author Jesse
030: * @since 1.0.0
031: */
032: public class DeleteLayerCommand extends AbstractCommand implements
033: UndoableMapCommand {
034:
035: private Layer layer;
036:
037: private int index;
038:
039: private Map map;
040:
041: private boolean wasSelected;
042:
043: /**
044: * Construct <code>DeleteLayerCommand</code>.
045: *
046: * @param map
047: */
048: public DeleteLayerCommand(Layer layer) {
049: this .map = layer.getMapInternal();
050: this .layer = layer;
051: }
052:
053: /**
054: * @see net.refractions.udig.project.command.UndoableCommand#rollback()
055: */
056: public void rollback(IProgressMonitor monitor) throws Exception {
057: if (layer == null)
058: return;
059: map.getLayersInternal().add(index, layer);
060: if (wasSelected)
061: map.getEditManagerInternal().setSelectedLayer(layer);
062: }
063:
064: /**
065: * @see net.refractions.udig.project.command.MapCommand#run()
066: */
067: public void run(IProgressMonitor monitor) throws Exception {
068: if (!map.getLayersInternal().contains(layer)) {
069: layer = null;
070: return;
071: }
072: List<Layer> layers = map.getLayersInternal();
073: index = layers.indexOf(layer);
074: wasSelected = selectNewLayer(map, Collections.singleton(layer));
075: layers.remove(layer);
076: map.getColourScheme().removeItem(layer.getID().toString(),
077: layer.getDefaultColor()); //remove from scheme
078: }
079:
080: /**
081: * Selects a new layer if the selected layer is being removed. The algorithm will try to select the bottom layer above the
082: * selected layer or if not possible then the top layer below (that isn't being removed).
083: *
084: * @param layers Layers in map
085: * @param toRemove layers being removed
086: * @param indexOfSelectedLayer
087: * @return true if the selected layer has been changed
088: */
089: static boolean selectNewLayer(Map map,
090: Collection<? extends ILayer> toRemove) {
091: EditManager editManager = map.getEditManagerInternal();
092: List<Layer> layers = map.getLayersInternal();
093: Layer selectedLayer = editManager.getSelectedLayer();
094: if (!toRemove.contains(selectedLayer))
095: // nothing to be done since it is not going to be removed
096: return false;
097:
098: // try to select bottom layer above the selected layer that is not being deleted. If
099: // not possible then select top layer below the selected layer
100: for (int i = layers.indexOf(selectedLayer) + 1; i < layers
101: .size(); i++) {
102: Layer layer = layers.get(i);
103: if (!toRemove.contains(layer)) {
104: editManager.setSelectedLayer(layer);
105: return true;
106: }
107: }
108: for (int i = layers.indexOf(selectedLayer) - 1; i >= 0; i--) {
109: Layer layer = layers.get(i);
110: if (!toRemove.contains(layer)) {
111: editManager.setSelectedLayer(layer);
112: return true;
113: }
114: }
115:
116: editManager.setSelectedLayer(null);
117: return true;
118: }
119:
120: /**
121: * @see net.refractions.udig.project.command.MapCommand#getName()
122: */
123: public String getName() {
124: return MessageFormat.format(
125: Messages.DeleteLayerCommand_deleteLayer,
126: new Object[] { layer.getName() });
127: }
128:
129: }
|