KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > SyncInfoModelElement


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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;
12
13 import org.eclipse.compare.ITypedElement;
14 import org.eclipse.compare.structuremergeviewer.*;
15 import org.eclipse.core.resources.IEncodedStorage;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.*;
18 import org.eclipse.team.core.TeamException;
19 import org.eclipse.team.core.synchronize.SyncInfo;
20 import org.eclipse.team.core.variants.IResourceVariant;
21 import org.eclipse.team.internal.ui.Policy;
22 import org.eclipse.team.internal.ui.TeamUIPlugin;
23
24 /**
25  * A diff node used to display the synchronization state for resources described by
26  * existing {@link SyncInfo} objects. The synchronization state for a node can
27  * change after it has been created. Since it implements the <code>ITypedElement</code>
28  * and <code>ICompareInput</code> interfaces it can be used directly to
29  * display the compare result in a <code>DiffTreeViewer</code> and as the
30  * input to any other compare/merge viewer.
31  * <p>
32  * Clients typically use this class as is, but may subclass if required.
33  * </p>
34  * @see DiffTreeViewer
35  * @see Differencer
36  */

37 public class SyncInfoModelElement extends SynchronizeModelElement {
38         
39     private ITypedElement ancestor;
40     private SyncInfo info;
41     
42     /**
43      * Construct a <code>SyncInfoModelElement</code> for the given resource.
44      *
45      * @param parent
46      * @param info
47      */

48     public SyncInfoModelElement(IDiffContainer parent, SyncInfo info) {
49         super(parent);
50         
51         Assert.isNotNull(info);
52         this.info = info;
53         // update state
54
setKind(info.getKind());
55         // local
56
setLeft(createLocalTypeElement(info));
57         // remote
58
setRight(createRemoteTypeElement(info));
59         // base
60
setAncestor(createBaseTypeElement(info));
61             
62         fireChange();
63     }
64
65     /**
66      * Update this element with a changed sync info. The remote and base handles have to be updated
67      * with the new handles in the sync info.
68      *
69      * @param info the new sync info
70      */

71     public void update(SyncInfo info) {
72         this.info = info;
73         // update state
74
setKind(info.getKind());
75             
76         // Remote
77
RemoteResourceTypedElement rightEl = (RemoteResourceTypedElement)getRight();
78         IResourceVariant remote = info.getRemote();
79         if(rightEl == null && remote != null) {
80             setRight(createRemoteTypeElement(info));
81         } else if(rightEl != null) {
82             if(remote == null) {
83                 setRight(null);
84             } else {
85                 setRight(createRemoteTypeElement(info));
86             }
87         }
88         // Base
89
RemoteResourceTypedElement ancestorEl = (RemoteResourceTypedElement)getAncestor();
90         IResourceVariant base = info.getBase();
91         if(ancestorEl == null && base != null) {
92             setAncestor(createBaseTypeElement(info));
93         } else if(ancestorEl != null) {
94             if(base == null) {
95                 setAncestor(null);
96             } else {
97                 setAncestor(createBaseTypeElement(info));
98             }
99         }
100         
101         fireChange();
102     }
103
104     /* (non-Javadoc)
105      * @see org.eclipse.compare.structuremergeviewer.DiffElement#getKind()
106      */

107     public int getKind() {
108         SyncInfo info = getSyncInfo();
109         if (info != null) {
110             return info.getKind();
111         } else {
112             return SyncInfo.IN_SYNC;
113         }
114     }
115     
116     /**
117      * We have to track the base because <code>DiffNode</code> doesn't provide a
118      * setter. See:
119      * https://bugs.eclipse.org/bugs/show_bug.cgi?id=52261
120      */

121     public void setAncestor(ITypedElement ancestor) {
122         this.ancestor = ancestor;
123     }
124         
125     /* (non-Javadoc)
126      * @see org.eclipse.compare.structuremergeviewer.DiffNode#getAncestor()
127      */

128     public ITypedElement getAncestor() {
129         return this.ancestor;
130     }
131     
132     /* (non-Javadoc)
133      * @see org.eclipse.compare.structuremergeviewer.DiffNode#getName()
134      */

135     public String JavaDoc getName() {
136         IResource resource = getResource();
137         if(resource != null) {
138             return resource.getName();
139         } else {
140             return super.getName();
141         }
142     }
143     
144     /* (non-Javadoc)
145      * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
146      */

147     public Object JavaDoc getAdapter(Class JavaDoc adapter) {
148         if(adapter == SyncInfo.class) {
149             return getSyncInfo();
150         }
151         return super.getAdapter(adapter);
152     }
153     
154     /**
155      * Helper method that returns the resource associated with this node. A node is not
156      * required to have an associated local resource.
157      * @return the resource associated with this node or <code>null</code> if the local
158      * contributor is not a resource.
159      */

160     public IResource getResource() {
161         return info.getLocal();
162     }
163     
164     /* (non-Javadoc)
165      * @see java.lang.Object#toString()
166      */

167     public String JavaDoc toString() {
168         return getResource().getFullPath().toString();
169     }
170     
171     /**
172      * Cache the contents for the base and remote.
173      * @param monitor
174      */

175     public void cacheContents(IProgressMonitor monitor) throws TeamException {
176         ITypedElement base = getAncestor();
177         ITypedElement remote = getRight();
178         int work = Math.min((remote== null ? 0 : 50) + (base == null ? 0 : 50), 10);
179         monitor.beginTask(null, work);
180         try {
181             if (base != null && base instanceof RemoteResourceTypedElement) {
182                 ((RemoteResourceTypedElement)base).cacheContents(Policy.subMonitorFor(monitor, 50));
183             }
184             if (remote != null && remote instanceof RemoteResourceTypedElement) {
185                 ((RemoteResourceTypedElement)remote).cacheContents(Policy.subMonitorFor(monitor, 50));
186             }
187         } catch (CoreException e) {
188             throw TeamException.asTeamException(e);
189         } finally {
190             monitor.done();
191         }
192     }
193     
194     public SyncInfo getSyncInfo() {
195         return info;
196     }
197
198     /**
199      * Create an ITypedElement for the given local resource. The returned ITypedElement
200      * will prevent editing of outgoing deletions.
201      */

202     private static ITypedElement createTypeElement(final IResource resource, final int kind) {
203         if(resource != null) {
204             return new LocalResourceTypedElement(resource);
205         }
206         return null;
207     }
208     
209     /**
210      * Create an ITypedElement for the given remote resource. The contents for the remote resource
211      * will be retrieved from the given IStorage which is a local cache used to buffer the remote contents
212      */

213     protected static ITypedElement createTypeElement(IResourceVariant remoteResource, String JavaDoc encoding) {
214         return new RemoteResourceTypedElement(remoteResource,encoding);
215     }
216
217     protected static ITypedElement createRemoteTypeElement(SyncInfo info) {
218         if(info != null && info.getRemote() != null) {
219             return createTypeElement(info.getRemote(), getEncoding(info.getLocal()));
220         }
221         return null;
222     }
223
224     private static String JavaDoc getEncoding(IResource local) {
225         if (local instanceof IEncodedStorage) {
226             IEncodedStorage es = (IEncodedStorage) local;
227             try {
228                 return es.getCharset();
229             } catch (CoreException e) {
230                 TeamUIPlugin.log(e);
231             }
232         }
233         return null;
234     }
235
236     protected static ITypedElement createLocalTypeElement(SyncInfo info) {
237         if(info != null && info.getLocal() != null) {
238             return createTypeElement(info.getLocal(), info.getKind());
239         }
240         return null;
241     }
242
243     protected static ITypedElement createBaseTypeElement(SyncInfo info) {
244         if(info != null && info.getBase() != null) {
245             return createTypeElement(info.getBase(), getEncoding(info.getLocal()));
246         }
247         return null;
248     }
249 }
250
Popular Tags