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 java.io.PrintWriter;
013: import java.io.StringWriter;
014: import java.util.ArrayList;
015: import java.util.Iterator;
016:
017: import org.eclipse.jface.viewers.IStructuredSelection;
018: import org.eclipse.pde.internal.core.text.toc.TocObject;
019: import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
020: import org.eclipse.swt.dnd.DND;
021: import org.eclipse.swt.dnd.DragSourceEvent;
022: import org.eclipse.swt.dnd.DragSourceListener;
023: import org.eclipse.swt.dnd.TextTransfer;
024:
025: /**
026: * TocDragAdapter implements the drag behaviour for the TOC tree section.
027: */
028: public class TocDragAdapter implements DragSourceListener {
029: //The TOC Tree Section being dragged from
030: private TocTreeSection fSection;
031: //The dragged items
032: private ArrayList fDraggedItems;
033:
034: /**
035: * Constructs a new Drag Adapter with the specified selection
036: * provider and TocTreeSection
037: *
038: * @param provider The provider of the dragged items
039: * @param section The section that will handle removal
040: */
041: public TocDragAdapter(TocTreeSection section) {
042: fSection = section;
043: }
044:
045: /* (non-Javadoc)
046: * @see org.eclipse.swt.dnd.DragSourceListener#dragStart(org.eclipse.swt.dnd.DragSourceEvent)
047: */
048: public void dragStart(DragSourceEvent event) {
049: if (event.doit) { //The event should only be enabled if there is a selection to drag
050: event.doit = !fSection.getSelection().isEmpty();
051: }
052: }
053:
054: /* (non-Javadoc)
055: * @see org.eclipse.swt.dnd.DragSourceListener#dragSetData(org.eclipse.swt.dnd.DragSourceEvent)
056: */
057: public void dragSetData(DragSourceEvent event) {
058: //Check if the drag is still enabled
059: if (event.doit) {
060: IStructuredSelection sel = (IStructuredSelection) fSection
061: .getSelection();
062:
063: if (TextTransfer.getInstance().isSupportedType(
064: event.dataType)) { //If the expected data is text, then write out the selection
065: //into its XML representation
066:
067: StringWriter sw = new StringWriter();
068: PrintWriter writer = new PrintWriter(sw);
069:
070: //Write the XML representation of each selected object
071: for (Iterator iter = sel.iterator(); iter.hasNext();) {
072: Object obj = iter.next();
073: if (obj instanceof TocObject) {
074: ((TocObject) obj).write("", writer); //$NON-NLS-1$
075: }
076: }
077:
078: //Set the event's drag object to be this String
079: event.data = sw.toString();
080: //Set the array of dragged items to null,
081: //since we are dragging a String
082: fDraggedItems = null;
083: } else if (ModelDataTransfer.getInstance().isSupportedType(
084: event.dataType)) {
085: //If we are dragging items from the model
086: fDraggedItems = getSelectedObjects(sel);
087: TocObject[] selectedObjects = (TocObject[]) fDraggedItems
088: .toArray(new TocObject[fDraggedItems.size()]);
089: if (selectedObjects.length == 0) { //disable the drag if there are no items selected
090: event.doit = false;
091: } else { //set the event's drag object to the selection
092: event.data = selectedObjects;
093: }
094: }
095: }
096: }
097:
098: /**
099: * @param selection The selection to place in the ArrayList
100: * @return an ArrayList containing all removable TocObjects in the selection
101: */
102: private ArrayList getSelectedObjects(IStructuredSelection selection) {
103: ArrayList objects = new ArrayList();
104: for (Iterator iter = selection.iterator(); iter.hasNext();) {
105: Object obj = iter.next();
106: if (obj instanceof TocObject
107: && ((TocObject) obj).canBeRemoved()) { //If the object is a removable TocObject, add it
108: objects.add(obj);
109: } else { //If the object is not a removable TocObject,
110: //we don't want to permit the drag, so return an empty list
111: return new ArrayList();
112: }
113: }
114:
115: return objects;
116: }
117:
118: /* (non-Javadoc)
119: * @see org.eclipse.swt.dnd.DragSourceListener#dragFinished(org.eclipse.swt.dnd.DragSourceEvent)
120: */
121: public void dragFinished(DragSourceEvent event) {
122: if (event.detail == DND.DROP_MOVE && fDraggedItems != null) {
123: fSection.handleDrag(fDraggedItems);
124: }
125:
126: fDraggedItems = null;
127: }
128:
129: public ArrayList getDraggedElements() {
130: return fDraggedItems;
131: }
132: }
|