1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.menu;
\r
14 import java.text.DecimalFormat;
\r
15 import java.text.DecimalFormatSymbols;
\r
17 import org.eclipse.jface.action.ToolBarContributionItem;
\r
18 import org.eclipse.jface.layout.GridDataFactory;
\r
19 import org.eclipse.jface.layout.GridLayoutFactory;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.events.SelectionEvent;
\r
22 import org.eclipse.swt.events.SelectionListener;
\r
23 import org.eclipse.swt.widgets.Composite;
\r
24 import org.eclipse.swt.widgets.Label;
\r
25 import org.eclipse.swt.widgets.Slider;
\r
26 import org.eclipse.swt.widgets.ToolBar;
\r
27 import org.eclipse.swt.widgets.ToolItem;
\r
28 import org.simantics.db.ReadGraph;
\r
29 import org.simantics.db.Resource;
\r
30 import org.simantics.db.exception.DatabaseException;
\r
31 import org.simantics.db.request.Read;
\r
32 import org.simantics.simulation.experiment.IExperiment;
\r
33 import org.simantics.simulation.project.IExperimentManager;
\r
34 import org.simantics.sysdyn.SysdynResource;
\r
35 import org.simantics.sysdyn.manager.SysdynPlaybackExperiment;
\r
36 import org.simantics.ui.SimanticsUI;
\r
39 * Contribution to the main toolbar. PlaybackSliderContribution contains a slider
\r
40 * that can be used to control the time in a playback experiment
\r
42 * @author Teemu Lempinen
\r
45 public class PlaybackSliderContribution extends ToolBarContributionItem {
\r
47 Runnable timeListener;
\r
48 SysdynPlaybackExperiment spe;
\r
50 Double startTime, endTime;
\r
51 boolean ignoreChange = false;
\r
54 public void fill(ToolBar parent, int index)
\r
56 if (parent != null) {
\r
58 IExperimentManager manager = SimanticsUI.getProject().getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
59 IExperiment experiment = manager.getActiveExperiment();
\r
60 if(experiment == null || !(experiment instanceof SysdynPlaybackExperiment))
\r
62 spe = (SysdynPlaybackExperiment)experiment;
\r
64 Double[] numbers = new Double[2];
\r
66 numbers = SimanticsUI.getSession().syncRequest(new Read<Double[]>() {
\r
68 public Double[] perform(ReadGraph graph) throws DatabaseException {
\r
69 Double[] numbers = new Double[2];
\r
70 Resource model = spe.getModel();
\r
71 SysdynResource sr = SysdynResource.getInstance(graph);
\r
72 numbers[0] = graph.getRelatedValue(model, sr.SysdynModel_startTime);
\r
73 numbers[1] = graph.getRelatedValue(model, sr.SysdynModel_stopTime);
\r
77 } catch (DatabaseException e1) {
\r
78 e1.printStackTrace();
\r
81 // Separator ToolItem can contain a composite. Add a composite with a slider to this item
\r
82 ToolItem ti = new ToolItem(parent, SWT.SEPARATOR);
\r
84 Composite composite = new Composite(parent, SWT.NONE);
\r
85 GridLayoutFactory.fillDefaults().margins(3, SWT.DEFAULT).numColumns(2).applyTo(composite);
\r
86 GridDataFactory.fillDefaults().applyTo(composite);
\r
88 s = new Slider(composite, SWT.NONE);
\r
92 s.setPageIncrement(1);
\r
95 final Label label = new Label(composite, SWT.NONE);
\r
96 GridDataFactory.fillDefaults().hint(70, SWT.DEFAULT).applyTo(label);
\r
97 label.setText("0.0");
\r
100 ti.setControl(composite);
\r
102 startTime = numbers[0];
\r
103 endTime = numbers[1];
\r
105 // Create a DesimalFormat for rounding the time
\r
106 final DecimalFormat format = new DecimalFormat();
\r
107 format.setMinimumFractionDigits(0);
\r
108 format.setMaximumFractionDigits(2);
\r
109 DecimalFormatSymbols symbols = new DecimalFormatSymbols();
\r
110 symbols.setDecimalSeparator('.');
\r
111 symbols.setGroupingSeparator(' ');
\r
112 format.setDecimalFormatSymbols(symbols);
\r
114 // Selection listener for the slider
\r
115 s.addSelectionListener(new SelectionListener() {
\r
118 public void widgetSelected(SelectionEvent e) {
\r
119 Slider s = (Slider)e.widget;
\r
120 Double time = s.getSelection() / 99.0 * (endTime - startTime) + startTime;
\r
121 spe.setTimeAndContinue(time);
\r
122 if(!label.isDisposed()) {
\r
123 label.setText(format.format(time));
\r
128 public void widgetDefaultSelected(SelectionEvent e) {
\r
132 if(timeListener != null) {
\r
133 spe.removeTimeListener(timeListener);
\r
136 // Time listener for setting the time in the slider if the time is changed somewhere else
\r
137 timeListener = new Runnable() {
\r
140 public void run() {
\r
141 s.getDisplay().asyncExec(new Runnable() {
\r
144 public void run() {
\r
145 if(!startTime.equals(spe.getStartTime()) || !endTime.equals(spe.getEndTime())) {
\r
146 startTime = spe.getStartTime();
\r
147 endTime = spe.getEndTime();
\r
149 int value = (int) Math.round(((spe.getTime() - startTime) / (endTime - startTime) * 99));
\r
150 s.setSelection(value);
\r
151 label.setText(format.format(spe.getTime()));
\r
158 spe.addTimeListener(timeListener);
\r
163 public void dispose() {
\r
164 if(this.timeListener != null && spe != null) {
\r
165 spe.removeTimeListener(timeListener);
\r
166 this.timeListener = null;
\r