Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b8000e70dc3a576a2a290f2493b021d24ffcade (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*******************************************************************************
 * Copyright (c) 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 ******************************************************************************/
/*
 * Created on Jun 24, 2003
 */
package org.eclipse.cdt.internal.ui.search;

import org.eclipse.cdt.ui.CSearchResultLabelProvider;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.search.ui.ISearchResultView;
import org.eclipse.search.ui.ISearchResultViewEntry;
import org.eclipse.search.ui.SearchUI;

/**
 * @author aniefer
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class ParentNameSorter extends ViewerSorter {
	
	public int compare(Viewer viewer, Object e1, Object e2) {
		String name1= null;
		String name2= null;
		
		ISearchResultViewEntry entry1 = null;
		ISearchResultViewEntry entry2 = null;
		
		if (e1 instanceof ISearchResultViewEntry){
			name1 = _labelProvider.getText(e1);
			entry1 = (ISearchResultViewEntry)e1;
		}
			
		if (e2 instanceof ISearchResultViewEntry){
			name2 = _labelProvider.getText(e2);
			entry2 = (ISearchResultViewEntry)e2;
		}
			
		if (name1 == null)
			name1= ""; //$NON-NLS-1$
			
		if (name2 == null)
			name2= ""; //$NON-NLS-1$
			
		int compare = getCollator().compare( name1, name2 );
		
		if( compare == 0 ){
			int startPos1 = -1;
			int startPos2 = -1;
			IMarker marker1 = entry1.getSelectedMarker();
			IMarker marker2 = entry2.getSelectedMarker();

			if (marker1 != null)
				startPos1 = marker1.getAttribute( IMarker.CHAR_START, -1 );
			if (marker2 != null)
				startPos2 = marker2.getAttribute( IMarker.CHAR_START, -1 );
			
			compare = startPos1 - startPos2;
		}
		
		return compare;
	}

	/*
	 * Overrides method from ViewerSorter
	 */
	public boolean isSorterProperty(Object element, String property) {
		return true;
	}

	/*
	 * Overrides method from ViewerSorter
	 */
	public void sort(Viewer viewer, Object[] elements) {
		// Set label provider to show "path - resource"
		ISearchResultView view = SearchUI.getSearchResultView();
		if (view == null)
			return;
			
		_labelProvider = view.getLabelProvider();
		
		if( _labelProvider instanceof CSearchResultLabelProvider )
			((CSearchResultLabelProvider)_labelProvider).setOrder( CSearchResultLabelProvider.SHOW_CONTAINER_ELEMENT );

		super.sort( viewer, elements );
	}
	
	private ILabelProvider _labelProvider;
}

Back to the top