]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/ui/AddVariableToChartAction.java
Set strokeWidth for new chart items already upon creation
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / AddVariableToChartAction.java
index 4caa80a66037275d2cb8a364075c185496b41901..4d2a1317aedc07ca9d63faa5c43c05383d3e29d1 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.charts.ui;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.eclipse.jface.dialogs.Dialog;\r
-import org.eclipse.swt.widgets.Display;\r
-import org.eclipse.swt.widgets.Shell;\r
-import org.eclipse.ui.PlatformUI;\r
-import org.simantics.Simantics;\r
-import org.simantics.charts.query.AddChartItem;\r
-import org.simantics.charts.query.ChartAndSubscriptionItemData;\r
-import org.simantics.charts.query.ChartItemDescriptor;\r
-import org.simantics.charts.query.NextChartItemIndexQuery;\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.databoard.type.BooleanType;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.request.ReadRequest;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.request.PossibleModel;\r
-import org.simantics.db.layer0.variable.Variable;\r
-import org.simantics.db.layer0.variable.VariableReference;\r
-import org.simantics.db.layer0.variable.Variables;\r
-import org.simantics.layer0.Layer0;\r
-import org.simantics.modeling.subscription.NewSubscription;\r
-import org.simantics.modeling.subscription.NewSubscriptionItem;\r
-import org.simantics.modeling.subscription.SubscriptionsQuery;\r
-import org.simantics.modeling.subscription.SubscriptionsQuery.SubscriptionsResult;\r
-import org.simantics.trend.configuration.TrendItem.Renderer;\r
-import org.simantics.utils.ui.dialogs.ShowError;\r
-\r
-public class AddVariableToChartAction implements Runnable {\r
-\r
-       Resource chart;\r
-       Resource subscription;\r
-       Resource model;\r
-       \r
-       String chartName;\r
-       String subscriptionName;\r
-       SubscriptionsResult subscriptions;\r
-       List<VariableReference> refs;\r
-       List<String> variableReferences; // String references\r
-       \r
-       public AddVariableToChartAction( Resource chart, Resource subscription, List<VariableReference> refs ) {\r
-               this.refs = refs;\r
-               this.chart = chart;\r
-               this.subscription = subscription;\r
-       }\r
-       \r
-       /**\r
-        * Reads necessary data from session.\r
-        * \r
-        * @throws DatabaseException\r
-        */\r
-       public AddVariableToChartAction init() throws DatabaseException \r
-       {\r
-       Simantics.getSession().sync( new ReadRequest() {\r
-                       @Override\r
-                       public void run(ReadGraph g) throws DatabaseException {\r
-                               init(g);\r
-                       }\r
-               } );\r
-       return this;\r
-       }\r
-\r
-       /**\r
-        * Reads necessary data from session.\r
-        * \r
-        * @throws DatabaseException\r
-        */\r
-       public AddVariableToChartAction init( ReadGraph g ) throws DatabaseException \r
-       {\r
-       Layer0 L0 = Layer0.getInstance(g);\r
-       if(chart != null) {\r
-           chartName = g.getPossibleRelatedValue(chart, L0.HasName, Bindings.STRING);\r
-           model = g.syncRequest(new PossibleModel(chart));\r
-       }\r
-       else\r
-           model = g.syncRequest(new PossibleModel(subscription));\r
-        if (model == null) throw new DatabaseException("Model was not found");\r
-\r
-               Variable configuration = Variables.getConfigurationContext(g, model);\r
-        \r
-               subscriptions = g.sync( new SubscriptionsQuery(model) );\r
-               if(subscription != null)\r
-                   subscriptionName = g.getPossibleRelatedValue(subscription, L0.HasLabel, Bindings.STRING);\r
-               variableReferences = new ArrayList<String>();\r
-               \r
-               for (VariableReference var : refs) {\r
-                       if ( var.label == null ) {\r
-                               variableReferences.add( var.getVariableId().toPossibleString(g, configuration) );\r
-                       }\r
-               }\r
-\r
-               return this;\r
-       }\r
-       \r
-       /**\r
-        * Opens dialog, if clicked OK, writes to graph async.\r
-        * \r
-        * Remember to run this in SWT Thread.\r
-        */\r
-       public void runInSwt() {\r
-                               \r
-           final Display display = Display.getCurrent();\r
-       final Shell shell = display.getActiveShell();\r
-\r
-        Simantics.getSession().markUndoPoint();\r
-\r
-               for (int i=0; i<refs.size(); i++) {\r
-                       VariableReference var = refs.get(i);\r
-                       String varStrRef = variableReferences.get(i);\r
-\r
-               try {\r
-               ChartAndSubscriptionItemData data = new ChartAndSubscriptionItemData();\r
-               data.index = chart != null ? Simantics.sync(new NextChartItemIndexQuery(chart)) : -1;\r
-               data.mutableCollectionSettings = true;\r
-               data.chartName = chartName;\r
-               data.hasSubscriptionItem = true;\r
-               data.subscriptions = subscriptions.toNames();\r
-               data.subscription = subscriptionName;\r
-               data.binaryMode = var.getDatatype() instanceof BooleanType;\r
-               data.variableReference = varStrRef;             \r
-                               ChartAndSubscriptionItemDialog d = new ChartAndSubscriptionItemDialog(shell, data, chart != null); \r
-                               \r
-                       if ( d.open() == Dialog.OK ) {\r
-                               \r
-                               // (New) Subscription\r
-                               int j = subscriptions.names.indexOf( data.subscription );\r
-                               Resource subscription;\r
-                               if (j<0) {\r
-                                       // Create new subscription\r
-                                       NewSubscription ns = new NewSubscription(model, data.subscription);\r
-                                               Simantics.getSession().sync( ns );\r
-                                       subscription = ns.subscription;\r
-                               } else {\r
-                                       subscription = subscriptions.resources.get(j);\r
-                               }                       \r
-                               if ( subscription == null ) continue;\r
-                               \r
-                               \r
-                               // New Subscription Item\r
-                               NewSubscriptionItem ns = new NewSubscriptionItem(\r
-                                               subscription,\r
-                                               data.interval,\r
-                                               data.deadband,\r
-                                               data.gain,\r
-                                               data.bias,\r
-                                               data.unit,\r
-                                               data.label,\r
-                                               var.getVariableId(),\r
-                                               var.getDatatype()\r
-                                               );\r
-                               \r
-                               Simantics.getSession().sync( ns );\r
-                               if (ns.subscriptionItem == null) continue;\r
-                                                       \r
-                               // New Chart Item\r
-                               if(chart != null) {\r
-                               ChartItemDescriptor cid = new ChartItemDescriptor();\r
-                               cid.drawMode = data.drawmode;\r
-                               cid.renderer = var.getDatatype() instanceof BooleanType ? Renderer.Binary : Renderer.Analog;\r
-                               cid.scale = data.scale;\r
-                               cid.min = data.min;\r
-                               cid.max = data.max;\r
-                               cid.subscriptionItem = ns.subscriptionItem;\r
-                               cid.color = data.color;\r
-                               AddChartItem ci = new AddChartItem(     chart, Collections.singletonList( cid ) );\r
-                               Simantics.getSession().sync( ci );\r
-                               }\r
-                       }\r
-                       } catch (DatabaseException e) {\r
-                               ShowError.showError(e.getClass().getName(), e.getLocalizedMessage(), e);\r
-                       }\r
-                       \r
-               }\r
-                               \r
-       }\r
-       \r
-       /**\r
-        * Executes run in SWT Thread\r
-        */\r
-       public void run() {\r
-           Display display = PlatformUI.getWorkbench().getDisplay();\r
-               if ( display.getThread() == Thread.currentThread() ) {\r
-                       runInSwt();\r
-               } else {\r
-                       display.asyncExec( new Runnable() {\r
-                               @Override\r
-                               public void run() {\r
-                                       runInSwt();\r
-                               }} );\r
-               }\r
-       }\r
-       \r
-}\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.charts.ui;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.PlatformUI;
+import org.simantics.Simantics;
+import org.simantics.charts.query.AddChartItem;
+import org.simantics.charts.query.ChartAndSubscriptionItemData;
+import org.simantics.charts.query.ChartItemDescriptor;
+import org.simantics.charts.query.NextChartItemIndexQuery;
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.type.BooleanType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.request.ReadRequest;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PossibleModel;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.db.layer0.variable.VariableReference;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.subscription.NewSubscription;
+import org.simantics.modeling.subscription.NewSubscriptionItem;
+import org.simantics.modeling.subscription.SubscriptionsQuery;
+import org.simantics.modeling.subscription.SubscriptionsQuery.SubscriptionsResult;
+import org.simantics.trend.configuration.TrendItem.Renderer;
+import org.simantics.utils.ui.dialogs.ShowError;
+
+public class AddVariableToChartAction implements Runnable {
+
+       Resource chart;
+       Resource subscription;
+       Resource model;
+       
+       String chartName;
+       String subscriptionName;
+       SubscriptionsResult subscriptions;
+       List<VariableReference> refs;
+       List<String> variableReferences; // String references
+       
+       public AddVariableToChartAction( Resource chart, Resource subscription, List<VariableReference> refs ) {
+               this.refs = refs;
+               this.chart = chart;
+               this.subscription = subscription;
+       }
+       
+       /**
+        * Reads necessary data from session.
+        * 
+        * @throws DatabaseException
+        */
+       public AddVariableToChartAction init() throws DatabaseException 
+       {
+       Simantics.getSession().sync( new ReadRequest() {
+                       @Override
+                       public void run(ReadGraph g) throws DatabaseException {
+                               init(g);
+                       }
+               } );
+       return this;
+       }
+
+       /**
+        * Reads necessary data from session.
+        * 
+        * @throws DatabaseException
+        */
+       public AddVariableToChartAction init( ReadGraph g ) throws DatabaseException 
+       {
+       Layer0 L0 = Layer0.getInstance(g);
+       if(chart != null) {
+           chartName = g.getPossibleRelatedValue(chart, L0.HasName, Bindings.STRING);
+           model = g.syncRequest(new PossibleModel(chart));
+       }
+       else
+           model = g.syncRequest(new PossibleModel(subscription));
+        if (model == null) throw new DatabaseException("Model was not found");
+
+               Variable configuration = Variables.getConfigurationContext(g, model);
+        
+               subscriptions = g.sync( new SubscriptionsQuery(model) );
+               if(subscription != null)
+                   subscriptionName = g.getPossibleRelatedValue(subscription, L0.HasLabel, Bindings.STRING);
+               variableReferences = new ArrayList<String>();
+               
+               for (VariableReference var : refs) {
+                       if ( var.label == null ) {
+                               variableReferences.add( var.getVariableId().toPossibleString(g, configuration) );
+                       }
+               }
+
+               return this;
+       }
+       
+       /**
+        * Opens dialog, if clicked OK, writes to graph async.
+        * 
+        * Remember to run this in SWT Thread.
+        */
+       public void runInSwt() {
+                               
+           final Display display = Display.getCurrent();
+       final Shell shell = display.getActiveShell();
+
+        Simantics.getSession().markUndoPoint();
+
+               for (int i=0; i<refs.size(); i++) {
+                       VariableReference var = refs.get(i);
+                       String varStrRef = variableReferences.get(i);
+
+               try {
+               ChartAndSubscriptionItemData data = new ChartAndSubscriptionItemData();
+               data.index = chart != null ? Simantics.sync(new NextChartItemIndexQuery(chart)) : -1;
+               data.mutableCollectionSettings = true;
+               data.chartName = chartName;
+               data.hasSubscriptionItem = true;
+               data.subscriptions = subscriptions.toNames();
+               data.subscription = subscriptionName;
+               data.binaryMode = var.getDatatype() instanceof BooleanType;
+               data.variableReference = varStrRef;             
+                               ChartAndSubscriptionItemDialog d = new ChartAndSubscriptionItemDialog(shell, data, chart != null); 
+                               
+                       if ( d.open() == Dialog.OK ) {
+                               
+                               // (New) Subscription
+                               int j = subscriptions.names.indexOf( data.subscription );
+                               Resource subscription;
+                               if (j<0) {
+                                       // Create new subscription
+                                       NewSubscription ns = new NewSubscription(model, data.subscription);
+                                               Simantics.getSession().sync( ns );
+                                       subscription = ns.subscription;
+                               } else {
+                                       subscription = subscriptions.resources.get(j);
+                               }                       
+                               if ( subscription == null ) continue;
+                               
+                               
+                               // New Subscription Item
+                               NewSubscriptionItem ns = new NewSubscriptionItem(
+                                               subscription,
+                                               data.interval,
+                                               data.deadband,
+                                               data.gain,
+                                               data.bias,
+                                               data.unit,
+                                               data.label,
+                                               var.getVariableId(),
+                                               var.getDatatype()
+                                               );
+                               
+                               Simantics.getSession().sync( ns );
+                               if (ns.subscriptionItem == null) continue;
+                                                       
+                               // New Chart Item
+                               if(chart != null) {
+                               ChartItemDescriptor cid = new ChartItemDescriptor();
+                               cid.drawMode = data.drawmode;
+                               cid.renderer = var.getDatatype() instanceof BooleanType ? Renderer.Binary : Renderer.Analog;
+                               cid.scale = data.scale;
+                               cid.min = data.min;
+                               cid.max = data.max;
+                               cid.subscriptionItem = ns.subscriptionItem;
+                               cid.color = data.color;
+                               cid.strokeWidth = data.strokeWidth;
+                               AddChartItem ci = new AddChartItem(     chart, Collections.singletonList( cid ) );
+                               Simantics.getSession().sync( ci );
+                               }
+                       }
+                       } catch (DatabaseException e) {
+                               ShowError.showError(e.getClass().getName(), e.getLocalizedMessage(), e);
+                       }
+                       
+               }
+                               
+       }
+       
+       /**
+        * Executes run in SWT Thread
+        */
+       public void run() {
+           Display display = PlatformUI.getWorkbench().getDisplay();
+               if ( display.getThread() == Thread.currentThread() ) {
+                       runInSwt();
+               } else {
+                       display.asyncExec( new Runnable() {
+                               @Override
+                               public void run() {
+                                       runInSwt();
+                               }} );
+               }
+       }
+       
+}
+