001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor;
011:
012: import org.eclipse.jface.viewers.Viewer;
013: import org.eclipse.jface.viewers.ViewerDropAdapter;
014: import org.eclipse.swt.dnd.DND;
015: import org.eclipse.swt.dnd.TransferData;
016:
017: /**
018: * PDEDropAdapter
019: *
020: */
021: public class PDEDropAdapter extends ViewerDropAdapter {
022:
023: private IPDEDropParticipant fDropParticipant;
024:
025: private IPDESourceParticipant fSourceParticipant;
026:
027: private int fLastValidOperation;
028:
029: /**
030: * @param viewer
031: */
032: public PDEDropAdapter(Viewer viewer,
033: IPDEDropParticipant dropParticipant,
034: IPDESourceParticipant sourceParticipant) {
035: super (viewer);
036: fDropParticipant = dropParticipant;
037: fSourceParticipant = sourceParticipant;
038: resetLastValidOperation();
039: }
040:
041: /**
042: *
043: */
044: protected void resetLastValidOperation() {
045: fLastValidOperation = DND.DROP_NONE;
046: }
047:
048: /**
049: * @param currentOperation
050: * @return
051: */
052: protected int getLastValidOperation(int currentOperation) {
053: if (currentOperation != DND.DROP_NONE) {
054: fLastValidOperation = currentOperation;
055: }
056: return fLastValidOperation;
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
061: */
062: public boolean performDrop(Object data) {
063: // Clear the last valid operation for the next drop event
064: resetLastValidOperation();
065: // Get the original target object to drop source objects on
066: Object targetObject = getCurrentTarget();
067: // Get the drop location relative to the target object
068: int targetLocation = getCurrentLocation();
069: // Get the serialized / deserialized source objects to drop
070: Object[] sourceObjects = null;
071: if (data instanceof Object[]) {
072: sourceObjects = (Object[]) data;
073: } else {
074: sourceObjects = new Object[] { data };
075: }
076: // Get the current operation
077: int operation = getCurrentOperation();
078: // Drop the source objects on the target
079: // object given the specified operation
080: if (operation == DND.DROP_COPY) {
081: fDropParticipant.doDropCopy(targetObject, sourceObjects,
082: targetLocation);
083: } else if (operation == DND.DROP_MOVE) {
084: fDropParticipant.doDropMove(targetObject, sourceObjects,
085: targetLocation);
086: } else if (operation == DND.DROP_LINK) {
087: fDropParticipant.doDropLink(targetObject, sourceObjects,
088: targetLocation);
089: } else if (operation == DND.DROP_DEFAULT) {
090: fDropParticipant.doDropMove(targetObject, sourceObjects,
091: targetLocation);
092: } else {
093: return false;
094: }
095:
096: return true;
097: }
098:
099: /* (non-Javadoc)
100: * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
101: */
102: public boolean validateDrop(Object targetObject, int operation,
103: TransferData transferType) {
104: // Current operation listed is not set until after the drop is validated
105: // i.e. This method call
106: // Replace the current operation with the last valid operation
107: operation = getLastValidOperation(operation);
108: // Get the original target object to drop source objects on
109: targetObject = getCurrentTarget();
110: // Get the drop location relative to the target object
111: int targetLocation = getCurrentLocation();
112: // Ensure we have a model transfer operation
113: if (ModelDataTransfer.getInstance().isSupportedType(
114: transferType) == false) {
115: return false;
116: }
117: // Ensure the location is defined
118: if (targetLocation == LOCATION_NONE) {
119: return false;
120: }
121: // Get the original source objects
122: Object[] sourceObjects = fSourceParticipant.getSourceObjects();
123: // Ensure we have source objects
124: if (sourceObjects == null) {
125: return false;
126: }
127: if (sourceObjects.length == 0) {
128: return false;
129: }
130: // Ensure the target object is defined
131: if (targetObject == null) {
132: return false;
133: }
134: // Determine whether the source objects can be dropped on the target
135: // object given the specified operation
136: if (operation == DND.DROP_COPY) {
137: return validateDropCopy(targetObject, sourceObjects,
138: targetLocation);
139: } else if (operation == DND.DROP_MOVE) {
140: return validateDropMove(targetObject, sourceObjects,
141: targetLocation);
142: } else if (operation == DND.DROP_LINK) {
143: return validateDropLink(targetObject, sourceObjects,
144: targetLocation);
145: } else if (operation == DND.DROP_DEFAULT) {
146: return validateDropDefault(targetObject, sourceObjects,
147: targetLocation);
148: }
149: return false;
150: }
151:
152: /**
153: * @param targetObject
154: * @param sourceObjects
155: * @param targetLocation
156: * @return
157: */
158: protected boolean validateDropCopy(Object targetObject,
159: Object[] sourceObjects, int targetLocation) {
160: return fDropParticipant.canDropCopy(targetObject,
161: sourceObjects, targetLocation);
162: }
163:
164: /**
165: * @param targetObject
166: * @param sourceObjects
167: * @param targetLocation
168: * @return
169: */
170: protected boolean validateDropMove(Object targetObject,
171: Object[] sourceObjects, int targetLocation) {
172: // Source objects have not been serialized yet.
173: // As a result we can compare whether a source and target object is
174: // equal
175: // Ensure the target is valid for a move operation and not redundant
176: // Meaning there is no effect of the move
177: for (int i = 0; i < sourceObjects.length; i++) {
178: if (targetObject.equals(sourceObjects[i])) {
179: // No source objects are allowed to be dropped on themselves for
180: // move operations
181: return false;
182: }
183: }
184: return fDropParticipant.canDropMove(targetObject,
185: sourceObjects, targetLocation);
186: }
187:
188: /**
189: * @param targetObject
190: * @param sourceObjects
191: * @param targetLocation
192: * @return
193: */
194: protected boolean validateDropLink(Object targetObject,
195: Object[] sourceObjects, int targetLocation) {
196: return fDropParticipant.canDropLink(targetObject,
197: sourceObjects, targetLocation);
198: }
199:
200: /**
201: * @param targetObject
202: * @param sourceObjects
203: * @param targetLocation
204: * @return
205: */
206: protected boolean validateDropDefault(Object targetObject,
207: Object[] sourceObjects, int targetLocation) {
208: return validateDropMove(targetObject, sourceObjects,
209: targetLocation);
210: }
211:
212: }
|