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.commands.MCommand;
9 import org.eclipse.e4.ui.model.application.ui.MUIElement;
10 import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
11 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
12 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
13 import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
14 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
15 import org.eclipse.e4.ui.workbench.modeling.EModelService;
16 import org.eclipse.e4.ui.workbench.modeling.EPartService;
17 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.PlatformUI;
20 import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
21 import org.simantics.db.Resource;
22 import org.simantics.utils.datastructures.ArrayMap;
23 import org.simantics.utils.ui.workbench.WorkbenchUtils;
26 * @author Tuukka Lehtonen
29 public class E4WorkbenchUtils {
31 private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };
36 private static final String TAG_EDITOR = "Editor";
39 * The Legacy IDE application ID for the "editor area" partstack. In E3
40 * compatibility mode legacy application the {@link MPartStack} lies under
41 * the shared {@link MArea} (org.eclipse.ui.editorss).
44 * In a pure E4 application this needs to be changed.
46 public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";
48 public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";
52 * @param contributionURI
57 public static MPart openEditor(String editorId, String contributionURI, String iconURI, String inputId, Map<String, Object> inputMap) {
59 throw new NullPointerException("null editor ID");
60 if (contributionURI == null)
61 throw new NullPointerException("null contributionURI");
63 throw new NullPointerException("null input ID");
65 throw new NullPointerException("null input map");
67 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
68 EModelService modelService = context.get(EModelService.class);
69 EPartService partService = context.get(EPartService.class);
70 MApplication app = context.get(MApplication.class);
72 String editorID = editorId + "_" + inputId;
74 List<MPart> parts = modelService.findElements(app, editorID, MPart.class, null);
76 if (parts == null || parts.isEmpty()) {
77 editor = modelService.createModelElement(MPart.class);
78 editor.setElementId(editorID);
79 editor.setContributionURI(contributionURI);
81 editor.setIconURI(iconURI);
82 editor.setCloseable(true);
83 editor.getTags().add(TAG_EDITOR);
84 editor.getTransientData().putAll(inputMap);
86 MPartStack stack = getEditorPartStack(modelService, app);
87 stack.getChildren().add(editor);
89 editor = parts.get(0);
92 partService.showPart(editor, PartState.ACTIVATE);
98 * @param contributionURI
102 public static MPart openEditor(String editorId, String contributionURI, String iconURI, Resource input) {
103 String inputId = Long.toString(input.getResourceId());
104 Map<String, Object> inputMap = ArrayMap.make(KEYS_RESOURCE, input);
105 return openEditor(editorId, contributionURI, iconURI, inputId, inputMap);
109 * @param editorElementId the unique element ID of the part (editor)
111 public static void closeEditor(String editorElementId) {
112 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
113 EModelService modelService = context.get(EModelService.class);
114 MApplication app = context.get(MApplication.class);
116 .findElements(app, editorElementId, MPart.class, null)
117 .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));
120 public static void activatePart(MPart part) {
121 EPartService partService = part.getContext().get(EPartService.class);
122 partService.activate(part);
125 public static MPartStack getEditorPartStack(EModelService modelService, MApplication app) {
126 MPartStack stack = (MPartStack) modelService.find(E4WorkbenchUtils.EDITOR_PART_STACK_ID, app);
128 MArea editorArea = null;
129 MUIElement area = modelService.find(E4WorkbenchUtils.EDITOR_AREA_ID, app);
130 if (area instanceof MPlaceholder) {
131 editorArea = (MArea) ((MPlaceholder) area).getRef();
132 } else if (area instanceof MArea) {
133 editorArea = (MArea) area;
135 if (editorArea == null)
136 throw new IllegalStateException("Shared editor area (" + E4WorkbenchUtils.EDITOR_AREA_ID + ") not found in application model");
137 for (MPartSashContainerElement container : editorArea.getChildren()) {
138 if (container instanceof MPartStack) {
139 stack = (MPartStack) container;
145 throw new IllegalStateException("Could not find part stack under editor area.");
149 public static void openAndShowPart(MPart part) {
150 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
151 EPartService partService = context.get(EPartService.class);
152 if (!partService.isPartVisible(part))
153 partService.showPart(part, PartState.ACTIVATE);
155 partService.activate(part);
159 public static void openAndShowPart(String partId) {
160 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
161 EPartService partService = context.get(EPartService.class);
162 partService.showPart(partId, PartState.ACTIVATE);
165 public static MPart getMPartById(String partId) {
166 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
167 EPartService partService = context.get(EPartService.class);
168 MPart part = partService.findPart(partId);
170 part = partService.createPart(partId);
174 public static MCommand getMCommandById(String id) {
175 IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
176 MApplication application = context.get(MApplication.class);
177 for (MCommand command : application.getCommands()) {
178 if (id.equals(command.getElementId())) {
185 @SuppressWarnings("restriction")
186 public static IEditorPart getActiveIEditorPart(MPart mActiveEditorPart) {
187 // TODO: Fix this when we get rid of CompatibilityEditors
188 IEditorPart activeEditor = null;
189 if (mActiveEditorPart != null) {
190 Object editor = mActiveEditorPart.getObject();
191 if (editor instanceof CompatibilityEditor) {
192 CompatibilityEditor compEditor = (CompatibilityEditor) editor;
193 activeEditor = compEditor.getEditor();
195 // TODO: This is not good practice with E4 but an OK fallback for now
196 activeEditor = WorkbenchUtils.getActiveEditor();