001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.schema;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.jface.viewers.ISelectionProvider;
016: import org.eclipse.jface.viewers.IStructuredSelection;
017: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
018: import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
019: import org.eclipse.swt.dnd.DragSource;
020: import org.eclipse.swt.dnd.DragSourceAdapter;
021: import org.eclipse.swt.dnd.DragSourceEvent;
022: import org.eclipse.swt.dnd.TextTransfer;
023: import org.eclipse.swt.widgets.Control;
024:
025: public class ElementSectionDragAdapter extends DragSourceAdapter {
026: private ISelectionProvider fSelectionProvider;
027: private Object fDragData;
028:
029: /**
030: * NavigatorDragAction constructor comment.
031: */
032: public ElementSectionDragAdapter(ISelectionProvider provider) {
033: fSelectionProvider = provider;
034:
035: }
036:
037: /**
038: * Returns the data to be transferred in a drag and drop
039: * operation.
040: */
041: public void dragSetData(DragSourceEvent event) {
042: if (event.doit == false)
043: return;
044: if (ModelDataTransfer.getInstance().isSupportedType(
045: event.dataType)) {
046: event.data = getSelectedModelObjects();
047: fDragData = event.data;
048: return;
049: }
050: if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
051: event.data = createTextualRepresentation((IStructuredSelection) fSelectionProvider
052: .getSelection());
053: fDragData = null;
054: return;
055: }
056: }
057:
058: private String createTextualRepresentation(IStructuredSelection sel) {
059: StringBuffer buf = new StringBuffer();
060: for (Iterator iter = sel.iterator(); iter.hasNext();) {
061: String name = iter.next().toString();
062: buf.append(name);
063: buf.append(" "); //$NON-NLS-1$
064: }
065: return buf.toString();
066: }
067:
068: /**
069: * All selection must be named model objects.
070: */
071: public void dragStart(DragSourceEvent event) {
072:
073: // Workaround for 1GEUS9V
074: DragSource dragSource = (DragSource) event.widget;
075: Control control = dragSource.getControl();
076: if (control != control.getDisplay().getFocusControl()) {
077: event.doit = false;
078: return;
079: }
080:
081: event.doit = canDrag();
082: }
083:
084: public void dragFinished(DragSourceEvent event) {
085: if (event.doit == false || fDragData == null)
086: return;
087: fDragData = null;
088: }
089:
090: private boolean canDrag() {
091: return canCopy((IStructuredSelection) fSelectionProvider
092: .getSelection());
093: }
094:
095: private boolean canCopy(IStructuredSelection selection) {
096: Object prev = null;
097: for (Iterator iter = selection.iterator(); iter.hasNext();) {
098: Object obj = iter.next();
099: if (!(obj instanceof ISchemaObject))
100: return false;
101: if (prev != null) {
102: if (prev.getClass().equals(obj.getClass()) == false)
103: return false;
104: } else
105: prev = obj;
106: }
107: return true;
108: }
109:
110: private ISchemaObject[] getSelectedModelObjects() {
111: return createObjectRepresentation((IStructuredSelection) fSelectionProvider
112: .getSelection());
113: }
114:
115: private ISchemaObject[] createObjectRepresentation(
116: IStructuredSelection selection) {
117: ArrayList objects = new ArrayList();
118: for (Iterator iter = selection.iterator(); iter.hasNext();) {
119: Object obj = iter.next();
120: if (obj instanceof ISchemaObject)
121: objects.add(obj);
122: else
123: return new ISchemaObject[0];
124: }
125: return (ISchemaObject[]) objects
126: .toArray(new ISchemaObject[objects.size()]);
127: }
128:
129: public Object[] getDragData() {
130: if (fDragData instanceof Object[])
131: return (Object[]) fDragData;
132: return new Object[] { fDragData };
133: }
134: }
|