KickJava   Java API By Example, From Geeks To Geeks.

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


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

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