Project

General

Profile

Download (2.71 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2010 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;
14
import java.util.Iterator;
15
import java.util.List;
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.IConfigurationElement;
24
import org.eclipse.core.runtime.SafeRunner;
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
		final IConfigurationElement[] children = element.getChildren();
47
		if (children.length == 0)
48
			return;
49
		SafeRunner.run(new NavigatorSafeRunnable() {
50
			public void run() throws Exception {
51
				fExpressions = new ArrayList();
52
				for (int i = 0; i < children.length; i++) {
53
					fExpressions.add(ElementHandler.getDefault().create(
54
							ExpressionConverter.getDefault(), children[i]));
55
				}
56
			}
57
		});
58

    
59
	}
60

    
61
	public EvaluationResult evaluate(IEvaluationContext scope) {
62
		if (fExpressions == null) {
63
			return EvaluationResult.TRUE;
64
		}
65
		NavigatorPlugin.Evaluator evaluator = new NavigatorPlugin.Evaluator();
66
		EvaluationResult result = EvaluationResult.TRUE;
67
		for (Iterator iter = fExpressions.iterator(); iter.hasNext();) {
68
			Expression expression = (Expression) iter.next();
69
			evaluator.expression = expression;
70
			evaluator.scope = scope;
71
			SafeRunner.run(evaluator);
72
			result = result.and(evaluator.result);
73
			// keep iterating even if we have a not loaded found. It can be
74
			// that we find a false which will result in a better result.
75
			if (result == EvaluationResult.FALSE) {
76
				return result;
77
			}
78
		}
79
		return result;
80
	}
81

    
82
}
(10-10/31)