KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.LinkedHashSet JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.jface.util.LocalSelectionTransfer;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.swt.dnd.TransferData;
23 import org.eclipse.ui.internal.navigator.NavigatorContentService;
24 import org.eclipse.ui.internal.navigator.extensions.CommonDragAssistantDescriptor;
25 import org.eclipse.ui.internal.navigator.extensions.NavigatorViewerDescriptor;
26 import org.eclipse.ui.navigator.CommonDragAdapterAssistant;
27 import org.eclipse.ui.navigator.CommonDropAdapterAssistant;
28 import org.eclipse.ui.navigator.INavigatorContentService;
29 import org.eclipse.ui.navigator.INavigatorDnDService;
30
31 /**
32  *
33  * Provides instances of {@link CommonDragAdapterAssistant} and
34  * {@link CommonDropAdapterAssistant} for the associated
35  * {@link INavigatorContentService}.
36  *
37  * <p>
38  * Clients may not extend, instantiate or directly reference this class.
39  * </p>
40  *
41  * @since 3.2
42  *
43  */

44 public class NavigatorDnDService implements INavigatorDnDService {
45
46     private static final CommonDropAdapterAssistant[] NO_ASSISTANTS = new CommonDropAdapterAssistant[0];
47
48     private NavigatorContentService contentService;
49
50     private CommonDragAdapterAssistant[] dragAssistants;
51
52     private final Map JavaDoc dropAssistants = new HashMap JavaDoc();
53
54     /**
55      *
56      * @param aContentService
57      * The associated content service
58      */

59     public NavigatorDnDService(NavigatorContentService aContentService) {
60         contentService = aContentService;
61     }
62
63     public synchronized CommonDragAdapterAssistant[] getCommonDragAssistants() {
64
65         if (dragAssistants == null)
66             initializeDragAssistants();
67         return dragAssistants;
68     }
69  
70     private void initializeDragAssistants() {
71         int i = 0;
72         Set JavaDoc dragDescriptors = ((NavigatorViewerDescriptor) contentService
73                 .getViewerDescriptor()).getDragAssistants();
74         dragAssistants = new CommonDragAdapterAssistant[dragDescriptors
75                 .size()];
76         for (Iterator JavaDoc iter = dragDescriptors.iterator(); iter.hasNext();) {
77             CommonDragAssistantDescriptor descriptor = (CommonDragAssistantDescriptor) iter
78                     .next();
79             dragAssistants[i++] = descriptor.createDragAssistant();
80         }
81     }
82     
83
84     public synchronized void bindDragAssistant(CommonDragAdapterAssistant anAssistant) {
85         if(dragAssistants == null)
86             initializeDragAssistants();
87         CommonDragAdapterAssistant[] newDragAssistants = new CommonDragAdapterAssistant[dragAssistants.length + 1];
88         System.arraycopy(dragAssistants, 0, newDragAssistants, 0, dragAssistants.length);
89         newDragAssistants[dragAssistants.length] = anAssistant;
90         dragAssistants = newDragAssistants;
91     }
92
93     public CommonDropAdapterAssistant[] findCommonDropAdapterAssistants(
94             Object JavaDoc aDropTarget, TransferData aTransferType) {
95  
96         // TODO Make sure descriptors are sorted by priority
97
CommonDropAdapterDescriptor[] descriptors = CommonDropDescriptorManager
98                 .getInstance().findCommonDropAdapterAssistants(aDropTarget,
99                         contentService);
100
101         if (descriptors.length == 0) {
102             return NO_ASSISTANTS;
103         }
104
105         if (LocalSelectionTransfer.getTransfer().isSupportedType(aTransferType)
106                         && LocalSelectionTransfer.getTransfer().getSelection() instanceof IStructuredSelection) {
107             return getAssistantsBySelection(descriptors, (IStructuredSelection) LocalSelectionTransfer.getTransfer().getSelection());
108         }
109         return getAssistantsByTransferData(descriptors, aTransferType);
110     }
111     
112
113     public CommonDropAdapterAssistant[] findCommonDropAdapterAssistants(
114             Object JavaDoc aDropTarget, IStructuredSelection theDragSelection) {
115  
116         // TODO Make sure descriptors are sorted by priority
117
CommonDropAdapterDescriptor[] descriptors = CommonDropDescriptorManager
118                 .getInstance().findCommonDropAdapterAssistants(aDropTarget,
119                         contentService);
120
121         if (descriptors.length == 0) {
122             return NO_ASSISTANTS;
123         }
124
125         return getAssistantsBySelection(descriptors, theDragSelection);
126     }
127
128     private CommonDropAdapterAssistant[] getAssistantsByTransferData(
129             CommonDropAdapterDescriptor[] descriptors,
130             TransferData aTransferType) {
131
132         Set JavaDoc assistants = new LinkedHashSet JavaDoc();
133         for (int i = 0; i < descriptors.length; i++) {
134             CommonDropAdapterAssistant asst = getAssistant(descriptors[i]);
135             if (asst.isSupportedType(aTransferType)) {
136                 assistants.add(asst);
137             }
138         }
139         return (CommonDropAdapterAssistant[]) assistants
140                 .toArray(new CommonDropAdapterAssistant[assistants.size()]);
141
142     }
143
144     private CommonDropAdapterAssistant[] getAssistantsBySelection(
145             CommonDropAdapterDescriptor[] descriptors, IStructuredSelection aSelection) {
146
147         Set JavaDoc assistants = new LinkedHashSet JavaDoc();
148             
149         for (int i = 0; i < descriptors.length; i++) {
150             if(descriptors[i].areDragElementsSupported(aSelection)) {
151                 assistants.add(getAssistant(descriptors[i]));
152             }
153         }
154
155         return (CommonDropAdapterAssistant[]) assistants
156                 .toArray(new CommonDropAdapterAssistant[assistants.size()]);
157     }
158
159     private CommonDropAdapterAssistant getAssistant(
160             CommonDropAdapterDescriptor descriptor) {
161         CommonDropAdapterAssistant asst = (CommonDropAdapterAssistant) dropAssistants
162                 .get(descriptor);
163         if (asst != null) {
164             return asst;
165         }
166         synchronized (dropAssistants) {
167             asst = (CommonDropAdapterAssistant) dropAssistants.get(descriptor);
168             if (asst == null) {
169                 dropAssistants.put(descriptor, (asst = descriptor
170                         .createDropAssistant()));
171                 asst.init(contentService);
172             }
173         }
174         return asst;
175     }
176
177 }
178
Popular Tags