001: /***************************************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others. All rights reserved. This program and the
003: * accompanying materials are made available under the terms of the Eclipse Public License v1.0
004: * which accompanies this distribution, and is available at
005: * http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM Corporation - initial API and implementation
008: **************************************************************************************************/package org.eclipse.ui.navigator;
009:
010: import java.util.Set;
011:
012: import org.eclipse.jface.viewers.TreePath;
013: import org.eclipse.jface.viewers.TreePathViewerSorter;
014: import org.eclipse.jface.viewers.Viewer;
015: import org.eclipse.jface.viewers.ViewerSorter;
016: import org.eclipse.ui.internal.navigator.NavigatorContentService;
017:
018: /**
019: *
020: * Provides an implementation of TreeViewerSorter that uses the given parent to determine the
021: * correct sort order based on the defined
022: * <b>org.eclipse.ui.navigator.navigatorContent/navigatorContent/commonSorter</b> elements
023: * available in the set of <i>visible</i> content extensions.
024: *
025: * <p>
026: * The CommonViewerSorter must be assigned a {@link INavigatorContentService} to drive its sorting
027: * algorithm. Without a vaild content service, the sorter will return the default ordering.
028: * </p>
029: * <p>
030: * A CommonViewerSorter may not be attached to more than one CommonViewer.
031: * </p>
032: *
033: * <p>
034: * Clients may not extend this class.
035: * </p>
036: *
037: *
038: *
039: * @since 3.2
040: *
041: */
042: public final class CommonViewerSorter extends TreePathViewerSorter {
043:
044: private NavigatorContentService contentService;
045:
046: private INavigatorSorterService sorterService;
047:
048: /**
049: * Create a sorter service attached to the given content service.
050: *
051: * @param aContentService
052: * The content service used by the viewer that will use this sorter service.
053: * @since 3.3
054: */
055: public void setContentService(
056: INavigatorContentService aContentService) {
057: contentService = (NavigatorContentService) aContentService;
058: sorterService = contentService.getSorterService();
059: }
060:
061: /*
062: * (non-Javadoc)
063: *
064: * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
065: */
066: public int category(Object element) {
067: if (contentService == null)
068: return 0;
069:
070: INavigatorContentDescriptor source = contentService
071: .getSourceOfContribution(element);
072: if (source == null)
073: source = getSource(element);
074: return source != null ? source.getPriority()
075: : Priority.NORMAL_PRIORITY_VALUE;
076: }
077:
078: public int compare(Viewer viewer, TreePath parentPath, Object e1,
079: Object e2) {
080: if (contentService == null)
081: return -1;
082: INavigatorContentDescriptor sourceOfLvalue = contentService
083: .getSourceOfContribution(e1);
084: INavigatorContentDescriptor sourceOfRvalue = contentService
085: .getSourceOfContribution(e2);
086:
087: if (sourceOfLvalue == null)
088: sourceOfLvalue = getSource(e1);
089: if (sourceOfRvalue == null)
090: sourceOfRvalue = getSource(e2);
091:
092: // identity comparison
093: if (sourceOfLvalue != null && sourceOfLvalue == sourceOfRvalue) {
094: Object parent;
095: if (parentPath == null) {
096: parent = viewer.getInput();
097: } else {
098: parent = parentPath.getLastSegment();
099: }
100: ViewerSorter sorter = sorterService.findSorter(
101: sourceOfLvalue, parent, e1, e2);
102: if (sorter != null) {
103: return sorter.compare(viewer, e1, e2);
104: }
105: }
106: int categoryDelta = category(e1) - category(e2);
107: if (categoryDelta == 0) {
108: return super .compare(viewer, e1, e2);
109: }
110: return categoryDelta;
111: }
112:
113: private INavigatorContentDescriptor getSource(Object o) {
114: Set descriptors = contentService
115: .findDescriptorsWithPossibleChild(o);
116: if (descriptors != null && descriptors.size() > 0) {
117: return (INavigatorContentDescriptor) descriptors.iterator()
118: .next();
119: }
120: return null;
121: }
122:
123: }
|