KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > registry > TeamContentProviderManager


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.registry;
12
13 import java.util.*;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.team.core.mapping.ISynchronizationScope;
19 import org.eclipse.team.internal.ui.TeamUIPlugin;
20 import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor;
21 import org.eclipse.team.ui.mapping.ITeamContentProviderManager;
22
23 /**
24  * Manages the team content provider extension point
25  */

26 public class TeamContentProviderManager implements ITeamContentProviderManager {
27     
28     public static final String JavaDoc PT_TEAM_CONTENT_PROVIDERS = "teamContentProviders"; //$NON-NLS-1$
29

30     private static ITeamContentProviderManager instance;
31     
32     Map descriptors;
33     
34     private ListenerList listeners = new ListenerList(ListenerList.IDENTITY);
35     
36     public static ITeamContentProviderManager getInstance() {
37         if (instance == null)
38             instance = new TeamContentProviderManager();
39         return instance;
40     }
41     
42     /* (non-Javadoc)
43      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderManager#getDescriptors()
44      */

45     public ITeamContentProviderDescriptor[] getDescriptors() {
46         lazyInitialize();
47         return (ITeamContentProviderDescriptor[]) descriptors.values().toArray(new ITeamContentProviderDescriptor[descriptors.size()]);
48     }
49     
50     /* (non-Javadoc)
51      * @see org.eclipse.team.ui.mapping.ITeamContentProviderManager#getContentProviderIds(org.eclipse.team.core.mapping.ISynchronizationScope)
52      */

53     public String JavaDoc[] getContentProviderIds(ISynchronizationScope scope) {
54         List result = new ArrayList();
55         ITeamContentProviderDescriptor[] descriptors = getDescriptors();
56         for (int i = 0; i < descriptors.length; i++) {
57             ITeamContentProviderDescriptor descriptor = descriptors[i];
58             if (descriptor.isEnabled() && scope.getMappings(descriptor.getModelProviderId()).length > 0)
59                 result.add(descriptor.getContentExtensionId());
60         }
61         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderManager#getDescriptor(java.lang.String)
66      */

67     public ITeamContentProviderDescriptor getDescriptor(String JavaDoc modelProviderId) {
68         lazyInitialize();
69         return (ITeamContentProviderDescriptor)descriptors.get(modelProviderId);
70     }
71     
72     protected void lazyInitialize() {
73         if (descriptors != null)
74             return;
75         IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(TeamUIPlugin.ID, PT_TEAM_CONTENT_PROVIDERS);
76         IExtension[] extensions = point.getExtensions();
77         descriptors = new HashMap(extensions.length * 2);
78         for (int i = 0, imax = extensions.length; i < imax; i++) {
79             ITeamContentProviderDescriptor desc = null;
80             try {
81                 desc = new TeamContentProviderDescriptor(extensions[i]);
82             } catch (CoreException e) {
83                 TeamUIPlugin.log(e);
84             }
85             if (desc != null)
86                 descriptors.put(desc.getModelProviderId(), desc);
87         }
88     }
89
90     public void addPropertyChangeListener(IPropertyChangeListener listener) {
91         listeners.add(listener);
92     }
93
94     public void removePropertyChangeListener(IPropertyChangeListener listener) {
95         listeners.remove(listener);
96     }
97     
98     private void firePropertyChange(final PropertyChangeEvent event) {
99         Object JavaDoc[] allListeners = listeners.getListeners();
100         for (int i = 0; i < allListeners.length; i++) {
101             final IPropertyChangeListener listener = (IPropertyChangeListener)allListeners[i];
102             SafeRunner.run(new ISafeRunnable() {
103                 public void run() throws Exception JavaDoc {
104                     listener.propertyChange(event);
105                 }
106                 public void handleException(Throwable JavaDoc exception) {
107                     // handler by runner
108
}
109             });
110         }
111     }
112     
113     public void enablementChanged(ITeamContentProviderDescriptor[] oldEnabled, ITeamContentProviderDescriptor[] newEnabled) {
114         firePropertyChange(new PropertyChangeEvent(this, PROP_ENABLED_MODEL_PROVIDERS, oldEnabled, newEnabled));
115     }
116     
117     public void setEnabledDescriptors(ITeamContentProviderDescriptor[] descriptors) {
118         List previouslyEnabled = new ArrayList();
119         for (Iterator iter = this.descriptors.values().iterator(); iter.hasNext();) {
120             TeamContentProviderDescriptor descriptor = (TeamContentProviderDescriptor) iter.next();
121             if (descriptor.isEnabled()) {
122                 previouslyEnabled.add(descriptor);
123                 descriptor.setEnabled(false);
124             }
125         }
126         for (int i = 0; i < descriptors.length; i++) {
127             TeamContentProviderDescriptor descriptor = (TeamContentProviderDescriptor)descriptors[i];
128             descriptor.setEnabled(true);
129         }
130         enablementChanged(
131                 (ITeamContentProviderDescriptor[]) previouslyEnabled.toArray(new ITeamContentProviderDescriptor[previouslyEnabled.size()]),
132                 descriptors);
133     }
134 }
135
Popular Tags