Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ef751ce9f1141d70b8b80cecce56a2456228fda (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
124
125
126
127
128
129
130
131
132
133
134
135
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 ******************************************************************************/

package org.eclipse.jface.tests.viewers;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.ICheckStateProvider;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.jface.viewers.ViewerSorter;

/**
 * Utilities for testing ICheckStateProviders.
 * @since 3.5
 */
public class CheckStateProviderTestsUtil {
	private static final int NUMBER_OF_STATES = 4;

	/**
	 * An ICheckStateProvider which records whether
	 * its isChecked and isGrayed methods are invoked.
	 * @since 3.5
	 */
	public static class TestMethodsInvokedCheckStateProvider implements ICheckStateProvider {
		public List isCheckedInvokedOn = new ArrayList();
		public List isGrayedInvokedOn = new ArrayList();
		
		public boolean isChecked(Object element) {
			isCheckedInvokedOn.add(element);
			return true;
		}

		public boolean isGrayed(Object element) {
			isGrayedInvokedOn.add(element);
			return true;
		}
		
		public void reset() {
			isCheckedInvokedOn = new ArrayList();
			isGrayedInvokedOn = new ArrayList();
		}
	}
	
	/**
	 * An ICheckStateProvider which provides a consistent
	 * variety of states for input elements based on the
	 * parameter provided in the constructor.
	 * @since 3.5
	 */
	public static final class TestCheckStateProvider extends TestMethodsInvokedCheckStateProvider {
		private int shift;
		
		/**
		 * A value from 0 to 2 which will change the 
		 * checkstate assignments.
		 * @param shift
		 */
		public TestCheckStateProvider(int shift) {
			this.shift = shift;
		}
		
		public boolean isChecked(Object element) {
			super.isChecked(element);
			return shouldBeChecked((TestElement)element, shift);
		}

		public boolean isGrayed(Object element) {
			super.isGrayed(element);
			return shouldBeGrayed((TestElement)element, shift);
		}
	}
	
	/**
	 * A sorter for TestElements.
	 * @since 3.5
	 */
	public static final class Sorter extends ViewerSorter {
		public int compare(Viewer viewer, Object e1, Object e2) {
			return constructNumber((TestElement)e1) - constructNumber((TestElement)e2);
		}
	}
	
	/**
	 * A filter for TestElements.
	 * @since 3.5
	 */
	public static final class Filter extends ViewerFilter {
		public boolean select(Viewer viewer, Object parentElement, Object element) {
			return (constructNumber((TestElement)element) % (NUMBER_OF_STATES * 2 - 1)) == (NUMBER_OF_STATES - 1);
		}
	}
    /**
     * @param te
     * @return	a number between 0 and 3 based on <code>te</code>. 
     * 	Given the same TestElement, this function always returns the
     * 	same value.
     */
	public static int constructNumber(TestElement te) {
		String id = te.getID();
		int number = Integer.parseInt(id.substring(id.lastIndexOf('-') + 1)) + id.length();
		return number % NUMBER_OF_STATES;
	}
	
	/**
	 * @param te
	 * @param shift	a parameter to change all check states
	 * 		to be different (use to simulate different
	 * 		providers over time)
	 * @return	true iff <code>te</code> should be checked
	 */
	public static boolean shouldBeChecked(TestElement te, int shift) {
		return ((constructNumber(te) + shift) % NUMBER_OF_STATES) > 1;
	}
	
	/**
	 * @param te
	 * @param shift	a parameter to change all check states
	 * 		to be different (use to simulate different
	 * 		providers over time)
	 * @return	true iff <code>te</code> should be grayed
	 */	
	public static boolean shouldBeGrayed(TestElement te, int shift) {
		return ((constructNumber(te) + shift) % NUMBER_OF_STATES) % 2 == 1;
	}
}

Back to the top