KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > CVSHistoryFilter


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.ccvs.ui;
12
13 import java.util.Date JavaDoc;
14
15 import org.eclipse.jface.viewers.Viewer;
16 import org.eclipse.jface.viewers.ViewerFilter;
17 import org.eclipse.team.internal.ccvs.core.filehistory.CVSFileRevision;
18 import org.eclipse.team.internal.ui.history.AbstractHistoryCategory;
19
20 public class CVSHistoryFilter extends ViewerFilter {
21     public String JavaDoc author;
22     public Date JavaDoc fromDate;
23     public Date JavaDoc toDate;
24     public String JavaDoc comment;
25     public boolean isOr;
26     private int matchCounter;
27     
28     public CVSHistoryFilter(String JavaDoc author, String JavaDoc comment, Date JavaDoc fromDate, Date JavaDoc toDate, boolean isOr) {
29         this.author = author;
30         this.comment = comment;
31         this.fromDate = fromDate;
32         this.toDate = toDate;
33         this.isOr = isOr;
34         this.matchCounter = 0;
35     }
36
37     /**
38      * @see ViewerFilter#select(Viewer, Object, Object)
39      */

40     public boolean select(Viewer aviewer, Object JavaDoc parentElement, Object JavaDoc element) {
41         if (element instanceof AbstractHistoryCategory)
42             return true;
43         
44         if (element instanceof CVSFileRevision) {
45             CVSFileRevision entry = (CVSFileRevision) element;
46             if (isOr) {
47                 //empty fields should be considered a non-match
48
boolean orSearch = (hasAuthor() && authorMatch(entry)) || (hasDate() && dateMatch(entry)) || (hasComment() && commentMatch(entry));
49                 if (orSearch)
50                     matchCounter++;
51                 
52                 return orSearch;
53             } else {
54                 //"and" search
55
//empty fields should be considered a match
56
boolean andSearch = (!hasAuthor() || authorMatch(entry)) && (!hasDate() || dateMatch(entry)) && (!hasComment() || commentMatch(entry));
57                 if (andSearch)
58                     matchCounter++;
59                 
60                 return andSearch;
61             }
62         }
63         return false;
64     }
65
66     protected boolean authorMatch(CVSFileRevision revision) {
67         return revision.getAuthor().equals(author);
68     }
69
70     protected boolean commentMatch(CVSFileRevision revision) {
71         return !(revision.getComment().toLowerCase().indexOf(comment.toLowerCase()) == -1);
72     }
73
74     protected boolean dateMatch(CVSFileRevision revision) {
75         return isAfterFromDate(revision) && isBeforeToDate(revision);
76     }
77
78     private boolean isBeforeToDate(CVSFileRevision revision) {
79         if (toDate == null)
80             return true;
81         return (toDate.after(new Date JavaDoc(revision.getTimestamp())));
82     }
83
84     private boolean isAfterFromDate(CVSFileRevision revision) {
85         if (fromDate == null)
86             return true;
87         return (fromDate.before(new Date JavaDoc(revision.getTimestamp())));
88     }
89
90     protected boolean hasAuthor() {
91         return !author.equals(""); //$NON-NLS-1$
92
}
93
94     protected boolean hasComment() {
95         return !comment.equals(""); //$NON-NLS-1$
96
}
97
98     protected boolean hasDate() {
99         return fromDate != null || toDate != null;
100     }
101     
102     public int getMatchCount(){
103         return matchCounter;
104     }
105 }
106
Popular Tags