1 11 package org.eclipse.team.ui.mapping; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.commands.*; 16 import org.eclipse.jface.operation.IRunnableContext; 17 import org.eclipse.jface.viewers.*; 18 import org.eclipse.team.internal.ui.Utils; 19 import org.eclipse.team.internal.ui.mapping.ResourceMarkAsMergedHandler; 20 import org.eclipse.team.internal.ui.mapping.ResourceMergeHandler; 21 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 22 23 34 public abstract class MergeActionHandler extends AbstractHandler { 35 36 private final ISynchronizePageConfiguration configuration; 37 private boolean enabled = false; 38 private IStructuredSelection selection; 39 private ISelectionChangedListener listener = new ISelectionChangedListener() { 40 public void selectionChanged(SelectionChangedEvent event) { 41 updatedEnablement(event); 42 } 43 }; 44 45 53 public static IHandler getDefaultHandler(String mergeActionId, ISynchronizePageConfiguration configuration) { 54 if (mergeActionId == SynchronizationActionProvider.MERGE_ACTION_ID) { 55 ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, false ); 56 resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection()); 57 return resourceMergeHandler; 58 } else if (mergeActionId == SynchronizationActionProvider.OVERWRITE_ACTION_ID) { 59 ResourceMergeHandler resourceMergeHandler = new ResourceMergeHandler(configuration, true ); 60 resourceMergeHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection()); 61 return resourceMergeHandler; 62 } else if (mergeActionId == SynchronizationActionProvider.MARK_AS_MERGE_ACTION_ID) { 63 ResourceMarkAsMergedHandler resourceMarkAsMergedHandler = new ResourceMarkAsMergedHandler(configuration); 64 resourceMarkAsMergedHandler.updateEnablement((IStructuredSelection)configuration.getSite().getSelectionProvider().getSelection()); 65 return resourceMarkAsMergedHandler; 66 } 67 return null; 68 } 69 70 74 public MergeActionHandler(ISynchronizePageConfiguration configuration) { 75 this.configuration = configuration; 76 ISelectionProvider selectionProvider = getConfiguration().getSite().getSelectionProvider(); 77 selectionProvider.addSelectionChangedListener(listener); 78 updateEnablement((IStructuredSelection)selectionProvider.getSelection()); 79 } 80 81 84 public void dispose() { 85 getConfiguration().getSite().getSelectionProvider().removeSelectionChangedListener(listener); 86 } 87 88 void updatedEnablement(SelectionChangedEvent event) { 89 updateEnablement((IStructuredSelection)event.getSelection()); 90 } 91 92 101 protected void updateEnablement(IStructuredSelection selection) { 102 this.selection = selection; 103 boolean isEnabled = getOperation().shouldRun(); 104 setEnabled(isEnabled); 105 } 106 107 112 protected final ISynchronizePageConfiguration getConfiguration() { 113 return configuration; 114 } 115 116 120 protected final IStructuredSelection getStructuredSelection() { 121 return selection; 122 } 123 124 127 public boolean isEnabled() { 128 return enabled; 129 } 130 131 135 protected void setEnabled(boolean isEnabled) { 136 if (enabled != isEnabled) { 137 enabled = isEnabled; 138 fireHandlerChanged(new HandlerEvent(this, true, false)); 139 } 140 } 141 142 145 public Object execute(final ExecutionEvent event) throws ExecutionException { 146 try { 147 SynchronizationOperation operation = getOperation(); 148 IRunnableContext context = getConfiguration().getRunnableContext(); 149 if (context != null) { 150 context.run(true, true, operation); 151 } else { 152 operation.run(); 153 } 154 } catch (InvocationTargetException e) { 155 Utils.handle(e); 156 } catch (InterruptedException e) { 157 } 159 return null; 160 } 161 162 167 protected abstract SynchronizationOperation getOperation(); 168 169 174 public SaveableComparison getSaveable() { 175 return getOperation().getSaveable(); 176 } 177 } 178 | Popular Tags |