1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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 *******************************************************************************/
12 package org.simantics.modeling.ui.actions;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IConfigurationElement;
25 import org.eclipse.core.runtime.IExecutableExtension;
26 import org.eclipse.jface.action.Action;
27 import org.simantics.browsing.ui.NodeContext;
28 import org.simantics.browsing.ui.common.NodeContextBuilder;
29 import org.simantics.browsing.ui.model.InvalidContribution;
30 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
31 import org.simantics.browsing.ui.model.actions.IActionCategory;
32 import org.simantics.db.ReadGraph;
33 import org.simantics.db.Resource;
34 import org.simantics.db.common.request.ResourceRead;
35 import org.simantics.db.exception.DatabaseException;
36 import org.simantics.project.ontology.ProjectResource;
37 import org.simantics.ui.DoubleClickEvent;
38 import org.simantics.ui.IDoubleClickAction;
39 import org.simantics.ui.utils.ResourceAdaptionUtils;
40 import org.simantics.utils.ui.action.PriorityAction;
42 public class ModeledDoubleClickActions implements IDoubleClickAction, IExecutableExtension {
44 private Set<String> browseContexts = defaultBrowseContexts;
46 final static public Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectDoubleClickActionContext);
48 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
49 if(data instanceof String) {
50 String str = (String)data;
51 String[] parms = str.split(";"); //$NON-NLS-1$
52 for(String parm : parms) {
53 String[] keyValue = parm.split("="); //$NON-NLS-1$
54 if(keyValue.length == 2) {
55 String key = keyValue[0].trim();
56 if("context".equals(key)) { //$NON-NLS-1$
57 browseContexts = Collections.singleton(keyValue[1]);
64 public Collection<String> getBrowseContexts() {
65 return browseContexts;
68 public Collection<Resource> getBrowseContextResources(ReadGraph graph) throws DatabaseException {
69 Collection<String> names = getBrowseContexts();
70 ArrayList<Resource> result = new ArrayList<Resource>(names.size());
71 for(String name : names)
72 result.add(graph.getResource(name));
77 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
79 ReadGraph g = e.getGraph();
80 final Resource resource = ResourceAdaptionUtils.toSingleResource(e.getResource());
84 Collection<PriorityAction> actions = g.syncRequest(new ResourceRead<Collection<PriorityAction>>(resource) {
87 public Collection<PriorityAction> perform(ReadGraph graph) throws DatabaseException {
89 List<NodeContext> contexts = Collections.singletonList(NodeContextBuilder.buildWithInput(resource));
93 NodeContext nodeContext = contexts.get(0);
95 ActionBrowseContext defaultContext = ActionBrowseContext.create(graph,
96 getBrowseContextResources(graph));
98 ActionBrowseContext browseContext = defaultContext;//ActionBrowseContext.get(graph, nodeContext, defaultContext);
100 Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
101 Map<IActionCategory, List<Action>> current = result;
103 for(int i=0;i<contexts.size();i++) {
105 defaultContext = ActionBrowseContext.create(graph,
106 getBrowseContextResources(graph));
108 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
110 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
112 result = new HashMap<IActionCategory, List<Action>>();
114 for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
115 List<Action> exist = current.get(entry.getKey());
119 ArrayList<Action> l = new ArrayList<Action>();
120 for(Action e : exist) {
121 String id = e.getId();
122 boolean found = false;
123 for(Action a : entry.getValue()) {
124 if(id.equals(a.getId())) {
131 if(!l.isEmpty()) result.put(entry.getKey(), l);
138 return toContributionItems(result);
140 } catch (InvalidContribution e) {
144 return Collections.emptyList();
150 for(PriorityAction act : actions)
155 private static Collection<PriorityAction> toContributionItems(
156 Map<IActionCategory, List<Action>> map) {
159 return Collections.emptyList();
161 IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
162 Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
164 ArrayList<PriorityAction> items = new ArrayList<PriorityAction>();
165 for(IActionCategory category : categories) {
166 List<Action> actions = map.get(category);
167 for(Action act : actions)
168 items.add(new PriorityAction(PriorityAction.HIGH+10) {