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.navigator;
|
13
|
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.eclipse.jface.viewers.TreePath;
|
17
|
import org.eclipse.jface.viewers.TreePathViewerSorter;
|
18
|
import org.eclipse.jface.viewers.Viewer;
|
19
|
import org.eclipse.jface.viewers.ViewerSorter;
|
20
|
import org.eclipse.osgi.util.NLS;
|
21
|
import org.eclipse.ui.internal.navigator.CommonNavigatorMessages;
|
22
|
import org.eclipse.ui.internal.navigator.NavigatorContentService;
|
23
|
import org.eclipse.ui.internal.navigator.NavigatorContentServiceContentProvider;
|
24
|
import org.eclipse.ui.internal.navigator.NavigatorPlugin;
|
25
|
import org.eclipse.ui.internal.navigator.Policy;
|
26
|
|
27
|
/**
|
28
|
*
|
29
|
* Provides an implementation of TreeViewerSorter that uses the given parent to determine the
|
30
|
* correct sort order based on the defined
|
31
|
* <b>org.eclipse.ui.navigator.navigatorContent/navigatorContent/commonSorter</b> elements
|
32
|
* available in the set of <i>visible</i> content extensions.
|
33
|
*
|
34
|
* <p>
|
35
|
* The CommonViewerSorter must be assigned a {@link INavigatorContentService} to drive its sorting
|
36
|
* algorithm. Without a valid content service, the sorter will return the default ordering.
|
37
|
* </p>
|
38
|
* <p>
|
39
|
* A CommonViewerSorter may not be attached to more than one CommonViewer.
|
40
|
* </p>
|
41
|
*
|
42
|
* @since 3.2
|
43
|
*
|
44
|
*/
|
45
|
public final class CommonViewerSorter extends TreePathViewerSorter {
|
46
|
|
47
|
private static final int LEFT_UNDERSTANDS = 1;
|
48
|
private static final int RIGHT_UNDERSTANDS = 2;
|
49
|
private static final int BOTH_UNDERSTAND = LEFT_UNDERSTANDS | RIGHT_UNDERSTANDS;
|
50
|
|
51
|
private NavigatorContentService contentService;
|
52
|
|
53
|
private INavigatorSorterService sorterService;
|
54
|
|
55
|
/**
|
56
|
* Create a sorter service attached to the given content service.
|
57
|
*
|
58
|
* @param aContentService
|
59
|
* The content service used by the viewer that will use this sorter service.
|
60
|
* @since 3.3
|
61
|
*/
|
62
|
public void setContentService(INavigatorContentService aContentService) {
|
63
|
contentService = (NavigatorContentService) aContentService;
|
64
|
sorterService = contentService.getSorterService();
|
65
|
}
|
66
|
|
67
|
/*
|
68
|
* (non-Javadoc)
|
69
|
*
|
70
|
* @see org.eclipse.jface.viewers.ViewerSorter#category(java.lang.Object)
|
71
|
*/
|
72
|
public int category(Object element) {
|
73
|
if (contentService == null)
|
74
|
return 0;
|
75
|
|
76
|
INavigatorContentDescriptor source = getSource(element);
|
77
|
return source != null ? source.getSequenceNumber() : Priority.NORMAL_PRIORITY_VALUE;
|
78
|
}
|
79
|
|
80
|
private void logMissingExtension(Object parent, Object object) {
|
81
|
NavigatorPlugin.logError(0, NLS.bind(CommonNavigatorMessages.CommonViewerSorter_NoContentExtensionForObject,
|
82
|
object != null ? object.toString() : "<null>", parent != null ? parent.toString() : "<null>"), null); //$NON-NLS-1$ //$NON-NLS-2$
|
83
|
}
|
84
|
|
85
|
public int compare(Viewer viewer, TreePath parentPath, Object e1, Object e2) {
|
86
|
if (contentService == null)
|
87
|
return -1;
|
88
|
INavigatorContentDescriptor sourceOfLvalue = getSource(e1);
|
89
|
INavigatorContentDescriptor sourceOfRvalue = getSource(e2);
|
90
|
|
91
|
Object parent;
|
92
|
if (parentPath == null) {
|
93
|
parent = viewer.getInput();
|
94
|
} else {
|
95
|
parent = parentPath.getLastSegment();
|
96
|
}
|
97
|
|
98
|
if (sourceOfLvalue == null) {
|
99
|
logMissingExtension(parent, e1);
|
100
|
return -1;
|
101
|
}
|
102
|
if (sourceOfRvalue == null) {
|
103
|
logMissingExtension(parent, e2);
|
104
|
return -1;
|
105
|
}
|
106
|
|
107
|
ViewerSorter sorter = null;
|
108
|
|
109
|
// shortcut if contributed by same source
|
110
|
if (sourceOfLvalue == sourceOfRvalue) {
|
111
|
sorter = sorterService.findSorter(sourceOfLvalue, parent, e1, e2);
|
112
|
} else {
|
113
|
|
114
|
boolean flags[] = new boolean[4];
|
115
|
flags[0] = sourceOfLvalue.isTriggerPoint(e1);
|
116
|
flags[1] = sourceOfLvalue.isTriggerPoint(e2);
|
117
|
flags[2] = sourceOfRvalue.isTriggerPoint(e1);
|
118
|
flags[3] = sourceOfRvalue.isTriggerPoint(e2);
|
119
|
|
120
|
int whoknows = 0;
|
121
|
whoknows = whoknows | (flags[0] & flags[1] ? LEFT_UNDERSTANDS : 0);
|
122
|
whoknows = whoknows | (flags[2] & flags[3] ? RIGHT_UNDERSTANDS : 0);
|
123
|
|
124
|
switch (whoknows) {
|
125
|
case BOTH_UNDERSTAND:
|
126
|
sorter = sourceOfLvalue.getSequenceNumber() > sourceOfRvalue.getSequenceNumber() ? sorterService
|
127
|
.findSorter(sourceOfLvalue, parent, e1, e2)
|
128
|
: sorterService.findSorter(sourceOfRvalue, parent, e1, e2);
|
129
|
break;
|
130
|
case LEFT_UNDERSTANDS:
|
131
|
sorter = sorterService.findSorter(sourceOfLvalue, parent, e1, e2);
|
132
|
break;
|
133
|
case RIGHT_UNDERSTANDS:
|
134
|
sorter = sorterService.findSorter(sourceOfRvalue, parent, e1, e2);
|
135
|
break;
|
136
|
}
|
137
|
}
|
138
|
|
139
|
if (sorter != null) {
|
140
|
return sorter.compare(viewer, e1, e2);
|
141
|
}
|
142
|
|
143
|
int categoryDelta = category(e1) - category(e2);
|
144
|
if (categoryDelta == 0) {
|
145
|
return super.compare(viewer, e1, e2);
|
146
|
}
|
147
|
return categoryDelta;
|
148
|
}
|
149
|
|
150
|
/* (non-Javadoc)
|
151
|
* @see org.eclipse.jface.viewers.ViewerComparator#isSorterProperty(java.lang.Object, java.lang.String)
|
152
|
*/
|
153
|
public boolean isSorterProperty(Object element, String property) {
|
154
|
// Have to get the parent path from the content provider
|
155
|
NavigatorContentServiceContentProvider cp = (NavigatorContentServiceContentProvider) contentService.createCommonContentProvider();
|
156
|
TreePath[] parentPaths = cp.getParents(element);
|
157
|
for (int i = 0; i < parentPaths.length; i++) {
|
158
|
if (isSorterProperty(parentPaths[i], element, property))
|
159
|
return true;
|
160
|
}
|
161
|
return false;
|
162
|
}
|
163
|
|
164
|
/* (non-Javadoc)
|
165
|
* @see org.eclipse.jface.viewers.TreePathViewerSorter#isSorterProperty(org.eclipse.jface.viewers.TreePath, java.lang.Object, java.lang.String)
|
166
|
*/
|
167
|
public boolean isSorterProperty(TreePath parentPath, Object element, String property) {
|
168
|
INavigatorContentDescriptor contentDesc = getSource(element);
|
169
|
if (parentPath.getSegmentCount() == 0)
|
170
|
return false;
|
171
|
ViewerSorter sorter = sorterService.findSorter(contentDesc, parentPath.getLastSegment(), element, null);
|
172
|
if (sorter != null)
|
173
|
return sorter.isSorterProperty(element, property);
|
174
|
return false;
|
175
|
}
|
176
|
|
177
|
|
178
|
private INavigatorContentDescriptor getSource(Object o) {
|
179
|
// Fast path - just an optimization for the common case
|
180
|
INavigatorContentDescriptor ncd = contentService.getSourceOfContribution(o);
|
181
|
if (ncd != null) {
|
182
|
if (Policy.DEBUG_SORT)
|
183
|
System.out.println("sort: " + ncd + " object: " + o); //$NON-NLS-1$//$NON-NLS-2$
|
184
|
return ncd;
|
185
|
}
|
186
|
|
187
|
Set descriptors = contentService.findDescriptorsByTriggerPoint(o, NavigatorContentService.CONSIDER_OVERRIDES);
|
188
|
if (descriptors != null && descriptors.size() > 0) {
|
189
|
ncd = (INavigatorContentDescriptor) descriptors.iterator().next();
|
190
|
if (Policy.DEBUG_SORT)
|
191
|
System.out.println("sort: " + ncd + " object: " + o); //$NON-NLS-1$//$NON-NLS-2$
|
192
|
return ncd;
|
193
|
}
|
194
|
if (Policy.DEBUG_SORT)
|
195
|
System.out.println("sort: NULL object: " + o); //$NON-NLS-1$
|
196
|
return null;
|
197
|
}
|
198
|
|
199
|
|
200
|
}
|