KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > schema > ElementSectionDragAdapter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.schema;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.pde.internal.core.ischema.ISchemaObject;
19 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer;
20 import org.eclipse.swt.dnd.DragSource;
21 import org.eclipse.swt.dnd.DragSourceAdapter;
22 import org.eclipse.swt.dnd.DragSourceEvent;
23 import org.eclipse.swt.dnd.TextTransfer;
24 import org.eclipse.swt.widgets.Control;
25
26 public class ElementSectionDragAdapter extends DragSourceAdapter {
27     private ISelectionProvider fSelectionProvider;
28     private Object JavaDoc fDragData;
29
30     /**
31      * NavigatorDragAction constructor comment.
32      */

33     public ElementSectionDragAdapter(ISelectionProvider provider) {
34         fSelectionProvider = provider;
35
36     }
37
38     /**
39      * Returns the data to be transferred in a drag and drop
40      * operation.
41      */

42     public void dragSetData(DragSourceEvent event) {
43         if (event.doit == false)
44             return;
45         if (ModelDataTransfer.getInstance().isSupportedType(event.dataType)) {
46             event.data = getSelectedModelObjects();
47             fDragData = event.data;
48             return;
49         }
50         if (TextTransfer.getInstance().isSupportedType(event.dataType)) {
51             event.data =
52                 createTextualRepresentation(
53                     (IStructuredSelection) fSelectionProvider.getSelection());
54             fDragData = null;
55             return;
56         }
57     }
58
59     private String JavaDoc createTextualRepresentation(IStructuredSelection sel) {
60         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
61         for (Iterator JavaDoc iter = sel.iterator(); iter.hasNext();) {
62             String JavaDoc name = iter.next().toString();
63             buf.append(name);
64             buf.append(" "); //$NON-NLS-1$
65
}
66         return buf.toString();
67     }
68     /**
69      * All selection must be named model objects.
70      */

71     public void dragStart(DragSourceEvent event) {
72
73         // Workaround for 1GEUS9V
74
DragSource dragSource = (DragSource) event.widget;
75         Control control = dragSource.getControl();
76         if (control != control.getDisplay().getFocusControl()) {
77             event.doit = false;
78             return;
79         }
80
81         event.doit = canDrag();
82     }
83
84     public void dragFinished(DragSourceEvent event) {
85         if (event.doit == false || fDragData == null)
86             return;
87         fDragData = null;
88     }
89
90     private boolean canDrag() {
91         return canCopy((IStructuredSelection) fSelectionProvider.getSelection());
92     }
93
94     private boolean canCopy(IStructuredSelection selection) {
95         Object JavaDoc prev = null;
96         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
97             Object JavaDoc obj = iter.next();
98             if (!(obj instanceof ISchemaObject))
99                 return false;
100             if (prev != null) {
101                 if (prev.getClass().equals(obj.getClass()) == false)
102                     return false;
103             } else
104                 prev = obj;
105         }
106         return true;
107     }
108
109     private ISchemaObject[] getSelectedModelObjects() {
110         return createObjectRepresentation(
111             (IStructuredSelection) fSelectionProvider.getSelection());
112     }
113
114     private ISchemaObject[] createObjectRepresentation(IStructuredSelection selection) {
115         ArrayList JavaDoc objects = new ArrayList JavaDoc();
116         for (Iterator JavaDoc iter = selection.iterator(); iter.hasNext();) {
117             Object JavaDoc obj = iter.next();
118             if (obj instanceof ISchemaObject)
119                 objects.add(obj);
120             else
121                 return new ISchemaObject[0];
122         }
123         return (ISchemaObject[]) objects.toArray(
124             new ISchemaObject[objects.size()]);
125     }
126     
127     public Object JavaDoc[] getDragData() {
128         if (fDragData instanceof Object JavaDoc[])
129             return (Object JavaDoc[])fDragData;
130         return new Object JavaDoc[] { fDragData };
131     }
132 }
133
Popular Tags