KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > AbstractDescriptor


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.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IConfigurationElement;
16
17 import org.eclipse.core.expressions.EvaluationContext;
18 import org.eclipse.core.expressions.EvaluationResult;
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.core.expressions.ExpressionConverter;
21 import org.eclipse.core.expressions.ExpressionTagNames;
22
23 public abstract class AbstractDescriptor {
24
25     protected IConfigurationElement fConfigurationElement;
26     protected Expression fExpression;
27     
28     protected static final String JavaDoc ID= "id"; //$NON-NLS-1$
29
protected static final String JavaDoc OBJECT_STATE= "objectState"; //$NON-NLS-1$
30
protected static final String JavaDoc CLASS= "class"; //$NON-NLS-1$
31

32     protected AbstractDescriptor(IConfigurationElement element) {
33         fConfigurationElement= element;
34     }
35     
36     public String JavaDoc getId() {
37         return fConfigurationElement.getAttribute(ID);
38     }
39     
40     public boolean matches(Object JavaDoc element, String JavaDoc variableName) throws CoreException {
41         Assert.isNotNull(element);
42         Assert.isNotNull(variableName);
43         Expression exp= getExpression();
44         EvaluationContext evaluationContext= new EvaluationContext(null, element);
45         evaluationContext.addVariable(variableName, element);
46         if (exp.evaluate(evaluationContext) == EvaluationResult.FALSE)
47             return false;
48         return true;
49     }
50     
51     public Expression getExpression() throws CoreException {
52         if (fExpression == null)
53             fExpression= createExpression(fConfigurationElement);
54         return fExpression;
55     }
56     
57     public void clear() {
58         fExpression= null;
59     }
60         
61     protected Expression createExpression(IConfigurationElement element) throws CoreException {
62         IConfigurationElement[] children= element.getChildren(ExpressionTagNames.ENABLEMENT);
63         if (children.length == 0)
64             return Expression.FALSE;
65         // TODO we should add some sort of syntax check and throw an core exception in this case
66
Assert.isTrue(children.length == 1);
67         return ExpressionConverter.getDefault().perform(children[0]);
68     }
69 }
70
Popular Tags