KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > CustomAndExpression


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.ui.internal.navigator;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.expressions.ElementHandler;
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.IEvaluationContext;
22 import org.eclipse.core.runtime.Assert;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IStatus;
26
27 /**
28  * Create an AND-type core expression from an IConfigurationElement of arbitrary
29  * name.
30  *
31  */

32 public class CustomAndExpression extends Expression {
33
34     protected List JavaDoc fExpressions;
35
36     /**
37      * Create an AND-type core expression from an IConfigurationElement of
38      * arbitrary name. The children elements are combined using boolean AND
39      * semantics to evaluate the expression.
40      *
41      * @param element
42      * An IConfigurationElement of arbitrary name.
43      */

44     public CustomAndExpression(IConfigurationElement element) {
45         Assert.isNotNull(element);
46
47         IConfigurationElement[] children = element.getChildren();
48
49         if (children.length > 0) {
50             fExpressions = new ArrayList JavaDoc();
51         }
52         for (int i = 0; i < children.length; i++) {
53             try {
54                 fExpressions.add(ElementHandler.getDefault().create(
55                         ExpressionConverter.getDefault(), children[i]));
56             } catch (CoreException ce) {
57                 NavigatorPlugin.log(IStatus.ERROR, 0, ce.getMessage(), ce);
58             }
59         }
60     }
61
62     public EvaluationResult evaluate(IEvaluationContext scope)
63             throws CoreException {
64         if (fExpressions == null) {
65             return EvaluationResult.TRUE;
66         }
67         EvaluationResult result = EvaluationResult.TRUE;
68         for (Iterator JavaDoc iter = fExpressions.iterator(); iter.hasNext();) {
69             Expression expression = (Expression) iter.next();
70             result = result.and(expression.evaluate(scope));
71             // keep iterating even if we have a not loaded found. It can be
72
// that we find a false which will result in a better result.
73
if (result == EvaluationResult.FALSE) {
74                 return result;
75             }
76         }
77         return result;
78     }
79
80 }
81
Popular Tags