1 11 package org.eclipse.ltk.ui.refactoring.model; 12 13 import java.util.Arrays ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Set ; 17 18 import org.eclipse.team.core.mapping.ISynchronizationContext; 19 import org.eclipse.team.core.mapping.ISynchronizationScope; 20 import org.eclipse.team.ui.mapping.MergeActionHandler; 21 import org.eclipse.team.ui.mapping.SynchronizationActionProvider; 22 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 23 24 import org.eclipse.core.runtime.Assert; 25 26 import org.eclipse.core.commands.AbstractHandler; 27 import org.eclipse.core.commands.ExecutionEvent; 28 import org.eclipse.core.commands.ExecutionException; 29 import org.eclipse.core.commands.IHandler; 30 31 import org.eclipse.core.resources.IResource; 32 import org.eclipse.core.resources.mapping.ResourceMapping; 33 34 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 36 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory; 37 import org.eclipse.ltk.core.refactoring.model.AbstractRefactoringDescriptorResourceMapping; 38 import org.eclipse.ltk.core.refactoring.model.AbstractRefactoringHistoryResourceMapping; 39 40 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringDescriptorProxyAdapter; 41 import org.eclipse.ltk.internal.ui.refactoring.actions.AcceptRefactoringsAction; 42 import org.eclipse.ltk.internal.ui.refactoring.actions.RejectRefactoringsAction; 43 44 import org.eclipse.jface.action.IMenuManager; 45 import org.eclipse.jface.viewers.ISelection; 46 import org.eclipse.jface.viewers.IStructuredSelection; 47 48 69 public class RefactoringSynchronizationActionProvider extends SynchronizationActionProvider { 70 71 72 private final class RefactoringHandlerDelegate extends AbstractHandler { 73 74 75 private final IHandler fDelegateHandler; 76 77 83 public RefactoringHandlerDelegate(final IHandler handler) { 84 Assert.isNotNull(handler); 85 fDelegateHandler= handler; 86 } 87 88 91 public void dispose() { 92 fDelegateHandler.dispose(); 93 super.dispose(); 94 } 95 96 99 public Object execute(final ExecutionEvent event) throws ExecutionException { 100 return fDelegateHandler.execute(event); 101 } 102 103 106 public boolean isEnabled() { 107 return !hasRefactorings(getSynchronizationContext(), getSynchronizePageConfiguration()) && fDelegateHandler.isEnabled(); 108 } 109 } 110 111 121 private static void getRefactoring(final ISynchronizationScope scope, final RefactoringDescriptorProxy proxy, final Set set) { 122 final ResourceMapping mapping= (ResourceMapping) proxy.getAdapter(ResourceMapping.class); 123 if (mapping instanceof AbstractRefactoringDescriptorResourceMapping) { 124 final AbstractRefactoringDescriptorResourceMapping extended= (AbstractRefactoringDescriptorResourceMapping) mapping; 125 final IResource resource= extended.getResource(); 126 if (resource != null && scope.contains(resource)) 127 set.add(proxy); 128 } 129 } 130 131 140 private static RefactoringDescriptorProxy[] getRefactorings(final ISynchronizationContext context, final ISynchronizePageConfiguration configuration) { 141 Assert.isNotNull(context); 142 Assert.isNotNull(configuration); 143 final Set set= new HashSet (); 144 final ISelection selection= configuration.getSite().getSelectionProvider().getSelection(); 145 if (selection instanceof IStructuredSelection) { 146 final IStructuredSelection structured= (IStructuredSelection) selection; 147 if (!structured.isEmpty()) { 148 final Object [] elements= structured.toArray(); 149 final ISynchronizationScope scope= context.getScope(); 150 for (int index= 0; index < elements.length; index++) { 151 if (elements[index] instanceof RefactoringHistory) { 152 getRefactorings(scope, (RefactoringHistory) elements[index], set); 153 } else if (elements[index] instanceof RefactoringDescriptorProxy) { 154 getRefactoring(scope, (RefactoringDescriptorProxy) elements[index], set); 155 } else if (elements[index] instanceof RefactoringDescriptor) { 156 getRefactoring(scope, new RefactoringDescriptorProxyAdapter(((RefactoringDescriptor) elements[index])), set); 157 } 158 } 159 } 160 } 161 return (RefactoringDescriptorProxy[]) set.toArray(new RefactoringDescriptorProxy[set.size()]); 162 } 163 164 174 private static void getRefactorings(final ISynchronizationScope scope, final RefactoringHistory history, final Set set) { 175 final ResourceMapping mapping= (ResourceMapping) history.getAdapter(ResourceMapping.class); 176 if (mapping instanceof AbstractRefactoringHistoryResourceMapping) { 177 final AbstractRefactoringHistoryResourceMapping extended= (AbstractRefactoringHistoryResourceMapping) mapping; 178 final IResource resource= extended.getResource(); 179 if (resource != null && scope.contains(resource)) 180 set.addAll(Arrays.asList(history.getDescriptors())); 181 } 182 } 183 184 194 private static boolean hasRefactoring(final ISynchronizationScope scope, final RefactoringDescriptorProxy proxy) { 195 final ResourceMapping mapping= (ResourceMapping) proxy.getAdapter(ResourceMapping.class); 196 if (mapping instanceof AbstractRefactoringDescriptorResourceMapping) { 197 final AbstractRefactoringDescriptorResourceMapping extended= (AbstractRefactoringDescriptorResourceMapping) mapping; 198 final IResource resource= extended.getResource(); 199 if (resource != null) 200 return scope.contains(resource); 201 } 202 return false; 203 } 204 205 216 private static boolean hasRefactorings(final ISynchronizationContext context, final ISynchronizePageConfiguration configuration) { 217 Assert.isNotNull(context); 218 Assert.isNotNull(configuration); 219 final ISelection selection= configuration.getSite().getSelectionProvider().getSelection(); 220 if (selection instanceof IStructuredSelection) { 221 final IStructuredSelection structured= (IStructuredSelection) selection; 222 if (!structured.isEmpty()) { 223 final Object [] elements= structured.toArray(); 224 final ISynchronizationScope scope= context.getScope(); 225 for (int index= 0; index < elements.length; index++) { 226 if (elements[index] instanceof RefactoringHistory) { 227 return hasRefactorings(scope, (RefactoringHistory) elements[index]); 228 } else if (elements[index] instanceof RefactoringDescriptorProxy) { 229 return hasRefactoring(scope, (RefactoringDescriptorProxy) elements[index]); 230 } else if (elements[index] instanceof RefactoringDescriptor) { 231 return hasRefactoring(scope, new RefactoringDescriptorProxyAdapter((RefactoringDescriptor) elements[index])); 232 } 233 } 234 } 235 } 236 return false; 237 } 238 239 250 private static boolean hasRefactorings(final ISynchronizationScope scope, final RefactoringHistory history) { 251 final ResourceMapping mapping= (ResourceMapping) history.getAdapter(ResourceMapping.class); 252 if (mapping instanceof AbstractRefactoringHistoryResourceMapping) { 253 final AbstractRefactoringHistoryResourceMapping extended= (AbstractRefactoringHistoryResourceMapping) mapping; 254 final IResource resource= extended.getResource(); 255 if (resource != null) 256 return scope.contains(resource); 257 } 258 return false; 259 } 260 261 264 public void fillContextMenu(final IMenuManager menu) { 265 super.fillContextMenu(menu); 266 if (isRefactoringElementSelected()) { 267 final ISynchronizationContext context= getSynchronizationContext(); 268 final RefactoringDescriptorProxy[] proxies= getRefactorings(context, getSynchronizePageConfiguration()); 269 final AcceptRefactoringsAction accept= new AcceptRefactoringsAction(context, getExtensionSite().getViewSite().getShell()); 270 accept.setRefactoringDescriptors(proxies); 271 menu.add(accept); 272 final RejectRefactoringsAction reject= new RejectRefactoringsAction(context); 273 reject.setRefactoringDescriptors(proxies); 274 menu.add(reject); 275 } 276 } 277 278 281 protected void initialize() { 282 super.initialize(); 283 final ISynchronizePageConfiguration configuration= getSynchronizePageConfiguration(); 284 registerHandler(MERGE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(MERGE_ACTION_ID, configuration))); 285 registerHandler(OVERWRITE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(OVERWRITE_ACTION_ID, configuration))); 286 registerHandler(MARK_AS_MERGE_ACTION_ID, new RefactoringHandlerDelegate(MergeActionHandler.getDefaultHandler(MARK_AS_MERGE_ACTION_ID, configuration))); 287 } 288 289 292 protected void initializeOpenActions() { 293 if (!hasRefactorings(getSynchronizationContext(), getSynchronizePageConfiguration())) 294 super.initializeOpenActions(); 295 } 296 297 private boolean isRefactoringElementSelected() { 298 final ISelection selection= getContext().getSelection(); 299 if (selection instanceof IStructuredSelection) { 300 final IStructuredSelection extended= (IStructuredSelection) selection; 301 for (final Iterator iterator= extended.iterator(); iterator.hasNext();) { 302 final Object element= iterator.next(); 303 if (element instanceof RefactoringDescriptorProxy || element instanceof RefactoringDescriptor || element instanceof RefactoringHistory) { 304 return true; 305 } 306 } 307 } 308 return false; 309 } 310 } | Popular Tags |