1 /*******************************************************************************
2 * Copyright (c) 2011, 2016 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - refactoring
12 *******************************************************************************/
13 package org.simantics.modeling.ui.actions;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Comparator;
20 import java.util.HashMap;
21 import java.util.List;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.core.runtime.IAdaptable;
27 import org.eclipse.core.runtime.IConfigurationElement;
28 import org.eclipse.core.runtime.IExecutableExtension;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.action.ActionContributionItem;
33 import org.eclipse.jface.action.IContributionItem;
34 import org.eclipse.jface.action.MenuManager;
35 import org.eclipse.jface.action.Separator;
36 import org.eclipse.osgi.util.NLS;
37 import org.simantics.browsing.ui.NodeContext;
38 import org.simantics.browsing.ui.common.NodeContextBuilder;
39 import org.simantics.browsing.ui.model.InvalidContribution;
40 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
41 import org.simantics.browsing.ui.model.actions.IActionCategory;
42 import org.simantics.db.ReadGraph;
43 import org.simantics.db.Resource;
44 import org.simantics.db.exception.DatabaseException;
45 import org.simantics.issues.common.IssueUtils;
46 import org.simantics.modeling.ui.Activator;
47 import org.simantics.project.ontology.ProjectResource;
48 import org.simantics.ui.contribution.DynamicMenuContribution;
49 import org.simantics.ui.selection.WorkbenchSelectionUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class ModeledActions extends DynamicMenuContribution implements IExecutableExtension {
54 private static final Logger LOGGER = LoggerFactory.getLogger(IssueUtils.class);
56 public static final Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectActionContext);
58 protected Set<String> browseContexts = defaultBrowseContexts;
60 public static final Comparator<Action> ACTION_COMPARATOR = new Comparator<Action>() {
62 public int compare(Action o1, Action o2) {
63 String t1 = o1.getText();
64 String t2 = o2.getText();
75 return t1.compareTo(t2);
81 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
82 if(data instanceof String) {
83 String str = (String)data;
84 String[] parms = str.split(";"); //$NON-NLS-1$
85 for(String parm : parms) {
86 String[] keyValue = parm.split("="); //$NON-NLS-1$
87 if(keyValue.length == 2) {
88 String key = keyValue[0].trim();
89 if("context".equals(key)) { //$NON-NLS-1$
90 browseContexts = Collections.singleton(keyValue[1]);
97 protected Collection<String> getBrowseContexts() {
98 return browseContexts;
101 protected Collection<Resource> getBrowseContextResources(ReadGraph graph) throws DatabaseException {
102 Collection<String> names = getBrowseContexts();
103 ArrayList<Resource> result = new ArrayList<>(names.size());
104 for(String name : names)
105 result.add(graph.getResource(name));
109 protected List<NodeContext> getContexts(Object[] selection) {
110 if (selection.length == 0)
111 return Collections.emptyList();
113 ArrayList<NodeContext> result = new ArrayList<>(selection.length);
115 for (Object o : selection) {
116 if ((o instanceof IAdaptable)) {
117 NodeContext nodeContext = ((IAdaptable) o).getAdapter(NodeContext.class);
118 if (nodeContext != null) {
119 result.add(nodeContext);
124 Resource res = WorkbenchSelectionUtils.getPossibleResource(o);
126 result.add(NodeContextBuilder.buildWithInput(res));
128 } catch (DatabaseException e) {
129 LOGGER.error("Failed to get node contexts for selection.", e); //$NON-NLS-1$
136 protected List<NodeContext> getContexts(ReadGraph graph, Object[] selection) throws DatabaseException {
137 return getContexts(selection);
140 protected static IContributionItem[] toContributionItems(Map<IActionCategory, List<Action>> map) {
144 IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
145 Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
147 ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();
148 boolean first = true;
149 for (IActionCategory category : categories) {
150 List<Action> actions = map.get(category);
151 Collections.sort(actions, ACTION_COMPARATOR);
153 if (category != null && category.isSubmenu()) {
154 MenuManager manager = new MenuManager(category.getLabel());
155 for (Action action : actions)
156 manager.add(new ActionContributionItem(action));
163 items.add(new Separator(category == null ? "" : category.getLabel())); //$NON-NLS-1$
164 for (Action action : actions)
165 items.add(new ActionContributionItem(action));
168 return items.toArray(new IContributionItem[items.size()]);
172 protected Object[] getSelectedObjects() {
173 Object[] sel = super.getSelectedObjects();
174 List<NodeContext> contexts = getContexts(sel);
175 return contexts.toArray(NodeContext.NONE);
179 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
180 List<NodeContext> contexts = Arrays.asList((NodeContext[]) selection);
181 if (contexts.isEmpty())
185 NodeContext nodeContext = contexts.get(0);
187 ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, getBrowseContextResources(graph));
188 ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
190 Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
191 Map<IActionCategory, List<Action>> current = result;
193 for (int i = 1; i < contexts.size(); i++) {
195 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
197 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
199 result = new HashMap<>();
201 for (Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
202 List<Action> exist = current.get(entry.getKey());
206 ArrayList<Action> l = new ArrayList<Action>();
207 for (Action e : exist) {
208 String id = e.getId();
209 boolean found = false;
210 for (Action a : entry.getValue()) {
211 if (id.equals(a.getId())) {
220 result.put(entry.getKey(), l);
227 return toContributionItems(result);
229 } catch (InvalidContribution e) {
230 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
231 NLS.bind(Messages.ModeledActions_ActivatorInvalidContributionsEncounteredIn,
232 getClass().getSimpleName()),