1 package org
.argeo
.cms
.e4
;
5 import org
.argeo
.cms
.CmsException
;
6 import org
.eclipse
.e4
.ui
.model
.application
.MApplication
;
7 import org
.eclipse
.e4
.ui
.model
.application
.commands
.MCommand
;
8 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
9 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MDirectMenuItem
;
10 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
11 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EModelService
;
12 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
13 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
.PartState
;
14 import org
.osgi
.framework
.Bundle
;
15 import org
.osgi
.framework
.FrameworkUtil
;
17 /** Static utilities simplifying recurring Eclipse 4 patterns. */
18 public class CmsE4Utils
{
19 /** Open an editor based on its id. */
20 public static void openEditor(EPartService partService
, String editorId
, String key
, String state
) {
21 for (MPart part
: partService
.getParts()) {
22 String id
= part
.getPersistedState().get(key
);
23 if (id
!= null && state
.equals(id
)) {
24 partService
.showPart(part
, PartState
.ACTIVATE
);
30 MPart part
= partService
.createPart(editorId
);
32 throw new CmsException("No editor found with id " + editorId
);
33 part
.getPersistedState().put(key
, state
);
34 partService
.showPart(part
, PartState
.ACTIVATE
);
37 /** Dynamically creates an handled menu item from a command ID. */
38 public static MHandledMenuItem
createHandledMenuItem(EModelService modelService
, MApplication app
,
40 MCommand command
= findCommand(modelService
, app
, commandId
);
43 MHandledMenuItem handledItem
= modelService
.createModelElement(MHandledMenuItem
.class);
44 handledItem
.setCommand(command
);
50 * Finds a command by ID.
52 * @return the {@link MCommand} or <code>null</code> if not found.
54 public static MCommand
findCommand(EModelService modelService
, MApplication app
, String commandId
) {
55 List
<MCommand
> cmds
= modelService
.findElements(app
, null, MCommand
.class, null);
56 for (MCommand cmd
: cmds
) {
57 if (cmd
.getElementId().equals(commandId
)) {
64 /** Dynamically creates a direct menu item from a class. */
65 public static MDirectMenuItem
createDirectMenuItem(EModelService modelService
, Class
<?
> clss
, String label
) {
66 MDirectMenuItem dynamicItem
= modelService
.createModelElement(MDirectMenuItem
.class);
67 dynamicItem
.setLabel(label
);
68 Bundle bundle
= FrameworkUtil
.getBundle(clss
);
69 dynamicItem
.setContributionURI("bundleclass://" + bundle
.getSymbolicName() + "/" + clss
.getName());
74 private CmsE4Utils() {