1 package org.simantics.ui.workbench.e4;
6 import org.eclipse.e4.core.contexts.IEclipseContext;
7 import org.eclipse.e4.ui.model.application.MApplication;
8 import org.eclipse.e4.ui.model.application.ui.MUIElement;
9 import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
10 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
13 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
14 import org.eclipse.e4.ui.workbench.modeling.EModelService;
15 import org.eclipse.e4.ui.workbench.modeling.EPartService;
16 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
17 import org.eclipse.ui.PlatformUI;
18 import org.simantics.db.Resource;
19 import org.simantics.utils.datastructures.ArrayMap;
22 * @author Tuukka Lehtonen
25 public class E4WorkbenchUtils {
27 private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };
32 private static final String TAG_EDITOR = "Editor";
35 * The Legacy IDE application ID for the "editor area" partstack. In E3
36 * compatibility mode legacy application the {@link MPartStack} lies under
37 * the shared {@link MArea} (org.eclipse.ui.editorss).
40 * In a pure E4 application this needs to be changed.
42 public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";
44 public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";
48 * @param contributionURI
53 public static MPart openEditor(String editorId, String contributionURI, String iconURI, String inputId, Map<String, Object> inputMap) {
55 throw new NullPointerException("null editor ID");
56 if (contributionURI == null)
57 throw new NullPointerException("null contributionURI");
59 throw new NullPointerException("null input ID");
61 throw new NullPointerException("null input map");
63 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
64 EModelService modelService = context.get(EModelService.class);
65 EPartService partService = context.get(EPartService.class);
66 MApplication app = context.get(MApplication.class);
68 String editorID = editorId + "_" + inputId;
70 List<MPart> parts = modelService.findElements(app, editorID, MPart.class, null);
72 if (parts == null || parts.isEmpty()) {
73 editor = modelService.createModelElement(MPart.class);
74 editor.setElementId(editorID);
75 editor.setContributionURI(contributionURI);
77 editor.setIconURI(iconURI);
78 editor.setCloseable(true);
79 editor.getTags().add(TAG_EDITOR);
80 editor.getTransientData().putAll(inputMap);
82 MPartStack stack = getEditorPartStack(modelService, app);
83 stack.getChildren().add(editor);
85 editor = parts.get(0);
88 partService.showPart(editor, PartState.ACTIVATE);
94 * @param contributionURI
98 public static MPart openEditor(String editorId, String contributionURI, String iconURI, Resource input) {
99 String inputId = Long.toString(input.getResourceId());
100 Map<String, Object> inputMap = ArrayMap.make(KEYS_RESOURCE, input);
101 return openEditor(editorId, contributionURI, iconURI, inputId, inputMap);
105 * @param editorElementId the unique element ID of the part (editor)
107 public static void closeEditor(String editorElementId) {
108 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
109 EModelService modelService = context.get(EModelService.class);
110 MApplication app = context.get(MApplication.class);
112 .findElements(app, editorElementId, MPart.class, null)
113 .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));
116 public static void activatePart(MPart part) {
117 EPartService partService = part.getContext().get(EPartService.class);
118 partService.activate(part);
121 public static MPartStack getEditorPartStack(EModelService modelService, MApplication app) {
122 MPartStack stack = (MPartStack) modelService.find(E4WorkbenchUtils.EDITOR_PART_STACK_ID, app);
124 MArea editorArea = null;
125 MUIElement area = modelService.find(E4WorkbenchUtils.EDITOR_AREA_ID, app);
126 if (area instanceof MPlaceholder) {
127 editorArea = (MArea) ((MPlaceholder) area).getRef();
128 } else if (area instanceof MArea) {
129 editorArea = (MArea) area;
131 if (editorArea == null)
132 throw new IllegalStateException("Shared editor area (" + E4WorkbenchUtils.EDITOR_AREA_ID + ") not found in application model");
133 for (MPartSashContainerElement container : editorArea.getChildren()) {
134 if (container instanceof MPartStack) {
135 stack = (MPartStack) container;
141 throw new IllegalStateException("Could not find part stack under editor area.");
145 public static void openAndShowPart(MPart part) {
146 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
147 EPartService partService = context.get(EPartService.class);
148 if (!partService.isPartVisible(part))
149 partService.showPart(part, PartState.ACTIVATE);
151 partService.activate(part);
155 public static void openAndShowPart(String partId) {
156 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
157 EPartService partService = context.get(EPartService.class);
158 partService.showPart(partId, PartState.ACTIVATE);
161 public static MPart getMPartById(String partId) {
162 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
163 EPartService partService = context.get(EPartService.class);
164 return partService.findPart(partId);