]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4WorkbenchUtils.java
1 package org.simantics.ui.workbench.e4;\r
2 \r
3 import java.util.List;\r
4 import java.util.Map;\r
5 \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
20 \r
21 /**\r
22  * @author Tuukka Lehtonen\r
23  *\r
24  */\r
25 public class E4WorkbenchUtils {\r
26 \r
27     private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };\r
28 \r
29     /**\r
30      * \r
31      */\r
32     private static final String TAG_EDITOR = "Editor";\r
33 \r
34     /**\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
38      * \r
39      * <p>\r
40      * In a pure E4 application this needs to be changed.\r
41      */\r
42     public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";\r
43 \r
44     public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";\r
45 \r
46     /**\r
47      * @param editorId\r
48      * @param contributionURI\r
49      * @param iconURI\r
50      * @param inputId\r
51      * @param inputMap\r
52      */\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
62 \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
67 \r
68         String editorID = editorId + "_" + inputId;\r
69 \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
81 \r
82             MPartStack stack = getEditorPartStack(modelService, app);\r
83             stack.getChildren().add(editor);\r
84         } else {\r
85             editor = parts.get(0);\r
86         }\r
87 \r
88         partService.showPart(editor, PartState.ACTIVATE);\r
89         return editor;\r
90     }\r
91 \r
92     /**\r
93      * @param editorId\r
94      * @param contributionURI\r
95      * @param iconURI\r
96      * @param input\r
97      */\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
102     }\r
103 \r
104     /**\r
105      * @param editorElementId the unique element ID of the part (editor)\r
106      */\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
111         modelService\r
112         .findElements(app, editorElementId, MPart.class, null)\r
113         .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));\r
114     }\r
115 \r
116     public static void activatePart(MPart part) {\r
117         EPartService partService = part.getContext().get(EPartService.class);\r
118         partService.activate(part);\r
119     }\r
120 \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
130             }\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
136                     break;\r
137                 }\r
138             }\r
139         }\r
140         if (stack == null)\r
141             throw new IllegalStateException("Could not find part stack under editor area.");\r
142         return stack;\r
143     }\r
144 \r
145     public static void openAndShowPart(MPart part) {\r
146         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
147         EPartService partService = context.get(EPartService.class);\r
148         if (!partService.isPartVisible(part))\r
149             partService.showPart(part, PartState.ACTIVATE);\r
150         else\r
151             partService.activate(part);\r
152     }\r
153 \r
154     \r
155     public static void openAndShowPart(String partId) {\r
156         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
157         EPartService partService = context.get(EPartService.class);\r
158         partService.showPart(partId, PartState.ACTIVATE);\r
159     }\r
160 \r
161     public static MPart getMPartById(String partId) {\r
162         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);\r
163         EPartService partService = context.get(EPartService.class);\r
164         return partService.findPart(partId);\r
165     }\r
166 \r
167 }\r