]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java
Mini desktop graalvm packaging.
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.e4 / src / org / argeo / cms / e4 / CmsE4Utils.java
1 package org.argeo.cms.e4;
2
3 import java.util.List;
4
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;
16
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);
25 return;
26 }
27 }
28
29 // new part
30 MPart part = partService.createPart(editorId);
31 if (part == null)
32 throw new CmsException("No editor found with id " + editorId);
33 part.getPersistedState().put(key, state);
34 partService.showPart(part, PartState.ACTIVATE);
35 }
36
37 /** Dynamically creates an handled menu item from a command ID. */
38 public static MHandledMenuItem createHandledMenuItem(EModelService modelService, MApplication app,
39 String commandId) {
40 MCommand command = findCommand(modelService, app, commandId);
41 if (command == null)
42 return null;
43 MHandledMenuItem handledItem = modelService.createModelElement(MHandledMenuItem.class);
44 handledItem.setCommand(command);
45 return handledItem;
46
47 }
48
49 /**
50 * Finds a command by ID.
51 *
52 * @return the {@link MCommand} or <code>null</code> if not found.
53 */
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)) {
58 return cmd;
59 }
60 }
61 return null;
62 }
63
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());
70 return dynamicItem;
71 }
72
73 /** Singleton. */
74 private CmsE4Utils() {
75 }
76
77 }