]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/workbench/e4/E4WorkbenchUtils.java
3c1888a0112c2c2f382e82c05915372ce51d440d
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / workbench / e4 / E4WorkbenchUtils.java
1 package org.simantics.ui.workbench.e4;
2
3 import java.util.List;
4 import java.util.Map;
5
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.ui.MUIElement;
9 import org.eclipse.e4.ui.model.application.ui.advanced.MArea;
10 import org.eclipse.e4.ui.model.application.ui.advanced.MPlaceholder;
11 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12 import org.eclipse.e4.ui.model.application.ui.basic.MPartSashContainerElement;
13 import org.eclipse.e4.ui.model.application.ui.basic.MPartStack;
14 import org.eclipse.e4.ui.workbench.modeling.EModelService;
15 import org.eclipse.e4.ui.workbench.modeling.EPartService;
16 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
17 import org.eclipse.ui.PlatformUI;
18 import org.simantics.db.Resource;
19 import org.simantics.utils.datastructures.ArrayMap;
20
21 /**
22  * @author Tuukka Lehtonen
23  *
24  */
25 public class E4WorkbenchUtils {
26
27     private static final String[] KEYS_RESOURCE = { E4ResourceEditorConstants.KEY_RESOURCE };
28
29     /**
30      * 
31      */
32     private static final String TAG_EDITOR = "Editor";
33
34     /**
35      * The Legacy IDE application ID for the "editor area" partstack. In E3
36      * compatibility mode legacy application the {@link MPartStack} lies under
37      * the shared {@link MArea} (org.eclipse.ui.editorss).
38      * 
39      * <p>
40      * In a pure E4 application this needs to be changed.
41      */
42     public static final String EDITOR_PART_STACK_ID = "org.eclipse.e4.primaryDataStack";
43
44     public static final String EDITOR_AREA_ID = "org.eclipse.ui.editorss";
45
46     /**
47      * @param editorId
48      * @param contributionURI
49      * @param iconURI
50      * @param inputId
51      * @param inputMap
52      */
53     public static MPart openEditor(String editorId, String contributionURI, String iconURI, String inputId, Map<String, Object> inputMap) {
54         if (editorId == null)
55             throw new NullPointerException("null editor ID");
56         if (contributionURI == null)
57             throw new NullPointerException("null contributionURI");
58         if (inputId == null)
59             throw new NullPointerException("null input ID");
60         if (inputMap == null)
61             throw new NullPointerException("null input map");
62
63         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
64         EModelService modelService = context.get(EModelService.class);
65         EPartService partService = context.get(EPartService.class);
66         MApplication app = context.get(MApplication.class);
67
68         String editorID = editorId + "_" + inputId;
69
70         List<MPart> parts = modelService.findElements(app, editorID, MPart.class, null);
71         MPart editor = null;
72         if (parts == null || parts.isEmpty()) {
73             editor = modelService.createModelElement(MPart.class);
74             editor.setElementId(editorID);
75             editor.setContributionURI(contributionURI);
76             if (iconURI != null)
77                 editor.setIconURI(iconURI);
78             editor.setCloseable(true);
79             editor.getTags().add(TAG_EDITOR);
80             editor.getTransientData().putAll(inputMap);
81
82             MPartStack stack = getEditorPartStack(modelService, app);
83             stack.getChildren().add(editor);
84         } else {
85             editor = parts.get(0);
86         }
87
88         partService.showPart(editor, PartState.ACTIVATE);
89         return editor;
90     }
91
92     /**
93      * @param editorId
94      * @param contributionURI
95      * @param iconURI
96      * @param input
97      */
98     public static MPart openEditor(String editorId, String contributionURI, String iconURI, Resource input) {
99         String inputId = Long.toString(input.getResourceId());
100         Map<String, Object> inputMap = ArrayMap.make(KEYS_RESOURCE, input);
101         return openEditor(editorId, contributionURI, iconURI, inputId, inputMap);
102     }
103
104     /**
105      * @param editorElementId the unique element ID of the part (editor)
106      */
107     public static void closeEditor(String editorElementId) {
108         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
109         EModelService modelService = context.get(EModelService.class);
110         MApplication app = context.get(MApplication.class);
111         modelService
112         .findElements(app, editorElementId, MPart.class, null)
113         .forEach(p -> p.getContext().get(EPartService.class).hidePart(p));
114     }
115
116     public static void activatePart(MPart part) {
117         EPartService partService = part.getContext().get(EPartService.class);
118         partService.activate(part);
119     }
120
121     public static MPartStack getEditorPartStack(EModelService modelService, MApplication app) {
122         MPartStack stack = (MPartStack) modelService.find(E4WorkbenchUtils.EDITOR_PART_STACK_ID, app);
123         if (stack == null) {
124             MArea editorArea = null;
125             MUIElement area = modelService.find(E4WorkbenchUtils.EDITOR_AREA_ID, app);
126             if (area instanceof MPlaceholder) {
127                 editorArea = (MArea) ((MPlaceholder) area).getRef();
128             } else if (area instanceof MArea) {
129                 editorArea = (MArea) area;
130             }
131             if (editorArea == null)
132                 throw new IllegalStateException("Shared editor area (" + E4WorkbenchUtils.EDITOR_AREA_ID + ") not found in application model");
133             for (MPartSashContainerElement container : editorArea.getChildren()) {
134                 if (container instanceof MPartStack) {
135                     stack = (MPartStack) container;
136                     break;
137                 }
138             }
139         }
140         if (stack == null)
141             throw new IllegalStateException("Could not find part stack under editor area.");
142         return stack;
143     }
144
145     public static void openAndShowPart(MPart part) {
146         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
147         EPartService partService = context.get(EPartService.class);
148         if (!partService.isPartVisible(part))
149             partService.showPart(part, PartState.ACTIVATE);
150         else
151             partService.activate(part);
152     }
153
154     
155     public static void openAndShowPart(String partId) {
156         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
157         EPartService partService = context.get(EPartService.class);
158         partService.showPart(partId, PartState.ACTIVATE);
159     }
160
161     public static MPart getMPartById(String partId) {
162         IEclipseContext context = PlatformUI.getWorkbench().getService(IEclipseContext.class);
163         EPartService partService = context.get(EPartService.class);
164         MPart part = partService.findPart(partId);
165         if (part == null)
166             part = partService.createPart(partId);
167         return part;
168     }
169
170 }