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.internal;
018:
019: import java.util.ArrayList;
020: import java.util.Collection;
021: import java.util.List;
022:
023: import net.refractions.udig.core.internal.CorePlugin;
024: import net.refractions.udig.project.internal.Layer;
025: import net.refractions.udig.project.internal.Map;
026: import net.refractions.udig.project.internal.Project;
027:
028: import org.eclipse.emf.ecore.util.EcoreUtil;
029: import org.eclipse.jface.viewers.IStructuredSelection;
030: import org.eclipse.jface.viewers.Viewer;
031: import org.eclipse.jface.viewers.ViewerDropAdapter;
032: import org.eclipse.swt.dnd.TransferData;
033:
034: public class ProjectExplorerDropAdapter extends ViewerDropAdapter {
035:
036: protected ProjectExplorerDropAdapter(Viewer viewer) {
037: super (viewer);
038: }
039:
040: @Override
041: public boolean performDrop(Object data) {
042:
043: /*
044: * First, get the data we can work with - Layers.
045: */
046:
047: List<Layer> layers = getLayers(data);
048:
049: /*
050: * What we do next depends on where the data is being dropped, and where it is coming from.
051: */
052:
053: /*
054: * They are being dropped on a Map
055: */
056: if (getCurrentLocation() == LOCATION_ON
057: && getCurrentTarget() instanceof Map) {
058: Map map = (Map) getCurrentTarget();
059:
060: /*
061: * If all the layers belong to the target map, do nothing.
062: */
063: if (map.getContextModel().getLayers().containsAll(layers)) {
064: return false;
065: }
066: copyToMap(map, layers);
067: }
068:
069: /*
070: * They are being dropped on a Project
071: */
072: if (getCurrentLocation() == LOCATION_ON
073: && getCurrentTarget() instanceof Project) {
074: createNewMap((Project) getCurrentTarget(), layers);
075: }
076:
077: return true;
078: }
079:
080: private void createNewMap(Project project, List<Layer> layers) {
081: MapFactory.instance().process(project, layers, true);
082: }
083:
084: private void copyToMap(Map map, List<Layer> layers) {
085: Collection<Layer> clonedLayers = EcoreUtil.copyAll(layers);
086: map.getContextModel().getLayers().addAll(clonedLayers);
087: }
088:
089: private List<Layer> getLayers(Object data) {
090: List<Object> resources = new ArrayList<Object>();
091:
092: if (data instanceof String || data instanceof String[]) {
093: if (data instanceof String) {
094: resources.addAll(CorePlugin
095: .stringsToURLs((String) data));
096: } else {
097: resources.addAll(CorePlugin
098: .stringsToURLs((String[]) data));
099: }
100: } else if (data instanceof IStructuredSelection) {
101: IStructuredSelection selection = (IStructuredSelection) data;
102:
103: resources.addAll(selection.toList());
104: } else {
105: resources.add(data);
106: }
107:
108: return MapFactory.instance().processResources(null, resources);
109: }
110:
111: @Override
112: public boolean validateDrop(Object target, int operation,
113: TransferData transferType) {
114: return true;
115: }
116:
117: }
|