1 11 12 package org.eclipse.ui.internal.navigator.dnd; 13 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.LinkedHashSet ; 17 import java.util.Map ; 18 import java.util.Set ; 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 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 dropAssistants = new HashMap (); 53 54 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 dragDescriptors = ((NavigatorViewerDescriptor) contentService 73 .getViewerDescriptor()).getDragAssistants(); 74 dragAssistants = new CommonDragAdapterAssistant[dragDescriptors 75 .size()]; 76 for (Iterator 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 aDropTarget, TransferData aTransferType) { 95 96 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 aDropTarget, IStructuredSelection theDragSelection) { 115 116 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 assistants = new LinkedHashSet (); 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 assistants = new LinkedHashSet (); 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 |