KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > actions > OpenInCompareAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.synchronize.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.Iterator JavaDoc;
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 /**
35  * Action to open a compare editor from a SyncInfo object.
36  *
37  * @see SyncInfoCompareInput
38  * @since 3.0
39  */

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."); //$NON-NLS-1$
47
}
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 JavaDoc iterator = ((IStructuredSelection) selection).iterator(); iterator.hasNext();) {
57                 Object JavaDoc obj = iterator.next();
58                 if (obj instanceof SyncInfoModelElement) {
59                     SyncInfo info = ((SyncInfoModelElement) obj).getSyncInfo();
60                     if (info != null) {
61                         // Use the open strategy to decide if the editor or the sync view should have focus
62
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         // do not open Compare Editor unless all elements have input
73
Object JavaDoc[] elements = ((IStructuredSelection) selection).toArray();
74         ISynchronizeParticipant participant = configuration
75                 .getParticipant();
76         // model synchronize
77
if (participant instanceof ModelSynchronizeParticipant) {
78             ModelSynchronizeParticipant msp = (ModelSynchronizeParticipant) participant;
79             for (int i = 0; i < elements.length; i++) {
80                 // TODO: This is inefficient
81
if (!msp.hasCompareInputFor(elements[i])) {
82                     return false;
83                 }
84             }
85         } else {
86             // all files
87
IResource resources[] = Utils.getResources(elements);
88             for (int i = 0; i < resources.length; i++) {
89                 if (resources[i].getType() != IResource.FILE) {
90                     // Only supported if all the items are files.
91
return false;
92                 }
93             }
94         }
95         return true;
96     }
97
98     public static IEditorInput openCompareEditor(ISynchronizePageConfiguration configuration, Object JavaDoc 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 JavaDoc,
126                             InterruptedException JavaDoc {
127                         try {
128                             result[0] = msp.checkForBufferChange(site.getShell(), (ISynchronizationCompareInput)input, true, monitor);
129                         } catch (CoreException e) {
130                             throw new InvocationTargetException JavaDoc(e);
131                         }
132                     }
133                 });
134             } catch (InvocationTargetException JavaDoc e) {
135                 Utils.handleError(site.getShell(), e, null, null);
136             } catch (InterruptedException JavaDoc 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         // this is how it worked before opening compare editors for multiple
190
// selection was enabled
191
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         // reuse editor only for single selection
199
if(editor != null && reuseEditorIfPossible) {
200             IEditorInput otherInput = editor.getEditorInput();
201             if(otherInput.equals(input)) {
202                 // simply provide focus to editor
203
page.activate(editor);
204             } else {
205                 // if editor is currently not open on that input either re-use existing
206
CompareUI.reuseCompareEditor(input, (IReusableEditor)editor);
207                 page.activate(editor);
208             }
209         } else {
210             CompareUI.openCompareEditorOnPage(input, page);
211         }
212     }
213     
214     /**
215      * Returns an editor that can be re-used. An open compare editor that
216      * has un-saved changes cannot be re-used.
217      * @param input the input being opened
218      * @param page
219      * @return the open editor
220      */

221     public static IEditorPart findReusableCompareEditor(CompareEditorInput input, IWorkbenchPage page) {
222         IEditorReference[] editorRefs = page.getEditorReferences();
223         // first loop looking for an editor with the same input
224
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 none found and "Reuse open compare editors" preference is on use
234
// a non-dirty editor
235
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         // no re-usable editor found
248
return null;
249     }
250
251     /**
252      * Returns an editor handle if a SyncInfoCompareInput compare editor is opened on
253      * the given IResource.
254      *
255      * @param site the view site in which to search for editors
256      * @param resource the resource to use to find the compare editor
257      * @return an editor handle if found and <code>null</code> otherwise
258      */

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 /* don't restore editor */);
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     /**
278      * Returns an editor handle if a compare editor is opened on
279      * the given object.
280      * @param site the view site in which to search for editors
281      * @param object the object to use to find the compare editor
282      * @param participant
283      * @return an editor handle if found and <code>null</code> otherwise
284      */

285     public static IEditorPart findOpenCompareEditor(IWorkbenchPartSite site, Object JavaDoc 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 /* don't restore editor */);
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