KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > participants > ParticipantExtensionPoint


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.core.refactoring.participants;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtensionRegistry;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24
25 import org.eclipse.core.expressions.EvaluationContext;
26
27 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
28
29 import org.eclipse.ltk.internal.core.refactoring.Messages;
30 import org.eclipse.ltk.internal.core.refactoring.ParticipantDescriptor;
31 import org.eclipse.ltk.internal.core.refactoring.RefactoringCoreMessages;
32 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
33
34 /* package */ class ParticipantExtensionPoint {
35     
36     private String JavaDoc fName;
37         
38     private String JavaDoc fParticipantID;
39     private List JavaDoc fParticipants;
40     private Class JavaDoc fParticipantClass;
41     
42     //---- debugging ----------------------------------------
43
/*
44     private static final boolean EXIST_TRACING;
45     static {
46         String value= Platform.getDebugOption("org.eclipse.jdt.ui/processor/existTracing"); //$NON-NLS-1$
47         EXIST_TRACING= value != null && value.equalsIgnoreCase("true"); //$NON-NLS-1$
48     }
49     
50     private void printTime(long start) {
51         System.out.println("[" + fName + //$NON-NLS-1$
52             " extension manager] - existing test: " + //$NON-NLS-1$
53             (System.currentTimeMillis() - start) + " ms"); //$NON-NLS-1$
54     }
55     */

56     
57     public ParticipantExtensionPoint(String JavaDoc name, String JavaDoc participantId, Class JavaDoc clazz) {
58         Assert.isNotNull(name);
59         Assert.isNotNull(participantId);
60         Assert.isNotNull(clazz);
61         fName= name;
62         fParticipantID= participantId;
63         fParticipantClass= clazz;
64     }
65     
66     public String JavaDoc getName() {
67         return fName;
68     }
69
70     public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, Object JavaDoc element, RefactoringArguments arguments, IParticipantDescriptorFilter filter, String JavaDoc[] affectedNatures, SharableParticipants shared) {
71         if (fParticipants == null)
72             init();
73         
74         EvaluationContext evalContext= createEvaluationContext(processor, element, affectedNatures);
75         List JavaDoc result= new ArrayList JavaDoc();
76         for (Iterator JavaDoc iter= fParticipants.iterator(); iter.hasNext();) {
77             ParticipantDescriptor descriptor= (ParticipantDescriptor)iter.next();
78             if (!descriptor.isEnabled()) {
79                 iter.remove();
80             } else {
81                 try {
82                     RefactoringStatus filterStatus= new RefactoringStatus();
83                     if (descriptor.matches(evalContext, filter, filterStatus)) {
84                         RefactoringParticipant participant= shared.get(descriptor);
85                         if (participant != null) {
86                             ((ISharableParticipant)participant).addElement(element, arguments);
87                         } else {
88                             participant= descriptor.createParticipant();
89                             if (fParticipantClass.isInstance(participant)) {
90                                 if (participant.initialize(processor, element, arguments)) {
91                                     participant.setDescriptor(descriptor);
92                                     result.add(participant);
93                                     if (participant instanceof ISharableParticipant)
94                                         shared.put(descriptor, participant);
95                                 }
96                             } else {
97                                 status.addError(Messages.format(
98                                     RefactoringCoreMessages.ParticipantExtensionPoint_participant_removed,
99                                     descriptor.getName()));
100                                 RefactoringCorePlugin.logErrorMessage(
101                                     Messages.format(
102                                         RefactoringCoreMessages.ParticipantExtensionPoint_wrong_type,
103                                         new String JavaDoc[] {descriptor.getName(), fParticipantClass.getName()}));
104                                 iter.remove();
105                             }
106                         }
107                     } else {
108                         status.merge(filterStatus);
109                     }
110                 } catch (CoreException e) {
111                     logMalfunctioningParticipant(status, descriptor, e);
112                     iter.remove();
113                 } catch (RuntimeException JavaDoc e) {
114                     logMalfunctioningParticipant(status, descriptor, e);
115                     iter.remove();
116                 }
117             }
118         }
119         
120         return (RefactoringParticipant[])result.toArray(new RefactoringParticipant[result.size()]);
121     }
122
123     private void logMalfunctioningParticipant(RefactoringStatus status, ParticipantDescriptor descriptor, Throwable JavaDoc e) {
124         status.addError(Messages.format(
125             RefactoringCoreMessages.ParticipantExtensionPoint_participant_removed,
126             descriptor.getName()));
127         RefactoringCorePlugin.logRemovedParticipant(descriptor, e);
128     }
129     
130     private void init() {
131         IExtensionRegistry registry= Platform.getExtensionRegistry();
132         IConfigurationElement[] ces= registry.getConfigurationElementsFor(
133             RefactoringCorePlugin.getPluginId(),
134             fParticipantID);
135         fParticipants= new ArrayList JavaDoc(ces.length);
136         for (int i= 0; i < ces.length; i++) {
137             ParticipantDescriptor descriptor= new ParticipantDescriptor(ces[i]);
138             IStatus status= descriptor.checkSyntax();
139             switch (status.getSeverity()) {
140                 case IStatus.ERROR:
141                     RefactoringCorePlugin.log(status);
142                     break;
143                 case IStatus.WARNING:
144                 case IStatus.INFO:
145                     RefactoringCorePlugin.log(status);
146                     // fall through
147
default:
148                     fParticipants.add(descriptor);
149             }
150         }
151     }
152     
153     //---- Helper methods ------------------------------------------------------------------
154

155     private static EvaluationContext createEvaluationContext(RefactoringProcessor processor, Object JavaDoc element, String JavaDoc[] affectedNatures) {
156         EvaluationContext result= new EvaluationContext(null, element);
157         result.addVariable("element", element); //$NON-NLS-1$
158
result.addVariable("affectedNatures", Arrays.asList(affectedNatures)); //$NON-NLS-1$
159
result.addVariable("processorIdentifier", processor.getIdentifier()); //$NON-NLS-1$
160
return result;
161     }
162 }
163
Popular Tags