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
9 * Christopher Lenz - initial API and implementation
11 * $Id: ProfileManager.java,v 1.1 2004-09-02 18:07:11 jsurfer Exp $
14 package net.sourceforge.phpeclipse.css.core.internal.profiles;
16 import java.lang.reflect.Constructor;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
21 import java.util.WeakHashMap;
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;
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;
41 * Implementation of the <code>IProfileManager</code> interface.
43 public class ProfileManager implements IProfileManager {
45 // Constants ---------------------------------------------------------------
48 * The qualified name used to persist the selected profile on projects and
51 public static final QualifiedName PROFILE_PROPERTY =
52 new QualifiedName(CssCore.getPluginId(), "profile"); //$NON-NLS-1$
54 // Instance Variables ------------------------------------------------------
57 * The plugin preferences.
59 private Preferences preferences;
62 * The profile descriptors, keyed by extension ID.
64 private Map profileDescriptors;
67 * Cache of the already loaded profiles.
69 private Map profiles = new WeakHashMap();
71 // Constructors ------------------------------------------------------------
76 * @param preferences the plugin preferences
78 public ProfileManager(Preferences preferences) {
79 this.preferences = preferences;
82 // IProfileManager Implementation ------------------------------------------
85 * @see IProfileManager#getProfileDescriptors()
87 public IProfileDescriptor[] getProfileDescriptors() {
88 if (profileDescriptors == null) {
89 loadProfileExtensions();
91 List retVal = new ArrayList();
92 retVal.addAll(profileDescriptors.values());
93 return (IProfileDescriptor[]) retVal.toArray(
94 new IProfileDescriptor[retVal.size()]);
98 * @see IProfileManager#getProfile(IResource)
100 public IProfile getProfile(IResource resource) {
101 String profileId = null;
102 if (resource != null) {
104 profileId = resource.getPersistentProperty(PROFILE_PROPERTY);
105 } catch (CoreException e) {
107 "Failed to get profile for resource", e); //$NON-NLS-1$
109 if (profileId == null) {
110 IProject project = resource.getProject();
112 profileId = project.getPersistentProperty(PROFILE_PROPERTY);
113 } catch (CoreException e) {
115 "Failed to get profile for project", e); //$NON-NLS-1$
119 if (profileId == null) {
120 profileId = preferences.getString(CssCorePreferences.PROFILE);
122 IProfile profile = getProfile(profileId);
127 * @see IProfileManager#setProfile(IResource, String)
129 public void setProfile(IResource resource, String profileId) {
130 if (resource != null) {
132 resource.setPersistentProperty(
133 ProfileManager.PROFILE_PROPERTY, profileId);
134 } catch (CoreException e) {
136 "Cannot store profile as resource property", //$NON-NLS-1$
139 } else if (profileId != null) {
140 preferences.setValue(CssCorePreferences.PROFILE, profileId);
142 preferences.setToDefault(CssCorePreferences.PROFILE);
146 // Private Methods ---------------------------------------------------------
149 * Returns the profile associated with the specified extension ID. The
150 * profile is instantiated if not already in the cache.
152 * @param profileId the ID of the extension implementing the profile
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();
163 if (descriptor != null) {
164 profile = instantiateProfile(descriptor);
165 profiles.put(profileId, profile);
172 * Returns the descriptor of the default profile.
174 * @return the descriptor of the default profile
176 private IProfileDescriptor getDefaultProfileDescriptor() {
177 String profileId = preferences.getDefaultString(
178 CssCorePreferences.PROFILE);
179 return getProfileDescriptor(profileId);
183 * Returns the descriptor of the specified profile.
185 * @param profileId the ID of the profile
186 * @return the descriptor of the profile with the given ID
188 private IProfileDescriptor getProfileDescriptor(String profileId) {
189 if (profileDescriptors == null) {
190 loadProfileExtensions();
192 return (IProfileDescriptor) profileDescriptors.get(profileId);
196 * Instantiates and the returns the profile.
198 * @param descriptor the profile descriptor
199 * @return the instantiated profile, or <code>null</code> if there was a
200 * problem instantiating the profile
202 private IProfile instantiateProfile(IProfileDescriptor descriptor) {
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$
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.
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);