1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.commands.ExecutionEvent; 16 import org.eclipse.core.commands.ExecutionException; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.StructuredViewer; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.team.core.diff.*; 25 import org.eclipse.team.core.mapping.IMergeContext; 26 import org.eclipse.team.internal.ui.TeamUIMessages; 27 import org.eclipse.team.internal.ui.Utils; 28 import org.eclipse.team.ui.mapping.SynchronizationOperation; 29 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 30 31 public class ResourceMergeHandler extends ResourceMergeActionHandler { 32 33 private final boolean overwrite; 34 private ResourceModelProviderOperation operation; 35 36 public ResourceMergeHandler(ISynchronizePageConfiguration configuration, boolean overwrite) { 37 super(configuration); 38 this.overwrite = overwrite; 39 } 40 41 44 protected synchronized SynchronizationOperation getOperation() { 45 if (operation == null) { 46 operation = new ResourceModelProviderOperation(getConfiguration(), getStructuredSelection()) { 47 50 public void execute(IProgressMonitor monitor) throws InvocationTargetException , 51 InterruptedException { 52 try { 53 IMergeContext context = (IMergeContext)getContext(); 54 IDiff[] diffs = getTargetDiffs(); 55 if (diffs.length == 0) { 56 promptForNoChanges(); 57 } 58 IStatus status = context.merge(diffs, overwrite, monitor); 59 if (!status.isOK()) 60 throw new CoreException(status); 61 } catch (CoreException e) { 62 throw new InvocationTargetException (e); 63 } 64 } 65 68 protected FastDiffFilter getDiffFilter() { 69 return new FastDiffFilter() { 70 public boolean select(IDiff node) { 71 if (node instanceof IThreeWayDiff) { 72 IThreeWayDiff twd = (IThreeWayDiff) node; 73 if ((twd.getDirection() == IThreeWayDiff.OUTGOING && overwrite) || twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) { 74 return true; 75 } 76 return false; 77 } 78 return overwrite; 80 } 81 }; 82 } 83 protected String getJobName() { 84 IDiff[] diffs = getTargetDiffs(); 85 if (overwrite) { 86 if (diffs.length == 1) 87 return TeamUIMessages.ResourceMergeHandler_0; 88 return NLS.bind(TeamUIMessages.ResourceMergeHandler_1, new Integer (diffs.length).toString()); 89 90 } 91 if (diffs.length == 1) 92 return TeamUIMessages.ResourceMergeHandler_2; 93 return NLS.bind(TeamUIMessages.ResourceMergeHandler_3, new Integer (diffs.length).toString()); 94 } 95 }; 96 } 97 return operation; 98 } 99 100 103 public void updateEnablement(IStructuredSelection selection) { 104 synchronized (this) { 105 operation = null; 106 } 107 super.updateEnablement(selection); 108 int mode = getConfiguration().getMode(); 109 if (mode == ISynchronizePageConfiguration.OUTGOING_MODE && !overwrite) { 110 setEnabled(false); 111 return; 112 } 113 } 114 115 public Object execute(ExecutionEvent event) throws ExecutionException { 116 if (saveDirtyEditors() && (!overwrite || promptToConfirm())) 117 return super.execute(event); 118 return null; 119 } 120 121 protected boolean promptToConfirm() { 122 if (Display.getCurrent() != null) 123 return internalPromptToConfirm(); 124 final boolean[] confirmed = new boolean[] { false }; 125 Shell shell = getConfiguration().getSite().getShell(); 126 if (!shell.isDisposed()) { 127 Utils.syncExec(new Runnable () { 128 public void run() { 129 confirmed[0] = promptToConfirm(); 130 } 131 132 }, shell); 133 } 134 return confirmed[0]; 135 } 136 137 private boolean internalPromptToConfirm() { 138 return MessageDialog.openQuestion(getConfiguration().getSite().getShell(), TeamUIMessages.ResourceMergeHandler_4, TeamUIMessages.ResourceMergeHandler_5); 139 } 140 141 protected void promptForNoChanges() { 142 Utils.syncExec(new Runnable () { 143 public void run() { 144 MessageDialog.openInformation(getConfiguration().getSite().getShell(), TeamUIMessages.ResourceMergeHandler_6, TeamUIMessages.ResourceMergeHandler_7); 145 } 146 }, (StructuredViewer)getConfiguration().getPage().getViewer()); 147 } 148 149 150 } 151 | Popular Tags |