1 11 package org.eclipse.team.internal.ui.registry; 12 13 import org.eclipse.core.runtime.*; 14 import org.eclipse.jface.preference.IPreferencePage; 15 import org.eclipse.jface.resource.ImageDescriptor; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.team.internal.ui.TeamUIMessages; 18 import org.eclipse.team.internal.ui.TeamUIPlugin; 19 import org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor; 20 import org.osgi.service.prefs.BackingStoreException; 21 import org.osgi.service.prefs.Preferences; 22 23 27 public class TeamContentProviderDescriptor implements ITeamContentProviderDescriptor { 28 29 private static final String TAG_TEAM_CONTENT_PROVIDER = "teamContentProvider"; 31 private static final String ATT_MODEL_PROVIDER_ID = "modelProviderId"; private static final String ATT_CONTENT_EXTENSION_ID = "contentExtensionId"; private static final String ATT_ICON = "icon"; private static final String ATT_PREFERENCE_PAGE = "preferencePage"; private static final String ATT_SUPPORTS_FLAT_LAYOUT = "supportsFlatLayout"; 37 private static final String PREF_TEAM_CONTENT_DESCRIPTORS = "teamContentDescriptors"; private static final String PREF_ENABLED = "enabled"; 40 private String modelProviderId; 41 private String contentExtensionId; 42 private String contentProviderName; 43 44 private ImageDescriptor imageDescriptor; 45 46 private IConfigurationElement configElement; 47 48 private boolean supportsFlatLayout; 49 50 public TeamContentProviderDescriptor(IExtension extension) throws CoreException { 51 readExtension(extension); 52 } 53 54 57 protected void readExtension(IExtension extension) throws CoreException { 58 String id = extension.getUniqueIdentifier(); IConfigurationElement[] elements = extension.getConfigurationElements(); 61 int count = elements.length; 62 for (int i = 0; i < count; i++) { 63 IConfigurationElement element = elements[i]; 64 configElement = element; 65 String name = element.getName(); 66 if (name.equalsIgnoreCase(TAG_TEAM_CONTENT_PROVIDER)) { 67 modelProviderId = element.getAttribute(ATT_MODEL_PROVIDER_ID); 68 contentExtensionId = element.getAttribute(ATT_CONTENT_EXTENSION_ID); 69 String supportsFlatLayoutString = element.getAttribute(ATT_SUPPORTS_FLAT_LAYOUT); 70 if (supportsFlatLayoutString != null) { 71 supportsFlatLayout = Boolean.valueOf(supportsFlatLayoutString).booleanValue(); 72 } 73 contentProviderName = extension.getLabel(); 74 } 75 break; 76 } 77 if (modelProviderId == null) 78 fail(NLS.bind(TeamUIMessages.TeamContentProviderDescriptor_1, new String [] { ATT_MODEL_PROVIDER_ID, TAG_TEAM_CONTENT_PROVIDER, id == null ? "" : id})); if (contentExtensionId == null) 80 fail(NLS.bind(TeamUIMessages.TeamContentProviderDescriptor_1, new String [] { ATT_CONTENT_EXTENSION_ID, TAG_TEAM_CONTENT_PROVIDER, id == null ? "" : id})); } 82 83 protected void fail(String reason) throws CoreException { 84 throw new CoreException(new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, reason, null)); 85 } 86 87 90 public String getContentExtensionId() { 91 return contentExtensionId; 92 } 93 94 97 public String getModelProviderId() { 98 return modelProviderId; 99 } 100 101 104 public ImageDescriptor getImageDescriptor() { 105 if (imageDescriptor != null) 106 return imageDescriptor; 107 String iconName = configElement.getAttribute(ATT_ICON); 108 if (iconName == null) 109 return null; 110 imageDescriptor = TeamUIPlugin.getImageDescriptorFromExtension(configElement.getDeclaringExtension(), iconName); 111 return imageDescriptor; 112 } 113 114 117 public IPreferencePage createPreferencePage() throws CoreException { 118 if (configElement.getAttribute(ATT_PREFERENCE_PAGE) == null) 119 return null; 120 Object obj = RegistryReader.createExtension(configElement, ATT_PREFERENCE_PAGE); 121 return (IPreferencePage) obj; 122 } 123 124 127 public boolean isEnabled() { 128 if (!hasPreferences()) { 129 return true; 130 } 131 return getPreferences().getBoolean(PREF_ENABLED, true); 132 } 133 134 public void setEnabled(boolean enable) { 135 if (isEnabled() != enable) { 136 getPreferences().putBoolean(PREF_ENABLED, enable); 137 flushPreferences(); 138 } 139 } 140 141 public Preferences getParentPreferences() { 142 return TeamUIPlugin.getPlugin().getInstancePreferences().node(PREF_TEAM_CONTENT_DESCRIPTORS); 143 } 144 147 public Preferences getPreferences() { 148 if (!hasPreferences()) { 149 ensurePreferencesStored(); 150 } 151 return internalGetPreferences(); 152 } 153 154 private Preferences internalGetPreferences() { 155 return getParentPreferences().node(getPreferenceName()); 156 } 157 158 private boolean hasPreferences() { 159 try { 160 return getParentPreferences().nodeExists(getPreferenceName()); 161 } catch (BackingStoreException e) { 162 TeamUIPlugin.log(IStatus.ERROR, NLS.bind("Error accessing team content preference store for {0}", new String [] { getModelProviderId() }), e); return false; 164 } 165 } 166 167 173 private String getPreferenceName() { 174 return getModelProviderId(); 175 } 176 177 public void storePreferences() { 178 Preferences prefs = internalGetPreferences(); 179 prefs.putBoolean(PREF_ENABLED, true); 181 flushPreferences(); 182 } 183 184 private void flushPreferences() { 185 try { 186 internalGetPreferences().flush(); 187 } catch (BackingStoreException e) { 188 TeamUIPlugin.log(IStatus.ERROR, NLS.bind("Error flushing team content preference store for {0}", new String [] { getModelProviderId() }), e); } 190 } 191 192 private void ensurePreferencesStored() { 193 if (!hasPreferences()) { 194 storePreferences(); 195 } 196 } 197 198 public String getName() { 199 if (contentProviderName != null) 200 return contentProviderName; 201 202 return null; 203 } 204 205 208 public boolean isFlatLayoutSupported() { 209 return supportsFlatLayout; 210 } 211 } 212 | Popular Tags |