]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java
Combination of Simantics-platform related changes and fixes for district
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4WorkbenchUtils.java
index 901772ba7b68f88a8f25e3d7c7a01b6c13794c7d..6d7daeb3c6ff191f8e04e444240bf99776f699d3 100644 (file)
-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
-    public static void openAndShowPart(MPart part) {\r
-        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
-        EPartService partService = context.get(EPartService.class);\r
-        if (!partService.isPartVisible(part))\r
-            partService.showPart(part, PartState.ACTIVATE);\r
-        else\r
-            partService.activate(part);\r
-    }\r
-\r
-    \r
-    public static void openAndShowPart(String partId) {\r
-        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
-        EPartService partService = context.get(EPartService.class);\r
-        partService.showPart(partId, PartState.ACTIVATE);\r
-    }\r
-\r
-    public static MPart getMPartById(String partId) {\r
-        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
-        EPartService partService = context.get(EPartService.class);\r
-        return partService.findPart(partId);\r
-    }\r
-\r
-}\r
+package org.simantics.ui.workbench.e4;
+
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.ui.model.application.MApplication;
+import org.eclipse.e4.ui.model.application.commands.MCommand;
+import org.eclipse.e4.ui.model.application.ui.MUIElement;
+import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
+import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
+import org.eclipse.e4.ui.model.application.ui.basic.MPart;
+import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
+import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
+import org.eclipse.e4.ui.workbench.modeling.EModelService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService;
+import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.internal.e4.compatibility.CompatibilityEditor;
+import org.simantics.db.Resource;
+import org.simantics.utils.datastructures.ArrayMap;
+import org.simantics.utils.ui.workbench.WorkbenchUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ *
+ */
+public class E4WorkbenchUtils {
+
+    private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };
+
+    /**
+     * 
+     */
+    private static final String TAG_EDITOR = "Editor";
+
+    /**
+     * The Legacy IDE application ID for the "editor area" partstack. In E3
+     * compatibility mode legacy application the {@link MPartStack} lies under
+     * the shared {@link MArea} (org.eclipse.ui.editorss).
+     * 
+     * <p>
+     * In a pure E4 application this needs to be changed.
+     */
+    public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";
+
+    public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";
+
+    /**
+     * @param editorId
+     * @param contributionURI
+     * @param iconURI
+     * @param inputId
+     * @param inputMap
+     */
+    public static MPart openEditor(String editorId, String contributionURI, String iconURI, String inputId, Map<String, Object> inputMap) {
+        if (editorId == null)
+            throw new NullPointerException("null editor ID");
+        if (contributionURI == null)
+            throw new NullPointerException("null contributionURI");
+        if (inputId == null)
+            throw new NullPointerException("null input ID");
+        if (inputMap == null)
+            throw new NullPointerException("null input map");
+
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        EModelService modelService = context.get(EModelService.class);
+        EPartService partService = context.get(EPartService.class);
+        MApplication app = context.get(MApplication.class);
+
+        String editorID = editorId + "_" + inputId;
+
+        List<MPart> parts = modelService.findElements(app, editorID, MPart.class, null);
+        MPart editor = null;
+        if (parts == null || parts.isEmpty()) {
+            editor = modelService.createModelElement(MPart.class);
+            editor.setElementId(editorID);
+            editor.setContributionURI(contributionURI);
+            if (iconURI != null)
+                editor.setIconURI(iconURI);
+            editor.setCloseable(true);
+            editor.getTags().add(TAG_EDITOR);
+            editor.getTransientData().putAll(inputMap);
+
+            MPartStack stack = getEditorPartStack(modelService, app);
+            stack.getChildren().add(editor);
+        } else {
+            editor = parts.get(0);
+        }
+
+        partService.showPart(editor, PartState.ACTIVATE);
+        return editor;
+    }
+
+    /**
+     * @param editorId
+     * @param contributionURI
+     * @param iconURI
+     * @param input
+     */
+    public static MPart openEditor(String editorId, String contributionURI, String iconURI, Resource input) {
+        String inputId = Long.toString(input.getResourceId());
+        Map<String, Object> inputMap = ArrayMap.make(KEYS_RESOURCE, input);
+        return openEditor(editorId, contributionURI, iconURI, inputId, inputMap);
+    }
+
+    /**
+     * @param editorElementId the unique element ID of the part (editor)
+     */
+    public static void closeEditor(String editorElementId) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        EModelService modelService = context.get(EModelService.class);
+        MApplication app = context.get(MApplication.class);
+        modelService
+        .findElements(app, editorElementId, MPart.class, null)
+        .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));
+    }
+
+    public static void activatePart(MPart part) {
+        EPartService partService = part.getContext().get(EPartService.class);
+        partService.activate(part);
+    }
+
+    public static MPartStack getEditorPartStack(EModelService modelService, MApplication app) {
+        MPartStack stack = (MPartStack) modelService.find(E4WorkbenchUtils.EDITOR_PART_STACK_ID, app);
+        if (stack == null) {
+            MArea editorArea = null;
+            MUIElement area = modelService.find(E4WorkbenchUtils.EDITOR_AREA_ID, app);
+            if (area instanceof MPlaceholder) {
+                editorArea = (MArea) ((MPlaceholder) area).getRef();
+            } else if (area instanceof MArea) {
+                editorArea = (MArea) area;
+            }
+            if (editorArea == null)
+                throw new IllegalStateException("Shared editor area (" + E4WorkbenchUtils.EDITOR_AREA_ID + ") not found in application model");
+            for (MPartSashContainerElement container : editorArea.getChildren()) {
+                if (container instanceof MPartStack) {
+                    stack = (MPartStack) container;
+                    break;
+                }
+            }
+        }
+        if (stack == null)
+            throw new IllegalStateException("Could not find part stack under editor area.");
+        return stack;
+    }
+
+    public static void openAndShowPart(MPart part) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        EPartService partService = context.get(EPartService.class);
+        if (!partService.isPartVisible(part))
+            partService.showPart(part, PartState.ACTIVATE);
+        else
+            partService.activate(part);
+    }
+
+    
+    public static void openAndShowPart(String partId) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        EPartService partService = context.get(EPartService.class);
+        partService.showPart(partId, PartState.ACTIVATE);
+    }
+
+    public static MPart getMPartById(String partId) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        EPartService partService = context.get(EPartService.class);
+        MPart part = partService.findPart(partId);
+        if (part == null)
+            part = partService.createPart(partId);
+        return part;
+    }
+    
+    public static MCommand getMCommandById(String id) {
+        IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
+        MApplication application = context.get(MApplication.class);
+        for (MCommand command : application.getCommands()) {
+            if (id.equals(command.getElementId())) {
+                return command;
+            }
+        }
+        return null;
+    }
+
+    @SuppressWarnings("restriction")
+    public static IEditorPart getActiveIEditorPart(MPart mActiveEditorPart) {
+        // TODO: Fix this when we get rid of CompatibilityEditors
+        IEditorPart activeEditor = null;
+        if (mActiveEditorPart != null) {
+            Object editor = mActiveEditorPart.getObject();
+            if (editor instanceof CompatibilityEditor) {
+                CompatibilityEditor compEditor = (CompatibilityEditor) editor;
+                activeEditor = compEditor.getEditor();
+            } else {
+                // TODO: This is not good practice with E4 but an OK fallback for now
+                activeEditor = WorkbenchUtils.getActiveEditor();
+            }
+        }
+        return activeEditor;
+    }
+}