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.simantics.browsing.ui.NodeContext;
37 import org.simantics.browsing.ui.common.NodeContextBuilder;
38 import org.simantics.browsing.ui.model.InvalidContribution;
39 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
40 import org.simantics.browsing.ui.model.actions.IActionCategory;
41 import org.simantics.db.ReadGraph;
42 import org.simantics.db.Resource;
43 import org.simantics.db.exception.DatabaseException;
44 import org.simantics.issues.common.IssueUtils;
45 import org.simantics.modeling.ui.Activator;
46 import org.simantics.project.ontology.ProjectResource;
47 import org.simantics.ui.contribution.DynamicMenuContribution;
48 import org.simantics.ui.selection.WorkbenchSelectionElement;
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(";");
85 for(String parm : parms) {
86 String[] keyValue = parm.split("=");
87 if(keyValue.length == 2) {
88 String key = keyValue[0].trim();
89 if("context".equals(key)) {
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);
120 } else if (o instanceof WorkbenchSelectionElement) {
122 Resource res = WorkbenchSelectionUtils.getPossibleResource((WorkbenchSelectionElement)o);
124 result.add(NodeContextBuilder.buildWithInput(res));
126 } catch (DatabaseException e) {
127 LOGGER.error("Failed to get node contexts for selection.", e);
135 protected List<NodeContext> getContexts(ReadGraph graph, Object[] selection) throws DatabaseException {
136 return getContexts(selection);
139 protected static IContributionItem[] toContributionItems(Map<IActionCategory, List<Action>> map) {
143 IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
144 Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
146 ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();
147 boolean first = true;
148 for (IActionCategory category : categories) {
149 List<Action> actions = map.get(category);
150 Collections.sort(actions, ACTION_COMPARATOR);
152 if (category != null && category.isSubmenu()) {
153 MenuManager manager = new MenuManager(category.getLabel());
154 for (Action action : actions)
155 manager.add(new ActionContributionItem(action));
162 items.add(new Separator(category == null ? "" : category.getLabel()));
163 for (Action action : actions)
164 items.add(new ActionContributionItem(action));
167 return items.toArray(new IContributionItem[items.size()]);
171 protected Object[] getSelectedObjects() {
172 Object[] sel = super.getSelectedObjects();
173 List<NodeContext> contexts = getContexts(sel);
174 return contexts.toArray(NodeContext.NONE);
178 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection)
179 throws DatabaseException
181 List<NodeContext> contexts = Arrays.asList( (NodeContext[]) selection );
182 if (contexts.isEmpty())
186 NodeContext nodeContext = contexts.get(0);
188 ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, getBrowseContextResources(graph));
189 ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
191 Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
192 Map<IActionCategory, List<Action>> current = result;
194 for (int i = 1; i < contexts.size(); i++) {
196 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
198 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
200 result = new HashMap<>();
202 for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
203 List<Action> exist = current.get(entry.getKey());
207 ArrayList<Action> l = new ArrayList<Action>();
208 for(Action e : exist) {
209 String id = e.getId();
210 boolean found = false;
211 for(Action a : entry.getValue()) {
212 if(id.equals(a.getId())) {
219 if(!l.isEmpty()) result.put(entry.getKey(), l);
226 return toContributionItems(result);
228 } catch (InvalidContribution e) {
229 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
230 "Invalid contribution encountered in " + getClass().getSimpleName() + ".", e));