]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ModeledDoubleClickActions.java
Allow simulation stop handling in Simantics/Sequences
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / ModeledDoubleClickActions.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.actions;
13
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;
20 import java.util.Map;
21 import java.util.Set;
22
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;
41
42 public class ModeledDoubleClickActions implements IDoubleClickAction, IExecutableExtension {
43
44     private Set<String> browseContexts = defaultBrowseContexts;
45
46     final static public Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectDoubleClickActionContext);
47     
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]);
58                                 }
59                         }
60                 }
61         }
62     }
63     
64     public Collection<String> getBrowseContexts() {
65         return browseContexts;
66     }
67     
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));
73         return result;
74     }
75         
76     @Override
77     public void doubleClickEvent(DoubleClickEvent e) throws DatabaseException {
78         
79         ReadGraph g = e.getGraph();
80         final Resource resource = ResourceAdaptionUtils.toSingleResource(e.getResource());
81         if (resource == null)
82             return;
83
84         Collection<PriorityAction> actions = g.syncRequest(new ResourceRead<Collection<PriorityAction>>(resource) {
85
86                         @Override
87                         public Collection<PriorityAction> perform(ReadGraph graph) throws DatabaseException {
88                                 
89                         List<NodeContext> contexts = Collections.singletonList(NodeContextBuilder.buildWithInput(resource));
90                         
91                         try {
92
93                                 NodeContext nodeContext = contexts.get(0);
94                                 
95                             ActionBrowseContext defaultContext = ActionBrowseContext.create(graph,
96                                     getBrowseContextResources(graph));
97                             
98                             ActionBrowseContext browseContext = defaultContext;//ActionBrowseContext.get(graph, nodeContext, defaultContext);
99                             
100                             Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
101                             Map<IActionCategory, List<Action>> current = result;
102                             
103                             for(int i=0;i<contexts.size();i++) {
104
105                                 defaultContext = ActionBrowseContext.create(graph,
106                                         getBrowseContextResources(graph));
107                                 
108                                 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
109                                 
110                                 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
111
112                                 result = new HashMap<IActionCategory, List<Action>>();
113                                 
114                                 for(Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
115                                         List<Action> exist = current.get(entry.getKey());
116                                         if (exist == null)
117                                                 continue;
118
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())) {
125                                                                 found = true;
126                                                                 break;
127                                                         }
128                                                 }
129                                                 if(found) l.add(e);
130                                         }
131                                         if(!l.isEmpty()) result.put(entry.getKey(), l);
132                                 }
133                                 
134                                 current = result;
135                                 
136                             }
137                             
138                             return toContributionItems(result);
139                             
140                         } catch (InvalidContribution e) {
141                             e.printStackTrace();
142                         }
143                         
144                         return Collections.emptyList();
145                         
146                         }
147                 
148         });
149         
150         for(PriorityAction act : actions)
151                 e.add(act);
152         
153     }
154     
155     private static Collection<PriorityAction> toContributionItems(
156             Map<IActionCategory, List<Action>> map) {
157
158         if(map.isEmpty())
159             return Collections.emptyList();
160         
161         IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
162         Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
163         
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) {
169                     @Override
170                     public void run() {
171                         act.run();
172                     }
173                 });
174         }
175         return items;
176     }    
177
178 }