X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=swt%2Forg.argeo.cms.e4%2Fsrc%2Forg%2Fargeo%2Fcms%2Fe4%2FCmsE4Utils.java;fp=swt%2Forg.argeo.cms.e4%2Fsrc%2Forg%2Fargeo%2Fcms%2Fe4%2FCmsE4Utils.java;h=a997de748875446664865e251abb910811af64f2;hb=7b242851c0094d13cbaca5b68261ad92c873a59f;hp=0000000000000000000000000000000000000000;hpb=dbb84b4ec2d313ec0724d035c32f482ac57974c5;p=lgpl%2Fargeo-commons.git diff --git a/swt/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java b/swt/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java new file mode 100644 index 000000000..a997de748 --- /dev/null +++ b/swt/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java @@ -0,0 +1,77 @@ +package org.argeo.cms.e4; + +import java.util.List; + +import org.argeo.cms.swt.CmsException; +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.basic.MPart; +import org.eclipse.e4.ui.model.application.ui.menu.MDirectMenuItem; +import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; +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.osgi.framework.Bundle; +import org.osgi.framework.FrameworkUtil; + +/** Static utilities simplifying recurring Eclipse 4 patterns. */ +public class CmsE4Utils { + /** Open an editor based on its id. */ + public static void openEditor(EPartService partService, String editorId, String key, String state) { + for (MPart part : partService.getParts()) { + String id = part.getPersistedState().get(key); + if (id != null && state.equals(id)) { + partService.showPart(part, PartState.ACTIVATE); + return; + } + } + + // new part + MPart part = partService.createPart(editorId); + if (part == null) + throw new CmsException("No editor found with id " + editorId); + part.getPersistedState().put(key, state); + partService.showPart(part, PartState.ACTIVATE); + } + + /** Dynamically creates an handled menu item from a command ID. */ + public static MHandledMenuItem createHandledMenuItem(EModelService modelService, MApplication app, + String commandId) { + MCommand command = findCommand(modelService, app, commandId); + if (command == null) + return null; + MHandledMenuItem handledItem = modelService.createModelElement(MHandledMenuItem.class); + handledItem.setCommand(command); + return handledItem; + + } + + /** + * Finds a command by ID. + * + * @return the {@link MCommand} or null if not found. + */ + public static MCommand findCommand(EModelService modelService, MApplication app, String commandId) { + List cmds = modelService.findElements(app, null, MCommand.class, null); + for (MCommand cmd : cmds) { + if (cmd.getElementId().equals(commandId)) { + return cmd; + } + } + return null; + } + + /** Dynamically creates a direct menu item from a class. */ + public static MDirectMenuItem createDirectMenuItem(EModelService modelService, Class clss, String label) { + MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class); + dynamicItem.setLabel(label); + Bundle bundle = FrameworkUtil.getBundle(clss); + dynamicItem.setContributionURI("bundleclass://" + bundle.getSymbolicName() + "/" + clss.getName()); + return dynamicItem; + } + + /** Singleton. */ + private CmsE4Utils() { + } + +}