KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > buildpaths > CPListElementSorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.jdt.internal.ui.wizards.buildpaths;
12
13 import org.eclipse.jface.viewers.ContentViewer;
14 import org.eclipse.jface.viewers.IBaseLabelProvider;
15 import org.eclipse.jface.viewers.ILabelProvider;
16 import org.eclipse.jface.viewers.Viewer;
17 import org.eclipse.jface.viewers.ViewerComparator;
18
19 import org.eclipse.jdt.core.IAccessRule;
20 import org.eclipse.jdt.core.IClasspathEntry;
21
22 public class CPListElementSorter extends ViewerComparator {
23     
24     private static final int SOURCE= 0;
25     private static final int PROJECT= 1;
26     private static final int LIBRARY= 2;
27     private static final int VARIABLE= 3;
28     private static final int CONTAINER= 4;
29     
30     private static final int ATTRIBUTE= 5;
31     private static final int CONTAINER_ENTRY= 6;
32     
33     private static final int OTHER= 7;
34     
35     /*
36      * @see ViewerSorter#category(Object)
37      */

38     public int category(Object JavaDoc obj) {
39         if (obj instanceof CPListElement) {
40             CPListElement element= (CPListElement) obj;
41             if (element.getParentContainer() != null) {
42                 return CONTAINER_ENTRY;
43             }
44             switch (element.getEntryKind()) {
45             case IClasspathEntry.CPE_LIBRARY:
46                 return LIBRARY;
47             case IClasspathEntry.CPE_PROJECT:
48                 return PROJECT;
49             case IClasspathEntry.CPE_SOURCE:
50                 return SOURCE;
51             case IClasspathEntry.CPE_VARIABLE:
52                 return VARIABLE;
53             case IClasspathEntry.CPE_CONTAINER:
54                 return CONTAINER;
55             }
56         } else if (obj instanceof CPListElementAttribute) {
57             return ATTRIBUTE;
58         } else if (obj instanceof IAccessRule) {
59             return ATTRIBUTE;
60         }
61         return OTHER;
62     }
63     
64     /* (non-Javadoc)
65      * @see org.eclipse.jface.viewers.ViewerSorter#compare(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
66      */

67     public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
68         
69         int cat1 = category(e1);
70         int cat2 = category(e2);
71
72         if (cat1 != cat2)
73             return cat1 - cat2;
74         
75         if (cat1 == ATTRIBUTE || cat1 == CONTAINER_ENTRY) {
76             return 0; // do not sort attributes or container entries
77
}
78         
79         if (viewer instanceof ContentViewer) {
80             IBaseLabelProvider prov = ((ContentViewer) viewer).getLabelProvider();
81             if (prov instanceof ILabelProvider) {
82                 ILabelProvider lprov = (ILabelProvider) prov;
83                 String JavaDoc name1 = lprov.getText(e1);
84                 String JavaDoc name2 = lprov.getText(e2);
85                 return getComparator().compare(name1, name2);
86             }
87         }
88         return 0;
89     }
90     
91
92 }
93
Popular Tags