From: Mathieu Baudier Date: Wed, 23 Oct 2019 08:15:20 +0000 (+0200) Subject: Improve CMS UI utilities. X-Git-Tag: argeo-commons-2.1.81~7 X-Git-Url: https://git.argeo.org/?p=lgpl%2Fargeo-commons.git;a=commitdiff_plain;h=45c7d6a67d68bb2e5316e7e3ce268690cf0f2c6d Improve CMS UI utilities. --- diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java index b8ad37e39..21abf5828 100644 --- a/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java +++ b/org.argeo.cms.e4/src/org/argeo/cms/e4/CmsE4Utils.java @@ -1,11 +1,22 @@ package org.argeo.cms.e4; +import java.util.List; + import org.argeo.cms.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); @@ -23,6 +34,44 @@ public class CmsE4Utils { 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() { } + } diff --git a/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java b/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java index 743691588..0c6eb53d4 100644 --- a/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java +++ b/org.argeo.cms.ui/src/org/argeo/cms/util/CmsUtils.java @@ -1,5 +1,6 @@ package org.argeo.cms.util; +import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; @@ -8,7 +9,6 @@ import javax.jcr.Node; import javax.jcr.RepositoryException; import javax.servlet.http.HttpServletRequest; -import org.apache.commons.io.IOUtils; import org.argeo.cms.CmsException; import org.argeo.cms.ui.CmsConstants; import org.argeo.cms.ui.CmsView; @@ -116,13 +116,15 @@ public class CmsUtils implements CmsConstants { } /** Style widget */ - public static void style(Widget widget, String style) { + public static T style(T widget, String style) { widget.setData(CmsConstants.STYLE, style); + return widget; } /** Enable markups on widget */ - public static void markup(Widget widget) { + public static T markup(T widget) { widget.setData(CmsConstants.MARKUP, true); + return widget; } public static void setItemHeight(Table table, int height) { @@ -198,10 +200,15 @@ public class CmsUtils implements CmsConstants { Image image = new Image(Display.getCurrent(), scaled); return image; } finally { - IOUtils.closeQuietly(in); + try { + in.close(); + } catch (IOException e) { + // silent + } } } + /** Lorem ipsum text to be used during development. */ public final static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit." + " Etiam eleifend hendrerit sem, ac ultricies massa ornare ac." + " Cras aliquam sodales risus, vitae varius lacus molestie quis." @@ -214,6 +221,7 @@ public class CmsUtils implements CmsConstants { + " Duis vitae turpis eros. Sed tincidunt lacinia rutrum." + " Aliquam velit velit, rutrum ut augue sed, condimentum lacinia augue."; + /** Singleton. */ private CmsUtils() { } }