KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > RepositorySorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ccvs.ui.repo;
12
13
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.jface.viewers.ViewerSorter;
16 import org.eclipse.team.internal.ccvs.core.CVSTag;
17 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile;
18 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFolder;
19 import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource;
20 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
21 import org.eclipse.team.internal.ccvs.ui.model.BranchCategory;
22 import org.eclipse.team.internal.ccvs.ui.model.CVSTagElement;
23 import org.eclipse.team.internal.ccvs.ui.model.DateTagCategory;
24 import org.eclipse.team.internal.ccvs.ui.model.RemoteModule;
25 import org.eclipse.team.internal.ccvs.ui.model.VersionCategory;
26
27 public class RepositorySorter extends ViewerSorter {
28     public int category(Object JavaDoc element) {
29         if (element instanceof ICVSRemoteFolder) {
30             if (((ICVSRemoteFolder)element).isDefinedModule()) {
31                 return 7;
32             }
33             return 1;
34         }
35         if (element instanceof RemoteModule) {
36             ICVSRemoteResource resource = ((RemoteModule)element).getCVSResource();
37             if (resource instanceof ICVSRemoteFolder) {
38                 ICVSRemoteFolder folder = (ICVSRemoteFolder) resource;
39                 if (folder.isDefinedModule()) {
40                     return 7;
41                 }
42             }
43             return 1;
44         }
45         if (element instanceof ICVSRemoteFile) {
46             return 2;
47         }
48         if (element instanceof CVSTagElement) {
49             CVSTagElement tagElement = (CVSTagElement)element;
50             if (tagElement.getTag().getType() == CVSTag.HEAD) {
51                 return 0;
52             } else if (tagElement.getTag().getType() == CVSTag.BRANCH) {
53                 return 4;
54             } else if (tagElement.getTag().getType() == CVSTag.VERSION) {
55                 return 5;
56             } else if (tagElement.getTag().getType() == CVSTag.DATE){
57                 return 6;
58             }else{
59                 return 7;
60             }
61         }
62         if (element instanceof BranchCategory) {
63             return 4;
64         }
65         if (element instanceof VersionCategory) {
66             return 5;
67         }
68         if (element instanceof DateTagCategory){
69             return 6;
70         }
71         return 0;
72     }
73
74     public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
75         int cat1 = category(o1);
76         int cat2 = category(o2);
77         if (cat1 != cat2) return cat1 - cat2;
78         
79         if (o1 instanceof CVSTagElement && o2 instanceof CVSTagElement) {
80             CVSTag tag1 = ((CVSTagElement)o1).getTag();
81             CVSTag tag2 = ((CVSTagElement)o2).getTag();
82             if (tag1.getType() == CVSTag.BRANCH) {
83                 return tag1.compareTo(tag2);
84             } else {
85                 return -1 * tag1.compareTo(tag2);
86             }
87         }
88         
89         // Sort versions in reverse alphabetical order
90
if (o1 instanceof ICVSRemoteFolder && o2 instanceof ICVSRemoteFolder) {
91             ICVSRemoteFolder f1 = (ICVSRemoteFolder)o1;
92             ICVSRemoteFolder f2 = (ICVSRemoteFolder)o2;
93             if (f1.getName().equals(f2.getName())) {
94                 return compare(f1, f2);
95             }
96         }
97         
98         if (o1 instanceof ICVSRepositoryLocation && o2 instanceof ICVSRepositoryLocation) {
99             return ((ICVSRepositoryLocation)o1).getLocation(false).compareTo(((ICVSRepositoryLocation)o2).getLocation(false));
100         }
101         
102         return super.compare(viewer, o1, o2);
103     }
104
105     /*
106      * Compare to remote folders whose names are the same.
107      */

108     private int compare(ICVSRemoteFolder f1, ICVSRemoteFolder f2) {
109         CVSTag tag1 = f1.getTag();
110         CVSTag tag2 = f2.getTag();
111         if (tag1 == null) return 1;
112         if (tag2 == null) return -1;
113         return tag2.compareTo(tag1);
114     }
115 }
116
117
Popular Tags