aboutsummaryrefslogtreecommitdiffstats
blob: 7f9ffdf4533e5d2bf6f1e03ec62439cdbc5a4c2e (plain)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*******************************************************************************
 * Copyright (c) 2006 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.Iterator;
import java.util.List;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

/**
 * @since 3.2
 *
 */
public class TreeViewerComparatorTest extends ViewerComparatorTest{

	protected class TeamModelTreeContentProvider extends TeamModelContentProvider
				implements ITreeContentProvider<Object,Object>{

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			return getChildren(inputElement);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
		 */
		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof List){
				List<Object> children = new ArrayList<Object>();
				Iterator<Object> iter = ((List<Object>)parentElement).iterator();
				while(iter.hasNext()){
					Object next = iter.next();
					if (next instanceof Team){
						Team team = (Team)next;
						children.add(team);
					}
				}
				return children.toArray(new Team[children.size()]);
			}
			else if (parentElement instanceof Team){
				return ((Team)parentElement).members;
			}
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
		 */
		public Object getParent(Object element) {
			if (element instanceof TeamMember){
				return ((TeamMember)element).team;
			}
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
		 */
		public boolean hasChildren(Object element) {
	        if (element instanceof Team) {
	            if (getChildren(element).length > 0) {
					return true;
				}
	        }
	        return false;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
		 */
		public void inputChanged(Viewer<Object> viewer, Object oldInput, Object newInput) {
			List<Object> oldElement = (List<Object>) oldInput;
	        if (oldElement != null) {
	        	Iterator<Object> iter = oldElement.iterator();
	        	while (iter.hasNext()){
	        		((Team)iter.next()).removeListener(this);
	        	}
	        }
	        List<Object> newElement = (List<Object>) newInput;
	        if (newElement != null) {
	        	Iterator<Object> iter = newElement.iterator();
	        	while (iter.hasNext()){
	        		((Team)iter.next()).addListener(this);
	        	}
	        }
		}
	}

	/**
	 * constructor
	 */
	public TreeViewerComparatorTest(String name) {
		super(name);
	}

	public void testViewerSorter(){
		comparatorTestViewer.setSorter(new ViewerSorter());
		getTreeViewer().expandAll();
		String[][] expected = {TEAM3_SORTED, TEAM2_SORTED, TEAM1_SORTED};
		assertSortedResult(expected);
	}

	public void testViewerSorterInsertElement(){
		comparatorTestViewer.setSorter(new ViewerSorter());
		getTreeViewer().expandAll();
		team1.addMember("Duong");
		String[][] expected = {TEAM3_SORTED, TEAM2_SORTED, TEAM1_SORTED_WITH_INSERT};
		assertSortedResult(expected);
	}

	public void testViewerComparator(){
		comparatorTestViewer.setComparator(new ViewerComparator<Object,Object>());
		getTreeViewer().expandAll();
		String[][] expected = {TEAM3_SORTED, TEAM2_SORTED, TEAM1_SORTED};
		assertSortedResult(expected);
	}

	public void testViewerComparatorInsertElement(){
		comparatorTestViewer.setComparator(new ViewerComparator<Object,Object>());
		getTreeViewer().expandAll();
		team1.addMember("Duong");
		String[][] expected = {TEAM3_SORTED, TEAM2_SORTED, TEAM1_SORTED_WITH_INSERT};
		assertSortedResult(expected);
	}

	private void assertSortedResult(String[][] resultArrays){
		TreeItem[] rootItems = getTreeViewer().getTree().getItems();
		assertEquals("Number of root items in tree not correct (actual=" + rootItems.length + ")", 3, rootItems.length);
		TreeItem item = rootItems[0];
		assertEquals("Item not expected.  actual=" + item.getText() + " expected=" + CORE, CORE, item.getText());
		item = rootItems[1];
		assertEquals("Item not expected.  actual=" + item.getText() + " expected=" + RUNTIME, RUNTIME, item.getText());
		item = rootItems[2];
		assertEquals("Item not expected.  actual=" + item.getText() + " expected=" + UI, UI, item.getText());

		TreeItem[] childItems = rootItems[0].getItems();
		for (int i = 0; i < childItems.length; i++){
			TreeItem child = childItems[i];
			String result = child.getText();
			assertEquals("", resultArrays[0][i], result);
		}
		childItems = rootItems[1].getItems();
		for (int i = 0; i < childItems.length; i++){
			TreeItem child = childItems[i];
			String result = child.getText();
			assertEquals("", resultArrays[1][i], result);
		}
		childItems = rootItems[2].getItems();
		for (int i = 0; i < childItems.length; i++){
			TreeItem child = childItems[i];
			String result = child.getText();
			assertEquals("", resultArrays[2][i], result);
		}
	}

	protected TreeViewer<Object,Object> getTreeViewer(){
		return (TreeViewer<Object,Object>)comparatorTestViewer;
	}

	protected StructuredViewer createViewer(Composite parent) {
		Tree tree = new Tree(fShell, SWT.SINGLE | SWT.H_SCROLL
                | SWT.V_SCROLL | SWT.BORDER);
		TreeViewer<Object,Object> viewer = new TreeViewer<Object,Object>(tree);
		viewer.setContentProvider(new TeamModelTreeContentProvider());
		viewer.setLabelProvider(new TeamModelLabelProvider());
		comparatorTestViewer = viewer;
		return viewer;
	}

	protected void setInput() {
		List<Object> input = new ArrayList<Object>(3);
		input.add(team1);
		input.add(team2);
		input.add(team3);
		comparatorTestViewer.setInput(input);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(TreeViewerComparatorTest.class);
	}

}