X-Git-Url: http://secure.phpeclipse.com diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/dnd/JdtViewerDropAdapter.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/dnd/JdtViewerDropAdapter.java index 805efb3..7e2143b 100644 --- a/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/dnd/JdtViewerDropAdapter.java +++ b/net.sourceforge.phpeclipse/src/net/sourceforge/phpdt/internal/ui/dnd/JdtViewerDropAdapter.java @@ -10,6 +10,8 @@ *******************************************************************************/ package net.sourceforge.phpdt.internal.ui.dnd; +import org.eclipse.jface.util.Assert; +import org.eclipse.jface.viewers.StructuredViewer; import org.eclipse.swt.dnd.DND; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.DropTargetListener; @@ -19,107 +21,116 @@ import org.eclipse.swt.widgets.Item; import org.eclipse.swt.widgets.TableItem; import org.eclipse.swt.widgets.TreeItem; -import org.eclipse.jface.util.Assert; -import org.eclipse.jface.viewers.StructuredViewer; - /** - * A drag and drop adapter to be used together with structured viewers. - * The adapater delegates the dragEnter, dragOperationChanged - * , dragOver and dropAccept method to the + * A drag and drop adapter to be used together with structured viewers. The + * adapater delegates the dragEnter, dragOperationChanged + * , + * dragOver and dropAccept method to the * validateDrop method. Furthermore it adds location feedback. */ public class JdtViewerDropAdapter implements DropTargetListener { /** - * Constant describing the position of the mouse cursor relative - * to the target object. This means the mouse is positioned - * slightly before the target. + * Constant describing the position of the mouse cursor relative to the + * target object. This means the mouse is positioned slightly before the + * target. */ - protected static final int LOCATION_BEFORE= 1; - + protected static final int LOCATION_BEFORE = 1; + /** - * Constant describing the position of the mouse cursor relative - * to the target object. This means the mouse is positioned - * slightly after the target. + * Constant describing the position of the mouse cursor relative to the + * target object. This means the mouse is positioned slightly after the + * target. */ - protected static final int LOCATION_AFTER= 2; - + protected static final int LOCATION_AFTER = 2; + /** - * Constant describing the position of the mouse cursor relative - * to the target object. This means the mouse is positioned - * directly on the target. + * Constant describing the position of the mouse cursor relative to the + * target object. This means the mouse is positioned directly on the target. */ - protected static final int LOCATION_ON= 3; - + protected static final int LOCATION_ON = 3; + /** - * Constant describing the position of the mouse cursor relative - * to the target object. This means the mouse is not positioned - * over or near any valid target. + * Constant describing the position of the mouse cursor relative to the + * target object. This means the mouse is not positioned over or near any + * valid target. */ - protected static final int LOCATION_NONE= 4; - + protected static final int LOCATION_NONE = 4; + /** - * The threshold used to determine if the mouse is before or after - * an item. + * The threshold used to determine if the mouse is before or after an item. */ - private static final int LOCATION_EPSILON= 5; - + private static final int LOCATION_EPSILON = 5; + /** * Style to enable location feedback. */ - public static final int INSERTION_FEEDBACK= 1 << 1; + public static final int INSERTION_FEEDBACK = 1 << 1; private StructuredViewer fViewer; + private int fFeedback; + private boolean fShowInsertionFeedback; + private int fRequestedOperation; + private int fLastOperation; + protected int fLocation; + protected Object fTarget; public JdtViewerDropAdapter(StructuredViewer viewer, int feedback) { - fViewer= viewer; + fViewer = viewer; Assert.isNotNull(fViewer); - fFeedback= feedback; - fLastOperation= -1; + fFeedback = feedback; + fLastOperation = -1; } /** * Controls whether the drop adapter shows insertion feedback or not. * - * @param showInsertionFeedback true if the drop adapter is supposed - * to show insertion feedback. Otherwise false + * @param showInsertionFeedback + * true if the drop adapter is supposed to show + * insertion feedback. Otherwise false */ public void showInsertionFeedback(boolean showInsertionFeedback) { - fShowInsertionFeedback= showInsertionFeedback; + fShowInsertionFeedback = showInsertionFeedback; } - + /** * Returns the viewer this adapter is working on. */ protected StructuredViewer getViewer() { return fViewer; - } - - //---- Hooks to override ----------------------------------------------------- - + } + + // ---- Hooks to override + // ----------------------------------------------------- + /** - * The actual drop has occurred. Calls drop(Object target, DropTargetEvent event) + * The actual drop has occurred. Calls + * drop(Object target, DropTargetEvent event) * . + * * @see DropTargetListener#drop(org.eclipse.swt.dnd.DropTargetEvent) - */ + */ public void drop(DropTargetEvent event) { drop(fTarget, event); } - + /** * The actual drop has occurred. - * @param target the drop target in form of a domain element. - * @param event the drop traget event - */ + * + * @param target + * the drop target in form of a domain element. + * @param event + * the drop traget event + */ public void drop(Object target, DropTargetEvent event) { } - + /** * Checks if the drop is valid. The method calls validateDrop * (Object target, DropTargetEvent event). Implementors can alter the @@ -129,79 +140,86 @@ public class JdtViewerDropAdapter implements DropTargetListener { public void validateDrop(DropTargetEvent event) { validateDrop(fTarget, event, fRequestedOperation); } - + /** - * Checks if the drop on the current target is valid. The method - * can alter the currentDataType field and the - * detail field to give feedback about drop acceptence. - * @param target the drop target in form of a domain element. - * @param event the drop traget event - * @param operation the operation requested by the user. + * Checks if the drop on the current target is valid. The method can alter + * the currentDataType field and the + * detail + * field to give feedback about drop acceptence. + * + * @param target + * the drop target in form of a domain element. + * @param event + * the drop traget event + * @param operation + * the operation requested by the user. */ public void validateDrop(Object target, DropTargetEvent event, int operation) { } - + public void dragEnter(DropTargetEvent event) { dragOperationChanged(event); } - + public void dragLeave(DropTargetEvent event) { - fTarget= null; - fLocation= LOCATION_NONE; + fTarget = null; + fLocation = LOCATION_NONE; } - + public void dragOperationChanged(DropTargetEvent event) { - fRequestedOperation= event.detail; - fTarget= computeTarget(event); - fLocation= computeLocation(event); + fRequestedOperation = event.detail; + fTarget = computeTarget(event); + fLocation = computeLocation(event); validateDrop(event); - fLastOperation= event.detail; + fLastOperation = event.detail; computeFeedback(event); } - + public void dragOver(DropTargetEvent event) { - Object oldTarget= fTarget; - fTarget= computeTarget(event); - - //set the location feedback - int oldLocation= fLocation; - fLocation= computeLocation(event); - if (oldLocation != fLocation || oldTarget != fTarget || fLastOperation != event.detail) { + Object oldTarget = fTarget; + fTarget = computeTarget(event); + + // set the location feedback + int oldLocation = fLocation; + fLocation = computeLocation(event); + if (oldLocation != fLocation || oldTarget != fTarget + || fLastOperation != event.detail) { validateDrop(event); - fLastOperation= event.detail; + fLastOperation = event.detail; } else { - event.detail= fLastOperation; + event.detail = fLastOperation; } computeFeedback(event); } - + public void dropAccept(DropTargetEvent event) { - fTarget= computeTarget(event); + fTarget = computeTarget(event); validateDrop(event); - fLastOperation= event.detail; + fLastOperation = event.detail; } - + /** - * Returns the data held by event.item. Inside a viewer - * this corresponds to the items data model element. + * Returns the data held by event.item. Inside a viewer this + * corresponds to the items data model element. */ protected Object computeTarget(DropTargetEvent event) { return event.item == null ? null : event.item.getData(); } - + /** - * Returns the position of the given coordinates relative to the given target. - * The position is determined to be before, after, or on the item, based on - * some threshold value. The return value is one of the LOCATION_* constants - * defined in this class. + * Returns the position of the given coordinates relative to the given + * target. The position is determined to be before, after, or on the item, + * based on some threshold value. The return value is one of the LOCATION_* + * constants defined in this class. */ final protected int computeLocation(DropTargetEvent event) { if (!(event.item instanceof Item)) return LOCATION_NONE; - - Item item= (Item) event.item; - Point coordinates= fViewer.getControl().toControl(new Point(event.x, event.y)); - Rectangle bounds= getBounds(item); + + Item item = (Item) event.item; + Point coordinates = fViewer.getControl().toControl( + new Point(event.x, event.y)); + Rectangle bounds = getBounds(item); if (bounds == null) { return LOCATION_NONE; } @@ -215,47 +233,48 @@ public class JdtViewerDropAdapter implements DropTargetListener { } /** - * Returns the bounds of the given item, or null if it is not a - * valid type of item. + * Returns the bounds of the given item, or null if it is not + * a valid type of item. */ private Rectangle getBounds(Item item) { if (item instanceof TreeItem) return ((TreeItem) item).getBounds(); - + if (item instanceof TableItem) return ((TableItem) item).getBounds(0); - + return null; } /** - * Sets the drag under feedback corresponding to the value of fLocation - * and the INSERTION_FEEDBACK style bit. + * Sets the drag under feedback corresponding to the value of + * fLocation and the INSERTION_FEEDBACK style + * bit. */ protected void computeFeedback(DropTargetEvent event) { if (!fShowInsertionFeedback && fLocation != LOCATION_NONE) { - event.feedback= DND.FEEDBACK_SELECT; + event.feedback = DND.FEEDBACK_SELECT; } else { if (fLocation == LOCATION_BEFORE) { - event.feedback= DND.FEEDBACK_INSERT_BEFORE; + event.feedback = DND.FEEDBACK_INSERT_BEFORE; } else if (fLocation == LOCATION_AFTER) { - event.feedback= DND.FEEDBACK_INSERT_AFTER; + event.feedback = DND.FEEDBACK_INSERT_AFTER; } } - event.feedback|= fFeedback; + event.feedback |= fFeedback; } - + /** * Sets the drop operation to DROP_NODE. */ protected void clearDropOperation(DropTargetEvent event) { - event.detail= DND.DROP_NONE; + event.detail = DND.DROP_NONE; } - + /** * Returns the requested drop operation. */ protected int getRequestedOperation() { return fRequestedOperation; - } + } }