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