Project

General

Profile

Download (3.21 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2006, 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

    
12
package org.eclipse.ui.internal.navigator.sorters;
13

    
14
import org.eclipse.core.expressions.EvaluationResult;
15
import org.eclipse.core.expressions.Expression;
16
import org.eclipse.core.expressions.IEvaluationContext;
17
import org.eclipse.core.runtime.IConfigurationElement;
18
import org.eclipse.core.runtime.SafeRunner;
19
import org.eclipse.jface.viewers.ViewerSorter;
20
import org.eclipse.ui.internal.navigator.CustomAndExpression;
21
import org.eclipse.ui.internal.navigator.NavigatorPlugin;
22
import org.eclipse.ui.internal.navigator.NavigatorSafeRunnable;
23
import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
24

    
25
/**
26
 * 
27
 * Describes a <b>commonSorter</b> element under a
28
 * <b>org.eclipse.ui.navigator.navigatorContent</b> extension.
29
 * 
30
 * @since 3.2
31
 */
32
public class CommonSorterDescriptor implements INavigatorContentExtPtConstants {
33

    
34
	private IConfigurationElement element;
35

    
36
	private Expression parentExpression;
37

    
38
	private String id;
39

    
40
	protected CommonSorterDescriptor(IConfigurationElement anElement) { 
41
		element = anElement;
42
		init();
43
	}
44

    
45
	private void init() {
46
		id = element.getAttribute(ATT_ID);
47
		if (id == null) {
48
			id = ""; //$NON-NLS-1$
49
		}
50
		IConfigurationElement[] children = element
51
				.getChildren(TAG_PARENT_EXPRESSION);
52
		if (children.length == 1) {
53
			parentExpression = new CustomAndExpression(children[0]);
54
		}
55
	}
56

    
57
	/**
58
	 * 
59
	 * @return An identifier used to determine whether the sorter is visible.
60
	 *         May not be unique.
61
	 */
62
	public String getId() {
63
		return id;
64
	}
65
 
66
	/**
67
	 * 
68
	 * @param aParent
69
	 *            An element from the viewer
70
	 * @return True if and only if this CommonSorter can sort the children of
71
	 *         the given parent.
72
	 */
73
	public boolean isEnabledForParent(Object aParent) {
74
		if(aParent == null) {
75
			return false;
76
		}
77

    
78
		if (parentExpression != null) {
79
			IEvaluationContext context = NavigatorPlugin.getEvalContext(aParent);
80
			return NavigatorPlugin.safeEvaluate(parentExpression, context) == EvaluationResult.TRUE;
81
		}
82
		return true;
83
	}
84

    
85
	/**
86
	 * 
87
	 * @return An instance of the ViewerSorter defined by the extension. Callers
88
	 *         of this method are responsible for managing the instantiated
89
	 *         filter.
90
	 */
91
	public ViewerSorter createSorter() {
92
		final ViewerSorter[] sorter = new ViewerSorter[1];
93

    
94
		SafeRunner.run(new NavigatorSafeRunnable(element) {
95
			public void run() throws Exception {
96
				sorter[0] = (ViewerSorter) element.createExecutableExtension(ATT_CLASS);
97
			}
98
		});
99
		if (sorter[0] != null)
100
			return sorter[0];
101
		return SkeletonViewerSorter.INSTANCE;
102
	}
103

    
104
	/*
105
	 * (non-Javadoc)
106
	 * 
107
	 * @see java.lang.Object#toString()
108
	 */
109
	public String toString() {
110
		return "CommonSorterDescriptor[" + getId() + "]"; //$NON-NLS-1$//$NON-NLS-2$
111
	}
112
}
(1-1/4)