KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > expressions > AdaptExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.internal.expressions;
12
13 import org.w3c.dom.Element JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IAdapterManager;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.Platform;
20
21 import org.eclipse.core.expressions.EvaluationResult;
22 import org.eclipse.core.expressions.ExpressionInfo;
23 import org.eclipse.core.expressions.IEvaluationContext;
24
25 public class AdaptExpression extends CompositeExpression {
26
27     private static final String JavaDoc ATT_TYPE= "type"; //$NON-NLS-1$
28

29     /**
30      * The seed for the hash code for all adapt expressions.
31      */

32     private static final int HASH_INITIAL= AdaptExpression.class.getName().hashCode();
33     
34     private String JavaDoc fTypeName;
35     
36     public AdaptExpression(IConfigurationElement configElement) throws CoreException {
37         fTypeName= configElement.getAttribute(ATT_TYPE);
38         Expressions.checkAttribute(ATT_TYPE, fTypeName);
39     }
40
41     public AdaptExpression(Element JavaDoc element) throws CoreException {
42         fTypeName= element.getAttribute(ATT_TYPE);
43         Expressions.checkAttribute(ATT_TYPE, fTypeName.length() > 0 ? fTypeName : null);
44     }
45
46     public AdaptExpression(String JavaDoc typeName) {
47         Assert.isNotNull(typeName);
48         fTypeName= typeName;
49     }
50
51     public boolean equals(final Object JavaDoc object) {
52         if (!(object instanceof AdaptExpression))
53             return false;
54         
55         final AdaptExpression that= (AdaptExpression)object;
56         return this.fTypeName.equals(that.fTypeName)
57                 && equals(this.fExpressions, that.fExpressions);
58     }
59
60     protected int computeHashCode() {
61         return HASH_INITIAL * HASH_FACTOR + hashCode(fExpressions)
62             * HASH_FACTOR + fTypeName.hashCode();
63     }
64     
65     /* (non-Javadoc)
66      * @see Expression#evaluate(IVariablePool)
67      */

68     public EvaluationResult evaluate(IEvaluationContext context) throws CoreException {
69         if (fTypeName == null)
70             return EvaluationResult.FALSE;
71         Object JavaDoc var= context.getDefaultVariable();
72         Object JavaDoc adapted= null;
73         IAdapterManager manager= Platform.getAdapterManager();
74         if (Expressions.isInstanceOf(var, fTypeName)) {
75             adapted= var;
76         } else {
77             if (!manager.hasAdapter(var, fTypeName))
78                 return EvaluationResult.FALSE;
79         
80             adapted= manager.getAdapter(var, fTypeName);
81         }
82         // the adapted result is null but hasAdapter returned true check
83
// if the adapter is loaded.
84
if (adapted == null) {
85             if (manager.queryAdapter(var, fTypeName) == IAdapterManager.NOT_LOADED) {
86                 return EvaluationResult.NOT_LOADED;
87             } else {
88                 return EvaluationResult.FALSE;
89             }
90         }
91         return evaluateAnd(new DefaultVariable(context, adapted));
92     }
93     
94     public void collectExpressionInfo(ExpressionInfo info) {
95         // Although the default variable is passed to the children of this
96
// expression as an instance of the adapted type it is OK to only
97
// mark a default variable access.
98
info.markDefaultVariableAccessed();
99         super.collectExpressionInfo(info);
100     }
101 }
102
Popular Tags