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.toc;
011:
012: import org.eclipse.core.resources.ResourcesPlugin;
013: import org.eclipse.core.runtime.IPath;
014: import org.eclipse.core.runtime.Path;
015: import org.eclipse.jface.viewers.TreeViewer;
016: import org.eclipse.jface.viewers.ViewerDropAdapter;
017: import org.eclipse.pde.core.IBaseModel;
018: import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
019: import org.eclipse.swt.dnd.DND;
020: import org.eclipse.swt.dnd.DropTargetEvent;
021: import org.eclipse.swt.dnd.FileTransfer;
022: import org.eclipse.swt.dnd.TransferData;
023: import org.eclipse.swt.graphics.Point;
024: import org.eclipse.swt.graphics.Rectangle;
025: import org.eclipse.swt.widgets.Item;
026:
027: /**
028: * TocDropAdapter - implements drop behaviour for the TOC Tree Section.
029: * It extends ViewerDropAdapter for advanced feedback behaviour, but
030: * mostly overrides the specified drop behaviour.
031: */
032: public class TocDropAdapter extends ViewerDropAdapter {
033: private TocTreeSection fSection;
034:
035: /**
036: * Constant describing the position of the cursor relative
037: * to the target object. This means the mouse is positioned
038: * slightly after the target, but not after its children if it is
039: * expanded.
040: * @see #getCurrentLocation()
041: */
042: public static final int LOCATION_JUST_AFTER = 5;
043:
044: public TocDropAdapter(TreeViewer tocTree, TocTreeSection section) {
045: super (tocTree);
046: fSection = section;
047: }
048:
049: /**
050: * Returns the position of the given event's coordinates relative to its target.
051: * The position is determined to be before, after, or on the item, based on
052: * some threshold value.
053: *
054: * @param event the event
055: * @return one of the <code>LOCATION_* </code>constants defined in this class
056: */
057: protected int determineLocation(DropTargetEvent event) {
058: if (!(event.item instanceof Item)) {
059: return LOCATION_NONE;
060: }
061: Item item = (Item) event.item;
062: Point coordinates = new Point(event.x, event.y);
063: coordinates = getViewer().getControl().toControl(coordinates);
064: if (item != null) {
065: Rectangle bounds = getBounds(item);
066: if (bounds == null) {
067: return LOCATION_NONE;
068: }
069: if ((coordinates.y - bounds.y) < 5) {
070: return LOCATION_BEFORE;
071: }
072: if ((bounds.y + bounds.height - coordinates.y) < 5) {
073: if ((bounds.y - coordinates.y) < 5) {
074: return LOCATION_JUST_AFTER;
075: }
076: return LOCATION_AFTER;
077: }
078: }
079: return LOCATION_ON;
080: }
081:
082: /* (non-Javadoc)
083: * A new drag has entered the widget. Do file validation if necessary,
084: * and then set the Drag and Drop mode.
085: */
086: public void dragEnter(DropTargetEvent event) {
087: validateFileDrop(event);
088: setDNDMode(event);
089: }
090:
091: /* (non-Javadoc)
092: * Override the dragOver behaviour to directly supply event feedback
093: * but do nothing else.
094: */
095: public void dragOver(DropTargetEvent event) {
096: int currentLocation = determineLocation(event);
097: switch (currentLocation) {
098: case LOCATION_BEFORE:
099: event.feedback = DND.FEEDBACK_INSERT_BEFORE;
100: break;
101: case LOCATION_AFTER:
102: case LOCATION_JUST_AFTER:
103: event.feedback = DND.FEEDBACK_INSERT_AFTER;
104: break;
105: case LOCATION_ON:
106: default:
107: event.feedback = DND.FEEDBACK_SELECT;
108: break;
109: }
110:
111: event.feedback |= DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL;
112: }
113:
114: /* (non-Javadoc)
115: * The Drag and Drop operation changed. Change the operation to a valid one
116: * if necessary.
117: */
118: public void dragOperationChanged(DropTargetEvent event) {
119: validateFileDrop(event);
120: setDNDMode(event);
121: }
122:
123: /**
124: * Set the Drag and Drop mode depending on the dragged items and event
125: * details. Files can only be copied, not linked or moved.
126: * Model data objects can have any operation occur.
127: *
128: * All other objects cannot be dropped.
129: *
130: * @param event The drop event to change.
131: */
132: private void setDNDMode(DropTargetEvent event) {
133: if (FileTransfer.getInstance().isSupportedType(
134: event.currentDataType)) { //If a file is being dragged
135: if (event.detail == DND.DROP_DEFAULT) { //If no modifier key is pressed
136: //set the operation to DROP_COPY if available
137: //DROP_NONE otherwise
138: event.detail = (event.operations & DND.DROP_COPY);
139: } else { //If a modifier key is pressed for a file and the operation isn't a copy,
140: //disallow it
141: event.detail &= DND.DROP_COPY;
142: }
143: }
144: //The only other transfer type allowed is a Model Data Transfer
145: else if (!ModelDataTransfer.getInstance().isSupportedType(
146: event.currentDataType)) { //disallow drag if the transfer is not Model Data or Files
147: event.detail = DND.DROP_NONE;
148: }
149: }
150:
151: /**
152: * Ensure that, if files are being dropped, they have valid
153: * file extensions for the TOC Editor (HTML pages and XML documents).
154: *
155: * Invalidate the drop if this condition is not met.
156: *
157: * @param event The drop event containing the transfer.
158: */
159: private void validateFileDrop(DropTargetEvent event) {
160: if (FileTransfer.getInstance().isSupportedType(
161: event.currentDataType)) {
162: IBaseModel model = fSection.getPage().getModel();
163: String[] fileNames = (String[]) FileTransfer.getInstance()
164: .nativeToJava(event.currentDataType);
165: for (int i = 0; i < fileNames.length; i++) {
166: IPath path = new Path(fileNames[i]);
167:
168: // Make sure that the file is in the workspace
169: if (ResourcesPlugin.getWorkspace().getRoot()
170: .getFileForLocation(path) == null) {
171: event.detail = DND.DROP_NONE;
172: return;
173: }
174:
175: if (!TocExtensionUtil.hasValidPageExtension(path)
176: && !TocExtensionUtil.isTOCFile(path)) {
177: event.detail = DND.DROP_NONE;
178: return;
179: }
180:
181: // Make sure that the user isn't dropping a TOC into itself
182: if (TocExtensionUtil.isCurrentResource(path, model)) {
183: event.detail = DND.DROP_NONE;
184: return;
185: }
186: }
187: }
188: }
189:
190: /* (non-Javadoc)
191: * Override the drop behaviour in order to directly manage the drop event
192: */
193: public void drop(DropTargetEvent event) {
194: Object target = determineTarget(event);
195: int location = determineLocation(event);
196: if (!fSection.performDrop(target, event.data, location)) {
197: event.detail = DND.DROP_NONE;
198: }
199: }
200:
201: /* (non-Javadoc)
202: * Override the behaviour of ViewerDropAdapter#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
203: *
204: * @see org.eclipse.swt.dnd.DropTargetAdapter#dragLeave(org.eclipse.swt.dnd.DropTargetEvent)
205: */
206: public void dragLeave(DropTargetEvent event) { //NO-OP
207: }
208:
209: /* (non-Javadoc)
210: * Override the behaviour of ViewerDropAdapter#dropAccept(org.eclipse.swt.dnd.DropTargetEvent)
211: *
212: * @see org.eclipse.swt.dnd.DropTargetAdapter#dropAccept(org.eclipse.swt.dnd.DropTargetEvent)
213: */
214: public void dropAccept(DropTargetEvent event) { //NO-OP
215: }
216:
217: //These methods are never called because much of ViewerDropAdapter's
218: //behaviour is overridden, but they must be implemented.
219:
220: /* (non-Javadoc)
221: * @see org.eclipse.jface.viewers.ViewerDropAdapter#performDrop(java.lang.Object)
222: */
223: public boolean performDrop(Object data) {
224: return false;
225: }
226:
227: /* (non-Javadoc)
228: * @see org.eclipse.jface.viewers.ViewerDropAdapter#validateDrop(java.lang.Object, int, org.eclipse.swt.dnd.TransferData)
229: */
230: public boolean validateDrop(Object target, int operation,
231: TransferData transferType) {
232: return false;
233: }
234:
235: }
|