1 11 package org.eclipse.team.internal.ui.mapping; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.compare.*; 16 import org.eclipse.compare.structuremergeviewer.ICompareInput; 17 import org.eclipse.core.commands.ExecutionEvent; 18 import org.eclipse.core.commands.ExecutionException; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.jface.action.*; 21 import org.eclipse.jface.util.IPropertyChangeListener; 22 import org.eclipse.jface.util.PropertyChangeEvent; 23 import org.eclipse.jface.viewers.*; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.team.core.ICache; 26 import org.eclipse.team.core.ICacheListener; 27 import org.eclipse.team.internal.ui.*; 28 import org.eclipse.team.internal.ui.synchronize.*; 29 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput; 30 import org.eclipse.team.ui.mapping.SaveableComparison; 31 import org.eclipse.team.ui.synchronize.*; 32 import org.eclipse.ui.*; 33 34 37 public class ModelCompareEditorInput extends SaveableCompareEditorInput implements IPropertyChangeListener { 38 39 private static final String IGNORE_WHITSPACE_PAGE_PROPERTY = "org.eclipse.compare." + CompareConfiguration.IGNORE_WHITESPACE; 41 private final ModelSynchronizeParticipant participant; 42 private final ICompareInput input; 43 private final ICacheListener contextListener; 44 private final ISynchronizePageConfiguration synchronizeConfiguration; 45 46 public ModelCompareEditorInput(ModelSynchronizeParticipant participant, ICompareInput input, IWorkbenchPage page, ISynchronizePageConfiguration synchronizeConfiguration) { 47 super(createCompareConfiguration(synchronizeConfiguration), page); 48 this.synchronizeConfiguration = synchronizeConfiguration; 49 Assert.isNotNull(participant); 50 Assert.isNotNull(input); 51 this.participant = participant; 52 this.input = input; 53 contextListener = new ICacheListener() { 54 public void cacheDisposed(ICache cache) { 55 closeEditor(true); 56 } 57 }; 58 getCompareConfiguration().addPropertyChangeListener(this); 59 setTitle(NLS.bind(TeamUIMessages.SyncInfoCompareInput_title, new String [] { input.getName() })); 60 } 61 62 private static CompareConfiguration createCompareConfiguration(ISynchronizePageConfiguration pageConfiguration) { 63 CompareConfiguration compareConfiguration = new CompareConfiguration(); 64 Object o = pageConfiguration.getProperty(IGNORE_WHITSPACE_PAGE_PROPERTY); 65 if (o != null && (o.equals(Boolean.TRUE) || o.equals(Boolean.FALSE))) { 66 compareConfiguration.setProperty(CompareConfiguration.IGNORE_WHITESPACE, o); 67 } 68 return compareConfiguration; 69 } 70 71 74 protected void contentsCreated() { 75 super.contentsCreated(); 76 participant.getContext().getCache().addCacheListener(contextListener); 77 } 78 79 82 protected void handleDispose() { 83 super.handleDispose(); 84 participant.getContext().getCache().removeCacheListener(contextListener); 85 getCompareConfiguration().removePropertyChangeListener(this); 86 ICompareNavigator navigator = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR); 87 if (navigator != null && navigator == super.getNavigator()) { 88 synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, new CompareNavigator() { 89 protected INavigatable[] getNavigatables() { 90 return new INavigatable[0]; 91 } 92 }); 93 } 94 } 95 96 99 protected Saveable createSaveable() { 100 if (input instanceof ISynchronizationCompareInput) { 101 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input; 102 SaveableComparison compareModel = mci.getSaveable(); 103 if (compareModel != null) 104 return compareModel; 105 } 106 return super.createSaveable(); 107 } 108 109 112 protected ICompareInput prepareCompareInput(IProgressMonitor monitor) 113 throws InvocationTargetException , InterruptedException { 114 monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100); 115 monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3); 116 try { 117 ISynchronizationCompareInput adapter = asModelCompareInput(input); 118 if (adapter != null) { 119 adapter.prepareInput(getCompareConfiguration(), Policy.subMonitorFor(monitor, 100)); 120 } 121 } catch (CoreException e) { 122 throw new InvocationTargetException (e); 123 } finally { 124 monitor.done(); 125 } 126 return input; 127 } 128 129 private ISynchronizationCompareInput asModelCompareInput(ICompareInput input) { 130 return (ISynchronizationCompareInput)Utils.getAdapter(input, ISynchronizationCompareInput.class); 131 } 132 133 141 public boolean matches(Object object, ISynchronizeParticipant participant) { 142 if (participant == this.participant && input instanceof ISynchronizationCompareInput) { 143 ISynchronizationCompareInput mci = (ISynchronizationCompareInput) input; 144 return mci.isCompareInputFor(object); 145 } 146 return false; 147 } 148 149 153 public String getToolTipText() { 154 String fullPath; 155 ISynchronizationCompareInput adapter = asModelCompareInput(input); 156 if (adapter != null) { 157 fullPath = adapter.getFullPath(); 158 } else { 159 fullPath = getName(); 160 } 161 return NLS.bind(TeamUIMessages.SyncInfoCompareInput_tooltip, new String [] { Utils.shortenText(SynchronizeView.MAX_NAME_LENGTH, participant.getName()), fullPath }); 162 } 163 164 167 protected void fireInputChange() { 168 if (input instanceof ResourceDiffCompareInput) { 169 ResourceDiffCompareInput rdci = (ResourceDiffCompareInput) input; 170 rdci.fireChange(); 171 } 172 } 173 174 177 public void registerContextMenu(MenuManager menu, ISelectionProvider provider) { 178 super.registerContextMenu(menu, provider); 179 Saveable saveable = getSaveable(); 180 if (saveable instanceof LocalResourceSaveableComparison) { 181 menu.addMenuListener(new IMenuListener() { 182 public void menuAboutToShow(IMenuManager manager) { 183 handleMenuAboutToShow(manager); 184 } 185 }); 186 } 187 } 188 189 protected void handleMenuAboutToShow(IMenuManager manager) { 190 StructuredSelection selection = new StructuredSelection(((ResourceDiffCompareInput)getCompareInput()).getResource()); 191 final ResourceMarkAsMergedHandler markAsMergedHandler = new ResourceMarkAsMergedHandler(getSynchronizeConfiguration()); 192 markAsMergedHandler.updateEnablement(selection); 193 Action markAsMergedAction = new Action(TeamUIMessages.ModelCompareEditorInput_0) { 194 public void run() { 195 try { 196 markAsMergedHandler.execute(new ExecutionEvent()); 197 } catch (ExecutionException e) { 198 TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e); 199 } 200 } 201 202 }; 203 Utils.initAction(markAsMergedAction, "action.markAsMerged."); markAsMergedAction.setEnabled(markAsMergedAction.isEnabled()); 205 206 final ResourceMergeHandler mergeHandler = new ResourceMergeHandler(getSynchronizeConfiguration(), false); 207 mergeHandler.updateEnablement(selection); 208 Action mergeAction = new Action(TeamUIMessages.ModelCompareEditorInput_1) { 209 public void run() { 210 try { 211 mergeHandler.execute(new ExecutionEvent()); 212 } catch (ExecutionException e) { 213 TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e); 214 } 215 } 216 217 }; 218 Utils.initAction(mergeAction, "action.merge."); mergeAction.setEnabled(markAsMergedAction.isEnabled()); 220 221 final ResourceMergeHandler overwriteHandler = new ResourceMergeHandler(getSynchronizeConfiguration(), true); 222 overwriteHandler.updateEnablement(selection); 223 Action overwriteAction = new Action(TeamUIMessages.ModelCompareEditorInput_2) { 224 public void run() { 225 try { 226 overwriteHandler.execute(new ExecutionEvent()); 227 } catch (ExecutionException e) { 228 TeamUIPlugin.log(IStatus.ERROR, e.getMessage(), e); 229 } 230 } 231 232 }; 233 Utils.initAction(overwriteAction, "action.overwrite."); overwriteAction.setEnabled(markAsMergedAction.isEnabled()); 235 236 manager.insertAfter(IWorkbenchActionConstants.MB_ADDITIONS, new Separator("merge")); manager.insertAfter("merge", new Separator("overwrite")); manager.insertAfter("merge", markAsMergedAction); manager.insertAfter("merge", mergeAction); manager.insertAfter("overwrite", overwriteAction); } 242 243 protected ISynchronizePageConfiguration getSynchronizeConfiguration() { 244 return synchronizeConfiguration; 245 } 246 247 public void propertyChange(PropertyChangeEvent event) { 248 if (event.getProperty().equals(CompareConfiguration.IGNORE_WHITESPACE)) { 249 synchronizeConfiguration.setProperty(IGNORE_WHITSPACE_PAGE_PROPERTY, event.getNewValue()); 250 } 251 } 252 253 256 public boolean belongsTo(Object family) { 257 return super.belongsTo(family) || family == participant; 258 } 259 260 public synchronized ICompareNavigator getNavigator() { 261 if (isSelectedInSynchronizeView()) { 262 ICompareNavigator nav = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_NAVIGATOR); 263 synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, super.getNavigator()); 265 return nav; 266 } 267 return super.getNavigator(); 268 } 269 270 private boolean isSelectedInSynchronizeView() { 271 ISelection s = synchronizeConfiguration.getSite().getSelectionProvider().getSelection(); 272 if (s instanceof IStructuredSelection) { 273 IStructuredSelection ss = (IStructuredSelection) s; 274 Object element = ss.getFirstElement(); 275 return matches(element, participant); 276 } 277 return false; 278 } 279 280 283 public boolean equals(Object obj) { 284 if (obj == this) 285 return true; 286 if (obj instanceof ModelCompareEditorInput) { 287 ModelCompareEditorInput other = (ModelCompareEditorInput) obj; 288 return other.input.equals(input) && other.participant.equals(participant); 289 } 290 return false; 291 } 292 293 296 public int hashCode() { 297 return input.hashCode(); 298 } 299 } 300 | Popular Tags |