KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > dnd > CommonDropAdapterDescriptor


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.navigator.dnd;
13
14 import org.eclipse.core.expressions.EvaluationContext;
15 import org.eclipse.core.expressions.EvaluationResult;
16 import org.eclipse.core.expressions.Expression;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.internal.navigator.CustomAndExpression;
21 import org.eclipse.ui.internal.navigator.NavigatorPlugin;
22 import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
23 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
24 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
25
26 /**
27  * @since 3.2
28  *
29  */

30 public final class CommonDropAdapterDescriptor implements
31         INavigatorContentExtPtConstants {
32
33     private final IConfigurationElement element;
34
35     private final INavigatorContentDescriptor contentDescriptor;
36
37     private Expression dropExpr;
38
39     /* package */CommonDropAdapterDescriptor(
40             IConfigurationElement aConfigElement,
41             INavigatorContentDescriptor aContentDescriptor) {
42         element = aConfigElement;
43         contentDescriptor = aContentDescriptor;
44         init();
45     }
46
47     private void init() {
48         IConfigurationElement[] children = element.getChildren(TAG_POSSIBLE_DROP_TARGETS);
49         if (children.length == 1) {
50             dropExpr = new CustomAndExpression(children[0]);
51         }
52     }
53
54     /**
55      *
56      * @param anElement
57      * The element from the set of elements being dragged.
58      * @return True if the element matches the drag expression from the
59      * extension.
60      */

61     public boolean isDragElementSupported(Object JavaDoc anElement) {
62         return contentDescriptor.isPossibleChild(anElement);
63     }
64
65     /**
66      *
67      * @param aSelection
68      * The set of elements being dragged.
69      * @return True if the element matches the drag expression from the
70      * extension.
71      */

72     public boolean areDragElementsSupported(IStructuredSelection aSelection) {
73         if (aSelection.isEmpty()) {
74             return false;
75         }
76         return contentDescriptor.arePossibleChildren(aSelection);
77     }
78
79     /**
80      *
81      * @param anElement
82      * The element from the set of elements benig dropped.
83      * @return True if the element matches the drop expression from the
84      * extension.
85      */

86     public boolean isDropElementSupported(Object JavaDoc anElement) {
87         if (dropExpr != null && anElement != null) {
88             try {
89                 EvaluationContext context = new EvaluationContext(null, anElement);
90                 context.setAllowPluginActivation(true);
91                 return dropExpr
92                         .evaluate(context) == EvaluationResult.TRUE;
93             } catch (CoreException e) {
94                 NavigatorPlugin.logError(0, e.getMessage(), e);
95             }
96         }
97         return false;
98     }
99
100     /**
101      *
102      * @return An instance of {@link CommonDropAdapterAssistant} from the
103      * descriptor or {@link SkeletonCommonDropAssistant}.
104      */

105     public CommonDropAdapterAssistant createDropAssistant() {
106
107         try {
108             return (CommonDropAdapterAssistant) element
109                     .createExecutableExtension(ATT_CLASS);
110         } catch (CoreException e) {
111             NavigatorPlugin.logError(0, e.getMessage(), e);
112         } catch (RuntimeException JavaDoc re) {
113             NavigatorPlugin.logError(0, re.getMessage(), re);
114         }
115         return SkeletonCommonDropAssistant.INSTANCE;
116     }
117
118     /**
119      *
120      * @return The content descriptor that contains this drop descriptor.
121      */

122     public INavigatorContentDescriptor getContentDescriptor() {
123         return contentDescriptor;
124     }
125
126 }
127
Popular Tags