]> gerrit.simantics Code Review - simantics/platform.git/blob
4943ce35268fe4122769bf946a734d73b9f941b9
[simantics/platform.git] /
1 package org.simantics.modeling.ui.modelBrowser2.contributions;
2
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Map;
7 import java.util.Set;
8
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;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class SetSubscriptionEnabledContribution extends DynamicMenuContribution implements IExecutableExtension {
33
34     String          name    = "";
35     ImageDescriptor image   = null;
36     boolean         include = true;
37
38     @Override
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"));
48         }
49     }
50
51     private Collection<Resource> toSubscriptions(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
52         ModelingResources mod = ModelingResources.getInstance(graph);
53
54         Set<Resource> result = new HashSet<Resource>(resources.size());
55         for (Resource o : resources) {
56             if (graph.isInstanceOf(o, mod.Subscription))
57                 result.add(o);
58         }
59
60         return result;
61     }
62
63     @Override
64     protected Object[] getSelectedObjects() {
65         return ResourceAdaptionUtils.toResources(getSelection());
66     }
67
68     @Override
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];
73
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];
79
80         ModelingResources mod = ModelingResources.getInstance(graph);
81
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;
89         }
90         if (!foundNonMatchingEnableStates)
91             return new IContributionItem[0];
92
93         return new IContributionItem[] {
94                 new ActionContributionItem(new Action(name, image) {
95                     @Override
96                     public void run() {
97                         Session session = SimanticsUI.peekSession();
98                         if (session == null)
99                             return;
100
101                         SimanticsUI.getSession().asyncRequest(new WriteRequest() {
102                             @Override
103                             public void perform(WriteGraph graph) throws DatabaseException {
104                                 new SetSubscriptionEnabled(subscriptions, include).perform(graph);
105                             }
106                         });
107                     }
108                 })
109         };
110     }
111
112 }