1 11 package org.eclipse.team.ui.synchronize; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.jface.dialogs.IDialogConstants; 18 import org.eclipse.jface.dialogs.MessageDialog; 19 import org.eclipse.jface.operation.IRunnableWithProgress; 20 import org.eclipse.jface.viewers.*; 21 import org.eclipse.osgi.util.NLS; 22 import org.eclipse.swt.events.DisposeEvent; 23 import org.eclipse.swt.events.DisposeListener; 24 import org.eclipse.swt.widgets.Shell; 25 import org.eclipse.team.core.diff.*; 26 import org.eclipse.team.core.mapping.ISynchronizationContext; 27 import org.eclipse.team.internal.ui.TeamUIMessages; 28 import org.eclipse.team.ui.mapping.*; 29 import org.eclipse.ui.PlatformUI; 30 import org.eclipse.ui.actions.BaseSelectionListenerAction; 31 32 37 public abstract class ModelParticipantAction extends BaseSelectionListenerAction { 38 39 private final ISynchronizePageConfiguration configuration; 40 41 46 public ModelParticipantAction(String text, ISynchronizePageConfiguration configuration) { 47 super(text); 48 this.configuration = configuration; 49 initialize(configuration); 50 } 51 52 private void initialize(ISynchronizePageConfiguration configuration) { 53 configuration.getSite().getSelectionProvider().addSelectionChangedListener(this); 54 configuration.getPage().getViewer().getControl().addDisposeListener(new DisposeListener() { 55 public void widgetDisposed(DisposeEvent e) { 56 getConfiguration().getSite().getSelectionProvider().removeSelectionChangedListener(ModelParticipantAction.this); 57 } 58 }); 59 } 60 61 65 protected ISynchronizePageConfiguration getConfiguration() { 66 return configuration; 67 } 68 69 74 public void selectionChanged(ISelection selection) { 75 if (selection instanceof IStructuredSelection) { 76 super.selectionChanged((IStructuredSelection)selection); 77 } else { 78 super.selectionChanged(StructuredSelection.EMPTY); 79 } 80 81 } 82 83 86 protected boolean updateSelection(IStructuredSelection selection) { 87 super.updateSelection(selection); 88 return isEnabledForSelection(selection); 89 } 90 91 96 protected abstract boolean isEnabledForSelection(IStructuredSelection selection); 97 98 102 protected ISynchronizationContext getSynchronizationContext() { 103 return (ISynchronizationContext)getConfiguration().getProperty(ITeamContentProviderManager.P_SYNCHRONIZATION_CONTEXT); 104 } 105 106 112 protected boolean isVisible(IDiff node) { 113 ISynchronizePageConfiguration configuration = getConfiguration(); 114 if (configuration.getComparisonType() == ISynchronizePageConfiguration.THREE_WAY 115 && node instanceof IThreeWayDiff) { 116 IThreeWayDiff twd = (IThreeWayDiff) node; 117 int mode = configuration.getMode(); 118 switch (mode) { 119 case ISynchronizePageConfiguration.INCOMING_MODE: 120 if (twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.INCOMING) { 121 return true; 122 } 123 break; 124 case ISynchronizePageConfiguration.OUTGOING_MODE: 125 if (twd.getDirection() == IThreeWayDiff.CONFLICTING || twd.getDirection() == IThreeWayDiff.OUTGOING) { 126 return true; 127 } 128 break; 129 case ISynchronizePageConfiguration.CONFLICTING_MODE: 130 if (twd.getDirection() == IThreeWayDiff.CONFLICTING) { 131 return true; 132 } 133 break; 134 case ISynchronizePageConfiguration.BOTH_MODE: 135 return true; 136 } 137 } else if (configuration.getComparisonType() == ISynchronizePageConfiguration.TWO_WAY 138 && node instanceof ITwoWayDiff) { 139 return true; 140 } 141 return false; 142 } 143 144 151 protected void handleTargetSaveableChange() throws InvocationTargetException , InterruptedException { 152 final SaveableComparison targetSaveable = getTargetSaveable(); 153 final SaveableComparison activeSaveable = getActiveSaveable(); 154 if (activeSaveable != null && activeSaveable.isDirty()) { 155 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { 156 public void run(IProgressMonitor monitor) throws InvocationTargetException , 157 InterruptedException { 158 try { 159 handleTargetSaveableChange(configuration.getSite().getShell(), targetSaveable, activeSaveable, true, monitor); 160 } catch (CoreException e) { 161 throw new InvocationTargetException (e); 162 } 163 } 164 }); 165 } 166 setActiveSaveable(targetSaveable); 167 } 168 169 180 public static void handleTargetSaveableChange(Shell shell, SaveableComparison targetSaveable, SaveableComparison activeSaveable, boolean allowCancel, IProgressMonitor monitor) throws CoreException, InterruptedException { 181 if (activeSaveable != null && targetSaveable != activeSaveable) { 182 if (activeSaveable.isDirty()) { 183 if (promptToSaveChanges(shell, activeSaveable, allowCancel)) { 184 activeSaveable.doSave(monitor); 185 } else { 186 activeSaveable.doRevert(monitor); 187 } 188 } 189 } 190 } 191 192 200 public static boolean promptToSaveChanges(final Shell shell, final SaveableComparison saveable, final boolean allowCancel) throws InterruptedException { 201 final int[] result = new int[] { 0 }; 202 Runnable runnable = new Runnable () { 203 public void run() { 204 String [] options; 205 if (allowCancel) { 206 options = new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL}; 207 } else { 208 options = new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL}; 209 } 210 MessageDialog dialog = new MessageDialog( 211 shell, 212 TeamUIMessages.ModelParticipantAction_0, null, 213 NLS.bind(TeamUIMessages.ModelParticipantAction_1, saveable.getName()), 214 MessageDialog.QUESTION, 215 options, 216 result[0]); 217 result[0] = dialog.open(); 218 } 219 }; 220 shell.getDisplay().syncExec(runnable); 221 if (result[0] == 2) 222 throw new InterruptedException (); 223 return result[0] == 0; 224 } 225 226 233 protected SaveableComparison getActiveSaveable() { 234 return ((ModelSynchronizeParticipant)configuration.getParticipant()).getActiveSaveable(); 235 } 236 237 243 protected void setActiveSaveable(SaveableComparison saveable) { 244 ((ModelSynchronizeParticipant)configuration.getParticipant()).setActiveSaveable(saveable); 245 } 246 247 252 protected SaveableComparison getTargetSaveable() { 253 return null; 254 } 255 256 261 public void updateEnablement() { 262 setEnabled(isEnabledForSelection(getStructuredSelection())); 263 } 264 265 } 266 | Popular Tags |