KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > sorters > CommonSorterDescriptor


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.navigator.sorters;
13
14 import org.eclipse.core.expressions.EvaluationContext;
15 import org.eclipse.core.expressions.EvaluationResult;
16 import org.eclipse.core.expressions.Expression;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
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.extensions.INavigatorContentExtPtConstants;
23
24 /**
25  *
26  * Describes a <b>commonSorter</b> element under a
27  * <b>org.eclipse.ui.navigator.navigatorContent</b> extension.
28  *
29  * @since 3.2
30  */

31 public class CommonSorterDescriptor implements INavigatorContentExtPtConstants {
32
33     private IConfigurationElement element;
34
35     private Expression parentExpression;
36
37     private String JavaDoc id;
38
39     protected CommonSorterDescriptor(IConfigurationElement anElement) {
40         element = anElement;
41         init();
42     }
43
44     private void init() {
45         id = element.getAttribute(ATT_ID);
46         if (id == null) {
47             id = ""; //$NON-NLS-1$
48
}
49         IConfigurationElement[] children = element
50                 .getChildren(TAG_PARENT_EXPRESSION);
51         if (children.length == 1) {
52             parentExpression = new CustomAndExpression(children[0]);
53         }
54     }
55
56     /**
57      *
58      * @return An identifier used to determine whether the sorter is visible.
59      * May not be unique.
60      */

61     public String JavaDoc getId() {
62         return id;
63     }
64  
65     /**
66      *
67      * @param aParent
68      * An element from the viewer
69      * @return True if and only if this CommonSorter can sort the children of
70      * the given parent.
71      */

72     public boolean isEnabledForParent(Object JavaDoc aParent) {
73         if(aParent == null) {
74             return false;
75         }
76
77         if (parentExpression != null) {
78             EvaluationContext context = new EvaluationContext(null, aParent);
79             context.setAllowPluginActivation(true);
80             try {
81                 return parentExpression.evaluate(context) == EvaluationResult.TRUE;
82             } catch (CoreException e) {
83                 NavigatorPlugin.logError(0, e.getMessage(), e);
84             }
85         }
86         return true;
87     }
88
89     /**
90      *
91      * @return An instance of the ViewerSorter defined by the extension. Callers
92      * of this method are responsible for managing the instantiated
93      * filter.
94      */

95     public ViewerSorter createSorter() {
96         try {
97             return (ViewerSorter) element.createExecutableExtension(ATT_CLASS);
98         } catch (RuntimeException JavaDoc re) {
99             NavigatorPlugin.logError(0, re.getMessage(), re);
100         } catch (CoreException e) {
101             NavigatorPlugin.logError(0, e.getMessage(), e);
102         }
103         return SkeletonViewerSorter.INSTANCE;
104     }
105
106     /*
107      * (non-Javadoc)
108      *
109      * @see java.lang.Object#toString()
110      */

111     public String JavaDoc toString() {
112         return "CommonSorterDescriptor[" + getId() + "]"; //$NON-NLS-1$//$NON-NLS-2$
113
}
114 }
115
Popular Tags