]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/diagram/ResetProfileMonitorTransformContribution.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / diagram / ResetProfileMonitorTransformContribution.java
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.db.ReadGraph;
14 import org.simantics.db.Resource;
15 import org.simantics.db.Statement;
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 ResetProfileMonitorTransformContribution extends DynamicMenuContribution implements IExecutableExtension {
31
32     String          name    = ""; //$NON-NLS-1$
33     ImageDescriptor image   = null;
34
35     @Override
36     public void setInitializationData(IConfigurationElement config, String propertyName, Object data)
37             throws CoreException {
38         if (data instanceof Map<?,?>) {
39             @SuppressWarnings("unchecked")
40             Map<String, String> args = (Map<String, String>) data;
41             name = args.get("name"); //$NON-NLS-1$
42             String imageId = args.get("image"); //$NON-NLS-1$
43             image = Activator.getDefault().getImageRegistry().getDescriptor(imageId);
44         }
45     }
46
47     @Override
48     protected Object[] getSelectedObjects() {
49         return ResourceAdaptionUtils.toResources(getSelection());
50     }
51
52     @Override
53     protected IContributionItem[] getContributionItems(ReadGraph graph, final Object[] elements) throws DatabaseException {
54         if (elements.length == 0)
55             return NONE;
56
57         boolean todo = false;
58         DiagramResource DIA = DiagramResource.getInstance(graph);
59         for (Object object : elements) {
60             Resource element = (Resource)object;
61             Statement stm = graph.getPossibleStatement(element, DIA.Element_profileMonitorOffset);
62             if (stm != null && !stm.isAsserted(element)) {
63                 todo = true;
64                 break;
65             }
66         }
67
68         if (!todo)
69             return NONE;
70
71         return new IContributionItem[] {
72                 new ActionContributionItem(new Action(name, image) {
73                     @Override
74                     public void run() {
75                         Simantics.async(new WriteRequest() {
76                             @Override
77                             public void perform(WriteGraph graph) throws DatabaseException {
78                                 Command cmd = Commands.get(graph, "Simantics/Profile/resetProfileMonitorPosition"); //$NON-NLS-1$
79                                 Resource model = graph.syncRequest(new IndexRoot((Resource)elements[0]));
80                                 for(Object element : elements)
81                                     cmd.execute(graph, model, element);
82                             }
83                         });
84                     }
85                 })
86         };
87     }
88
89 }