KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
24  * A team content provider descriptor associates a model provider
25  * with a navigator content extension
26  */

27 public class TeamContentProviderDescriptor implements ITeamContentProviderDescriptor {
28
29     private static final String JavaDoc TAG_TEAM_CONTENT_PROVIDER = "teamContentProvider"; //$NON-NLS-1$
30

31     private static final String JavaDoc ATT_MODEL_PROVIDER_ID = "modelProviderId"; //$NON-NLS-1$
32
private static final String JavaDoc ATT_CONTENT_EXTENSION_ID = "contentExtensionId"; //$NON-NLS-1$
33
private static final String JavaDoc ATT_ICON = "icon"; //$NON-NLS-1$
34
private static final String JavaDoc ATT_PREFERENCE_PAGE = "preferencePage"; //$NON-NLS-1$
35
private static final String JavaDoc ATT_SUPPORTS_FLAT_LAYOUT = "supportsFlatLayout"; //$NON-NLS-1$
36

37     private static final String JavaDoc PREF_TEAM_CONTENT_DESCRIPTORS = "teamContentDescriptors"; //$NON-NLS-1$
38
private static final String JavaDoc PREF_ENABLED = "enabled"; //$NON-NLS-1$
39

40     private String JavaDoc modelProviderId;
41     private String JavaDoc contentExtensionId;
42     private String JavaDoc 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     /**
55      * Initialize this descriptor based on the provided extension point.
56      */

57     protected void readExtension(IExtension extension) throws CoreException {
58         //read the extension
59
String JavaDoc id = extension.getUniqueIdentifier(); // id not required
60
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 JavaDoc 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 JavaDoc 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 JavaDoc[] { ATT_MODEL_PROVIDER_ID, TAG_TEAM_CONTENT_PROVIDER, id == null ? "" : id})); //$NON-NLS-1$
79
if (contentExtensionId == null)
80             fail(NLS.bind(TeamUIMessages.TeamContentProviderDescriptor_1, new String JavaDoc[] { ATT_CONTENT_EXTENSION_ID, TAG_TEAM_CONTENT_PROVIDER, id == null ? "" : id})); //$NON-NLS-1$
81
}
82     
83     protected void fail(String JavaDoc reason) throws CoreException {
84         throw new CoreException(new Status(IStatus.ERROR, TeamUIPlugin.ID, 0, reason, null));
85     }
86
87     /* (non-Javadoc)
88      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderDescriptor#getContentExtensionId()
89      */

90     public String JavaDoc getContentExtensionId() {
91         return contentExtensionId;
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderDescriptor#getModelProviderId()
96      */

97     public String JavaDoc getModelProviderId() {
98         return modelProviderId;
99     }
100
101     /* (non-Javadoc)
102      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderDescriptor#getImageDescriptor()
103      */

104     public ImageDescriptor getImageDescriptor() {
105         if (imageDescriptor != null)
106             return imageDescriptor;
107         String JavaDoc 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     /* (non-Javadoc)
115      * @see org.eclipse.team.internal.ui.registry.ITeamContentProviderDescriptor#createPreferencePage()
116      */

117     public IPreferencePage createPreferencePage() throws CoreException {
118         if (configElement.getAttribute(ATT_PREFERENCE_PAGE) == null)
119             return null;
120         Object JavaDoc obj = RegistryReader.createExtension(configElement, ATT_PREFERENCE_PAGE);
121         return (IPreferencePage) obj;
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor#isEnabled()
126      */

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     /*
145      * Return the preferences node for this repository
146      */

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 JavaDoc[] { getModelProviderId() }), e); //$NON-NLS-1$
163
return false;
164         }
165     }
166     
167     /**
168      * Return a unique name that identifies this location but
169      * does not contain any slashes (/). Also, do not use ':'.
170      * Although a valid path character, the initial core implementation
171      * didn't handle it well.
172      */

173     private String JavaDoc getPreferenceName() {
174         return getModelProviderId();
175     }
176
177     public void storePreferences() {
178         Preferences prefs = internalGetPreferences();
179         // Must store at least one preference in the node
180
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 JavaDoc[] { getModelProviderId() }), e); //$NON-NLS-1$
189
}
190     }
191
192     private void ensurePreferencesStored() {
193         if (!hasPreferences()) {
194             storePreferences();
195         }
196     }
197
198     public String JavaDoc getName() {
199         if (contentProviderName != null)
200             return contentProviderName;
201         
202         return null;
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.team.ui.mapping.ITeamContentProviderDescriptor#isFlatLayoutSupported()
207      */

208     public boolean isFlatLayoutSupported() {
209         return supportsFlatLayout;
210     }
211 }
212
Popular Tags