KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.TreeMap JavaDoc;
21
22 import org.eclipse.core.runtime.IConfigurationElement;
23 import org.eclipse.ui.internal.navigator.extensions.ExtensionPriorityComparator;
24 import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
25 import org.eclipse.ui.internal.navigator.extensions.NavigatorContentRegistryReader;
26 import org.eclipse.ui.navigator.INavigatorContentDescriptor;
27 import org.eclipse.ui.navigator.INavigatorContentService;
28
29 /**
30  * @since 3.2
31  *
32  */

33 public class CommonDropDescriptorManager {
34
35     private static final CommonDropDescriptorManager INSTANCE = new CommonDropDescriptorManager();
36
37     private static final CommonDropAdapterDescriptor[] NO_DESCRIPTORS = new CommonDropAdapterDescriptor[0];
38
39     /**
40      * A map of (INavigatorContentDescriptor,
41      * CommonDropAdapterDescriptor)-pairs.
42      */

43     private final Map JavaDoc dropDescriptors = new TreeMap JavaDoc(ExtensionPriorityComparator.INSTANCE);
44
45     /**
46      *
47      * @return An initialized singleton instance of the
48      * CommonDropDescriptorManager.
49      */

50     public static CommonDropDescriptorManager getInstance() {
51         return INSTANCE;
52     }
53
54     private CommonDropDescriptorManager() {
55         new CommonDropAdapterRegistry().readRegistry();
56     }
57
58     /**
59      *
60      * @param aDropTarget
61      * The drop target of the operation
62      * @param aContentService
63      * The associated content service to filter results by.
64      * @return An array of drop descriptors that can handle the given drop
65      * target and are <i>visible</i> and <i>active</i> for the given
66      * service and <i>enabled</i> for the given drop target..
67      */

68     public CommonDropAdapterDescriptor[] findCommonDropAdapterAssistants(Object JavaDoc aDropTarget, INavigatorContentService aContentService) {
69
70         Set JavaDoc foundDescriptors = new LinkedHashSet JavaDoc();
71         for (Iterator JavaDoc iter = dropDescriptors.keySet().iterator(); iter
72                 .hasNext();) {
73             INavigatorContentDescriptor contentDescriptor = (INavigatorContentDescriptor) iter
74                     .next();
75             if (aContentService.isVisible(contentDescriptor.getId())
76                     && aContentService.isActive(contentDescriptor.getId())) {
77                 List JavaDoc dropDescriptors = getDropDescriptors(contentDescriptor);
78                 for (Iterator JavaDoc iterator = dropDescriptors.iterator(); iterator
79                         .hasNext();) {
80                     CommonDropAdapterDescriptor dropDescriptor = (CommonDropAdapterDescriptor) iterator
81                             .next();
82                     if (dropDescriptor.isDropElementSupported(aDropTarget)) {
83                         foundDescriptors.add(dropDescriptor);
84                     }
85                 }
86             }
87         }
88
89         if (foundDescriptors.isEmpty()) {
90             return NO_DESCRIPTORS;
91         }
92         return (CommonDropAdapterDescriptor[]) foundDescriptors
93                 .toArray(new CommonDropAdapterDescriptor[foundDescriptors
94                         .size()]);
95     }
96
97     private List JavaDoc getDropDescriptors(
98             INavigatorContentDescriptor aContentDescriptor) {
99         List JavaDoc descriptors = (List JavaDoc) dropDescriptors.get(aContentDescriptor);
100         if (descriptors != null) {
101             return descriptors;
102         }
103         synchronized (dropDescriptors) {
104             descriptors = (List JavaDoc) dropDescriptors.get(aContentDescriptor);
105             if (descriptors == null) {
106                 dropDescriptors.put(aContentDescriptor,
107                         (descriptors = new ArrayList JavaDoc()));
108             }
109
110         }
111         return descriptors;
112     }
113
114     /**
115      * @param aContentDescriptor
116      * A non-null content descriptor.
117      * @param aDropDescriptor
118      * A non-null drop descriptor.
119      */

120     private void addCommonDropAdapter(
121             INavigatorContentDescriptor aContentDescriptor,
122             CommonDropAdapterDescriptor aDropDescriptor) {
123         getDropDescriptors(aContentDescriptor).add(aDropDescriptor);
124     }
125
126     private class CommonDropAdapterRegistry extends
127             NavigatorContentRegistryReader implements
128             INavigatorContentExtPtConstants {
129
130         private CommonDropAdapterRegistry() {
131         }
132
133         /*
134          * (non-Javadoc)
135          *
136          * @see org.eclipse.ui.internal.navigator.extensions.RegistryReader#readElement(org.eclipse.core.runtime.IConfigurationElement)
137          */

138         protected boolean readElement(IConfigurationElement element) {
139
140             if (TAG_NAVIGATOR_CONTENT.equals(element.getName())) {
141
142                 String JavaDoc id = element.getAttribute(ATT_ID);
143                 if (id != null) {
144                     INavigatorContentDescriptor contentDescriptor = CONTENT_DESCRIPTOR_MANAGER
145                             .getContentDescriptor(id);
146                     if (contentDescriptor != null) {
147
148                         IConfigurationElement[] commonDropAdapters = element
149                                 .getChildren(TAG_COMMON_DROP_ADAPTER);
150
151                         for (int i = 0; i < commonDropAdapters.length; i++) {
152                             addCommonDropAdapter(contentDescriptor,
153                                     new CommonDropAdapterDescriptor(commonDropAdapters[i], contentDescriptor));
154                         }
155                     }
156                 }
157
158             }
159             return super.readElement(element);
160         }
161
162     }
163
164 }
165
Popular Tags