01: /*******************************************************************************
02: * Copyright (c) 2005, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.navigator;
11:
12: import java.util.ArrayList;
13: import java.util.Iterator;
14: import java.util.List;
15:
16: import org.eclipse.core.expressions.ElementHandler;
17: import org.eclipse.core.expressions.EvaluationResult;
18: import org.eclipse.core.expressions.Expression;
19: import org.eclipse.core.expressions.ExpressionConverter;
20: import org.eclipse.core.expressions.IEvaluationContext;
21: import org.eclipse.core.runtime.Assert;
22: import org.eclipse.core.runtime.CoreException;
23: import org.eclipse.core.runtime.IConfigurationElement;
24: import org.eclipse.core.runtime.IStatus;
25:
26: /**
27: * Create an AND-type core expression from an IConfigurationElement of arbitrary
28: * name.
29: *
30: */
31: public class CustomAndExpression extends Expression {
32:
33: protected List fExpressions;
34:
35: /**
36: * Create an AND-type core expression from an IConfigurationElement of
37: * arbitrary name. The children elements are combined using boolean AND
38: * semantics to evaluate the expression.
39: *
40: * @param element
41: * An IConfigurationElement of arbitrary name.
42: */
43: public CustomAndExpression(IConfigurationElement element) {
44: Assert.isNotNull(element);
45:
46: IConfigurationElement[] children = element.getChildren();
47:
48: if (children.length > 0) {
49: fExpressions = new ArrayList();
50: }
51: for (int i = 0; i < children.length; i++) {
52: try {
53: fExpressions.add(ElementHandler.getDefault().create(
54: ExpressionConverter.getDefault(), children[i]));
55: } catch (CoreException ce) {
56: NavigatorPlugin.log(IStatus.ERROR, 0, ce.getMessage(),
57: 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 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: }
|