KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > history > RefactoringContributionManager


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ltk.internal.core.refactoring.history;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IRegistryChangeEvent;
20 import org.eclipse.core.runtime.IRegistryChangeListener;
21 import org.eclipse.core.runtime.Platform;
22
23 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
24 import org.eclipse.ltk.core.refactoring.RefactoringCore;
25 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
26
27 import org.eclipse.ltk.internal.core.refactoring.Messages;
28 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
29 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
30
31 /**
32  * Contribution manager for refactorings.
33  *
34  * @since 3.2
35  */

36 public final class RefactoringContributionManager implements IRegistryChangeListener {
37
38     /** The class attribute */
39     private static final String JavaDoc ATTRIBUTE_CLASS= "class"; //$NON-NLS-1$
40

41     /** The id attribute */
42     private static final String JavaDoc ATTRIBUTE_ID= "id"; //$NON-NLS-1$
43

44     /** The singleton instance */
45     private static RefactoringContributionManager fInstance= null;
46
47     /** The refactoring contributions extension point */
48     private static final String JavaDoc REFACTORING_CONTRIBUTIONS_EXTENSION_POINT= "refactoringContributions"; //$NON-NLS-1$
49

50     /**
51      * Returns the singleton instance of the refactoring contribution manager.
52      *
53      * @return the singleton instance
54      */

55     public static RefactoringContributionManager getInstance() {
56         if (fInstance == null)
57             fInstance= new RefactoringContributionManager();
58         return fInstance;
59     }
60
61     /**
62      * The refactoring contribution cache (element type: <String,
63      * <code>RefactoringContribution&gt;</code>)
64      */

65     private Map JavaDoc fContributionCache= null;
66
67     /**
68      * The refactoring contribution cache (element type:
69      * &lt;RefactoringContribution, <code>String&gt;</code>)
70      *
71      * @since 3.3
72      */

73     private Map JavaDoc fIdCache= null;
74
75     /**
76      * Creates a new refactoring contribution manager.
77      */

78     private RefactoringContributionManager() {
79         // Not instantiatable
80
}
81
82     /**
83      * Connects this manager to the platform's extension registry.
84      */

85     public void connect() {
86         Platform.getExtensionRegistry().addRegistryChangeListener(this, RefactoringCore.ID_PLUGIN);
87     }
88
89     /**
90      * Creates a new refactoring descriptor for the specified input data.
91      *
92      * @param id
93      * the unique id of the refactoring
94      * @param project
95      * the project name, or <code>null</code>
96      * @param description
97      * a description
98      * @param comment
99      * the comment, or <code>null</code>
100      * @param arguments
101      * the argument map
102      * @param flags
103      * the flags
104      * @return the refactoring descriptor
105      */

106     public RefactoringDescriptor createDescriptor(final String JavaDoc id, final String JavaDoc project, final String JavaDoc description, final String JavaDoc comment, final Map JavaDoc arguments, final int flags) {
107         Assert.isNotNull(id);
108         Assert.isNotNull(description);
109         Assert.isNotNull(arguments);
110         Assert.isLegal(flags >= RefactoringDescriptor.NONE);
111         final RefactoringContribution contribution= getRefactoringContribution(id);
112         if (contribution != null)
113             return contribution.createDescriptor(id, project, description, comment, arguments, flags);
114         return new DefaultRefactoringDescriptor(id, project, description, comment, arguments, flags);
115     }
116
117     /**
118      * Disconnects this manager from the platform's extension registry.
119      */

120     public void disconnect() {
121         Platform.getExtensionRegistry().removeRegistryChangeListener(this);
122     }
123
124     /**
125      * Returns the refactoring contribution for the refactoring with the
126      * specified id.
127      *
128      * @param id
129      * the unique id of the refactoring
130      * @return the refactoring contribution, or <code>null</code> if no
131      * refactoring contribution has been registered with the specified
132      * id
133      */

134     public RefactoringContribution getRefactoringContribution(final String JavaDoc id) {
135         Assert.isNotNull(id);
136         Assert.isTrue(!"".equals(id)); //$NON-NLS-1$
137
populateCache();
138         return (RefactoringContribution) fContributionCache.get(id);
139     }
140
141     /**
142      * Returns the refactoring id for the specified refactoring contribution.
143      *
144      * @param contribution
145      * the refactoring contribution
146      * @return the corresonding refactoring id
147      *
148      * @since 3.3
149      */

150     public String JavaDoc getRefactoringId(final RefactoringContribution contribution) {
151         Assert.isNotNull(contribution);
152         populateCache();
153         return (String JavaDoc) fIdCache.get(contribution);
154     }
155
156     /**
157      * Populates the refactoring contribution cache if necessary.
158      *
159      * @since 3.3
160      */

161     private void populateCache() {
162         if (fContributionCache == null || fIdCache == null) {
163             fContributionCache= new HashMap JavaDoc(32);
164             fIdCache= new HashMap JavaDoc(32);
165             final IConfigurationElement[] elements= Platform.getExtensionRegistry().getConfigurationElementsFor(RefactoringCore.ID_PLUGIN, REFACTORING_CONTRIBUTIONS_EXTENSION_POINT);
166             for (int index= 0; index < elements.length; index++) {
167                 final IConfigurationElement element= elements[index];
168                 final String JavaDoc attributeId= element.getAttribute(ATTRIBUTE_ID);
169                 final String JavaDoc point= RefactoringCore.ID_PLUGIN + "." + REFACTORING_CONTRIBUTIONS_EXTENSION_POINT; //$NON-NLS-1$
170
if (attributeId != null && !"".equals(attributeId)) { //$NON-NLS-1$
171
final String JavaDoc className= element.getAttribute(ATTRIBUTE_CLASS);
172                     if (className != null && !"".equals(className)) { //$NON-NLS-1$
173
try {
174                             final Object JavaDoc implementation= element.createExecutableExtension(ATTRIBUTE_CLASS);
175                             if (implementation instanceof RefactoringContribution) {
176                                 if (fContributionCache.get(attributeId) != null)
177                                     RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_duplicate_warning, new String JavaDoc[] { attributeId, point}));
178                                 fContributionCache.put(attributeId, implementation);
179                                 fIdCache.put(implementation, attributeId);
180                             } else
181                                 RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_creation_error, new String JavaDoc[] { point, attributeId}));
182                         } catch (CoreException exception) {
183                             RefactoringCorePlugin.log(exception);
184                         }
185                     } else
186                         RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_missing_class_attribute, new String JavaDoc[] { point, attributeId, ATTRIBUTE_CLASS}));
187                 } else
188                     RefactoringCorePlugin.logErrorMessage(Messages.format(RefactoringCoreMessages.RefactoringCorePlugin_missing_attribute, new String JavaDoc[] { point, ATTRIBUTE_ID}));
189             }
190         }
191     }
192
193     /**
194      * {@inheritDoc}
195      */

196     public void registryChanged(final IRegistryChangeEvent event) {
197         fContributionCache= null;
198         fIdCache= null;
199     }
200 }
201
Popular Tags