1 package org.simantics.modeling.ui.modelBrowser2.contributions;
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.HashSet;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IConfigurationElement;
11 import org.eclipse.core.runtime.IExecutableExtension;
12 import org.eclipse.jface.action.Action;
13 import org.eclipse.jface.action.ActionContributionItem;
14 import org.eclipse.jface.action.IContributionItem;
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.Session;
19 import org.simantics.db.WriteGraph;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.modeling.ModelingResources;
23 import org.simantics.modeling.requests.SetSubscriptionEnabled;
24 import org.simantics.modeling.ui.Activator;
25 import org.simantics.ui.SimanticsUI;
26 import org.simantics.ui.contribution.DynamicMenuContribution;
27 import org.simantics.ui.utils.ResourceAdaptionUtils;
30 * @author Tuukka Lehtonen
32 public class SetSubscriptionEnabledContribution extends DynamicMenuContribution implements IExecutableExtension {
35 ImageDescriptor image = null;
36 boolean include = true;
39 public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
40 throws CoreException {
41 if (data instanceof Map<?,?>) {
42 @SuppressWarnings("unchecked")
43 Map<String, String> args = (Map<String, String>) data;
44 name = args.get("name");
45 String imageId = args.get("image");
46 image = Activator.getDefault().getImageRegistry().getDescriptor(imageId);
47 include = Boolean.parseBoolean(args.get("include"));
51 private Collection<Resource> toSubscriptions(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
52 ModelingResources mod = ModelingResources.getInstance(graph);
54 Set<Resource> result = new HashSet<Resource>(resources.size());
55 for (Resource o : resources) {
56 if (graph.isInstanceOf(o, mod.Subscription))
64 protected Object[] getSelectedObjects() {
65 return ResourceAdaptionUtils.toResources(getSelection());
69 protected IContributionItem[] getContributionItems(ReadGraph graph, Object[] elements) throws DatabaseException {
70 final Resource[] resources = (Resource[]) elements;
71 if (resources.length == 0)
72 return new IContributionItem[0];
74 // Are there any components in the selection whose inclusion state
75 // is different from the argument 'included'?
76 final Collection<Resource> subscriptions = toSubscriptions(graph, Arrays.asList(resources));
77 if (subscriptions.isEmpty())
78 return new IContributionItem[0];
80 ModelingResources mod = ModelingResources.getInstance(graph);
82 boolean foundNonMatchingEnableStates = false;
83 for (Resource subscription : subscriptions) {
84 Boolean isIncluded = graph.getPossibleRelatedValue(subscription, mod.Subscription_Enabled);
85 if (isIncluded == null)
86 isIncluded = Boolean.TRUE;
87 if (isIncluded != include)
88 foundNonMatchingEnableStates = true;
90 if (!foundNonMatchingEnableStates)
91 return new IContributionItem[0];
93 return new IContributionItem[] {
94 new ActionContributionItem(new Action(name, image) {
97 Session session = SimanticsUI.peekSession();
101 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
103 public void perform(WriteGraph graph) throws DatabaseException {
104 new SetSubscriptionEnabled(subscriptions, include).perform(graph);