KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > navigator > CommonViewerSorter


1 /***************************************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors: IBM Corporation - initial API and implementation
8  **************************************************************************************************/

9
10 package org.eclipse.ui.navigator;
11
12 import java.util.Set JavaDoc;
13
14 import org.eclipse.jface.viewers.TreePath;
15 import org.eclipse.jface.viewers.TreePathViewerSorter;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerSorter;
18 import org.eclipse.ui.internal.navigator.NavigatorContentService;
19
20 /**
21  *
22  * Provides an implementation of TreeViewerSorter that uses the given parent to determine the
23  * correct sort order based on the defined
24  * <b>org.eclipse.ui.navigator.navigatorContent/navigatorContent/commonSorter</b> elements
25  * available in the set of <i>visible</i> content extensions.
26  *
27  * <p>
28  * The CommonViewerSorter must be assigned a {@link INavigatorContentService} to drive its sorting
29  * algorithm. Without a vaild content service, the sorter will return the default ordering.
30  * </p>
31  * <p>
32  * A CommonViewerSorter may not be attached to more than one CommonViewer.
33  * </p>
34  *
35  * <p>
36  * Clients may not extend this class.
37  * </p>
38  *
39  *
40  *
41  * @since 3.2
42  *
43  */

44 public final class CommonViewerSorter extends TreePathViewerSorter {
45
46     private NavigatorContentService contentService;
47
48     private INavigatorSorterService sorterService;
49
50     /**
51      * Create a sorter service attached to the given content service.
52      *
53      * @param aContentService
54      * The content service used by the viewer that will use this sorter service.
55      * @since 3.3
56      */

57     public void setContentService(INavigatorContentService aContentService) {
58         contentService = (NavigatorContentService) aContentService;
59         sorterService = contentService.getSorterService();
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
66      */

67     public int category(Object JavaDoc element) {
68         if (contentService == null)
69             return 0;
70
71         INavigatorContentDescriptor source = contentService.getSourceOfContribution(element);
72         if (source == null)
73             source = getSource(element);
74         return source != null ? source.getPriority() : Priority.NORMAL_PRIORITY_VALUE;
75     }
76
77     public int compare(Viewer viewer, TreePath parentPath, Object JavaDoc e1, Object JavaDoc e2) {
78         if (contentService == null)
79             return -1;
80         INavigatorContentDescriptor sourceOfLvalue = contentService.getSourceOfContribution(e1);
81         INavigatorContentDescriptor sourceOfRvalue = contentService.getSourceOfContribution(e2);
82
83         if (sourceOfLvalue == null)
84             sourceOfLvalue = getSource(e1);
85         if (sourceOfRvalue == null)
86             sourceOfRvalue = getSource(e2);
87
88         // identity comparison
89
if (sourceOfLvalue != null && sourceOfLvalue == sourceOfRvalue) {
90             Object JavaDoc parent;
91             if (parentPath == null) {
92                 parent = viewer.getInput();
93             } else {
94                 parent = parentPath.getLastSegment();
95             }
96             ViewerSorter sorter = sorterService.findSorter(sourceOfLvalue, parent, e1, e2);
97             if (sorter != null) {
98                 return sorter.compare(viewer, e1, e2);
99             }
100         }
101         int categoryDelta = category(e1) - category(e2);
102         if (categoryDelta == 0) {
103             return super.compare(viewer, e1, e2);
104         }
105         return categoryDelta;
106     }
107
108     private INavigatorContentDescriptor getSource(Object JavaDoc o) {
109         Set JavaDoc descriptors = contentService.findDescriptorsWithPossibleChild(o);
110         if (descriptors != null && descriptors.size() > 0) {
111             return (INavigatorContentDescriptor) descriptors.iterator().next();
112         }
113         return null;
114     }
115
116 }
117
Popular Tags