1 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 26 public class TeamContentProviderManager implements ITeamContentProviderManager { 27 28 public static final String PT_TEAM_CONTENT_PROVIDERS = "teamContentProviders"; 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 45 public ITeamContentProviderDescriptor[] getDescriptors() { 46 lazyInitialize(); 47 return (ITeamContentProviderDescriptor[]) descriptors.values().toArray(new ITeamContentProviderDescriptor[descriptors.size()]); 48 } 49 50 53 public String [] 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 []) result.toArray(new String [result.size()]); 62 } 63 64 67 public ITeamContentProviderDescriptor getDescriptor(String 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 [] 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 { 104 listener.propertyChange(event); 105 } 106 public void handleException(Throwable exception) { 107 } 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 |