1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.ui;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.PlatformUI;
22 import org.simantics.Simantics;
23 import org.simantics.charts.query.AddChartItem;
24 import org.simantics.charts.query.ChartAndSubscriptionItemData;
25 import org.simantics.charts.query.ChartItemDescriptor;
26 import org.simantics.charts.query.NextChartItemIndexQuery;
27 import org.simantics.databoard.Bindings;
28 import org.simantics.databoard.type.BooleanType;
29 import org.simantics.db.ReadGraph;
30 import org.simantics.db.Resource;
31 import org.simantics.db.common.request.ReadRequest;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.layer0.request.PossibleModel;
34 import org.simantics.db.layer0.variable.Variable;
35 import org.simantics.db.layer0.variable.VariableReference;
36 import org.simantics.db.layer0.variable.Variables;
37 import org.simantics.layer0.Layer0;
38 import org.simantics.modeling.subscription.NewSubscription;
39 import org.simantics.modeling.subscription.NewSubscriptionItem;
40 import org.simantics.modeling.subscription.SubscriptionsQuery;
41 import org.simantics.modeling.subscription.SubscriptionsQuery.SubscriptionsResult;
42 import org.simantics.trend.configuration.TrendItem.Renderer;
43 import org.simantics.utils.ui.dialogs.ShowError;
45 public class AddVariableToChartAction implements Runnable {
48 Resource subscription;
52 String subscriptionName;
53 SubscriptionsResult subscriptions;
54 List<VariableReference> refs;
55 List<String> variableReferences; // String references
57 public AddVariableToChartAction( Resource chart, Resource subscription, List<VariableReference> refs ) {
60 this.subscription = subscription;
64 * Reads necessary data from session.
66 * @throws DatabaseException
68 public AddVariableToChartAction init() throws DatabaseException
70 Simantics.getSession().sync( new ReadRequest() {
72 public void run(ReadGraph g) throws DatabaseException {
80 * Reads necessary data from session.
82 * @throws DatabaseException
84 public AddVariableToChartAction init( ReadGraph g ) throws DatabaseException
86 Layer0 L0 = Layer0.getInstance(g);
88 chartName = g.getPossibleRelatedValue(chart, L0.HasName, Bindings.STRING);
89 model = g.syncRequest(new PossibleModel(chart));
92 model = g.syncRequest(new PossibleModel(subscription));
93 if (model == null) throw new DatabaseException("Model was not found");
95 Variable configuration = Variables.getConfigurationContext(g, model);
97 subscriptions = g.sync( new SubscriptionsQuery(model) );
98 if(subscription != null)
99 subscriptionName = g.getPossibleRelatedValue(subscription, L0.HasLabel, Bindings.STRING);
100 variableReferences = new ArrayList<String>();
102 for (VariableReference var : refs) {
103 if ( var.label == null ) {
104 variableReferences.add( var.getVariableId().toPossibleString(g, configuration) );
112 * Opens dialog, if clicked OK, writes to graph async.
114 * Remember to run this in SWT Thread.
116 public void runInSwt() {
118 final Display display = Display.getCurrent();
119 final Shell shell = display.getActiveShell();
121 Simantics.getSession().markUndoPoint();
123 for (int i=0; i<refs.size(); i++) {
124 VariableReference var = refs.get(i);
125 String varStrRef = variableReferences.get(i);
128 ChartAndSubscriptionItemData data = new ChartAndSubscriptionItemData();
129 data.index = chart != null ? Simantics.sync(new NextChartItemIndexQuery(chart)) : -1;
130 data.mutableCollectionSettings = true;
131 data.chartName = chartName;
132 data.hasSubscriptionItem = true;
133 data.subscriptions = subscriptions.toNames();
134 data.subscription = subscriptionName;
135 data.binaryMode = var.getDatatype() instanceof BooleanType;
136 data.variableReference = varStrRef;
137 ChartAndSubscriptionItemDialog d = new ChartAndSubscriptionItemDialog(shell, data, chart != null);
139 if ( d.open() == Dialog.OK ) {
141 // (New) Subscription
142 int j = subscriptions.names.indexOf( data.subscription );
143 Resource subscription;
145 // Create new subscription
146 NewSubscription ns = new NewSubscription(model, data.subscription);
147 Simantics.getSession().sync( ns );
148 subscription = ns.subscription;
150 subscription = subscriptions.resources.get(j);
152 if ( subscription == null ) continue;
155 // New Subscription Item
156 NewSubscriptionItem ns = new NewSubscriptionItem(
168 Simantics.getSession().sync( ns );
169 if (ns.subscriptionItem == null) continue;
173 ChartItemDescriptor cid = new ChartItemDescriptor();
174 cid.drawMode = data.drawmode;
175 cid.renderer = var.getDatatype() instanceof BooleanType ? Renderer.Binary : Renderer.Analog;
176 cid.scale = data.scale;
179 cid.subscriptionItem = ns.subscriptionItem;
180 cid.color = data.color;
181 cid.strokeWidth = data.strokeWidth;
182 AddChartItem ci = new AddChartItem( chart, Collections.singletonList( cid ) );
183 Simantics.getSession().sync( ci );
186 } catch (DatabaseException e) {
187 ShowError.showError(e.getClass().getName(), e.getLocalizedMessage(), e);
195 * Executes run in SWT Thread
198 Display display = PlatformUI.getWorkbench().getDisplay();
199 if ( display.getThread() == Thread.currentThread() ) {
202 display.asyncExec( new Runnable() {