KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > TeamViewerSorter


1 /*******************************************************************************
2  * Copyright (c) 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.team.internal.ui.mapping;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.mapping.IModelProviderDescriptor;
15 import org.eclipse.core.resources.mapping.ModelProvider;
16 import org.eclipse.jface.viewers.*;
17 import org.eclipse.team.internal.ui.Utils;
18 import org.eclipse.ui.navigator.CommonViewerSorter;
19
20 public class TeamViewerSorter extends TreePathViewerSorter {
21
22     private CommonViewerSorter sorter;
23     
24     public TeamViewerSorter(CommonViewerSorter sorter) {
25         this.sorter = sorter;
26     }
27
28     public int category(Object JavaDoc element) {
29         if (element instanceof ModelProvider) {
30             return 2;
31         }
32         IResource resource = Utils.getResource(element);
33         if (resource != null && resource.getType() == IResource.PROJECT) {
34             return 1;
35         }
36         
37         return super.category(element);
38     }
39     
40     public int compare(Viewer viewer, TreePath parentPath, Object JavaDoc e1, Object JavaDoc e2) {
41         if (parentPath == null || parentPath.getSegmentCount() == 0) {
42             // We need to handle the sorting at the top level
43
int cat1 = category(e1);
44             int cat2 = category(e2);
45
46             if (cat1 != cat2)
47                 return cat1 - cat2;
48             
49             if (e1 instanceof ModelProvider && e2 instanceof ModelProvider) {
50                 ModelProvider mp1 = (ModelProvider) e1;
51                 ModelProvider mp2 = (ModelProvider) e2;
52                 if (isExtends(mp1, mp2.getDescriptor())) {
53                     return 1;
54                 }
55                 if (isExtends(mp2, mp1.getDescriptor())) {
56                     return -1;
57                 }
58                 return mp1.getDescriptor().getLabel().compareTo(mp2.getDescriptor().getLabel());
59             }
60             IResource r1 = Utils.getResource(e1);
61             IResource r2 = Utils.getResource(e2);
62             if (r1 != null && r2 != null) {
63                 return r1.getName().compareTo(r2.getName());
64             }
65         }
66         return sorter.compare(viewer, parentPath, e1, e2);
67     }
68     
69     private boolean isExtends(ModelProvider mp1, IModelProviderDescriptor desc) {
70         String JavaDoc[] extended = mp1.getDescriptor().getExtendedModels();
71         for (int i = 0; i < extended.length; i++) {
72             String JavaDoc id = extended[i];
73             if (id.equals(desc.getId())) {
74                 return true;
75             }
76         }
77         for (int i = 0; i < extended.length; i++) {
78             String JavaDoc id = extended[i];
79             IModelProviderDescriptor desc2 = ModelProvider.getModelProviderDescriptor(id);
80             if (isExtends(mp1, desc2)) {
81                 return true;
82             }
83         }
84         return false;
85     }
86 }
87
Popular Tags