]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/actions/ModeledActions.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / actions / ModeledActions.java
1 /*******************************************************************************
2  * Copyright (c) 2011, 2016 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  *     Semantum Oy - refactoring
12  *******************************************************************************/
13 package org.simantics.modeling.ui.actions;
14
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;
22 import java.util.Map;
23 import java.util.Set;
24
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.eclipse.osgi.util.NLS;
37 import org.simantics.browsing.ui.NodeContext;
38 import org.simantics.browsing.ui.common.NodeContextBuilder;
39 import org.simantics.browsing.ui.model.InvalidContribution;
40 import org.simantics.browsing.ui.model.actions.ActionBrowseContext;
41 import org.simantics.browsing.ui.model.actions.IActionCategory;
42 import org.simantics.db.ReadGraph;
43 import org.simantics.db.Resource;
44 import org.simantics.db.exception.DatabaseException;
45 import org.simantics.issues.common.IssueUtils;
46 import org.simantics.modeling.ui.Activator;
47 import org.simantics.project.ontology.ProjectResource;
48 import org.simantics.ui.contribution.DynamicMenuContribution;
49 import org.simantics.ui.selection.WorkbenchSelectionUtils;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class ModeledActions extends DynamicMenuContribution implements IExecutableExtension {
54     private static final Logger LOGGER = LoggerFactory.getLogger(IssueUtils.class);
55
56     public static final Set<String> defaultBrowseContexts = Collections.singleton(ProjectResource.URIs.ProjectActionContext);
57
58     protected Set<String> browseContexts = defaultBrowseContexts;
59
60     public static final Comparator<Action> ACTION_COMPARATOR = new Comparator<Action>() {
61         @Override
62         public int compare(Action o1, Action o2) {
63             String t1 = o1.getText();
64             String t2 = o2.getText();
65             if(t1 == null) {
66                 if(t2 == null)
67                     return 0;
68                 else
69                     return -1;
70             }           
71             else {
72                 if(t2 == null)
73                     return 1;
74                 else
75                     return t1.compareTo(t2);
76             }
77         }
78     };
79
80     @Override
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(";"); //$NON-NLS-1$
85             for(String parm : parms) {
86                 String[] keyValue = parm.split("="); //$NON-NLS-1$
87                 if(keyValue.length == 2) {
88                     String key = keyValue[0].trim();
89                     if("context".equals(key)) { //$NON-NLS-1$
90                         browseContexts = Collections.singleton(keyValue[1]);
91                     }
92                 }
93             }
94         }
95     }
96
97     protected Collection<String> getBrowseContexts() {
98         return browseContexts;
99     }
100
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));
106         return result;
107     }
108
109     protected List<NodeContext> getContexts(Object[] selection) {
110         if (selection.length == 0)
111             return Collections.emptyList();
112
113         ArrayList<NodeContext> result = new ArrayList<>(selection.length);
114
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                         continue;
121                     }
122             }
123             try {
124                 Resource res = WorkbenchSelectionUtils.getPossibleResource(o);
125                 if(res != null) {
126                         result.add(NodeContextBuilder.buildWithInput(res));
127                 }
128             } catch (DatabaseException e) {
129                 LOGGER.error("Failed to get node contexts for selection.", e); //$NON-NLS-1$
130             }
131         }
132
133         return result;
134     }
135
136     protected List<NodeContext> getContexts(ReadGraph graph, Object[] selection) throws DatabaseException {
137         return getContexts(selection);
138     }
139
140     protected static IContributionItem[] toContributionItems(Map<IActionCategory, List<Action>> map) {
141         if (map.isEmpty())
142             return NONE;
143
144         IActionCategory[] categories = map.keySet().toArray(new IActionCategory[map.size()]);
145         Arrays.sort(categories, IActionCategory.ACTION_CATEGORY_COMPARATOR);
146
147         ArrayList<IContributionItem> items = new ArrayList<IContributionItem>();
148         boolean first = true;
149         for (IActionCategory category : categories) {
150             List<Action> actions = map.get(category);
151             Collections.sort(actions, ACTION_COMPARATOR);
152
153             if (category != null && category.isSubmenu()) {
154                 MenuManager manager = new MenuManager(category.getLabel());
155                 for (Action action : actions)
156                     manager.add(new ActionContributionItem(action));
157                 items.add(manager);
158             }
159             else {
160                 if (first)
161                     first = false;
162                 else
163                     items.add(new Separator(category == null ? "" : category.getLabel())); //$NON-NLS-1$
164                 for (Action action : actions)
165                     items.add(new ActionContributionItem(action));
166             }
167         }
168         return items.toArray(new IContributionItem[items.size()]);
169     }
170
171     @Override
172     protected Object[] getSelectedObjects() {
173         Object[] sel = super.getSelectedObjects();
174         List<NodeContext> contexts = getContexts(sel);
175         return contexts.toArray(NodeContext.NONE);
176     }
177
178     @Override
179     protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] selection) throws DatabaseException {
180         List<NodeContext> contexts = Arrays.asList((NodeContext[]) selection);
181         if (contexts.isEmpty())
182             return NONE;
183
184         try {
185             NodeContext nodeContext = contexts.get(0);
186
187             ActionBrowseContext defaultContext = ActionBrowseContext.create(graph, getBrowseContextResources(graph));
188             ActionBrowseContext browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
189
190             Map<IActionCategory, List<Action>> result = browseContext.getActions(graph, nodeContext, contexts);
191             Map<IActionCategory, List<Action>> current = result;
192
193             for (int i = 1; i < contexts.size(); i++) {
194
195                 browseContext = ActionBrowseContext.get(graph, nodeContext, defaultContext);
196
197                 Map<IActionCategory, List<Action>> m = browseContext.getActions(graph, contexts.get(i), contexts);
198
199                 result = new HashMap<>();
200
201                 for (Map.Entry<IActionCategory, List<Action>> entry : m.entrySet()) {
202                     List<Action> exist = current.get(entry.getKey());
203                     if (exist == null)
204                         continue;
205
206                     ArrayList<Action> l = new ArrayList<Action>();
207                     for (Action e : exist) {
208                         String id = e.getId();
209                         boolean found = false;
210                         for (Action a : entry.getValue()) {
211                             if (id.equals(a.getId())) {
212                                 found = true;
213                                 break;
214                             }
215                         }
216                         if (found)
217                             l.add(e);
218                     }
219                     if (!l.isEmpty())
220                         result.put(entry.getKey(), l);
221                 }
222
223                 current = result;
224
225             }
226
227             return toContributionItems(result);
228
229         } catch (InvalidContribution e) {
230             Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
231                     NLS.bind(Messages.ModeledActions_ActivatorInvalidContributionsEncounteredIn,
232                             getClass().getSimpleName()),
233                     e));
234         }
235         return NONE;
236     }
237
238 }