1 11 package org.eclipse.team.internal.ui.synchronize.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.Iterator ; 15 16 import org.eclipse.compare.CompareEditorInput; 17 import org.eclipse.compare.CompareUI; 18 import org.eclipse.compare.structuremergeviewer.ICompareInput; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.jface.action.Action; 22 import org.eclipse.jface.operation.IRunnableWithProgress; 23 import org.eclipse.jface.util.OpenStrategy; 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.team.core.synchronize.SyncInfo; 27 import org.eclipse.team.internal.ui.*; 28 import org.eclipse.team.internal.ui.mapping.ModelCompareEditorInput; 29 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement; 30 import org.eclipse.team.ui.mapping.ISynchronizationCompareInput; 31 import org.eclipse.team.ui.synchronize.*; 32 import org.eclipse.ui.*; 33 34 40 public class OpenInCompareAction extends Action { 41 42 private final ISynchronizePageConfiguration configuration; 43 44 public OpenInCompareAction(ISynchronizePageConfiguration configuration) { 45 this.configuration = configuration; 46 Utils.initAction(this, "action.openInCompareEditor."); } 48 49 public void run() { 50 ISelection selection = configuration.getSite().getSelectionProvider().getSelection(); 51 if(selection instanceof IStructuredSelection) { 52 if (!isOkToRun(selection)) 53 return; 54 55 boolean reuseEditorIfPossible = ((IStructuredSelection) selection).size()==1; 56 for (Iterator iterator = ((IStructuredSelection) selection).iterator(); iterator.hasNext();) { 57 Object obj = iterator.next(); 58 if (obj instanceof SyncInfoModelElement) { 59 SyncInfo info = ((SyncInfoModelElement) obj).getSyncInfo(); 60 if (info != null) { 61 openCompareEditorOnSyncInfo(configuration, info, !OpenStrategy.activateOnOpen(), reuseEditorIfPossible); 63 } 64 } else if (obj != null){ 65 openCompareEditor(configuration, obj, !OpenStrategy.activateOnOpen(), reuseEditorIfPossible); 66 } 67 } 68 } 69 } 70 71 private boolean isOkToRun(ISelection selection) { 72 Object [] elements = ((IStructuredSelection) selection).toArray(); 74 ISynchronizeParticipant participant = configuration 75 .getParticipant(); 76 if (participant instanceof ModelSynchronizeParticipant) { 78 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 79 for (int i = 0; i < elements.length; i++) { 80 if (!msp.hasCompareInputFor(elements[i])) { 82 return false; 83 } 84 } 85 } else { 86 IResource resources[] = Utils.getResources(elements); 88 for (int i = 0; i < resources.length; i++) { 89 if (resources[i].getType() != IResource.FILE) { 90 return false; 92 } 93 } 94 } 95 return true; 96 } 97 98 public static IEditorInput openCompareEditor(ISynchronizePageConfiguration configuration, Object object, boolean keepFocus, boolean reuseEditorIfPossible) { 99 Assert.isNotNull(object); 100 Assert.isNotNull(configuration); 101 ISynchronizeParticipant participant = configuration.getParticipant(); 102 ISynchronizePageSite site = configuration.getSite(); 103 if (object instanceof SyncInfoModelElement) { 104 SyncInfo info = ((SyncInfoModelElement) object).getSyncInfo(); 105 if (info != null) 106 return openCompareEditorOnSyncInfo(configuration, info, keepFocus, reuseEditorIfPossible); 107 } 108 if (participant instanceof ModelSynchronizeParticipant) { 109 ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 110 ICompareInput input = msp.asCompareInput(object); 111 IWorkbenchPage workbenchPage = getWorkbenchPage(site); 112 if (input != null && workbenchPage != null && isOkToOpen(site, participant, input)) { 113 return openCompareEditor(workbenchPage, new ModelCompareEditorInput(msp, input, workbenchPage, configuration), keepFocus, site, reuseEditorIfPossible); 114 } 115 } 116 return null; 117 } 118 119 private static boolean isOkToOpen(final ISynchronizePageSite site, final ISynchronizeParticipant participant, final ICompareInput input) { 120 if (participant instanceof ModelSynchronizeParticipant && input instanceof ISynchronizationCompareInput) { 121 final ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant; 122 final boolean[] result = new boolean[] { false }; 123 try { 124 PlatformUI.getWorkbench().getProgressService().run(true, true, new IRunnableWithProgress() { 125 public void run(IProgressMonitor monitor) throws InvocationTargetException , 126 InterruptedException { 127 try { 128 result[0] = msp.checkForBufferChange(site.getShell(), (ISynchronizationCompareInput)input, true, monitor); 129 } catch (CoreException e) { 130 throw new InvocationTargetException (e); 131 } 132 } 133 }); 134 } catch (InvocationTargetException e) { 135 Utils.handleError(site.getShell(), e, null, null); 136 } catch (InterruptedException e) { 137 return false; 138 } 139 return result[0]; 140 } 141 return true; 142 } 143 144 public static CompareEditorInput openCompareEditorOnSyncInfo(ISynchronizePageConfiguration configuration, SyncInfo info, boolean keepFocus, boolean reuseEditorIfPossible) { 145 Assert.isNotNull(info); 146 Assert.isNotNull(configuration); 147 if(info.getLocal().getType() != IResource.FILE) return null; 148 SyncInfoCompareInput input = new SyncInfoCompareInput(configuration, info); 149 return openCompareEditor(getWorkbenchPage(configuration.getSite()), input, keepFocus, configuration.getSite(), reuseEditorIfPossible); 150 } 151 152 public static CompareEditorInput openCompareEditor(ISynchronizeParticipant participant, SyncInfo info, ISynchronizePageSite site) { 153 Assert.isNotNull(info); 154 Assert.isNotNull(participant); 155 if(info.getLocal().getType() != IResource.FILE) return null; 156 SyncInfoCompareInput input = new SyncInfoCompareInput(participant, info); 157 return openCompareEditor(getWorkbenchPage(site), input, false, site, false); 158 } 159 160 private static CompareEditorInput openCompareEditor( 161 IWorkbenchPage page, 162 CompareEditorInput input, 163 boolean keepFocus, 164 ISynchronizePageSite site, 165 boolean reuseEditorIfPossible) { 166 if (page == null) 167 return null; 168 169 openCompareEditor(input, page, reuseEditorIfPossible); 170 if(site != null && keepFocus) { 171 site.setFocus(); 172 } 173 return input; 174 } 175 176 private static IWorkbenchPage getWorkbenchPage(ISynchronizePageSite site) { 177 IWorkbenchPage page = null; 178 if(site == null || site.getWorkbenchSite() == null) { 179 IWorkbenchWindow window= PlatformUI.getWorkbench().getActiveWorkbenchWindow(); 180 if (window != null) 181 page = window.getActivePage(); 182 } else { 183 page = site.getWorkbenchSite().getPage(); 184 } 185 return page; 186 } 187 188 public static void openCompareEditor(CompareEditorInput input, IWorkbenchPage page) { 189 openCompareEditor(input, page, false); 192 } 193 194 public static void openCompareEditor(CompareEditorInput input, IWorkbenchPage page, boolean reuseEditorIfPossible) { 195 if (page == null || input == null) 196 return; 197 IEditorPart editor = findReusableCompareEditor(input, page); 198 if(editor != null && reuseEditorIfPossible) { 200 IEditorInput otherInput = editor.getEditorInput(); 201 if(otherInput.equals(input)) { 202 page.activate(editor); 204 } else { 205 CompareUI.reuseCompareEditor(input, (IReusableEditor)editor); 207 page.activate(editor); 208 } 209 } else { 210 CompareUI.openCompareEditorOnPage(input, page); 211 } 212 } 213 214 221 public static IEditorPart findReusableCompareEditor(CompareEditorInput input, IWorkbenchPage page) { 222 IEditorReference[] editorRefs = page.getEditorReferences(); 223 for (int i = 0; i < editorRefs.length; i++) { 225 IEditorPart part = editorRefs[i].getEditor(false); 226 if(part != null 227 && (part.getEditorInput() instanceof SyncInfoCompareInput || part.getEditorInput() instanceof ModelCompareEditorInput) 228 && part instanceof IReusableEditor 229 && part.getEditorInput().equals(input)) { 230 return part; 231 } 232 } 233 if (isReuseOpenEditor()) { 236 for (int i = 0; i < editorRefs.length; i++) { 237 IEditorPart part = editorRefs[i].getEditor(false); 238 if(part != null 239 && (part.getEditorInput() instanceof SyncInfoCompareInput || part.getEditorInput() instanceof ModelCompareEditorInput) 240 && part instanceof IReusableEditor 241 && !part.isDirty()) { 242 return part; 243 } 244 } 245 } 246 247 return null; 249 } 250 251 259 public static IEditorPart findOpenCompareEditor(IWorkbenchPartSite site, IResource resource) { 260 IWorkbenchPage page = site.getPage(); 261 IEditorReference[] editorRefs = page.getEditorReferences(); 262 for (int i = 0; i < editorRefs.length; i++) { 263 final IEditorPart part = editorRefs[i].getEditor(false ); 264 if(part != null) { 265 IEditorInput input = part.getEditorInput(); 266 if(part != null && input instanceof SyncInfoCompareInput) { 267 SyncInfo inputInfo = ((SyncInfoCompareInput)input).getSyncInfo(); 268 if(inputInfo.getLocal().equals(resource)) { 269 return part; 270 } 271 } 272 } 273 } 274 return null; 275 } 276 277 285 public static IEditorPart findOpenCompareEditor(IWorkbenchPartSite site, Object object, ISynchronizeParticipant participant) { 286 if (object instanceof SyncInfoModelElement) { 287 SyncInfoModelElement element = (SyncInfoModelElement) object; 288 SyncInfo info = element.getSyncInfo(); 289 return findOpenCompareEditor(site, info.getLocal()); 290 } 291 IWorkbenchPage page = site.getPage(); 292 IEditorReference[] editorRefs = page.getEditorReferences(); 293 for (int i = 0; i < editorRefs.length; i++) { 294 final IEditorPart part = editorRefs[i].getEditor(false ); 295 if(part != null) { 296 IEditorInput input = part.getEditorInput(); 297 if(input instanceof ModelCompareEditorInput) { 298 if(((ModelCompareEditorInput)input).matches(object, participant)) { 299 return part; 300 } 301 } 302 } 303 } 304 return null; 305 } 306 307 private static boolean isReuseOpenEditor() { 308 return TeamUIPlugin.getPlugin().getPreferenceStore().getBoolean(IPreferenceIds.REUSE_OPEN_COMPARE_EDITOR); 309 } 310 } 311 | Popular Tags |