001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.project.ui;
018:
019: import java.util.ArrayList;
020: import java.util.Iterator;
021:
022: import net.refractions.udig.project.ILayer;
023: import net.refractions.udig.project.IProjectElement;
024: import net.refractions.udig.project.internal.Layer;
025: import net.refractions.udig.project.internal.Project;
026: import net.refractions.udig.project.internal.ProjectElement;
027: import net.refractions.udig.project.internal.commands.edit.DeleteManyFeaturesCommand;
028: import net.refractions.udig.project.ui.internal.AdaptingFilter;
029: import net.refractions.udig.project.ui.internal.actions.Rename;
030:
031: import org.eclipse.emf.common.ui.action.WorkbenchWindowActionDelegate;
032: import org.eclipse.jface.action.IAction;
033: import org.eclipse.jface.viewers.ISelection;
034: import org.eclipse.jface.viewers.IStructuredSelection;
035: import org.geotools.feature.Feature;
036:
037: /**
038: * Calls implemented operate method on the each element of selection
039: *
040: * @see Rename
041: * @author jeichar
042: * @since 0.6.0
043: */
044: public abstract class UDIGGenericAction extends
045: WorkbenchWindowActionDelegate {
046:
047: private IStructuredSelection selection;
048:
049: /**
050: * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
051: */
052: public void run(IAction action) {
053:
054: /*
055: * TODO: Optimization for a set of objects in selection of the same nature. The goal: run an
056: * operation once over all selected objects.
057: */
058: ArrayList<Layer> layers = new ArrayList<Layer>(selection.size());
059:
060: for (Iterator iter = selection.iterator(); iter.hasNext();) {
061: Object element = iter.next();
062:
063: if (element instanceof Project) {
064: operate((Project) element);
065: } else if (element instanceof IProjectElement) {
066: operate((ProjectElement) element);
067: } else if (element instanceof Layer) {
068: layers.add((Layer) element);
069: } else if (element instanceof Feature) {
070: operate((Feature) element);
071: }
072: if (element instanceof AdaptingFilter) {
073: AdaptingFilter f = (AdaptingFilter) element;
074: ILayer layer = (ILayer) f.getAdapter(ILayer.class);
075: layer.getMap().sendCommandASync(
076: new DeleteManyFeaturesCommand(layer, f));
077: }
078:
079: }
080:
081: if (!layers.isEmpty()) {
082: operate(layers.toArray(new Layer[layers.size()]));
083: }
084:
085: // layers = null;
086: }
087:
088: /**
089: * Operates on a Feature. Default Implementation does nothing.
090: *
091: * @param feature
092: */
093: protected void operate(Feature feature) {
094: // do nothing
095: }
096:
097: /**
098: * Operates on a layer. Default Implementation does nothing.
099: *
100: * @param layer
101: * @deprecated
102: */
103: protected void operate(Layer layer) {
104: // do nothing
105: }
106:
107: /**
108: * Operates on an array of layers.
109: * <p>
110: * Default Implementation does nothing.
111: *
112: * @param layers
113: */
114: protected void operate(Layer[] layers) {
115: // do nothing
116: }
117:
118: /**
119: * Operates on a IProjectElement. Default Implementation does nothing.
120: *
121: * @param element
122: */
123: protected void operate(ProjectElement element) {
124: // do nothing
125: }
126:
127: /**
128: * Operates on a Project. Default Implementation does nothing.
129: *
130: * @param project
131: */
132: protected void operate(Project project) {
133: // do nothing
134: }
135:
136: /**
137: * @see org.eclipse.emf.common.ui.action.WorkbenchWindowActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
138: * org.eclipse.jface.viewers.ISelection)
139: */
140: public void selectionChanged(IAction action, ISelection selection) {
141: if (selection instanceof IStructuredSelection)
142: this .selection = (IStructuredSelection) selection;
143: }
144:
145: }
|