KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > core > Team


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.core;
12
13 import java.io.*;
14 import java.util.*;
15 import java.util.Map.Entry;
16
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.Preferences.PropertyChangeEvent;
20 import org.eclipse.core.runtime.content.IContentType;
21 import org.eclipse.team.core.mapping.IStorageMerger;
22 import org.eclipse.team.internal.core.*;
23
24 /**
25  * The Team class provides a global point of reference for the global ignore set
26  * and the text/binary registry.
27  *
28  * @since 2.0
29  */

30 public final class Team {
31     
32     private static class StringMappingWrapper implements IFileTypeInfo {
33         
34         private final IStringMapping fMapping;
35
36         public StringMappingWrapper(IStringMapping mapping) {
37             fMapping= mapping;
38         }
39
40         public String JavaDoc getExtension() {
41             return fMapping.getString();
42         }
43
44         public int getType() {
45             return fMapping.getType();
46         }
47         
48     }
49     
50     private static final String JavaDoc PREF_TEAM_IGNORES = "ignore_files"; //$NON-NLS-1$
51
private static final String JavaDoc PREF_TEAM_SEPARATOR = "\n"; //$NON-NLS-1$
52
public static final Status OK_STATUS = new Status(IStatus.OK, TeamPlugin.ID, IStatus.OK, Messages.ok, null);
53     
54     // File type constants
55
public static final int UNKNOWN = 0;
56     public static final int TEXT = 1;
57     public static final int BINARY = 2;
58     
59
60     // The ignore list that is read at startup from the persisted file
61
protected static SortedMap globalIgnore, pluginIgnore;
62     private static StringMatcher[] ignoreMatchers;
63     
64     private final static FileContentManager fFileContentManager;
65     
66     static {
67         fFileContentManager= new FileContentManager();
68     }
69     
70     
71     /**
72      * Return the type of the given IStorage. First, we check whether a mapping has
73      * been defined for the name of the IStorage. If this is not the case, we check for
74      * a mapping with the extension. If no mapping is defined, UNKNOWN is returned.
75      *
76      * Valid return values are:
77      * Team.TEXT
78      * Team.BINARY
79      * Team.UNKNOWN
80      *
81      * @param storage the IStorage
82      * @return whether the given IStorage is TEXT, BINARY, or UNKNOWN
83      *
84      * @deprecated Use <code>getFileContentManager().getType(IStorage storage)</code> instead.
85      */

86     public static int getType(IStorage storage) {
87         return fFileContentManager.getType(storage);
88     }
89
90     /**
91      * Returns whether the given file should be ignored.
92      *
93      * This method answers true if the file matches one of the global ignore
94      * patterns, or if the file is marked as derived.
95      *
96      * @param resource the file
97      * @return whether the file should be ignored
98      */

99     public static boolean isIgnoredHint(IResource resource) {
100         if (resource.isDerived()) return true;
101         return matchesEnabledIgnore(resource);
102     }
103     
104     /**
105      * Returns whether the given file should be ignored.
106      * @deprecated use isIgnoredHint(IResource) instead
107      */

108     public static boolean isIgnoredHint(IFile file) {
109         if (file.isDerived()) return true;
110         return matchesEnabledIgnore(file);
111     }
112     
113     private static boolean matchesEnabledIgnore(IResource resource) {
114         StringMatcher[] matchers = getStringMatchers();
115         for (int i = 0; i < matchers.length; i++) {
116             if (matchers[i].match(resource.getName())) return true;
117         }
118         return false;
119     }
120     
121     /**
122      * Returns whether the given file should be ignored.
123      * @deprecated use isIgnoredHint instead
124      */

125     public static boolean isIgnored(IFile file) {
126         return matchesEnabledIgnore(file);
127     }
128
129     
130     /**
131      * Return all known file types.
132      *
133      * @return all known file types
134      * @deprecated Use <code>getFileContentManager().getExtensionMappings()</code> instead.
135      */

136     public static IFileTypeInfo[] getAllTypes() {
137         final IStringMapping [] mappings= fFileContentManager.getExtensionMappings();
138         final IFileTypeInfo [] infos= new IFileTypeInfo[mappings.length];
139         for (int i = 0; i < infos.length; i++) {
140             infos[i]= new StringMappingWrapper(mappings[i]);
141         }
142         return infos;
143     }
144     
145     /**
146      * Returns the list of global ignores.
147      */

148     public synchronized static IIgnoreInfo[] getAllIgnores() {
149         // The ignores are cached and when the preferences change the
150
// cache is cleared. This makes it faster to lookup without having
151
// to re-parse the preferences.
152
initializeIgnores();
153         IIgnoreInfo[] result = getIgnoreInfo(globalIgnore);
154         return result;
155     }
156
157     private static void initializeIgnores() {
158         if (globalIgnore == null) {
159             globalIgnore = new TreeMap();
160             pluginIgnore = new TreeMap();
161             ignoreMatchers = null;
162             try {
163                 readIgnoreState();
164             } catch (TeamException e) {
165                 TeamPlugin.log(IStatus.ERROR, Messages.Team_Error_loading_ignore_state_from_disk_1, e);
166             }
167             initializePluginIgnores(pluginIgnore, globalIgnore);
168         }
169     }
170
171     private static IIgnoreInfo[] getIgnoreInfo(Map gIgnore) {
172         IIgnoreInfo[] result = new IIgnoreInfo[gIgnore.size()];
173         Iterator e = gIgnore.entrySet().iterator();
174         int i = 0;
175         while (e.hasNext() ) {
176             Map.Entry entry = (Entry) e.next();
177             final String JavaDoc pattern = (String JavaDoc) entry.getKey();
178             final boolean enabled = ((Boolean JavaDoc)entry.getValue()).booleanValue();
179             result[i++] = new IIgnoreInfo() {
180                 private String JavaDoc p = pattern;
181                 private boolean e1 = enabled;
182                 public String JavaDoc getPattern() {
183                     return p;
184                 }
185                 public boolean getEnabled() {
186                     return e1;
187                 }
188             };
189         }
190         return result;
191     }
192
193     private synchronized static StringMatcher[] getStringMatchers() {
194         if (ignoreMatchers==null) {
195             IIgnoreInfo[] ignorePatterns = getAllIgnores();
196             ArrayList matchers = new ArrayList(ignorePatterns.length);
197             for (int i = 0; i < ignorePatterns.length; i++) {
198                 if (ignorePatterns[i].getEnabled()) {
199                     matchers.add(new StringMatcher(ignorePatterns[i].getPattern(), true, false));
200                 }
201             }
202             ignoreMatchers = new StringMatcher[matchers.size()];
203             ignoreMatchers = (StringMatcher[]) matchers.toArray(ignoreMatchers);
204         }
205         return ignoreMatchers;
206     }
207     
208     
209     /**
210      * Set the file type for the give extensions. This
211      * will replace the existing file types with this new list.
212      *
213      * Valid types are:
214      * Team.TEXT
215      * Team.BINARY
216      * Team.UNKNOWN
217      *
218      * @param extensions the file extensions
219      * @param types the file types
220      *
221      * @deprecated Use <code>getFileContentManager().setExtensionMappings()</code> instead.
222      */

223     public static void setAllTypes(String JavaDoc[] extensions, int[] types) {
224         fFileContentManager.addExtensionMappings(extensions, types);
225     }
226     
227
228
229     /**
230      * Add patterns to the list of global ignores.
231      */

232     public static void setAllIgnores(String JavaDoc[] patterns, boolean[] enabled) {
233         initializeIgnores();
234         globalIgnore = new TreeMap();
235         ignoreMatchers = null;
236         for (int i = 0; i < patterns.length; i++) {
237             globalIgnore.put(patterns[i], Boolean.valueOf(enabled[i]));
238         }
239         // Now set into preferences
240
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
241         Iterator e = globalIgnore.entrySet().iterator();
242         while (e.hasNext()) {
243             Map.Entry entry = (Entry) e.next();
244             String JavaDoc pattern = (String JavaDoc) entry.getKey();
245             Boolean JavaDoc value = (Boolean JavaDoc) entry.getValue();
246             boolean isCustom = (!pluginIgnore.containsKey(pattern)) ||
247                 !((Boolean JavaDoc)pluginIgnore.get(pattern)).equals(value);
248             if (isCustom) {
249                 buf.append(pattern);
250                 buf.append(PREF_TEAM_SEPARATOR);
251                 boolean en = value.booleanValue();
252                 buf.append(en);
253                 buf.append(PREF_TEAM_SEPARATOR);
254             }
255             
256         }
257         TeamPlugin.getPlugin().getPluginPreferences().setValue(PREF_TEAM_IGNORES, buf.toString());
258     }
259     
260     
261
262
263     /*
264      * IGNORE
265      *
266      * Reads the ignores currently defined by extensions.
267      */

268     private static void initializePluginIgnores(SortedMap pIgnore, SortedMap gIgnore) {
269         TeamPlugin plugin = TeamPlugin.getPlugin();
270         if (plugin != null) {
271             IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(TeamPlugin.ID, TeamPlugin.IGNORE_EXTENSION);
272             if (extension != null) {
273                 IExtension[] extensions = extension.getExtensions();
274                 for (int i = 0; i < extensions.length; i++) {
275                     IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
276                     for (int j = 0; j < configElements.length; j++) {
277                         String JavaDoc pattern = configElements[j].getAttribute("pattern"); //$NON-NLS-1$
278
if (pattern != null) {
279                             String JavaDoc selected = configElements[j].getAttribute("enabled"); //$NON-NLS-1$
280
if (selected == null) {
281                                 // Check for selected because this used to be the field name
282
selected = configElements[j].getAttribute("selected"); //$NON-NLS-1$
283
}
284                             boolean enabled = selected != null && selected.equalsIgnoreCase("true"); //$NON-NLS-1$
285
// if this ignore does already exist
286
if (gIgnore.containsKey(pattern)){
287                                 // ignore plugins settings
288
pIgnore.put(pattern, gIgnore.get(pattern));
289                             } else {
290                                 // add ignores
291
pIgnore.put(pattern, Boolean.valueOf(enabled));
292                                 gIgnore.put(pattern, Boolean.valueOf(enabled));
293                             }
294                         }
295                     }
296                 }
297             }
298         }
299     }
300
301     /*
302      * IGNORE
303      *
304      * Reads global ignore preferences and populates globalIgnore
305      */

306     private static void readIgnoreState() throws TeamException {
307         if (readBackwardCompatibleIgnoreState()) return;
308         Preferences pref = TeamPlugin.getPlugin().getPluginPreferences();
309         if (!pref.contains(PREF_TEAM_IGNORES)) return;
310         pref.addPropertyChangeListener(new Preferences.IPropertyChangeListener() {
311             public void propertyChange(PropertyChangeEvent event) {
312                 // when a property is changed, invalidate our cache so that
313
// properties will be recalculated.
314
if(event.getProperty().equals(PREF_TEAM_IGNORES))
315                     globalIgnore = null;
316             }
317         });
318         String JavaDoc prefIgnores = pref.getString(PREF_TEAM_IGNORES);
319         StringTokenizer tok = new StringTokenizer(prefIgnores, PREF_TEAM_SEPARATOR);
320         String JavaDoc pattern, enabled;
321         try {
322             while (true) {
323                 pattern = tok.nextToken();
324                 if (pattern.length()==0) return;
325                 enabled = tok.nextToken();
326                 globalIgnore.put(pattern, Boolean.valueOf(enabled));
327             }
328         } catch (NoSuchElementException e) {
329             return;
330         }
331     }
332
333     /*
334      * For backward compatibility, we still look at if we have .globalIgnores
335      */

336     private static boolean readBackwardCompatibleIgnoreState() throws TeamException {
337         String JavaDoc GLOBALIGNORE_FILE = ".globalIgnores"; //$NON-NLS-1$
338
IPath pluginStateLocation = TeamPlugin.getPlugin().getStateLocation().append(GLOBALIGNORE_FILE);
339         File f = pluginStateLocation.toFile();
340         if (!f.exists()) return false;
341         try {
342             DataInputStream dis = new DataInputStream(new FileInputStream(f));
343             try {
344                 int ignoreCount = 0;
345                 try {
346                     ignoreCount = dis.readInt();
347                 } catch (EOFException e) {
348                     // Ignore the exception, it will occur if there are no ignore
349
// patterns stored in the provider state file.
350
return false;
351                 }
352                 for (int i = 0; i < ignoreCount; i++) {
353                     String JavaDoc pattern = dis.readUTF();
354                     boolean enabled = dis.readBoolean();
355                     globalIgnore.put(pattern, Boolean.valueOf(enabled));
356                 }
357             } finally {
358                 dis.close();
359             }
360             f.delete();
361         } catch (FileNotFoundException e) {
362             // not a fatal error, there just happens not to be any state to read
363
} catch (IOException ex) {
364             throw new TeamException(new Status(IStatus.ERROR, TeamPlugin.ID, 0, Messages.Team_readError, ex));
365         }
366         return true;
367     }
368     /**
369      * Initialize the registry, restoring its state.
370      *
371      * This method is called by the plug-in upon startup, clients should not call this method
372      */

373     public static void startup() {
374         // Register a delta listener that will tell the provider about a project move and meta-file creation
375
ResourcesPlugin.getWorkspace().addResourceChangeListener(new TeamResourceChangeListener(), IResourceChangeEvent.POST_CHANGE);
376     }
377     
378     /**
379      * Shut down the registry, persisting its state.
380      *
381      * This method is called by the plug-in upon shutdown, clients should not call this method
382      */

383     public static void shutdown() {
384         TeamPlugin.getPlugin().savePluginPreferences();
385     }
386     /**
387      * @deprecated
388      * Use {@link org.eclipse.team.core.RepositoryProviderType#getProjectSetCapability()}
389      * to obtain an instance of {@link ProjectSetCapability} instead.
390      */

391     public static IProjectSetSerializer getProjectSetSerializer(String JavaDoc id) {
392         TeamPlugin plugin = TeamPlugin.getPlugin();
393         if (plugin != null) {
394             IExtensionPoint extension = plugin.getDescriptor().getExtensionPoint(TeamPlugin.PROJECT_SET_EXTENSION);
395             if (extension != null) {
396                 IExtension[] extensions = extension.getExtensions();
397                 for (int i = 0; i < extensions.length; i++) {
398                     IConfigurationElement [] configElements = extensions[i].getConfigurationElements();
399                     for (int j = 0; j < configElements.length; j++) {
400                         String JavaDoc extensionId = configElements[j].getAttribute("id"); //$NON-NLS-1$
401
if (extensionId != null && extensionId.equals(id)) {
402                             try {
403                                 return (IProjectSetSerializer)configElements[j].createExecutableExtension("class"); //$NON-NLS-1$
404
} catch (CoreException e) {
405                                 TeamPlugin.log(e);
406                                 return null;
407                             }
408                         }
409                     }
410                 }
411             }
412         }
413         return null;
414     }
415     
416
417     /**
418      * Return the default ignore infos
419      * (i.e. those that are specified in
420      * plugin manifests).
421      * @return the default ignore infos.
422      * @since 3.0
423      */

424     public static IIgnoreInfo[] getDefaultIgnores() {
425         SortedMap gIgnore = new TreeMap();
426         SortedMap pIgnore = new TreeMap();
427         initializePluginIgnores(pIgnore, gIgnore);
428         return getIgnoreInfo(gIgnore);
429     }
430
431     /**
432      * TODO: change to file content manager
433      * Return the default file type bindings
434      * (i.e. those that are specified in
435      * plugin manifests).
436      * @return the default file type bindings
437      * @since 3.0
438      * @deprecated Use Team.getFileContentManager().getDefaultExtensionMappings() instead.
439      */

440     public static IFileTypeInfo[] getDefaultTypes() {
441         return asFileTypeInfo(getFileContentManager().getDefaultExtensionMappings());
442     }
443
444     private static IFileTypeInfo [] asFileTypeInfo(IStringMapping [] mappings) {
445         final IFileTypeInfo [] infos= new IFileTypeInfo[mappings.length];
446         for (int i = 0; i < infos.length; i++) {
447             infos[i]= new StringMappingWrapper(mappings[i]);
448         }
449         return infos;
450     }
451
452     /**
453      * Get the file content manager which implements the API for manipulating the mappings between
454      * file names, file extensions and content types.
455      *
456      * @return an instance of IFileContentManager
457      *
458      * @see IFileContentManager
459      *
460      * @since 3.1
461      */

462     public static IFileContentManager getFileContentManager() {
463         return fFileContentManager;
464     }
465     
466     /**
467      * Creates a storage merger for the given content type.
468      * If no storage merger is registered for the given content type <code>null</code> is returned.
469      *
470      * @param type the type for which to find a storage merger
471      * @return a storage merger for the given type, or <code>null</code> if no
472      * storage merger has been registered
473      *
474      * @since 3.2
475      */

476     public IStorageMerger createStorageMerger(IContentType type) {
477         return StorageMergerRegistry.getInstance().createStreamMerger(type);
478     }
479     
480     /**
481      * Creates a storage merger for the given file extension.
482      * If no storage merger is registered for the file extension <code>null</code> is returned.
483      *
484      * @param extension the extension for which to find a storage merger
485      * @return a stream merger for the given type, or <code>null</code> if no
486      * storage merger has been registered
487      *
488      * @since 3.2
489      */

490     public IStorageMerger createStorageMerger(String JavaDoc extension) {
491         return StorageMergerRegistry.getInstance().createStreamMerger(extension);
492     }
493 }
494
Popular Tags