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