001: package net.refractions.udig.project.ui.internal.actions;
002:
003: import java.net.URL;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.List;
007: import java.util.Set;
008:
009: import net.refractions.udig.catalog.IGeoResource;
010: import net.refractions.udig.catalog.internal.ui.actions.CatalogImportDropAction;
011: import net.refractions.udig.catalog.ui.workflow.ResourceSelectionState;
012: import net.refractions.udig.catalog.ui.workflow.Workflow;
013: import net.refractions.udig.catalog.ui.workflow.WorkflowWizard;
014: import net.refractions.udig.catalog.ui.workflow.WorkflowWizardPage;
015: import net.refractions.udig.catalog.ui.workflow.Workflow.State;
016: import net.refractions.udig.project.ILayer;
017: import net.refractions.udig.project.IMap;
018: import net.refractions.udig.project.internal.Layer;
019: import net.refractions.udig.project.internal.impl.LayerResource;
020: import net.refractions.udig.project.ui.ApplicationGIS;
021: import net.refractions.udig.project.ui.internal.MapImport;
022: import net.refractions.udig.project.ui.internal.ProjectUIPlugin;
023: import net.refractions.udig.project.ui.internal.Trace;
024: import net.refractions.udig.ui.ViewerDropLocation;
025:
026: import org.eclipse.core.runtime.IAdaptable;
027: import org.eclipse.core.runtime.IProgressMonitor;
028:
029: public class MapDropAction extends CatalogImportDropAction {
030:
031: @Override
032: public boolean accept() {
033: Object data2 = getData();
034: if (data2.getClass().isArray()) {
035: Object[] objects = ((Object[]) data2);
036: for (Object object : objects) {
037: if (!canAccept(object)) {
038: return false;
039: }
040: }
041: return true;
042: } else {
043: return canAccept(data2);
044: }
045:
046: }
047:
048: private boolean canAccept(Object data2) {
049: if (data2 instanceof LayerResource) {
050: ILayer layer = ((LayerResource) data2).getLayer();
051: if (desinationContainsLayer(layer))
052: return false;
053: if (destinationLayerMapContainsLayer(layer))
054: return false;
055: }
056: if (data2 instanceof IGeoResource) {
057: return true;
058: }
059: if (data2 instanceof IAdaptable) {
060: IAdaptable adaptable = (IAdaptable) data2;
061: if (adaptable.getAdapter(IGeoResource.class) != null)
062: return true;
063: }
064: return canImport(data2);
065: }
066:
067: private boolean destinationLayerMapContainsLayer(ILayer layer) {
068: if (getDestination() instanceof ILayer) {
069: ILayer dest = (ILayer) getDestination();
070: if (dest.getMap().getMapLayers().contains(layer))
071: return true;
072: }
073: return false;
074: }
075:
076: private boolean desinationContainsLayer(ILayer layer) {
077: if (getDestination() instanceof IMap) {
078: IMap map = (IMap) getDestination();
079: if (map.getMapLayers().contains(layer))
080: return true;
081: }
082: return false;
083: }
084:
085: @Override
086: public void perform(IProgressMonitor monitor) {
087: List<IGeoResource> resources = new ArrayList<IGeoResource>();
088: List<Object> otherData = new ArrayList<Object>();
089:
090: Object data2 = getData();
091: Object[] array;
092: if (data2.getClass().isArray()) {
093: array = (Object[]) data2;
094: for (int i = 0; i < array.length; i++) {
095: Object object = array[i];
096: seperateGeoResources(resources, otherData, object);
097: }
098: } else {
099: seperateGeoResources(resources, otherData, data2);
100: }
101:
102: int layerpos = -1;
103: layerpos = calculateDropPosition();
104: IMap map = null;
105: if (!otherData.isEmpty()) {
106: for (Object object : otherData) {
107: resources.addAll(toResources(monitor, object,
108: getClass()));
109: }
110: }
111: addResourcesToMap(resources, layerpos, map);
112: }
113:
114: static Collection<IGeoResource> toResources(
115: IProgressMonitor monitor, Object object, Class callingClass) {
116: // create a wizard that does not add to map. We will add all resources at once.
117: MapImport mapImport = new MapImport() {
118:
119: @Override
120: protected WorkflowWizard createWorkflowWizard(
121: Workflow workflow,
122: java.util.Map<Class<? extends State>, WorkflowWizardPage> map) {
123: return new MapImportWizard(workflow, map) {
124: @Override
125: protected boolean performFinish(
126: IProgressMonitor monitor) {
127: return true;
128: }
129: };
130: }
131: };
132: mapImport.run(monitor, object);
133:
134: ResourceSelectionState state = mapImport.getDialog()
135: .getWorkflowWizard().getWorkflow().getState(
136: ResourceSelectionState.class);
137:
138: Set<IGeoResource> keySet = state.getResources().keySet();
139:
140: ProjectUIPlugin
141: .trace(
142: Trace.DND,
143: callingClass,
144: "converted " + object + " to " + keySet + " IGeoResources.", null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
145:
146: return keySet;
147: }
148:
149: private void addResourcesToMap(List<IGeoResource> resources,
150: int layerpos, IMap map2) {
151: IMap map = map2;
152: if (getDestination() instanceof Layer) {
153: Layer layer = (Layer) getDestination();
154: map = layer.getMap();
155: }
156: if (map == null)
157: map = ApplicationGIS.getActiveMap();
158: if (map == null) {
159: ProjectUIPlugin
160: .trace(
161: Trace.DND,
162: getClass(),
163: "Creating a new map with from resources: " + resources, null); //$NON-NLS-1$
164: ApplicationGIS.addLayersToMap((IMap) null, resources,
165: layerpos);
166: } else {
167: ProjectUIPlugin
168: .trace(
169: Trace.DND,
170: getClass(),
171: "Adding resources: " + resources + " to map:" + map.getName(), null); //$NON-NLS-1$//$NON-NLS-2$
172: ApplicationGIS.addLayersToMap(map, resources, layerpos,
173: null, true);
174: }
175: }
176:
177: private void seperateGeoResources(List<IGeoResource> resources,
178: List<Object> otherData, Object object) {
179: if (object instanceof IGeoResource)
180: resources.add((IGeoResource) object);
181: else
182: otherData.add(processDropItem(object));
183: }
184:
185: private int calculateDropPosition() {
186: int layerpos = -1;
187: if (getDestination() instanceof ILayer) {
188: ILayer target = (ILayer) getDestination();
189: ViewerDropLocation location = getViewerLocation();
190: layerpos = target.getZorder();
191:
192: if (location == ViewerDropLocation.NONE) {
193: layerpos = 0;
194: }
195:
196: // Moving something AFTER a layer is the same as moving something BEFORE a layer.
197: // So we will use BEFORE as much as possible to prevent duplication here.
198: // This code will retrieve the layer before. Or the first one, if we are at the
199: // beginning of the list.
200: if (location == ViewerDropLocation.BEFORE) {
201: layerpos++;
202: }
203: if (location == ViewerDropLocation.ON) {
204: layerpos++;
205: }
206: }
207: return layerpos;
208: }
209:
210: private Object processDropItem(Object concreteData2) {
211: Object concreteData = concreteData2;
212: if (concreteData instanceof String) {
213: URL url = extractURL((String) concreteData);
214: if (url != null) {
215: concreteData = url;
216: }
217: }
218: return concreteData;
219: }
220:
221: }
|