KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > synchronize > ChangeSetModelSorter


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.ui.synchronize;
12
13 import org.eclipse.jface.viewers.Viewer;
14 import org.eclipse.jface.viewers.ViewerSorter;
15 import org.eclipse.team.internal.core.subscribers.*;
16 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
17
18 /**
19  * Sorter for the change log model provider.
20  *
21  * @since 3.0
22  */

23 public class ChangeSetModelSorter extends ViewerSorter {
24     
25     private int commentCriteria;
26     private ChangeSetModelProvider provider;
27     
28     // Comment sorting options
29
public final static int DATE = 1;
30     public final static int COMMENT = 2;
31     public final static int USER = 3;
32     
33     public ChangeSetModelSorter(ChangeSetModelProvider provider, int commentCriteria) {
34         this.provider = provider;
35         this.commentCriteria = commentCriteria;
36     }
37     
38     protected int classComparison(Object JavaDoc element) {
39         if (element instanceof ChangeSetDiffNode) {
40             ChangeSet set = ((ChangeSetDiffNode)element).getSet();
41             if (set instanceof ActiveChangeSet) {
42                 return 0;
43             }
44             return 1;
45         }
46         return 2;
47     }
48     
49     protected int compareClass(Object JavaDoc element1, Object JavaDoc element2) {
50         return classComparison(element1) - classComparison(element2);
51     }
52     
53     protected int compareNames(String JavaDoc s1, String JavaDoc s2) {
54         return collator.compare(s1, s2);
55     }
56     
57     /* (non-Javadoc)
58      * Method declared on ViewerSorter.
59      */

60     public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
61         //have to deal with non-resources in navigator
62
//if one or both objects are not resources, returned a comparison
63
//based on class.
64
if (o1 instanceof ChangeSetDiffNode && o2 instanceof ChangeSetDiffNode) {
65             ChangeSet s1 = ((ChangeSetDiffNode) o1).getSet();
66             ChangeSet s2 = ((ChangeSetDiffNode) o2).getSet();
67             if (s1 instanceof ActiveChangeSet && s2 instanceof ActiveChangeSet) {
68                 return compareNames(((ActiveChangeSet)s1).getTitle(), ((ActiveChangeSet)s2).getTitle());
69             }
70             if (s1 instanceof CheckedInChangeSet && s2 instanceof CheckedInChangeSet) {
71                 CheckedInChangeSet r1 = (CheckedInChangeSet)s1;
72                 CheckedInChangeSet r2 = (CheckedInChangeSet)s2;
73                 if (commentCriteria == DATE)
74                     return r1.getDate().compareTo(r2.getDate());
75                 else if (commentCriteria == COMMENT)
76                     return compareNames(r1.getComment(), r2.getComment());
77                 else if (commentCriteria == USER)
78                     return compareNames(r1.getAuthor(), r2.getAuthor());
79                 else
80                     return 0;
81             }
82             if (s1 instanceof ActiveChangeSet) {
83                 return -1;
84             } else if (s2 instanceof ActiveChangeSet) {
85                 return 1;
86             }
87             if (s1 instanceof CheckedInChangeSet) {
88                 return -1;
89             } else if (s2 instanceof CheckedInChangeSet) {
90                 return 1;
91             }
92         }
93
94         if (o1 instanceof ISynchronizeModelElement && o2 instanceof ISynchronizeModelElement) {
95             ViewerSorter embeddedSorter = provider.getEmbeddedSorter();
96             if (embeddedSorter != null) {
97                 return embeddedSorter.compare(viewer, o1, o2);
98             } else {
99                 compareNames(((ISynchronizeModelElement)o1).getName(), ((ISynchronizeModelElement)o2).getName());
100             }
101         } else if (o1 instanceof ISynchronizeModelElement)
102             return 1;
103         else if (o2 instanceof ISynchronizeModelElement)
104             return -1;
105         
106         return 0;
107     }
108
109     public int getCommentCriteria() {
110         return commentCriteria;
111     }
112 }
113
Popular Tags