Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 79235975256b92c54a572208cd81a75376d64727 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (c) 2006, 2011 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

import java.util.Date;

import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.team.core.history.ITag;
import org.eclipse.team.internal.ccvs.core.filehistory.CVSFileRevision;
import org.eclipse.team.internal.ui.history.AbstractHistoryCategory;

public class CVSHistoryFilter extends ViewerFilter {
	public String branchName;
	public String author;
	public Date fromDate;
	public Date toDate;
	public String comment;
	public boolean isOr;
	private int matchCounter;
	
	public CVSHistoryFilter(String branchName, String author, String comment, Date fromDate, Date toDate, boolean isOr) {
		this.branchName = branchName;
		this.author = author;
		this.comment = comment;
		this.fromDate = fromDate;
		this.toDate = toDate;
		this.isOr = isOr;
		this.matchCounter = 0;
	}

	@Override
	public boolean select(Viewer aviewer, Object parentElement, Object element) {
		if (element instanceof AbstractHistoryCategory)
			return true;
		
		if (element instanceof CVSFileRevision) {
			CVSFileRevision entry = (CVSFileRevision) element;
			if (isOr) {
				//empty fields should be considered a non-match
				boolean orSearch = (hasBranchName() && branchMatch(entry)) || (hasAuthor() && authorMatch(entry)) || (hasDate() && dateMatch(entry)) || (hasComment() && commentMatch(entry));
				if (orSearch)
					matchCounter++;
				
				return orSearch;
			} else {
				//"and" search
				//empty fields should be considered a match
				boolean andSearch = (!hasBranchName() || branchMatch(entry)) && (!hasAuthor() || authorMatch(entry)) && (!hasDate() || dateMatch(entry)) && (!hasComment() || commentMatch(entry));
				if (andSearch)
					matchCounter++;
				
				return andSearch;
			}
		}
		return false;
	}

	protected boolean branchMatch(CVSFileRevision revision) {
		ITag[] branches = revision.getBranches();
		for (int i = 0; i < branches.length; i++) {
			if ((branches[i].getName().toLowerCase().indexOf(branchName.toLowerCase()) != -1)) {
				return true;
			}
		}		
		return false;
	}

	protected boolean authorMatch(CVSFileRevision revision) {
		return revision.getAuthor().equals(author);
	}

	protected boolean commentMatch(CVSFileRevision revision) {
		return !(revision.getComment().toLowerCase().indexOf(comment.toLowerCase()) == -1);
	}

	protected boolean dateMatch(CVSFileRevision revision) {
		return isAfterFromDate(revision) && isBeforeToDate(revision);
	}

	private boolean isBeforeToDate(CVSFileRevision revision) {
		if (toDate == null)
			return true;
		return (toDate.after(new Date(revision.getTimestamp())));
	}

	private boolean isAfterFromDate(CVSFileRevision revision) {
		if (fromDate == null)
			return true;
		return (fromDate.before(new Date(revision.getTimestamp())));
	}

	protected boolean hasBranchName() {
		return !branchName.equals(""); //$NON-NLS-1$
	}

	protected boolean hasAuthor() {
		return !author.equals(""); //$NON-NLS-1$
	}

	protected boolean hasComment() {
		return !comment.equals(""); //$NON-NLS-1$
	}

	protected boolean hasDate() {
		return fromDate != null || toDate != null;
	}
	
	public int getMatchCount(){
		return matchCounter;
	}
}

Back to the top