1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.modeling.ui.actions;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Arrays;
\r
16 import java.util.Collection;
\r
17 import java.util.Collections;
\r
18 import java.util.HashMap;
\r
19 import java.util.List;
\r
20 import java.util.Map;
\r
21 import java.util.Set;
\r
23 import org.eclipse.core.runtime.CoreException;
\r
24 import org.eclipse.core.runtime.IConfigurationElement;
\r
25 import org.eclipse.core.runtime.IExecutableExtension;
\r
26 import org.eclipse.jface.action.Action;
\r
27 import org.simantics.browsing.ui.NodeContext;
\r
28 import org.simantics.browsing.ui.common.NodeContextBuilder;
\r
29 import org.simantics.browsing.ui.model.InvalidContribution;
\r
30 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
\r
31 import org.simantics.browsing.ui.model.actions.IActionCategory;
\r
32 import org.simantics.db.ReadGraph;
\r
33 import org.simantics.db.Resource;
\r
34 import org.simantics.db.common.request.ResourceRead;
\r
35 import org.simantics.db.exception.DatabaseException;
\r
36 import org.simantics.project.ontology.ProjectResource;
\r
37 import org.simantics.ui.DoubleClickEvent;
\r
38 import org.simantics.ui.IDoubleClickAction;
\r
39 import org.simantics.ui.utils.ResourceAdaptionUtils;
\r
40 import org.simantics.utils.ui.action.PriorityAction;
\r
42 public class ModeledDoubleClickActions implements IDoubleClickAction, IExecutableExtension {
\r
44 private Set<String> browseContexts = defaultBrowseContexts;
\r
46 final static public Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectDoubleClickActionContext);
\r
48 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
\r
49 if(data instanceof String) {
\r
50 String str = (String)data;
\r
51 String[] parms = str.split(";");
\r
52 for(String parm : parms) {
\r
53 String[] keyValue = parm.split("=");
\r
54 if(keyValue.length == 2) {
\r
55 String key = keyValue[0].trim();
\r
56 if("context".equals(key)) {
\r
57 browseContexts = Collections.singleton(keyValue[1]);
\r
64 public Collection<String> getBrowseContexts() {
\r
65 return browseContexts;
\r
68 public Collection<Resource> getBrowseContextResources(ReadGraph graph) throws DatabaseException {
\r
69 Collection<String> names = getBrowseContexts();
\r
70 ArrayList<Resource> result = new ArrayList<Resource>(names.size());
\r
71 for(String name : names)
\r
72 result.add(graph.getResource(name));
\r
77 public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
\r
79 ReadGraph g = e.getGraph();
\r
80 final Resource resource = ResourceAdaptionUtils.toSingleResource(e.getResource());
\r
81 if (resource == null)
\r
84 Collection<PriorityAction> actions = g.syncRequest(new ResourceRead<Collection<PriorityAction>>(resource) {
\r
87 public Collection<PriorityAction> perform(ReadGraph graph) throws DatabaseException {
\r
89 List<NodeContext> contexts = Collections.singletonList(NodeContextBuilder.buildWithInput(resource));
\r
93 NodeContext nodeContext = contexts.get(0);
\r
95 ActionBrowseContext defaultContext = ActionBrowseContext.create(graph,
\r
96 getBrowseContextResources(graph));
\r
98 ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
\r
100 Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
\r
101 Map<IActionCategory, List<Action>> current = result;
\r
103 for(int i=0;i<contexts.size();i++) {
\r
105 defaultContext = ActionBrowseContext.create(graph,
\r
106 getBrowseContextResources(graph));
\r
108 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
\r
110 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
\r
112 result = new HashMap<IActionCategory, List<Action>>();
\r
114 for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
\r
115 List<Action> exist = current.get(entry.getKey());
\r
119 ArrayList<Action> l = new ArrayList<Action>();
\r
120 for(Action e : exist) {
\r
121 String id = e.getId();
\r
122 boolean found = false;
\r
123 for(Action a : entry.getValue()) {
\r
124 if(id.equals(a.getId())) {
\r
129 if(found) l.add(e);
\r
131 if(!l.isEmpty()) result.put(entry.getKey(), l);
\r
138 return toContributionItems(result);
\r
140 } catch (InvalidContribution e) {
\r
141 e.printStackTrace();
\r
144 return Collections.emptyList();
\r
150 for(PriorityAction act : actions)
\r
155 private static Collection<PriorityAction> toContributionItems(
\r
156 Map<IActionCategory, List<Action>> map) {
\r
159 return Collections.emptyList();
\r
161 IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
\r
162 Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
\r
164 ArrayList<PriorityAction> items = new ArrayList<PriorityAction>();
\r
165 for(IActionCategory category : categories) {
\r
166 List<Action> actions = map.get(category);
\r
167 for(Action act : actions)
\r
168 items.add(new PriorityAction(PriorityAction.HIGH+10) {
\r
170 public void run() {
\r