]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/e4/GlobalModeledToolbarActions.java
Externalize strings
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / e4 / GlobalModeledToolbarActions.java
index 6d459d3c22e111567316ddf04521cb90970be1df..a19df0e2d0593f929cba0efc5e82ddfbfeec9bcd 100644 (file)
-package org.simantics.modeling.ui.actions.e4;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.Comparator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.concurrent.atomic.AtomicReference;\r
-\r
-import javax.annotation.PostConstruct;\r
-import javax.annotation.PreDestroy;\r
-import javax.inject.Inject;\r
-\r
-import org.eclipse.core.runtime.IStatus;\r
-import org.eclipse.core.runtime.Status;\r
-import org.eclipse.e4.ui.di.UISynchronize;\r
-import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;\r
-import org.eclipse.jface.action.Action;\r
-import org.eclipse.jface.action.ActionContributionItem;\r
-import org.eclipse.jface.action.IContributionItem;\r
-import org.eclipse.jface.action.MenuManager;\r
-import org.eclipse.jface.action.Separator;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.swt.widgets.Control;\r
-import org.eclipse.swt.widgets.ToolBar;\r
-import org.eclipse.swt.widgets.ToolItem;\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.common.NodeContextBuilder;\r
-import org.simantics.browsing.ui.model.InvalidContribution;\r
-import org.simantics.browsing.ui.model.actions.ActionBrowseContext;\r
-import org.simantics.browsing.ui.model.actions.IActionCategory;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.BinaryRead;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.modeling.ui.Activator;\r
-\r
-/**\r
- * Used like this in E4 model fragments:\r
- * \r
- * <pre>\r
- * <fragments xsi:type="fragment:StringModelFragment" featurename="trimContributions" parentElementId="org.eclipse.e4.legacy.ide.application">\r
- *  <elements xsi:type="menu:TrimContribution" elementId="modeled.trim.id" parentId="org.eclipse.ui.main.toolbar" positionInParent="after=additions">\r
- *    <children xsi:type="menu:ToolBar" elementId="toolbar.id">\r
- *      <children xsi:type="menu:ToolControl" elementId="toolcontrol.id" contributionURI="bundleclass://org.simantics.modeling.ui/org.simantics.modeling.ui.actions.GlobalModeledToolbarActions">\r
- *        <tags>http://www.simantics.org/Project-1.2/MainToolbarActionContext</tags>\r
- *      </children>\r
- *    </children>\r
- *  </elements>\r
- * </fragments>\r
- * </pre>\r
- * \r
- * @author Antti Villberg\r
- * @author Jani Simomaa\r
- * @author Tuukka Lehtonen\r
- */\r
-public class GlobalModeledToolbarActions {\r
-\r
-    @Inject UISynchronize sync;\r
-\r
-    private List<String> browseContexts;\r
-    private boolean disposed;\r
-    private Composite composite;\r
-\r
-    @PostConstruct\r
-    protected void create(Composite parent, MToolControl control) {\r
-        this.composite = parent;\r
-        browseContexts = new ArrayList<>(control.getTags());\r
-        Simantics.getSession().asyncRequest(\r
-                new GetContributions(Simantics.getProjectResource(), browseContexts),\r
-                new ContributionListener());\r
-    }\r
-\r
-    @PreDestroy\r
-    private void dispose() {\r
-        disposed = true;\r
-        if (composite != null) {\r
-            for (Control c : composite.getChildren())\r
-                c.dispose();\r
-            composite = null;\r
-        }\r
-    }\r
-\r
-    class ContributionListener implements Listener<List<IContributionItem>>, Runnable {\r
-\r
-        AtomicReference<List<IContributionItem>> lastResult = new AtomicReference<>();\r
-\r
-        @Override\r
-        public void execute(List<IContributionItem> result) {\r
-            if (composite != null) {\r
-                lastResult.set(result);\r
-                sync.asyncExec(this);\r
-            }\r
-        }\r
-\r
-        @Override\r
-        public void run() {\r
-            List<IContributionItem> result = lastResult.getAndSet(null);\r
-            if (result == null || composite == null || composite.isDisposed())\r
-                return;\r
-\r
-            ToolBar tb = (ToolBar) composite.getParent();\r
-            for (ToolItem item : tb.getItems())\r
-                item.dispose();\r
-            for (IContributionItem item : result)\r
-                item.fill(tb, -1);\r
-\r
-            Composite tbp = tb.getParent();\r
-            tbp.layout(true, true);\r
-        }\r
-\r
-        @Override\r
-        public void exception(Throwable t) {\r
-            Activator.getDefault().getLog().log(\r
-                    new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
-                            "Global modeled toolbar contribution listener ran into an unexpected exception.",\r
-                            t));\r
-        }\r
-\r
-        @Override\r
-        public boolean isDisposed() {\r
-            return disposed;\r
-        }\r
-    }\r
-\r
-    static class GetContributions extends BinaryRead<Resource, Collection<String>, List<IContributionItem>> {\r
-        public GetContributions(Resource from, Collection<String> browseContextNames) {\r
-            super(from, browseContextNames);\r
-        }\r
-        @Override\r
-        public List<IContributionItem> perform(ReadGraph graph) throws DatabaseException {\r
-            return getContributionItems(graph, parameter, parameter2);\r
-        }\r
-    }\r
-\r
-    private static Collection<Resource> getBrowseContextResources(ReadGraph graph, Collection<String> browseContexts) throws DatabaseException {\r
-        List<Resource> result = new ArrayList<Resource>(browseContexts.size());\r
-        for (String name : browseContexts)\r
-            result.add(graph.getResource(name));\r
-        return result;\r
-    }\r
-\r
-    private static List<IContributionItem> getContributionItems(ReadGraph graph, Resource from, Collection<String> browseContextNames) throws DatabaseException {\r
-        Collection<Resource> browseContexts = getBrowseContextResources(graph, browseContextNames);\r
-        NodeContext nodeContext = NodeContextBuilder.buildWithInput(from);\r
-        try {\r
-            ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, browseContexts);\r
-            ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);\r
-            Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, Collections.singletonList(nodeContext));\r
-            return toContributionItems(result);\r
-        } catch (InvalidContribution e) {\r
-            Activator.getDefault().getLog().log(\r
-                    new Status(IStatus.ERROR, Activator.PLUGIN_ID,\r
-                            "Encountered invalid modeled contribution(s) while loading global modeled toolbar contributions.",\r
-                            e));\r
-        }\r
-\r
-        return Collections.emptyList();\r
-    }\r
-\r
-    private static List<IContributionItem> toContributionItems(Map<IActionCategory, List<Action>> map) {\r
-        if (map.isEmpty())\r
-            return Collections.emptyList();\r
-\r
-        IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);\r
-        Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);\r
-\r
-        ArrayList<IContributionItem> items = new ArrayList<>();\r
-        boolean first = true;\r
-        for (IActionCategory category : categories) {\r
-            List<Action> actions = map.get(category);\r
-            Collections.sort(actions, ACTION_COMPARATOR);\r
-\r
-            if (category != null && category.isSubmenu()) {\r
-                MenuManager manager = new MenuManager(category.getLabel());\r
-                for (Action action : actions)\r
-                    manager.add(new ActionContributionItem(action));\r
-                items.add(manager);\r
-            }\r
-            else {\r
-                if (first)\r
-                    first = false;\r
-                else\r
-                    items.add(new Separator(category == null ? "" : category.getLabel()));\r
-                for (Action action : actions)\r
-                    items.add(new ActionContributionItem(action));\r
-            }\r
-        }\r
-\r
-        return items;\r
-    }\r
-\r
-    private static final Comparator<Action> ACTION_COMPARATOR = (o1, o2) -> {\r
-        String t1 = o1.getText();\r
-        String t2 = o2.getText();\r
-        return t1 == null ?\r
-                (t2 == null ? 0 : -1)\r
-                : (t2 == null ? 1 : t1.compareTo(t2));\r
-    };\r
-\r
-}\r
+package org.simantics.modeling.ui.actions.e4;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.e4.ui.di.UISynchronize;
+import org.eclipse.e4.ui.model.application.ui.menu.MToolControl;
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.ActionContributionItem;
+import org.eclipse.jface.action.IContributionItem;
+import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.common.NodeContextBuilder;
+import org.simantics.browsing.ui.model.InvalidContribution;
+import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
+import org.simantics.browsing.ui.model.actions.IActionCategory;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.BinaryRead;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.modeling.ui.Activator;
+
+/**
+ * Used like this in E4 model fragments:
+ * 
+ * <pre>
+ * <fragments xsi:type="fragment:StringModelFragment" featurename="trimContributions" parentElementId="org.eclipse.e4.legacy.ide.application">
+ *  <elements xsi:type="menu:TrimContribution" elementId="modeled.trim.id" parentId="org.eclipse.ui.main.toolbar" positionInParent="after=additions">
+ *    <children xsi:type="menu:ToolBar" elementId="toolbar.id">
+ *      <children xsi:type="menu:ToolControl" elementId="toolcontrol.id" contributionURI="bundleclass://org.simantics.modeling.ui/org.simantics.modeling.ui.actions.GlobalModeledToolbarActions">
+ *        <tags>http://www.simantics.org/Project-1.2/MainToolbarActionContext</tags>
+ *      </children>
+ *    </children>
+ *  </elements>
+ * </fragments>
+ * </pre>
+ * 
+ * @author Antti Villberg
+ * @author Jani Simomaa
+ * @author Tuukka Lehtonen
+ */
+public class GlobalModeledToolbarActions {
+
+    @Inject UISynchronize sync;
+
+    private List<String> browseContexts;
+    private boolean disposed;
+    private Composite composite;
+
+    @PostConstruct
+    protected void create(Composite parent, MToolControl control) {
+        this.composite = parent;
+        browseContexts = new ArrayList<>(control.getTags());
+        Simantics.getSession().asyncRequest(
+                new GetContributions(Simantics.getProjectResource(), browseContexts),
+                new ContributionListener());
+    }
+
+    @PreDestroy
+    private void dispose() {
+        disposed = true;
+        if (composite != null) {
+            for (Control c : composite.getChildren())
+                c.dispose();
+            composite = null;
+        }
+    }
+
+    class ContributionListener implements Listener<List<IContributionItem>>, Runnable {
+
+        AtomicReference<List<IContributionItem>> lastResult = new AtomicReference<>();
+
+        @Override
+        public void execute(List<IContributionItem> result) {
+            if (composite != null) {
+                lastResult.set(result);
+                sync.asyncExec(this);
+            }
+        }
+
+        @Override
+        public void run() {
+            List<IContributionItem> result = lastResult.getAndSet(null);
+            if (result == null || composite == null || composite.isDisposed())
+                return;
+
+            ToolBar tb = (ToolBar) composite.getParent();
+            for (ToolItem item : tb.getItems())
+                item.dispose();
+            for (IContributionItem item : result)
+                item.fill(tb, -1);
+
+            Composite tbp = tb.getParent();
+            tbp.layout(true, true);
+        }
+
+        @Override
+        public void exception(Throwable t) {
+            Activator.getDefault().getLog().log(
+                    new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                            Messages.GlobalModeledToolbarActions_ActivatorGlobalModeledToolbarException,
+                            t));
+        }
+
+        @Override
+        public boolean isDisposed() {
+            return disposed;
+        }
+    }
+
+    static class GetContributions extends BinaryRead<Resource, Collection<String>, List<IContributionItem>> {
+        public GetContributions(Resource from, Collection<String> browseContextNames) {
+            super(from, browseContextNames);
+        }
+        @Override
+        public List<IContributionItem> perform(ReadGraph graph) throws DatabaseException {
+            return getContributionItems(graph, parameter, parameter2);
+        }
+    }
+
+    private static Collection<Resource> getBrowseContextResources(ReadGraph graph, Collection<String> browseContexts) throws DatabaseException {
+        List<Resource> result = new ArrayList<Resource>(browseContexts.size());
+        for (String name : browseContexts)
+            result.add(graph.getResource(name));
+        return result;
+    }
+
+    private static List<IContributionItem> getContributionItems(ReadGraph graph, Resource from, Collection<String> browseContextNames) throws DatabaseException {
+        Collection<Resource> browseContexts = getBrowseContextResources(graph, browseContextNames);
+        NodeContext nodeContext = NodeContextBuilder.buildWithInput(from);
+        try {
+            ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, browseContexts);
+            ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
+            Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, Collections.singletonList(nodeContext));
+            return toContributionItems(result);
+        } catch (InvalidContribution e) {
+            Activator.getDefault().getLog().log(
+                    new Status(IStatus.ERROR, Activator.PLUGIN_ID,
+                            Messages.GlobalModeledToolbarActions_ActivatorEncounteredInvalidContributionException,
+                            e));
+        }
+
+        return Collections.emptyList();
+    }
+
+    private static List<IContributionItem> toContributionItems(Map<IActionCategory, List<Action>> map) {
+        if (map.isEmpty())
+            return Collections.emptyList();
+
+        IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
+        Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
+
+        ArrayList<IContributionItem> items = new ArrayList<>();
+        boolean first = true;
+        for (IActionCategory category : categories) {
+            List<Action> actions = map.get(category);
+            Collections.sort(actions, ACTION_COMPARATOR);
+
+            if (category != null && category.isSubmenu()) {
+                MenuManager manager = new MenuManager(category.getLabel());
+                for (Action action : actions)
+                    manager.add(new ActionContributionItem(action));
+                items.add(manager);
+            }
+            else {
+                if (first)
+                    first = false;
+                else
+                    items.add(new Separator(category == null ? "" : category.getLabel())); //$NON-NLS-1$
+                for (Action action : actions)
+                    items.add(new ActionContributionItem(action));
+            }
+        }
+
+        return items;
+    }
+
+    private static final Comparator<Action> ACTION_COMPARATOR = (o1, o2) -> {
+        String t1 = o1.getText();
+        String t2 = o2.getText();
+        return t1 == null ?
+                (t2 == null ? 0 : -1)
+                : (t2 == null ? 1 : t1.compareTo(t2));
+    };
+
+}