1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Collections ; 15 16 import org.eclipse.core.commands.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.dialogs.ErrorDialog; 20 import org.eclipse.jface.operation.IRunnableWithProgress; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.team.internal.ui.*; 23 import org.eclipse.team.ui.mapping.*; 24 import org.eclipse.team.ui.synchronize.*; 25 import org.eclipse.ui.PlatformUI; 26 27 33 public class MergeAction extends Action { 34 35 private final String handlerId; 36 private final CommonMenuManager manager; 37 private final ISynchronizePageConfiguration configuration; 38 private IHandler defaultHandler; 39 40 public MergeAction(String handlerId, CommonMenuManager manager, ISynchronizePageConfiguration configuration) { 41 Assert.isNotNull(handlerId); 42 Assert.isNotNull(manager); 43 Assert.isNotNull(configuration); 44 this.handlerId = handlerId; 45 this.manager = manager; 46 this.configuration = configuration; 47 } 48 49 public void runWithEvent(Event event) { 50 IHandler handler = getHandler(); 51 if (handler != null && handler.isEnabled()) { 52 final SaveableComparison currentBuffer = ((ModelSynchronizeParticipant)configuration.getParticipant()).getActiveSaveable(); 53 if (currentBuffer != null && currentBuffer.isDirty()) { 54 SaveableComparison targetBuffer = null; 55 if (handler instanceof MergeActionHandler) { 56 MergeActionHandler mah = (MergeActionHandler) handler; 57 targetBuffer = mah.getSaveable(); 58 } 59 final SaveableComparison target = targetBuffer; 60 try { 61 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { 62 public void run(IProgressMonitor monitor) throws InvocationTargetException , 63 InterruptedException { 64 try { 65 ModelParticipantAction.handleTargetSaveableChange(configuration.getSite().getShell(), target, currentBuffer, true, monitor); 66 } catch (CoreException e) { 67 throw new InvocationTargetException (e); 68 } 69 } 70 }); 71 } catch (InvocationTargetException e) { 72 Utils.handle(e); 73 return; 74 } catch (InterruptedException e) { 75 return; 76 } 77 ((ModelSynchronizeParticipant)configuration.getParticipant()).setActiveSaveable(targetBuffer); 78 } 79 try { 80 handler.execute(new ExecutionEvent(null, Collections.EMPTY_MAP, event, null)); 81 } catch (ExecutionException e) { 82 handle(e); 83 } 84 } 85 } 86 87 91 protected void handle(Throwable exception) { 92 if (exception instanceof ExecutionException) { 93 ExecutionException ee = (ExecutionException) exception; 94 if (ee.getCause() != null) { 95 handle(exception.getCause()); 96 } 97 } 98 IStatus status; 99 if (exception instanceof CoreException) { 100 CoreException ce = (CoreException) exception; 101 status = ce.getStatus(); 102 } else { 103 status = new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, TeamUIMessages.exception, exception); 104 } 105 ErrorDialog.openError(configuration.getSite().getShell(), null, null, status); 106 } 107 108 private IHandler getHandler() { 109 IHandler handler = manager.getHandler(handlerId); 110 if (handler == null) { 111 if (defaultHandler == null) 112 defaultHandler = getDefaultHandler(); 113 return defaultHandler; 114 } 115 return handler; 116 } 117 118 private IHandler getDefaultHandler() { 119 return MergeActionHandler.getDefaultHandler(handlerId, configuration); 120 } 121 122 private boolean calculateEnablement() { 123 IHandler handler = getHandler(); 124 return handler != null && handler.isEnabled(); 125 } 126 127 public void dispose() { 128 if (defaultHandler != null) 129 defaultHandler.dispose(); 130 } 131 132 public void update() { 133 setEnabled(calculateEnablement()); 134 } 135 136 } 137 | Popular Tags |