KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > ui > mapping > SynchronizationCompareAdapter


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.ui.mapping;
12
13 import java.util.*;
14
15 import org.eclipse.compare.structuremergeviewer.ICompareInput;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.mapping.ModelProvider;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20 import org.eclipse.core.runtime.*;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.team.core.diff.IDiff;
23 import org.eclipse.team.core.mapping.ISynchronizationContext;
24 import org.eclipse.team.internal.ui.TeamUIMessages;
25 import org.eclipse.team.internal.ui.Utils;
26 import org.eclipse.team.internal.ui.mapping.ResourceDiffCompareInput;
27 import org.eclipse.team.ui.TeamUI;
28 import org.eclipse.ui.model.IWorkbenchAdapter;
29
30 /**
31  * A abstract implementation of {@link ISynchronizationCompareAdapter}.
32  * The {@link #asCompareInput(ISynchronizationContext, Object) }
33  * which will convert file objects to an appropriate compare input.
34  * <p>
35  * Clients may subclass this class.
36  *
37  * @since 3.2
38  */

39 public abstract class SynchronizationCompareAdapter implements ISynchronizationCompareAdapter {
40
41     /**
42      * Default implementation that is capable of returning a compare input for objects
43      * that adapt to {@link IFile}. Subclasses should override if compare inputs are
44      * available for other types of model elements.
45      * @see ISynchronizationCompareAdapter#asCompareInput(ISynchronizationContext, Object)
46      */

47     public ICompareInput asCompareInput(ISynchronizationContext context, Object JavaDoc o) {
48         IResource resource = Utils.getResource(o);
49         if (resource != null) {
50             if (resource.getType() == IResource.FILE) {
51                 IDiff node = context.getDiffTree().getDiff(resource);
52                 if (node != null)
53                     return new ResourceDiffCompareInput(node, context);
54             }
55         }
56         return null;
57     }
58
59     /* (non-Javadoc)
60      * @see org.eclipse.team.ui.mapping.ICompareAdapter#hasCompareInput(org.eclipse.team.core.mapping.ISynchronizationContext, java.lang.Object)
61      */

62     public boolean hasCompareInput(ISynchronizationContext context, Object JavaDoc object) {
63         return asCompareInput(context, object) != null;
64     }
65
66     /* (non-Javadoc)
67      * @see org.eclipse.team.ui.mapping.IResourceMappingPersistenceAdapter#getName(org.eclipse.core.resources.mapping.ResourceMapping)
68      */

69     public String JavaDoc getName(ResourceMapping mapping) {
70         Object JavaDoc object = mapping.getModelObject();
71         IWorkbenchAdapter adapter = (IWorkbenchAdapter) Utils.getAdapter(
72                 object, IWorkbenchAdapter.class);
73         if (adapter != null) {
74             String JavaDoc label = adapter.getLabel(object);
75             if (label != null)
76                 return label;
77         }
78         IResource resource = Utils.getResource(object);
79         if (resource != null)
80             return resource.getName();
81         if (object instanceof ModelProvider) {
82             ModelProvider provider = (ModelProvider) object;
83             if (provider.getId().equals(ModelProvider.RESOURCE_MODEL_PROVIDER_ID)) {
84                 return TeamUIMessages.SynchronizationCompareAdapter_0;
85             }
86             return provider.getDescriptor().getLabel();
87         }
88         return ""; //$NON-NLS-1$
89
}
90     
91     /* (non-Javadoc)
92      * @see org.eclipse.team.ui.mapping.IResourceMappingPersistenceAdapter#getFullPath(org.eclipse.core.resources.mapping.ResourceMapping)
93      */

94     public String JavaDoc getPathString(ResourceMapping mapping) {
95         Object JavaDoc object = mapping.getModelObject();
96         IWorkbenchAdapter adapter = (IWorkbenchAdapter) Utils.getAdapter(
97                 object, IWorkbenchAdapter.class);
98         if (adapter != null) {
99             List segments = new ArrayList();
100             Object JavaDoc parent = object;
101             do {
102                 String JavaDoc segment = adapter.getLabel(parent);
103                 if (segment != null && segment.length() > 0)
104                     segments.add(0, segment);
105                 parent = adapter.getParent(parent);
106             } while (parent != null);
107             if (!segments.isEmpty()) {
108                 IPath path = Path.EMPTY;
109                 for (Iterator iter = segments.iterator(); iter.hasNext();) {
110                     String JavaDoc segment = (String JavaDoc) iter.next();
111                     path = path.append(segment);
112                 }
113                 return path.toString();
114             }
115         }
116         return getName(mapping);
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter#getImageDescriptor(org.eclipse.core.resources.mapping.ResourceMapping)
121      */

122     public ImageDescriptor getImageDescriptor(ResourceMapping mapping) {
123         Object JavaDoc object = mapping.getModelObject();
124         ImageDescriptor image = getImageDescriptorFromWorkbenchAdapter(object);
125         if (image != null)
126             return image;
127         IResource resource = Utils.getResource(object);
128         if (resource != null) {
129             image = getImageDescriptorFromWorkbenchAdapter(resource);
130             if (image != null)
131                 return image;
132         }
133         if (object instanceof ModelProvider) {
134             ModelProvider provider = (ModelProvider) object;
135             ITeamContentProviderDescriptor desc = TeamUI.getTeamContentProviderManager().getDescriptor(provider.getId());
136             if (desc != null)
137                 return desc.getImageDescriptor();
138         }
139         return null;
140     }
141
142     private ImageDescriptor getImageDescriptorFromWorkbenchAdapter(Object JavaDoc object) {
143         IWorkbenchAdapter adapter = (IWorkbenchAdapter) Utils.getAdapter(
144                 object, IWorkbenchAdapter.class);
145         if (adapter != null) {
146             return adapter.getImageDescriptor(object);
147         }
148         return null;
149     }
150     
151     /**
152      * Return the synchronization state of the resource mapping with respect to
153      * the given team state provider. This method is invoked from instances of
154      * {@link ITeamStateProvider} when the synchronization state description for
155      * an element is requested.
156      *
157      * @param provider
158      * the team state provider
159      * @param mapping
160      * the element
161      * @param stateMask
162      * the state mask that indicates which state flags are desired
163      * @param monitor
164      * a progress monitor
165      * @return the synchronization state of the element or -1 if the calculation
166      * of the state should be done using the resources of the mapping.
167      * @throws CoreException
168      *
169      * @since 3.3
170      */

171     public int getSynchronizationState(ITeamStateProvider provider, ResourceMapping mapping, int stateMask, IProgressMonitor monitor) throws CoreException {
172         return -1;
173     }
174     
175 }
176
Popular Tags