X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2FToggleProfileMonitorsContribution.java;fp=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2Fdiagram%2FToggleProfileMonitorsContribution.java;h=5858b4d1d9853bf15817670cc6e609a9449a8ee0;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ToggleProfileMonitorsContribution.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ToggleProfileMonitorsContribution.java new file mode 100644 index 000000000..5858b4d1d --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ToggleProfileMonitorsContribution.java @@ -0,0 +1,99 @@ +package org.simantics.modeling.ui.diagram; + +import java.util.Map; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExecutableExtension; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.ActionContributionItem; +import org.eclipse.jface.action.IContributionItem; +import org.eclipse.jface.resource.ImageDescriptor; +import org.simantics.Simantics; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.IndexRoot; +import org.simantics.db.common.request.WriteRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.modeling.ui.Activator; +import org.simantics.scl.commands.Command; +import org.simantics.scl.commands.Commands; +import org.simantics.ui.contribution.DynamicMenuContribution; +import org.simantics.ui.utils.ResourceAdaptionUtils; + +/** + * @author Tuukka Lehtonen + */ +public class ToggleProfileMonitorsContribution extends DynamicMenuContribution implements IExecutableExtension { + + String name = ""; + ImageDescriptor image = null; + boolean allow = true; + + @Override + public void setInitializationData(IConfigurationElement config, String propertyName, Object data) + throws CoreException { + if (data instanceof Map) { + @SuppressWarnings("unchecked") + Map args = (Map) data; + name = args.get("name"); + String imageId = args.get("image"); + image = Activator.getDefault().getImageRegistry().getDescriptor(imageId); + allow = Boolean.parseBoolean(args.get("allow")); + } + } + + @Override + protected Object[] getSelectedObjects() { + return ResourceAdaptionUtils.toResources(getSelection()); + } + + @Override + protected IContributionItem[] getContributionItems(ReadGraph graph, final Object[] elements) throws DatabaseException { + + if(elements.length == 0) return NONE; + + Boolean allOk = true; + + DiagramResource DIA = DiagramResource.getInstance(graph); + for(Object object : elements) { + Resource element = (Resource)object; + Boolean value = graph.getPossibleRelatedValue(element, DIA.Element_hideProfileMonitors, Bindings.BOOLEAN); + if(value == null) value = false; + if(value.equals(allow)) { + allOk = false; + } + } + + if(allOk) return NONE; + + return new IContributionItem[] { + + new ActionContributionItem(new Action(name, image) { + @Override + public void run() { + + Simantics.getSession().markUndoPoint(); + Simantics.async(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + Command command = Commands.get(graph, "Simantics/Diagram/showProfileMonitors"); + for(Object object : elements) { + Resource element = (Resource)object; + command.execute(graph, graph.syncRequest(new IndexRoot(element)), element, allow); + } + } + + }); + + } + }) + }; + + } + +}