]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4WorkbenchUtils.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java b/bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java
new file mode 100644 (file)
index 0000000..217185d
--- /dev/null
@@ -0,0 +1,145 @@
+package org.simantics.ui.workbench.e4;\r
+\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import org.eclipse.e4.core.contexts.IEclipseContext;\r
+import org.eclipse.e4.ui.model.application.MApplication;\r
+import org.eclipse.e4.ui.model.application.ui.MUIElement;\r
+import org.eclipse.e4.ui.model.application.ui.advanced.MArea;\r
+import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;\r
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;\r
+import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;\r
+import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;\r
+import org.eclipse.e4.ui.workbench.modeling.EModelService;\r
+import org.eclipse.e4.ui.workbench.modeling.EPartService;\r
+import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.db.Resource;\r
+import org.simantics.utils.datastructures.ArrayMap;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ *\r
+ */\r
+public class E4WorkbenchUtils {\r
+\r
+    private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };\r
+\r
+    /**\r
+     * \r
+     */\r
+    private static final String TAG_EDITOR = "Editor";\r
+\r
+    /**\r
+     * The Legacy IDE application ID for the "editor area" partstack. In E3\r
+     * compatibility mode legacy application the {@link MPartStack} lies under\r
+     * the shared {@link MArea} (org.eclipse.ui.editorss).\r
+     * \r
+     * <p>\r
+     * In a pure E4 application this needs to be changed.\r
+     */\r
+    public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";\r
+\r
+    public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";\r
+\r
+    /**\r
+     * @param editorId\r
+     * @param contributionURI\r
+     * @param iconURI\r
+     * @param inputId\r
+     * @param inputMap\r
+     */\r
+    public static MPart openEditor(String editorId, String contributionURI, String iconURI, String inputId, Map<String, Object> inputMap) {\r
+        if (editorId == null)\r
+            throw new NullPointerException("null editor ID");\r
+        if (contributionURI == null)\r
+            throw new NullPointerException("null contributionURI");\r
+        if (inputId == null)\r
+            throw new NullPointerException("null input ID");\r
+        if (inputMap == null)\r
+            throw new NullPointerException("null input map");\r
+\r
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
+        EModelService modelService = context.get(EModelService.class);\r
+        EPartService partService = context.get(EPartService.class);\r
+        MApplication app = context.get(MApplication.class);\r
+\r
+        String editorID = editorId + "_" + inputId;\r
+\r
+        List<MPart> parts = modelService.findElements(app, editorID, MPart.class, null);\r
+        MPart editor = null;\r
+        if (parts == null || parts.isEmpty()) {\r
+            editor = modelService.createModelElement(MPart.class);\r
+            editor.setElementId(editorID);\r
+            editor.setContributionURI(contributionURI);\r
+            if (iconURI != null)\r
+                editor.setIconURI(iconURI);\r
+            editor.setCloseable(true);\r
+            editor.getTags().add(TAG_EDITOR);\r
+            editor.getTransientData().putAll(inputMap);\r
+\r
+            MPartStack stack = getEditorPartStack(modelService, app);\r
+            stack.getChildren().add(editor);\r
+        } else {\r
+            editor = parts.get(0);\r
+        }\r
+\r
+        partService.showPart(editor, PartState.ACTIVATE);\r
+        return editor;\r
+    }\r
+\r
+    /**\r
+     * @param editorId\r
+     * @param contributionURI\r
+     * @param iconURI\r
+     * @param input\r
+     */\r
+    public static MPart openEditor(String editorId, String contributionURI, String iconURI, Resource input) {\r
+        String inputId = Long.toString(input.getResourceId());\r
+        Map<String, Object> inputMap = ArrayMap.make(KEYS_RESOURCE, input);\r
+        return openEditor(editorId, contributionURI, iconURI, inputId, inputMap);\r
+    }\r
+\r
+    /**\r
+     * @param editorElementId the unique element ID of the part (editor)\r
+     */\r
+    public static void closeEditor(String editorElementId) {\r
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
+        EModelService modelService = context.get(EModelService.class);\r
+        MApplication app = context.get(MApplication.class);\r
+        modelService\r
+        .findElements(app, editorElementId, MPart.class, null)\r
+        .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));\r
+    }\r
+\r
+    public static void activatePart(MPart part) {\r
+        EPartService partService = part.getContext().get(EPartService.class);\r
+        partService.activate(part);\r
+    }\r
+\r
+    public static MPartStack getEditorPartStack(EModelService modelService, MApplication app) {\r
+        MPartStack stack = (MPartStack) modelService.find(E4WorkbenchUtils.EDITOR_PART_STACK_ID, app);\r
+        if (stack == null) {\r
+            MArea editorArea = null;\r
+            MUIElement area = modelService.find(E4WorkbenchUtils.EDITOR_AREA_ID, app);\r
+            if (area instanceof MPlaceholder) {\r
+                editorArea = (MArea) ((MPlaceholder) area).getRef();\r
+            } else if (area instanceof MArea) {\r
+                editorArea = (MArea) area;\r
+            }\r
+            if (editorArea == null)\r
+                throw new IllegalStateException("Shared editor area (" + E4WorkbenchUtils.EDITOR_AREA_ID + ") not found in application model");\r
+            for (MPartSashContainerElement container : editorArea.getChildren()) {\r
+                if (container instanceof MPartStack) {\r
+                    stack = (MPartStack) container;\r
+                    break;\r
+                }\r
+            }\r
+        }\r
+        if (stack == null)\r
+            throw new IllegalStateException("Could not find part stack under editor area.");\r
+        return stack;\r
+    }\r
+\r
+}\r