1 package org.simantics.modeling.ui.diagram;
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;
28 * @author Tuukka Lehtonen
30 public class ToggleProfileMonitorsContribution extends DynamicMenuContribution implements IExecutableExtension {
33 ImageDescriptor image = null;
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"));
50 protected Object[] getSelectedObjects() {
51 return ResourceAdaptionUtils.toResources(getSelection());
55 protected IContributionItem[] getContributionItems(ReadGraph graph, final Object[] elements) throws DatabaseException {
57 if(elements.length == 0) return NONE;
61 DiagramResource DIA = DiagramResource.getInstance(graph);
62 for(Object object : elements) {
63 Resource element = (Resource)object;
64 Boolean value = graph.getPossibleRelatedValue(element, DIA.Element_hideProfileMonitors, Bindings.BOOLEAN);
65 if(value == null) value = false;
66 if(value.equals(allow)) {
71 if(allOk) return NONE;
73 return new IContributionItem[] {
75 new ActionContributionItem(new Action(name, image) {
79 Simantics.getSession().markUndoPoint();
80 Simantics.async(new WriteRequest() {
83 public void perform(WriteGraph graph) throws DatabaseException {
84 Command command = Commands.get(graph, "Simantics/Diagram/showProfileMonitors");
85 for(Object object : elements) {
86 Resource element = (Resource)object;
87 command.execute(graph, graph.syncRequest(new IndexRoot(element)), element, allow);