]> gerrit.simantics Code Review - simantics/platform.git/blob
eda7fc977a852b6255b400020a88078710e2c08a
[simantics/platform.git] /
1 package org.simantics.modeling.ui.diagram;
2
3 import java.util.Map;
4
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.core.runtime.IConfigurationElement;
7 import org.eclipse.core.runtime.IExecutableExtension;
8 import org.eclipse.jface.action.Action;
9 import org.eclipse.jface.action.ActionContributionItem;
10 import org.eclipse.jface.action.IContributionItem;
11 import org.eclipse.jface.resource.ImageDescriptor;
12 import org.simantics.Simantics;
13 import org.simantics.databoard.Bindings;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteGraph;
17 import org.simantics.db.common.request.IndexRoot;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.diagram.stubs.DiagramResource;
21 import org.simantics.modeling.ui.Activator;
22 import org.simantics.scl.commands.Command;
23 import org.simantics.scl.commands.Commands;
24 import org.simantics.ui.contribution.DynamicMenuContribution;
25 import org.simantics.ui.utils.ResourceAdaptionUtils;
26
27 /**
28  * @author Tuukka Lehtonen
29  */
30 public class UpDownProfileMonitorsContribution extends DynamicMenuContribution implements IExecutableExtension {
31
32     String          name    = "";
33     ImageDescriptor image   = null;
34     boolean         allow = true;
35
36     @Override
37     public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
38     throws CoreException {
39         if (data instanceof Map<?,?>) {
40             @SuppressWarnings("unchecked")
41             Map<String, String> args = (Map<String, String>) data;
42             name = args.get("name");
43             String imageId = args.get("image");
44             image = Activator.getDefault().getImageRegistry().getDescriptor(imageId);
45             allow = Boolean.parseBoolean(args.get("allow"));
46         }
47     }
48
49     @Override
50     protected Object[] getSelectedObjects() {
51         return ResourceAdaptionUtils.toResources(getSelection());
52     }
53
54     @Override
55     protected IContributionItem[] getContributionItems(ReadGraph graph, final Object[] elements) throws DatabaseException {
56
57         if(elements.length == 0) return NONE;
58
59         Boolean allOk = true;
60
61         DiagramResource DIA = DiagramResource.getInstance(graph);
62         for(Object object : elements) {
63             Resource element = (Resource)object;
64             Boolean value = graph.getPossibleRelatedValue(element, DIA.Element_upProfileMonitors, Bindings.BOOLEAN);
65             if(value == null) value = true;
66             if(!value.equals(allow)) {
67                 allOk = false;
68             }
69         }
70
71         if(allOk) return NONE;
72
73         return new IContributionItem[] {
74
75                 new ActionContributionItem(new Action(name, image) {
76                     @Override
77                     public void run() {
78
79                         Simantics.getSession().markUndoPoint();
80                         Simantics.async(new WriteRequest() {
81
82                             @Override
83                             public void perform(WriteGraph graph) throws DatabaseException {
84                                 Command command = Commands.get(graph, "Simantics/Diagram/setProfileMonitorsDirectionUp");
85                                 for(Object object : elements) {
86                                     Resource element = (Resource)object;
87                                     command.execute(graph, graph.syncRequest(new IndexRoot(element)), element, allow);
88                                 }
89                             }
90
91                         });
92
93                     }
94                 })
95         };
96
97     }
98
99 }