aboutsummaryrefslogtreecommitdiffstats
blob: 677e84e0431ac2329ec5e801c64a72b402c2fcb1 (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*******************************************************************************
 * 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
 *     Brock Janiczak  (brockj@tpg.com.au) - Bug 142960 Performance tweak for ignored file processing
 ******************************************************************************/
package org.eclipse.jface.tests.viewers;

import java.util.Vector;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.viewers.AbstractTreeViewer;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.IBasicPropertyConstants;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;

/**
 * @since 3.2
 *
 */
public abstract class ViewerComparatorTest extends ViewerTestCase {
	protected String UI = "UI";
	protected String[] TEAM1 = {"Karice", "Tod", "Eric", "Paul",
				"Mike", "Michael", "Andrea", "Kim", "Boris", "Susan"};
	protected String[] TEAM1_SORTED = {"Andrea", "Boris", "Eric", "Karice", "Kim",
			"Michael", "Mike", "Paul", "Susan", "Tod"};
	protected String[] TEAM1_SORTED_WITH_INSERT = {"Andrea", "Boris", "Duong", "Eric", "Karice", "Kim",
			"Michael", "Mike", "Paul", "Susan", "Tod"};

	protected String RUNTIME = "Runtime";
	protected String[] TEAM2 = {"Pascal", "DJ", "Jeff", "Andrew", "Oleg"};
	protected String[] TEAM2_SORTED = {"Andrew", "DJ", "Jeff", "Oleg", "Pascal"};

	protected String CORE = "Core";
	protected String[] TEAM3 = {"John", "Michael", "Bogdan"};
	protected String[] TEAM3_SORTED = {"Bogdan", "John", "Michael"};

	protected Team team1 = new Team(UI, TEAM1);
	protected Team team2 = new Team(RUNTIME, TEAM2);
	protected Team team3 = new Team(CORE, TEAM3);

	protected StructuredViewer<Object, Object> comparatorTestViewer;

	/*
	 * model object - parent
	 */
	protected class Team {
		Vector<IComparatorModelListener> fListeners = new Vector<IComparatorModelListener>();

		TeamMember[] members;
		String name;

		public Team(String name, String[] members){
			this.name = name;
			this.members = new TeamMember[members.length];
			for (int i = 0; i < members.length; i++)
				this.members[i] = new TeamMember(members[i], this);
		}

		public void addMember(String person){
			TeamMember newMember = new TeamMember(person, this);
			TeamMember[] newMembers = new TeamMember[members.length + 1];
			for (int i = 0; i < members.length; i++){
				newMembers[i] = members[i];
			}
			newMembers[newMembers.length - 1] = newMember;
			members = null;
			members = newMembers;
			newMembers = null;
			fireModelChanged(new ComparatorModelChange(TestModelChange.INSERT, this, newMember));
		}

	    public void addListener(IComparatorModelListener listener) {
	        fListeners.addElement(listener);
	    }

	    /**
	     * Fires a model changed event to all listeners.
	     */
	    public void fireModelChanged(ComparatorModelChange change) {
	        for (int i = 0; i < fListeners.size(); ++i) {
	        	IComparatorModelListener listener = fListeners
	                    .get(i);
	            listener.modelChanged(change);
	        }
	    }

	    public void removeListener(IComparatorModelListener listener) {
	        fListeners.removeElement(listener);
	    }
	}

	/*
	 * model object - child
	 */
	protected class TeamMember {
		String name;
		Team team;

		public TeamMember(String name, Team team){
			this.name = name;
			this.team = team;
		}
	}

	/*
	 * label provider
	 */
	protected class TeamModelLabelProvider extends LabelProvider<Object>{
		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
		 */
		public String getText(Object element) {
			if (element instanceof Team)
				return ((Team)element).name;
			else if (element instanceof TeamMember){
				return ((TeamMember)element).name;
			}
			return element.toString();
		}
	}

	/*
	 * content provider
	 */
	protected class TeamModelContentProvider implements IComparatorModelListener,IStructuredContentProvider<Object,Object>{
		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
		 */
		public Object[] getElements(Object inputElement) {
			if (inputElement instanceof Team){
				return ((Team)inputElement).members;
			}
			return new Object[0];
		}

		/* (non-Javadoc)
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 */
		public void dispose() {
		}

		/* (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) {
			if (oldInput != null){
				((Team) oldInput).removeListener(this);;
			}

	        if (newInput != null){
	        	((Team) newInput).addListener(this);
	        }
		}

	    public void modelChanged(ComparatorModelChange change) {
	        switch (change.getKind()) {
	        case TestModelChange.INSERT:
	            doInsert(change);
	            break;
	        case TestModelChange.REMOVE:
	            doRemove(change);
	            break;
	        case TestModelChange.STRUCTURE_CHANGE:
	            doStructureChange(change);
	            break;
	        case TestModelChange.NON_STRUCTURE_CHANGE:
	            doNonStructureChange(change);
	            break;
	        default:
	            throw new IllegalArgumentException("Unknown kind of change");
	        }

	        StructuredSelection selection = new StructuredSelection(change
	                .getChildren());
	        if ((change.getModifiers() & TestModelChange.SELECT) != 0) {
	            comparatorTestViewer.setSelection(selection);
	        }
	        if ((change.getModifiers() & TestModelChange.REVEAL) != 0) {
	            Object element = selection.getFirstElement();
	            if (element != null) {
	                comparatorTestViewer.reveal(element);
	            }
	        }
	    }

	    protected void doInsert(ComparatorModelChange change) {
	        if (comparatorTestViewer instanceof ListViewer) {
	            if (change.getParent() != null
	                    && change.getParent().equals(comparatorTestViewer.getInput())) {
	                ((ListViewer<Object,Object>) comparatorTestViewer).add(change.getChildren());
	            }
	        } else if (comparatorTestViewer instanceof TableViewer) {
	            if (change.getParent() != null
	                    && change.getParent().equals(comparatorTestViewer.getInput())) {
	                ((TableViewer<Object,Object>) comparatorTestViewer).add(change.getChildren());
	            }
	        } else if (comparatorTestViewer instanceof AbstractTreeViewer) {
	            ((AbstractTreeViewer<Object,Object>) comparatorTestViewer).add(change.getParent(), change
	                    .getChildren());
	        } else if (comparatorTestViewer instanceof ComboViewer) {
	            ((ComboViewer<Object,Object>) comparatorTestViewer).add(change.getChildren());
	        } else {
	            Assert.isTrue(false, "Unknown kind of viewer");
	        }
	    }

	    protected void doNonStructureChange(ComparatorModelChange change) {
           comparatorTestViewer.update(change.getParent(),
                    new String[] { IBasicPropertyConstants.P_TEXT });
	    }

	    protected void doRemove(ComparatorModelChange change) {
	        if (comparatorTestViewer instanceof ListViewer) {
	            ((ListViewer<Object,Object>) comparatorTestViewer).remove(change.getChildren());
	        } else if (comparatorTestViewer instanceof TableViewer) {
	            ((TableViewer<Object,Object>) comparatorTestViewer).remove(change.getChildren());
	        } else if (comparatorTestViewer instanceof AbstractTreeViewer) {
	            ((AbstractTreeViewer<Object,Object>) comparatorTestViewer).remove(change.getChildren());
	        } else if (comparatorTestViewer instanceof ComboViewer) {
	            ((ComboViewer<Object,Object>) comparatorTestViewer).remove(change.getChildren());
	        } else {
	            Assert.isTrue(false, "Unknown kind of viewer");
	        }
	    }

	    protected void doStructureChange(ComparatorModelChange change) {
            comparatorTestViewer.refresh(change.getParent());
	    }

	}

	public ViewerComparatorTest(String name) {
		super(name);
	}

}