KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > subscriber > ChangeLogModelSorter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui.subscriber;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jface.viewers.Viewer;
15 import org.eclipse.jface.viewers.ViewerSorter;
16 import org.eclipse.team.internal.ccvs.core.ILogEntry;
17 import org.eclipse.team.ui.synchronize.ISynchronizeModelElement;
18
19 /**
20  * Sorter for the change log model provider.
21  *
22  * @since 3.0
23  */

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

63     public int compare(Viewer viewer, Object JavaDoc o1, Object JavaDoc o2) {
64         //have to deal with non-resources in navigator
65
//if one or both objects are not resources, returned a comparison
66
//based on class.
67
if (o1 instanceof ChangeLogDiffNode && o2 instanceof ChangeLogDiffNode) {
68             ILogEntry r1 = ((ChangeLogDiffNode) o1).getComment();
69             ILogEntry r2 = ((ChangeLogDiffNode) o2).getComment();
70                     
71             if (commentCriteria == DATE)
72                 return r1.getDate().compareTo(r2.getDate());
73             else if (commentCriteria == COMMENT)
74                 return compareNames(r1.getComment(), r2.getComment());
75             else if (commentCriteria == USER)
76                 return compareNames(r1.getAuthor(), r2.getAuthor());
77             else
78                 return 0;
79         }
80
81         if (o1 instanceof ChangeLogModelProvider.FullPathSyncInfoElement && o2 instanceof ChangeLogModelProvider.FullPathSyncInfoElement) {
82             IResource r1 = ((ISynchronizeModelElement)o1).getResource();
83             IResource r2 = ((ISynchronizeModelElement)o2).getResource();
84             if(resourceCriteria == NAME)
85                 return compareNames(r1.getName(), r2.getName());
86             else if(resourceCriteria == PATH)
87                 return compareNames(r1.getFullPath().toString(), r2.getFullPath().toString());
88             else if(resourceCriteria == PARENT_NAME)
89                 return compareNames(r1.getParent().getName(), r2.getParent().getName());
90             else return 0;
91         } else if (o1 instanceof ISynchronizeModelElement)
92             return 1;
93         else if (o2 instanceof ISynchronizeModelElement)
94             return -1;
95         
96         return 0;
97     }
98
99     public int getCommentCriteria() {
100         return commentCriteria;
101     }
102
103     public int getResourceCriteria() {
104         return resourceCriteria;
105     }
106 }
107
Popular Tags