public static void createParts( MApplication application, EModelService service, EPartService partService) { // Sometimes, when switching windows at startup, the active context // is null or doesn't have a window, and the part instantiation fails. // Ensure that a child context with a window is activated: IEclipseContext activeChild = application.getContext().getActiveChild(); if (activeChild == null || activeChild.get(MTrimmedWindow.class) == null) { boolean activated = false; if (application.getContext() instanceof EclipseContext) { for (IEclipseContext child : ((EclipseContext) application.getContext()).getChildren()) { MTrimmedWindow window = child.get(MTrimmedWindow.class); if (window != null) { child.activate(); activated = true; break; } } } if (!activated) { logger.error("Could not activate window for part instantiation"); // $NON-NLS-1$ return; } } List<MPart> ontops = new ArrayList<MPart>(); for (MPartDescriptor descriptor : application.getDescriptors()) { if (!(descriptor.getPersistedState().containsKey(VISIBLE_ID) && Boolean.toString(true) .equalsIgnoreCase(descriptor.getPersistedState().get(VISIBLE_ID)))) { continue; } List<MPart> existingParts = service.findElements(application, descriptor.getElementId(), MPart.class, null); if (!existingParts.isEmpty()) { // part is already instantiated continue; } MPart part = partService.createPart(descriptor.getElementId()); if (part == null) { continue; } addPartToAppropriateContainer(part, descriptor, application, service); partService.activate(part); if (descriptor.getPersistedState().containsKey(ONTOP_ID) && Boolean.toString(true) .equalsIgnoreCase(descriptor.getPersistedState().get(ONTOP_ID))) { ontops.add(part); } } // reactivate ontop parts to ensure they are on-top for (MPart ontop : ontops) { partService.activate(ontop); } }
@Test public void testFocusChangesOnExplicitPartActivation() { assertFalse(((PartBackend) part.getObject()).text1.isFocusControl()); eps.activate(part); processEvents(); assertTrue(((PartBackend) part.getObject()).text1.isFocusControl()); }
@Execute public void execute(EPartService partService, @Named("partId") String partId) { MPart part = partService.findPart(partId); if (part == null) { partService.createPart(partId); } partService.activate(part); }
private void activate(final MPart newPart) { partService.activate(newPart); }
@Before public void setUp() throws Exception { appContext = E4Application.createDefaultContext(); appContext.set(E4Workbench.PRESENTATION_URI_ARG, PartRenderingEngine.engineURI); ems = appContext.get(EModelService.class); window = ems.createModelElement(MWindow.class); window.setWidth(500); window.setHeight(500); MPartSashContainer sash = ems.createModelElement(MPartSashContainer.class); window.getChildren().add(sash); window.setSelectedElement(sash); MPartStack stack = ems.createModelElement(MPartStack.class); sash.getChildren().add(stack); sash.setSelectedElement(stack); part = ems.createModelElement(MPart.class); part.setElementId("Part"); part.setLabel("Part"); part.setToolbar(ems.createModelElement(MToolBar.class)); part.setContributionURI(Activator.asURI(PartBackend.class)); stack.getChildren().add(part); toolControl = ems.createModelElement(MToolControl.class); toolControl.setElementId("ToolControl"); toolControl.setContributionURI(Activator.asURI(TextField.class)); part.getToolbar().getChildren().add(toolControl); stack = ems.createModelElement(MPartStack.class); sash.getChildren().add(stack); sash.setSelectedElement(stack); otherPart = ems.createModelElement(MPart.class); otherPart.setElementId("OtherPart"); otherPart.setLabel("OtherPart"); otherPart.setContributionURI(Activator.asURI(PartBackend.class)); stack.getChildren().add(otherPart); MApplication application = ems.createModelElement(MApplication.class); application.getChildren().add(window); application.setContext(appContext); appContext.set(MApplication.class, application); wb = new E4Workbench(application, appContext); wb.createAndRunUI(window); eps = window.getContext().get(EPartService.class); // ensure the parts are populated and the contributions instantiated eps.activate(part); eps.activate(otherPart); processEvents(); // ensure our model backend objects are created assertNotNull(part.getObject()); assertNotNull(toolControl.getObject()); assertNotNull(otherPart.getObject()); assertNotNull(part.getWidget()); assertNotNull(toolControl.getWidget()); assertNotNull(otherPart.getWidget()); // ensure focus is set to otherPart.text1 eps.activate(otherPart); processEvents(); assertTrue(((PartBackend) otherPart.getObject()).text1.isFocusControl()); }