001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.navigator.sorters;
011:
012: import org.eclipse.core.expressions.EvaluationContext;
013: import org.eclipse.core.expressions.EvaluationResult;
014: import org.eclipse.core.expressions.Expression;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.jface.viewers.ViewerSorter;
018: import org.eclipse.ui.internal.navigator.CustomAndExpression;
019: import org.eclipse.ui.internal.navigator.NavigatorPlugin;
020: import org.eclipse.ui.internal.navigator.extensions.INavigatorContentExtPtConstants;
021:
022: /**
023: *
024: * Describes a <b>commonSorter</b> element under a
025: * <b>org.eclipse.ui.navigator.navigatorContent</b> extension.
026: *
027: * @since 3.2
028: */
029: public class CommonSorterDescriptor implements
030: INavigatorContentExtPtConstants {
031:
032: private IConfigurationElement element;
033:
034: private Expression parentExpression;
035:
036: private String id;
037:
038: protected CommonSorterDescriptor(IConfigurationElement anElement) {
039: element = anElement;
040: init();
041: }
042:
043: private void init() {
044: id = element.getAttribute(ATT_ID);
045: if (id == null) {
046: id = ""; //$NON-NLS-1$
047: }
048: IConfigurationElement[] children = element
049: .getChildren(TAG_PARENT_EXPRESSION);
050: if (children.length == 1) {
051: parentExpression = new CustomAndExpression(children[0]);
052: }
053: }
054:
055: /**
056: *
057: * @return An identifier used to determine whether the sorter is visible.
058: * May not be unique.
059: */
060: public String getId() {
061: return id;
062: }
063:
064: /**
065: *
066: * @param aParent
067: * An element from the viewer
068: * @return True if and only if this CommonSorter can sort the children of
069: * the given parent.
070: */
071: public boolean isEnabledForParent(Object aParent) {
072: if (aParent == null) {
073: return false;
074: }
075:
076: if (parentExpression != null) {
077: EvaluationContext context = new EvaluationContext(null,
078: aParent);
079: context.setAllowPluginActivation(true);
080: try {
081: return parentExpression.evaluate(context) == EvaluationResult.TRUE;
082: } catch (CoreException e) {
083: NavigatorPlugin.logError(0, e.getMessage(), e);
084: }
085: }
086: return true;
087: }
088:
089: /**
090: *
091: * @return An instance of the ViewerSorter defined by the extension. Callers
092: * of this method are responsible for managing the instantiated
093: * filter.
094: */
095: public ViewerSorter createSorter() {
096: try {
097: return (ViewerSorter) element
098: .createExecutableExtension(ATT_CLASS);
099: } catch (RuntimeException re) {
100: NavigatorPlugin.logError(0, re.getMessage(), re);
101: } catch (CoreException e) {
102: NavigatorPlugin.logError(0, e.getMessage(), e);
103: }
104: return SkeletonViewerSorter.INSTANCE;
105: }
106:
107: /*
108: * (non-Javadoc)
109: *
110: * @see java.lang.Object#toString()
111: */
112: public String toString() {
113: return "CommonSorterDescriptor[" + getId() + "]"; //$NON-NLS-1$//$NON-NLS-2$
114: }
115: }
|