KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > ParticipantDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18
19 import org.eclipse.core.expressions.EvaluationResult;
20 import org.eclipse.core.expressions.Expression;
21 import org.eclipse.core.expressions.ExpressionConverter;
22 import org.eclipse.core.expressions.ExpressionTagNames;
23 import org.eclipse.core.expressions.IEvaluationContext;
24
25 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
26
27 import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter;
28 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
29
30 public class ParticipantDescriptor {
31     
32     private IConfigurationElement fConfigurationElement;
33     private boolean fEnabled;
34
35     private static final String JavaDoc ID= "id"; //$NON-NLS-1$
36
private static final String JavaDoc NAME= "name"; //$NON-NLS-1$
37
private static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
38
private static final String JavaDoc PROCESS_ON_CANCEL= "processOnCancel"; //$NON-NLS-1$
39

40     public ParticipantDescriptor(IConfigurationElement element) {
41         fConfigurationElement= element;
42         fEnabled= true;
43     }
44     
45     public String JavaDoc getId() {
46         return fConfigurationElement.getAttribute(ID);
47     }
48     
49     public String JavaDoc getName() {
50         return fConfigurationElement.getAttribute(NAME);
51     }
52     
53     public IStatus checkSyntax() {
54         if (fConfigurationElement.getAttribute(ID) == null) {
55             return new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IStatus.ERROR,
56                 RefactoringCoreMessages.ParticipantDescriptor_error_id_missing, null);
57         }
58         if (fConfigurationElement.getAttribute(NAME) == null) {
59             return new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IStatus.ERROR,
60                 Messages.format( RefactoringCoreMessages.ParticipantDescriptor_error_name_missing, getId()),
61                 null);
62         }
63         if (fConfigurationElement.getAttribute(CLASS) == null) {
64             return new Status(IStatus.ERROR, RefactoringCorePlugin.getPluginId(), IStatus.ERROR,
65                 Messages.format( RefactoringCoreMessages.ParticipantDescriptor_error_class_missing, getId()),
66                 null);
67         }
68         return new Status(IStatus.OK, RefactoringCorePlugin.getPluginId(), IStatus.OK,
69             RefactoringCoreMessages.ParticipantDescriptor_correct, null);
70     }
71     
72     public boolean matches(IEvaluationContext context, IParticipantDescriptorFilter filter, RefactoringStatus status) throws CoreException {
73         IConfigurationElement[] elements= fConfigurationElement.getChildren(ExpressionTagNames.ENABLEMENT);
74         if (elements.length == 0)
75             return false;
76         Assert.isTrue(elements.length == 1);
77         Expression exp= ExpressionConverter.getDefault().perform(elements[0]);
78         if (!convert(exp.evaluate(context)))
79             return false;
80         if (filter != null && !filter.select(fConfigurationElement, status))
81             return false;
82         
83         return true;
84     }
85
86     public RefactoringParticipant createParticipant() throws CoreException {
87         return (RefactoringParticipant)fConfigurationElement.createExecutableExtension(CLASS);
88     }
89     
90     public boolean isEnabled() {
91         return fEnabled;
92     }
93     
94     public void disable() {
95         fEnabled= false;
96     }
97     
98     public boolean processOnCancel() {
99         String JavaDoc attr= fConfigurationElement.getAttribute(PROCESS_ON_CANCEL);
100         if (attr == null)
101             return false;
102         return Boolean.valueOf(attr).booleanValue();
103     }
104     
105     private boolean convert(EvaluationResult eval) {
106         if (eval == EvaluationResult.FALSE)
107             return false;
108         return true;
109     }
110     
111     public String JavaDoc toString() {
112         return "name= " + getName() + (isEnabled() ? " (enabled)" : " (disabled)") + //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
113
"\nid= " + getId() + //$NON-NLS-1$
114
"\nclass= " + fConfigurationElement.getAttribute(CLASS); //$NON-NLS-1$
115
}
116 }
Popular Tags