KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > tags > TagSource


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.ccvs.ui.tags;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.team.core.TeamException;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
25
26 /**
27  * A tag source provides access to a set of tags.
28  */

29 public abstract class TagSource {
30     
31     /*
32      * Special constant representing the BASE tag
33      */

34     public static final int BASE = -1;
35     
36     public static final TagSource EMPTY = new TagSource() {
37         public void commit(CVSTag[] tags, boolean replace, IProgressMonitor monitor) throws CVSException {
38             // No-op
39
}
40         public ICVSRepositoryLocation getLocation() {
41             // TODO Auto-generated method stub
42
return null;
43         }
44         public String JavaDoc getShortDescription() {
45             return "Empty"; //$NON-NLS-1$
46
}
47         public CVSTag[] getTags(int type) {
48             return new CVSTag[0];
49         }
50         public CVSTag[] refresh(boolean bestEffort, IProgressMonitor monitor) throws TeamException {
51             return new CVSTag[0];
52         }
53         public ICVSResource[] getCVSResources() {
54             return new ICVSResource[0];
55         }
56     };
57     
58     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
59     
60     /**
61      * Simple interface for providing notification when the tags
62      * for this source have changed.
63      */

64     public interface ITagSourceChangeListener {
65         void tagsChanged(TagSource source);
66     }
67     
68     public static int[] convertIncludeFlaqsToTagTypes(int includeFlags) {
69         List JavaDoc types = new ArrayList JavaDoc();
70         if ((includeFlags & TagSelectionArea.INCLUDE_BRANCHES) > 0)
71             types.add(new Integer JavaDoc(CVSTag.BRANCH));
72         if ((includeFlags & TagSelectionArea.INCLUDE_VERSIONS) > 0)
73             types.add(new Integer JavaDoc(CVSTag.VERSION));
74         if ((includeFlags & (TagSelectionArea.INCLUDE_HEAD_TAG)) > 0)
75             types.add(new Integer JavaDoc(CVSTag.HEAD));
76         if ((includeFlags & (TagSelectionArea.INCLUDE_DATES)) > 0)
77             types.add(new Integer JavaDoc(CVSTag.DATE));
78         if ((includeFlags & (TagSelectionArea.INCLUDE_BASE_TAG)) > 0)
79             types.add(new Integer JavaDoc(BASE));
80         int[] result = new int[types.size()];
81         for (int i = 0; i < result.length; i++) {
82             result[i] = ((Integer JavaDoc)types.get(i)).intValue();
83             
84         }
85         return result;
86     }
87     
88     /**
89      * Create a tag source for the given folders
90      * @param folders one or more folders
91      * @return a tag source for the supplied folders
92      */

93     public static TagSource create(ICVSFolder[] folders) {
94         if (folders.length == 1) {
95             return new SingleFolderTagSource(folders[0]);
96         } else {
97             return new MultiFolderTagSource(folders);
98         }
99     }
100     
101     /**
102      * Create a tag source for a list of resources
103      * @param resources one or more resources
104      * @return a tag source
105      */

106     public static TagSource create(ICVSResource[] resources) {
107         if (resources.length == 1 && !resources[0].isFolder())
108             return new SingleFileTagSource((ICVSFile)resources[0]);
109         return create(getFolders(resources));
110     }
111
112     private static ICVSFolder[] getFolders(ICVSResource[] resources) {
113         return new ICVSFolder[] { getFirstFolder(resources) } ;
114     }
115
116     /**
117      * Create a tag source for a list of resources
118      * @param resources one or more resources
119      * @return a tag source
120      */

121     public static TagSource create(IResource[] resources) {
122         return create(getCVSResources(getProjects(resources)));
123     }
124     
125     /**
126      * Create a tag source for the given mappers.
127      * @param mappers the mappers
128      * @return a tag source
129      */

130     public static TagSource create(ResourceMapping[] mappers) {
131         return create(getCVSResources(getProjects(mappers)));
132     }
133     
134     private static IResource[] getProjects(ResourceMapping[] mappers) {
135         Set JavaDoc projects = new HashSet JavaDoc();
136         for (int i = 0; i < mappers.length; i++) {
137             ResourceMapping mapper = mappers[i];
138             projects.addAll(Arrays.asList(mapper.getProjects()));
139         }
140         return (IResource[]) projects.toArray(new IResource[projects.size()]);
141     }
142
143     private static IResource[] getProjects(IResource[] resources) {
144         Set JavaDoc result = new HashSet JavaDoc();
145         for (int i = 0; i < resources.length; i++) {
146             IResource resource = resources[i];
147             result.add(resource.getProject());
148         }
149         return (IResource[]) result.toArray(new IResource[result.size()]);
150     }
151
152     /**
153      * Return a tag source for a single remote folder
154      * @param remote the remote folder
155      * @return a tag source for that folder
156      */

157     public static TagSource create(ICVSRemoteFolder remote) {
158         return new SingleFolderTagSource(remote);
159     }
160     
161     private static ICVSResource[] getCVSResources(IResource[] resources) {
162         List JavaDoc cvsResources = new ArrayList JavaDoc();
163         for (int i = 0; i < resources.length; i++) {
164             IResource resource = resources[i];
165             cvsResources.add(CVSWorkspaceRoot.getCVSResourceFor(resource));
166         }
167         return (ICVSResource[]) cvsResources.toArray(new ICVSResource[cvsResources.size()]);
168     }
169
170     private static ICVSFolder getFirstFolder(ICVSResource[] resources) {
171         if (resources[0].isFolder()) {
172             return (ICVSFolder)resources[0];
173         } else {
174             return resources[0].getParent();
175         }
176     }
177     
178     public CVSTag[] getTags(int type) {
179         switch (type) {
180             case BASE:
181                 return new CVSTag[] { CVSTag.BASE };
182             case CVSTag.HEAD:
183                 return new CVSTag[] { CVSTag.DEFAULT };
184         }
185         return new CVSTag[0];
186     }
187     
188     public CVSTag[] getTags(int[] types) {
189         if (types.length == 0) {
190             return new CVSTag[0];
191         }
192         if (types.length == 1) {
193             return getTags(types[0]);
194         }
195         List JavaDoc result = new ArrayList JavaDoc();
196         for (int i = 0; i < types.length; i++) {
197             int type = types[i];
198             CVSTag[] tags = getTags(type);
199             result.addAll(Arrays.asList(tags));
200         }
201         return (CVSTag[]) result.toArray(new CVSTag[result.size()]);
202     }
203
204     /**
205      * Refresh the tags by contacting the server if appropriate
206      * @param monitor a progress monitor
207      * @param bestEffort if best effort is true, then the whole folder contents may be searched
208      * @return any discovered tags
209      */

210     public abstract CVSTag[] refresh(boolean bestEffort, IProgressMonitor monitor) throws TeamException;
211     
212     public abstract ICVSRepositoryLocation getLocation();
213
214     /**
215      * Return a short description of the tag source for displaying in UI.
216      * @return a short description of the tag source for displaying in UI.
217      */

218     public abstract String JavaDoc getShortDescription();
219
220     /**
221      * Commit a set of tag changes to the tag cache
222      * @param tags the tags that should be cached
223      * @param replace whether existing tags not in the list should be removed
224      * @param monitor a progress monitor
225      * @throws CVSException
226      */

227     public abstract void commit(CVSTag[] tags, boolean replace, IProgressMonitor monitor) throws CVSException;
228     
229     public void addListener(ITagSourceChangeListener listener) {
230         listeners.add(listener);
231     }
232
233     public void removeListener(ITagSourceChangeListener listener) {
234         listeners.remove(listener);
235     }
236     
237     /**
238      * Notify all listeners that the tags from this source may have changed
239      */

240     public void fireChange() {
241         Object JavaDoc[] list = listeners.getListeners();
242         for (int i = 0; i < list.length; i++) {
243             final ITagSourceChangeListener listener = (ITagSourceChangeListener)list[i];
244             Platform.run(new ISafeRunnable() {
245                 public void handleException(Throwable JavaDoc exception) {
246                     // logged by run
247
}
248                 public void run() throws Exception JavaDoc {
249                     listener.tagsChanged(TagSource.this);
250                 }
251             });
252         }
253     }
254     
255     public abstract ICVSResource[] getCVSResources();
256 }
257
Popular Tags