Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 295003 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/e4/ui/tests/application/EPartServiceTest.java (+367 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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.e4.ui.tests.application;
12
13
import java.util.Collection;
14
15
import junit.framework.TestCase;
16
17
import org.eclipse.e4.core.services.IContributionFactory;
18
import org.eclipse.e4.core.services.IDisposable;
19
import org.eclipse.e4.core.services.context.IEclipseContext;
20
import org.eclipse.e4.ui.model.application.MApplication;
21
import org.eclipse.e4.ui.model.application.MApplicationFactory;
22
import org.eclipse.e4.ui.model.application.MPart;
23
import org.eclipse.e4.ui.model.application.MPartStack;
24
import org.eclipse.e4.ui.model.application.MWindow;
25
import org.eclipse.e4.ui.workbench.swt.internal.E4Application;
26
import org.eclipse.e4.workbench.modeling.EPartService;
27
import org.eclipse.e4.workbench.ui.IPresentationEngine;
28
29
public class EPartServiceTest extends TestCase {
30
31
	private IEclipseContext applicationContext;
32
33
	private IPresentationEngine engine;
34
35
	@Override
36
	protected void setUp() throws Exception {
37
		applicationContext = E4Application.createDefaultContext();
38
39
		IContributionFactory contributionFactory = (IContributionFactory) applicationContext
40
				.get(IContributionFactory.class.getName());
41
		Object newEngine = contributionFactory.create(getEngineURI(),
42
				applicationContext);
43
		assertTrue(newEngine instanceof IPresentationEngine);
44
		applicationContext.set(IPresentationEngine.class.getName(), newEngine);
45
46
		engine = (IPresentationEngine) newEngine;
47
48
		super.setUp();
49
	}
50
51
	protected String getEngineURI() {
52
		return "platform:/plugin/org.eclipse.e4.ui.tests/org.eclipse.e4.ui.tests.application.HeadlessContextPresentationEngine"; //$NON-NLS-1$
53
	}
54
55
	@Override
56
	protected void tearDown() throws Exception {
57
		super.tearDown();
58
		if (applicationContext instanceof IDisposable) {
59
			((IDisposable) applicationContext).dispose();
60
		}
61
	}
62
63
	public void testFindPart_PartInWindow() {
64
		MApplication application = createApplication("partId");
65
66
		MWindow window = application.getChildren().get(0);
67
		engine.createGui(window);
68
69
		EPartService partService = (EPartService) window.getContext().get(
70
				EPartService.class.getName());
71
		MPart part = partService.findPart("partId");
72
		assertNotNull(part);
73
74
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
75
		assertEquals(partStack.getChildren().get(0), part);
76
77
		part = partService.findPart("invalidPartId");
78
		assertNull(part);
79
	}
80
81
	public void testFindPart_PartNotInWindow() {
82
		MApplication application = createApplication("partId");
83
84
		MWindow window = application.getChildren().get(0);
85
		engine.createGui(window);
86
87
		EPartService partService = (EPartService) window.getContext().get(
88
				EPartService.class.getName());
89
		MPart part = partService.findPart("invalidPartId");
90
		assertNull(part);
91
	}
92
93
	public void testFindPart_PartInAnotherWindow() {
94
		MApplication application = createApplication(
95
				new String[] { "partInWindow1" },
96
				new String[] { "partInWindow2" });
97
98
		MWindow window1 = application.getChildren().get(0);
99
		MWindow window2 = application.getChildren().get(1);
100
101
		engine.createGui(window1);
102
		engine.createGui(window2);
103
104
		EPartService partService = (EPartService) window1.getContext().get(
105
				EPartService.class.getName());
106
		MPart part = partService.findPart("partInWindow2");
107
		assertNull(part);
108
		part = partService.findPart("partInWindow1");
109
		assertNotNull(part);
110
111
		MPartStack partStack = (MPartStack) window1.getChildren().get(0);
112
		assertEquals(partStack.getChildren().get(0), part);
113
114
		partService = (EPartService) window2.getContext().get(
115
				EPartService.class.getName());
116
		part = partService.findPart("partInWindow1");
117
		assertNull(part);
118
		part = partService.findPart("partInWindow2");
119
		assertNotNull(part);
120
121
		partStack = (MPartStack) window2.getChildren().get(0);
122
		assertEquals(partStack.getChildren().get(0), part);
123
	}
124
125
	public void testBringToTop_PartOnTop() {
126
		MApplication application = createApplication("partFront", "partBack");
127
128
		MWindow window = application.getChildren().get(0);
129
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
130
		MPart partFront = partStack.getChildren().get(0);
131
		partStack.setActiveChild(partFront);
132
133
		engine.createGui(window);
134
135
		EPartService partService = (EPartService) window.getContext().get(
136
				EPartService.class.getName());
137
138
		partService.bringToTop(partFront);
139
		assertEquals(partStack.getActiveChild(), partFront);
140
	}
141
142
	public void testBringToTop_PartNotOnTop() {
143
		MApplication application = createApplication("partFront", "partBack");
144
145
		MWindow window = application.getChildren().get(0);
146
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
147
		MPart partFront = partStack.getChildren().get(0);
148
		MPart partBack = partStack.getChildren().get(1);
149
		partStack.setActiveChild(partFront);
150
151
		engine.createGui(window);
152
153
		EPartService partService = (EPartService) window.getContext().get(
154
				EPartService.class.getName());
155
156
		partService.bringToTop(partBack);
157
		assertEquals(partStack.getActiveChild(), partBack);
158
	}
159
160
	public void testBringToTop_PartInAnotherWindow() {
161
		MApplication application = createApplication(new String[] {
162
				"partFrontA", "partBackA" }, new String[] { "partFrontB",
163
				"partBackB" });
164
165
		MWindow windowA = application.getChildren().get(0);
166
		MPartStack partStackA = (MPartStack) windowA.getChildren().get(0);
167
		MPart partFrontA = partStackA.getChildren().get(0);
168
		MPart partBackA = partStackA.getChildren().get(1);
169
		partStackA.setActiveChild(partFrontA);
170
171
		MWindow windowB = application.getChildren().get(1);
172
		MPartStack partStackB = (MPartStack) windowB.getChildren().get(0);
173
		MPart partFrontB = partStackB.getChildren().get(0);
174
		MPart partBackB = partStackB.getChildren().get(1);
175
		partStackB.setActiveChild(partFrontB);
176
177
		engine.createGui(windowA);
178
		engine.createGui(windowB);
179
180
		EPartService partServiceA = (EPartService) windowA.getContext().get(
181
				EPartService.class.getName());
182
		EPartService partServiceB = (EPartService) windowB.getContext().get(
183
				EPartService.class.getName());
184
185
		partServiceA.bringToTop(partBackB);
186
		assertEquals(partStackA.getActiveChild(), partFrontA);
187
		assertEquals(partStackB.getActiveChild(), partFrontB);
188
189
		partServiceB.bringToTop(partBackA);
190
		assertEquals(partStackA.getActiveChild(), partFrontA);
191
		assertEquals(partStackB.getActiveChild(), partFrontB);
192
193
		partServiceA.bringToTop(partBackA);
194
		assertEquals(partStackA.getActiveChild(), partBackA);
195
		assertEquals(partStackB.getActiveChild(), partFrontB);
196
197
		partServiceB.bringToTop(partBackB);
198
		assertEquals(partStackA.getActiveChild(), partBackA);
199
		assertEquals(partStackB.getActiveChild(), partBackB);
200
	}
201
202
	public void testGetParts_Empty() {
203
		MApplication application = createApplication(1, new String[1][0]);
204
		MWindow window = application.getChildren().get(0);
205
206
		engine.createGui(window);
207
208
		EPartService partService = (EPartService) window.getContext().get(
209
				EPartService.class.getName());
210
		Collection<MPart> parts = partService.getParts();
211
		assertNotNull(parts);
212
		assertEquals(0, parts.size());
213
	}
214
215
	public void testGetParts_OneWindow() {
216
		MApplication application = createApplication("partId", "partId2");
217
		MWindow window = application.getChildren().get(0);
218
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
219
220
		engine.createGui(window);
221
222
		EPartService partService = (EPartService) window.getContext().get(
223
				EPartService.class.getName());
224
		Collection<MPart> parts = partService.getParts();
225
		assertNotNull(parts);
226
		assertEquals(2, parts.size());
227
		assertTrue(parts.containsAll(partStack.getChildren()));
228
	}
229
230
	public void testGetParts_TwoWindows() {
231
		MApplication application = createApplication(new String[] { "partId",
232
				"partId2" }, new String[] { "partIA", "partIdB", "partIdC" });
233
234
		MWindow windowA = application.getChildren().get(0);
235
		MWindow windowB = application.getChildren().get(1);
236
237
		engine.createGui(windowA);
238
		engine.createGui(windowB);
239
240
		EPartService partServiceA = (EPartService) windowA.getContext().get(
241
				EPartService.class.getName());
242
		EPartService partServiceB = (EPartService) windowB.getContext().get(
243
				EPartService.class.getName());
244
245
		MPartStack partStackA = (MPartStack) windowA.getChildren().get(0);
246
		MPartStack partStackB = (MPartStack) windowB.getChildren().get(0);
247
248
		Collection<MPart> partsA = partServiceA.getParts();
249
		Collection<MPart> partsB = partServiceB.getParts();
250
251
		assertNotNull(partsA);
252
		assertEquals(2, partsA.size());
253
		assertTrue(partsA.containsAll(partStackA.getChildren()));
254
255
		assertNotNull(partsB);
256
		assertEquals(3, partsB.size());
257
		assertTrue(partsB.containsAll(partStackB.getChildren()));
258
259
		for (MPart partA : partsA) {
260
			assertFalse(partsB.contains(partA));
261
		}
262
	}
263
264
	public void testIsVisible_ViewVisible() {
265
		MApplication application = createApplication("partId");
266
267
		MWindow window = application.getChildren().get(0);
268
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
269
		MPart part = partStack.getChildren().get(0);
270
		partStack.setActiveChild(part);
271
272
		engine.createGui(window);
273
274
		EPartService partService = (EPartService) window.getContext().get(
275
				EPartService.class.getName());
276
		assertTrue(partService.isPartVisible(part));
277
	}
278
279
	public void testIsVisible_ViewNotVisible() {
280
		MApplication application = createApplication("partId", "partId2");
281
282
		MWindow window = application.getChildren().get(0);
283
		MPartStack partStack = (MPartStack) window.getChildren().get(0);
284
		partStack.setActiveChild(partStack.getChildren().get(0));
285
286
		engine.createGui(window);
287
288
		MPart part = partStack.getChildren().get(1);
289
290
		EPartService partService = (EPartService) window.getContext().get(
291
				EPartService.class.getName());
292
		assertFalse(partService.isPartVisible(part));
293
	}
294
295
	public void testIsVisible_ViewInAnotherWindow() {
296
		MApplication application = createApplication(new String[] {
297
				"partFrontA", "partBackA" }, new String[] { "partFrontB",
298
				"partBackB" });
299
300
		MWindow windowA = application.getChildren().get(0);
301
		MPartStack partStackA = (MPartStack) windowA.getChildren().get(0);
302
		MPart partFrontA = partStackA.getChildren().get(0);
303
		MPart partBackA = partStackA.getChildren().get(1);
304
		partStackA.setActiveChild(partFrontA);
305
306
		MWindow windowB = application.getChildren().get(1);
307
		MPartStack partStackB = (MPartStack) windowB.getChildren().get(0);
308
		MPart partFrontB = partStackB.getChildren().get(0);
309
		MPart partBackB = partStackB.getChildren().get(1);
310
		partStackB.setActiveChild(partFrontB);
311
312
		engine.createGui(windowA);
313
		engine.createGui(windowB);
314
315
		EPartService partServiceA = (EPartService) windowA.getContext().get(
316
				EPartService.class.getName());
317
		EPartService partServiceB = (EPartService) windowB.getContext().get(
318
				EPartService.class.getName());
319
320
		assertTrue(partServiceA.isPartVisible(partFrontA));
321
		assertFalse(partServiceA.isPartVisible(partBackA));
322
		assertFalse(partServiceA.isPartVisible(partFrontB));
323
		assertFalse(partServiceA.isPartVisible(partBackB));
324
325
		assertFalse(partServiceB.isPartVisible(partFrontA));
326
		assertFalse(partServiceB.isPartVisible(partBackA));
327
		assertTrue(partServiceB.isPartVisible(partFrontB));
328
		assertFalse(partServiceB.isPartVisible(partBackB));
329
	}
330
331
	private MApplication createApplication(String partId) {
332
		return createApplication(new String[] { partId });
333
	}
334
335
	private MApplication createApplication(String... partIds) {
336
		return createApplication(new String[][] { partIds });
337
	}
338
339
	private MApplication createApplication(String[]... partIds) {
340
		return createApplication(partIds.length, partIds);
341
	}
342
343
	private MApplication createApplication(int windows, String[][] partIds) {
344
		MApplication application = MApplicationFactory.eINSTANCE
345
				.createApplication();
346
347
		for (int i = 0; i < windows; i++) {
348
			MWindow window = MApplicationFactory.eINSTANCE.createWindow();
349
			application.getChildren().add(window);
350
351
			MPartStack partStack = MApplicationFactory.eINSTANCE
352
					.createPartStack();
353
			window.getChildren().add(partStack);
354
355
			for (int j = 0; j < partIds[i].length; j++) {
356
				MPart part = MApplicationFactory.eINSTANCE.createPart();
357
				part.setId(partIds[i][j]);
358
				partStack.getChildren().add(part);
359
			}
360
		}
361
362
		applicationContext.set(MApplication.class.getName(), application);
363
		application.setContext(applicationContext);
364
365
		return application;
366
	}
367
}
(-)src/org/eclipse/e4/workbench/modeling/EPartService.java (+34 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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.e4.workbench.modeling;
12
13
import java.util.Collection;
14
import org.eclipse.e4.ui.model.application.MPart;
15
16
public interface EPartService {
17
18
	// public MPart activate(String id);
19
20
	// public void activate(MPart part);
21
22
	public void bringToTop(MPart part);
23
24
	public MPart findPart(String id);
25
26
	public Collection<MPart> getParts();
27
28
	public boolean isPartVisible(MPart part);
29
30
	// public MPart showPart(String id);
31
	//
32
	// public MPart showPart(MPart part);
33
34
}
(-)src/org/eclipse/e4/workbench/ui/internal/EPartServiceImpl.java (+115 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2009 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.e4.workbench.ui.internal;
12
13
import java.util.ArrayList;
14
import java.util.Collection;
15
import javax.inject.Inject;
16
import org.eclipse.e4.ui.model.application.MContext;
17
import org.eclipse.e4.ui.model.application.MElementContainer;
18
import org.eclipse.e4.ui.model.application.MPart;
19
import org.eclipse.e4.ui.model.application.MUIElement;
20
import org.eclipse.e4.workbench.modeling.EPartService;
21
22
public class EPartServiceImpl implements EPartService {
23
24
	@Inject
25
	private MElementContainer<MUIElement> elementContainer;
26
27
	protected MContext getParentWithContext(MUIElement part) {
28
		MElementContainer<MUIElement> parent = part.getParent();
29
		while (parent != null) {
30
			if (parent instanceof MContext) {
31
				if (((MContext) parent).getContext() != null)
32
					return (MContext) parent;
33
			}
34
			parent = parent.getParent();
35
		}
36
		return null;
37
	}
38
39
	public void bringToTop(MPart part) {
40
		if (isInContainer(part)) {
41
			internalBringToTop(part);
42
		}
43
	}
44
45
	private void internalBringToTop(MPart part) {
46
		MElementContainer<MUIElement> parent = part.getParent();
47
		if (parent.getActiveChild() != part) {
48
			parent.setActiveChild(part);
49
		}
50
	}
51
52
	public MPart findPart(String id) {
53
		return findPart(elementContainer, id);
54
	}
55
56
	public Collection<MPart> getParts() {
57
		return getParts(new ArrayList<MPart>(), elementContainer);
58
	}
59
60
	private Collection<MPart> getParts(Collection<MPart> parts,
61
			MElementContainer<?> elementContainer) {
62
		for (Object child : elementContainer.getChildren()) {
63
			if (child instanceof MPart) {
64
				parts.add((MPart) child);
65
			} else if (child instanceof MElementContainer<?>) {
66
				getParts(parts, (MElementContainer<?>) child);
67
			}
68
		}
69
		return parts;
70
	}
71
72
	public boolean isPartVisible(MPart part) {
73
		if (isInContainer(part)) {
74
			MElementContainer<MUIElement> parent = part.getParent();
75
			return parent.getActiveChild() == part;
76
		}
77
		return false;
78
	}
79
80
	private MPart findPart(MElementContainer<?> container, String id) {
81
		for (Object object : container.getChildren()) {
82
			if (object instanceof MPart) {
83
				MPart part = (MPart) object;
84
				if (id.equals(part.getId())) {
85
					return part;
86
				}
87
			} else if (object instanceof MElementContainer<?>) {
88
				MPart part = findPart((MElementContainer<?>) object, id);
89
				if (part != null) {
90
					return part;
91
				}
92
			}
93
		}
94
95
		return null;
96
	}
97
98
	private boolean isInContainer(MPart part) {
99
		return isInContainer(elementContainer, part);
100
	}
101
102
	private boolean isInContainer(MElementContainer<?> container, MPart part) {
103
		for (Object object : container.getChildren()) {
104
			if (object == part) {
105
				return true;
106
			} else if (object instanceof MElementContainer<?>) {
107
				if (isInContainer((MElementContainer<?>) object, part)) {
108
					return true;
109
				}
110
			}
111
		}
112
113
		return false;
114
	}
115
}

Return to bug 295003