KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > synchronize > SyncInfoCompareInput


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.ui.synchronize;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.compare.*;
16 import org.eclipse.compare.structuremergeviewer.ICompareInput;
17 import org.eclipse.compare.structuremergeviewer.IDiffContainer;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.team.core.TeamException;
24 import org.eclipse.team.core.synchronize.SyncInfo;
25 import org.eclipse.team.internal.ui.*;
26 import org.eclipse.team.internal.ui.synchronize.SyncInfoModelElement;
27 import org.eclipse.team.internal.ui.synchronize.SynchronizePageConfiguration;
28 import org.eclipse.ui.progress.UIJob;
29
30 /**
31  * A {@link SyncInfo} editor input used as input to a two-way or three-way
32  * compare viewer. It defines methods for accessing the three sides for the
33  * compare, and a name and image which is used when displaying the three way input
34  * in an editor. This input can alternately be used to show compare results in
35  * a dialog by calling {@link CompareUI#openCompareDialog(org.eclipse.compare.CompareEditorInput)}.
36  * <p>
37  * The editor will not update when the elements in the sync info are changed.
38  * </p><p>
39  * Supports saving the local resource that is changed in the editor and will be updated
40  * when the local resources is changed.
41  * </p><p>
42  * This class cannot be subclassed by clients.
43  * </p>
44  * @see SyncInfo
45  * @since 3.0
46  */

47 public final class SyncInfoCompareInput extends SaveableCompareEditorInput implements IResourceChangeListener {
48
49     private MyDiffNode node;
50     private String JavaDoc description;
51     private IResource resource;
52     private ISynchronizeParticipant participant;
53     private ISynchronizePageConfiguration synchronizeConfiguration;
54
55     /*
56      * This class exists so that we can force the text merge viewers to update by
57      * calling #fireChange when we save the compare input to disk. The side
58      * effect is that the compare viewers will be updated to reflect the new changes
59      * that have been made. Compare doesn't do this by default.
60      */

61     private static class MyDiffNode extends SyncInfoModelElement {
62         public MyDiffNode(IDiffContainer parent, SyncInfo info) {
63             super(parent, info);
64         }
65         public void fireChange() {
66             super.fireChange();
67         }
68     }
69     
70     /**
71      * Creates a compare editor input based on an existing <code>SyncInfo</code>.
72      *
73      * @param description a description of the context of this sync info. This
74      * is displayed to the user.
75      * @param sync the <code>SyncInfo</code> used as the base for the compare input.
76      */

77     public SyncInfoCompareInput(String JavaDoc description, SyncInfo sync) {
78         super(getDefaultCompareConfiguration(), null);
79         Assert.isNotNull(sync);
80         Assert.isNotNull(description);
81         this.description = description;
82         this.resource = sync.getLocal();
83         this.node = new MyDiffNode(null, sync);
84         setTitle(NLS.bind(TeamUIMessages.SyncInfoCompareInput_title, new String JavaDoc[] { sync.getLocal().getName() }));
85     }
86     
87     /**
88      * Creates a compare editor input based on an existing <code>SyncInfo</code>
89      * from the given participant.
90      *
91      * @param participant the participant from which the sync info was obtained. The
92      * name of the participant is used as the description which is displayed to the user.
93      * @param sync the <code>SyncInfo</code> used as the base for the compare input.
94      *
95      * @since 3.1
96      */

97     public SyncInfoCompareInput(ISynchronizeParticipant participant, SyncInfo sync) {
98         this(participant.getName(), sync);
99         this.participant = participant;
100     }
101     
102     public SyncInfoCompareInput(ISynchronizePageConfiguration configuration,
103             SyncInfo info) {
104         this(configuration.getParticipant(), info);
105         this.synchronizeConfiguration = configuration;
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.compare.CompareEditorInput#handleDispose()
110      */

111     protected void handleDispose() {
112         super.handleDispose();
113         if (synchronizeConfiguration != null) {
114             ICompareNavigator navigator = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR);
115             if (navigator != null && navigator == super.getNavigator()) {
116                 synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, new CompareNavigator() {
117                     protected INavigatable[] getNavigatables() {
118                         return new INavigatable[0];
119                     }
120                 });
121             }
122         }
123     }
124     /* (non-Javadoc)
125      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
126      */

127     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
128         if (IFile.class.equals(adapter) && resource.getType() == IResource.FILE) {
129             return (IFile)resource;
130         }
131         return super.getAdapter(adapter);
132     }
133     
134     private static CompareConfiguration getDefaultCompareConfiguration() {
135         CompareConfiguration cc = new CompareConfiguration();
136         //cc.setProperty(CompareConfiguration.USE_OUTLINE_VIEW, true);
137
return cc;
138     }
139     
140     /**
141      * Note that until the compare editor inputs can be part of the compare editors lifecycle we
142      * can't register as a listener because there is no dispose() method to remove the listener.
143      */

144     public void resourceChanged(IResourceChangeEvent event) {
145         IResourceDelta delta = event.getDelta();
146         if (delta != null) {
147             IResourceDelta resourceDelta = delta.findMember(resource.getFullPath());
148             if (resourceDelta != null) {
149                 UIJob job = new UIJob("") { //$NON-NLS-1$
150
public IStatus runInUIThread(IProgressMonitor monitor) {
151                         if (!isSaveNeeded()) {
152                             //updateNode();
153
}
154                         return Status.OK_STATUS;
155                     }
156                 };
157                 job.setSystem(true);
158                 job.schedule();
159             }
160         }
161     }
162
163     /* (non-Javadoc)
164      * @see org.eclipse.team.ui.synchronize.SaveableCompareEditorInput#internalPrepareInput(org.eclipse.core.runtime.IProgressMonitor)
165      */

166     protected ICompareInput prepareCompareInput(IProgressMonitor monitor)
167             throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
168         // update the title now that the remote revision number as been fetched
169
// from the server
170
setTitle(getTitle());
171         monitor.beginTask(TeamUIMessages.SyncInfoCompareInput_3, 100);
172         monitor.setTaskName(TeamUIMessages.SyncInfoCompareInput_3);
173         try {
174             if (participant != null) {
175                 participant.prepareCompareInput(node, getCompareConfiguration(), Policy.subMonitorFor(monitor, 100));
176             } else {
177                 Utils.updateLabels(node.getSyncInfo(), getCompareConfiguration());
178                 node.cacheContents(Policy.subMonitorFor(monitor, 100));
179             }
180         } catch (TeamException e) {
181             throw new InvocationTargetException JavaDoc(e);
182         } finally {
183             monitor.done();
184         }
185         return node;
186     }
187
188     /*
189      * (non-Javadoc)
190      * @see org.eclipse.ui.IEditorInput#getToolTipText()
191      */

192     public String JavaDoc getToolTipText() {
193         return NLS.bind(TeamUIMessages.SyncInfoCompareInput_tooltip, new String JavaDoc[] { Utils.shortenText(30, description), node.getResource().getFullPath().toString() });
194     }
195
196     /*
197      * (non-Javadoc)
198      * @see java.lang.Object#equals(java.lang.Object)
199      */

200     public boolean equals(Object JavaDoc other) {
201         if (other == this)
202             return true;
203         if (other instanceof SyncInfoCompareInput) {
204             SyncInfo otherSyncInfo = ((SyncInfoCompareInput) other).getSyncInfo();
205             SyncInfo thisSyncInfo = getSyncInfo();
206             // Consider the inputs equal if the sync info are equal and the
207
// left nodes are equal (i.e they have the same timestamp)
208
return thisSyncInfo.equals(otherSyncInfo)
209                 && node.getLeft().equals(((SyncInfoCompareInput) other).node.getLeft());
210         }
211         return false;
212     }
213     
214     public SyncInfo getSyncInfo() {
215         return node.getSyncInfo();
216     }
217     
218     /* (non-Javadoc)
219      * @see org.eclipse.compare.CompareEditorInput#canRunInBackground()
220      */

221     public boolean canRunAsJob() {
222         return true;
223     }
224     
225     /* (non-Javadoc)
226      * @see org.eclipse.compare.CompareEditorInput#getNavigator()
227      */

228     public synchronized ICompareNavigator getNavigator() {
229         if (synchronizeConfiguration != null && isSelectedInSynchronizeView()) {
230             ICompareNavigator nav = (ICompareNavigator)synchronizeConfiguration.getProperty(SynchronizePageConfiguration.P_NAVIGATOR);
231             synchronizeConfiguration.setProperty(SynchronizePageConfiguration.P_INPUT_NAVIGATOR, super.getNavigator());
232             return nav;
233         }
234         return super.getNavigator();
235     }
236
237     private boolean isSelectedInSynchronizeView() {
238         if (synchronizeConfiguration != null) {
239             ISelection s = synchronizeConfiguration.getSite().getSelectionProvider().getSelection();
240             if (s instanceof IStructuredSelection) {
241                 IStructuredSelection ss = (IStructuredSelection) s;
242                 Object JavaDoc element = ss.getFirstElement();
243                 if (element instanceof SyncInfoModelElement) {
244                     SyncInfoModelElement sime = (SyncInfoModelElement) element;
245                     return sime.getSyncInfo().getLocal().equals(resource);
246                 }
247             }
248         }
249         return false;
250     }
251
252     /* (non-Javadoc)
253      * @see org.eclipse.team.ui.synchronize.SaveableCompareEditorInput#fireInputChange()
254      */

255     protected void fireInputChange() {
256         node.fireChange();
257     }
258 }
259
Popular Tags