KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > search > ParentSorter


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

11 package org.eclipse.jdt.internal.ui.search;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.IType;
16 import org.eclipse.jface.viewers.Viewer;
17
18 public class ParentSorter extends NameSorter {
19     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
20         String JavaDoc leftParent= getParentName(e1);
21         String JavaDoc rightParent= getParentName(e2);
22         int result= collator.compare(leftParent, rightParent);
23         if (result == 0)
24             return super.compare(viewer, e1, e2);
25         else
26             return result;
27     }
28     
29     private String JavaDoc getParentName(Object JavaDoc element) {
30         if (element instanceof IJavaElement) {
31             IJavaElement parent= ((IJavaElement)element).getParent();
32             if (parent instanceof IType)
33                 return ((IType)parent).getFullyQualifiedName();
34             if (parent != null)
35                 return parent.getElementName();
36         }
37         if (element instanceof IResource) {
38             IResource parent= ((IResource)element).getParent();
39             if (parent != null)
40                 return parent.getName();
41         }
42         return ""; //$NON-NLS-1$
43
}
44 }
45
Popular Tags