intial source from ttp://www.sf.net/projects/wdte
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.css.core / src / net / sourceforge / phpeclipse / css / core / internal / profiles / ProfileManager.java
1 /*
2  * Copyright (c) 2003-2004 Christopher Lenz and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     Christopher Lenz - initial API and implementation
10  * 
11  * $Id: ProfileManager.java,v 1.1 2004-09-02 18:07:11 jsurfer Exp $
12  */
13
14 package net.sourceforge.phpeclipse.css.core.internal.profiles;
15
16 import java.lang.reflect.Constructor;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.WeakHashMap;
22
23 import net.sourceforge.phpeclipse.css.core.CssCore;
24 import net.sourceforge.phpeclipse.css.core.internal.CssCorePreferences;
25 import net.sourceforge.phpeclipse.css.core.profiles.IProfile;
26 import net.sourceforge.phpeclipse.css.core.profiles.IProfileDescriptor;
27 import net.sourceforge.phpeclipse.css.core.profiles.IProfileManager;
28
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IConfigurationElement;
33 import org.eclipse.core.runtime.IExtension;
34 import org.eclipse.core.runtime.IExtensionPoint;
35 import org.eclipse.core.runtime.IExtensionRegistry;
36 import org.eclipse.core.runtime.Platform;
37 import org.eclipse.core.runtime.Preferences;
38 import org.eclipse.core.runtime.QualifiedName;
39
40 /**
41  * Implementation of the <code>IProfileManager</code> interface.
42  */
43 public class ProfileManager implements IProfileManager {
44
45         // Constants ---------------------------------------------------------------
46
47         /**
48          * The qualified name used to persist the selected profile on projects and
49          * resources.
50          */
51         public static final QualifiedName PROFILE_PROPERTY =
52                 new QualifiedName(CssCore.getPluginId(), "profile"); //$NON-NLS-1$
53
54         // Instance Variables ------------------------------------------------------
55
56         /**
57          * The plugin preferences.
58          */
59         private Preferences preferences;
60
61         /**
62          * The profile descriptors, keyed by extension ID.
63          */
64         private Map profileDescriptors;
65
66         /**
67          * Cache of the already loaded profiles.
68          */
69         private Map profiles = new WeakHashMap();
70
71         // Constructors ------------------------------------------------------------
72
73         /**
74          * Constructor.
75          * 
76          * @param preferences the plugin preferences
77          */
78         public ProfileManager(Preferences preferences) {
79                 this.preferences = preferences;
80         }
81
82         // IProfileManager Implementation ------------------------------------------
83
84         /**
85          * @see IProfileManager#getProfileDescriptors()
86          */
87         public IProfileDescriptor[] getProfileDescriptors() {
88                 if (profileDescriptors == null) {
89                         loadProfileExtensions();
90                 }
91                 List retVal = new ArrayList();
92                 retVal.addAll(profileDescriptors.values());
93                 return (IProfileDescriptor[]) retVal.toArray(
94                         new IProfileDescriptor[retVal.size()]);
95         }
96
97         /**
98          * @see IProfileManager#getProfile(IResource)
99          */
100         public IProfile getProfile(IResource resource) {
101                 String profileId = null;
102                 if (resource != null) {
103                         try {
104                                 profileId = resource.getPersistentProperty(PROFILE_PROPERTY);
105                         } catch (CoreException e) {
106                                 CssCore.log(
107                                         "Failed to get profile for resource", e); //$NON-NLS-1$
108                         }
109                         if (profileId == null) {
110                                 IProject project = resource.getProject();
111                                 try {
112                                         profileId = project.getPersistentProperty(PROFILE_PROPERTY);
113                                 } catch (CoreException e) {
114                                         CssCore.log(
115                                                 "Failed to get profile for project", e); //$NON-NLS-1$
116                                 }
117                         }
118                 }
119                 if (profileId == null) {
120                         profileId = preferences.getString(CssCorePreferences.PROFILE);
121                 }
122                 IProfile profile = getProfile(profileId);
123                 return profile;
124         }
125
126         /**
127          * @see IProfileManager#setProfile(IResource, String)
128          */
129         public void setProfile(IResource resource, String profileId) {
130                 if (resource != null) {
131                         try {
132                                 resource.setPersistentProperty(
133                                         ProfileManager.PROFILE_PROPERTY, profileId);
134                         } catch (CoreException e) {
135                                 CssCore.log(
136                                         "Cannot store profile as resource property", //$NON-NLS-1$
137                                         e);
138                         }
139                 } else if (profileId != null) {
140                         preferences.setValue(CssCorePreferences.PROFILE, profileId);
141                 } else {
142                         preferences.setToDefault(CssCorePreferences.PROFILE);
143                 }
144         }
145
146         // Private Methods ---------------------------------------------------------
147
148         /**
149          * Returns the profile associated with the specified extension ID. The
150          * profile is instantiated if not already in the cache.
151          * 
152          * @param profileId the ID of the extension implementing the profile
153          */
154         private IProfile getProfile(String profileId) {
155                 IProfile profile = (IProfile) profiles.get(profileId);
156                 if (profile == null) {
157                         IProfileDescriptor descriptor = getProfileDescriptor(profileId);
158                         if (descriptor == null) {
159                                 CssCore.log("Profile '" + profileId + //$NON-NLS-1$
160                                         "' not found."); //$NON-NLS-1$
161                                 descriptor = getDefaultProfileDescriptor();
162                         }
163                         if (descriptor != null) {
164                                 profile = instantiateProfile(descriptor);
165                                 profiles.put(profileId, profile);
166                         }
167                 }
168                 return profile;
169         }
170
171         /**
172          * Returns the descriptor of the default profile.
173          * 
174          * @return the descriptor of the default profile
175          */
176         private IProfileDescriptor getDefaultProfileDescriptor() {
177                 String profileId = preferences.getDefaultString(
178                                 CssCorePreferences.PROFILE);
179                 return getProfileDescriptor(profileId);
180         }
181
182         /**
183          * Returns the descriptor of the specified profile.
184          * 
185          * @param profileId the ID of the profile
186          * @return the descriptor of the profile with the given ID
187          */
188         private IProfileDescriptor getProfileDescriptor(String profileId) {
189                 if (profileDescriptors == null) {
190                         loadProfileExtensions();
191                 }
192                 return (IProfileDescriptor) profileDescriptors.get(profileId);
193         }
194
195         /**
196          * Instantiates and the returns the profile.
197          * 
198          * @param descriptor the profile descriptor
199          * @return the instantiated profile, or <code>null</code> if there was a 
200          *         problem instantiating the profile
201          */
202         private IProfile instantiateProfile(IProfileDescriptor descriptor) {
203                 try {
204                         Class clazz = Class.forName(descriptor.getClassName());
205                         Constructor ctor = clazz.getConstructor(
206                                 new Class[] { IProfileDescriptor.class });
207                         return (IProfile) ctor.newInstance(new Object[] { descriptor });
208                 } catch (Exception e) {
209                         CssCore.log("Failed to get CSS profile", e); //$NON-NLS-1$
210                 }
211                 return null;
212         }
213
214         /**
215          * Initializes the list of available profiles by querying the platform for
216          * extensions of the <em>profiles</em> extension point and storing an
217          * <code>IProfileDescriptor</code> for each extension found.
218          */
219         private void loadProfileExtensions() {
220                 profileDescriptors = new HashMap();
221                 IExtensionRegistry registry = Platform.getExtensionRegistry();
222                 IExtensionPoint extensionPoint = registry.getExtensionPoint(
223                         IProfileDescriptor.EXTENSION_POINT_ID);
224                 IExtension[] extensions = extensionPoint.getExtensions();
225                 for (int i = 0; i < extensions.length; i++) {
226                         IExtension extension = extensions[i];
227                         IConfigurationElement[] elements =
228                                 extension.getConfigurationElements();
229                         for (int j = 0; j < elements.length; j++) {
230                                 IConfigurationElement element = elements[j];
231                                 IProfileDescriptor descriptor = new ProfileDescriptor(element);
232                                 profileDescriptors.put(descriptor.getId(), descriptor);
233                         }
234                 }
235         }
236 }