]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/chart/property/ChartComposite.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / chart / property / ChartComposite.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.chart.property;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.jface.dialogs.IInputValidator;
17 import org.eclipse.jface.layout.GridDataFactory;
18 import org.eclipse.jface.layout.GridLayoutFactory;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.ui.IWorkbenchSite;
26 import org.simantics.NameLabelUtil;
27 import org.simantics.Simantics;
28 import org.simantics.browsing.ui.swt.widgets.Button;
29 import org.simantics.browsing.ui.swt.widgets.Combo;
30 import org.simantics.browsing.ui.swt.widgets.Label;
31 import org.simantics.browsing.ui.swt.widgets.Scale;
32 import org.simantics.browsing.ui.swt.widgets.TrackedText;
33 import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl;
34 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
35 import org.simantics.charts.ontology.ChartResource;
36 import org.simantics.databoard.Bindings;
37 import org.simantics.db.ReadGraph;
38 import org.simantics.db.Resource;
39 import org.simantics.db.WriteGraph;
40 import org.simantics.db.common.request.WriteRequest;
41 import org.simantics.db.exception.DatabaseException;
42 import org.simantics.db.management.ISessionContext;
43 import org.simantics.db.request.Read;
44 import org.simantics.modeling.ui.Activator;
45 import org.simantics.selectionview.ConfigurationComposite;
46 import org.simantics.ui.utils.ResourceAdaptionUtils;
47 import org.simantics.utils.ui.ISelectionUtils;
48
49 /**
50  * @author Tuukka Lehtonen
51  */
52 public class ChartComposite extends ConfigurationComposite {
53
54     public static final TimeInputValidator LENGTH_VALIDATOR = new TimeInputValidator(1.0);
55     public static final TimeInputValidator START_VALIDATOR = new TimeInputValidator(-Double.MAX_VALUE); 
56
57     public static Read<String> getPartTitleRequest(final ISelection selection) {
58         return new Read<String>() {
59             @Override
60             public String perform(ReadGraph graph) throws DatabaseException {
61                 Resource r = ResourceAdaptionUtils.toSingleResource(selection);
62                 if (r == null)
63                     return "Selection";
64                 return NameLabelUtil.modalName(graph, r);
65             }
66         };
67     }
68
69     @Override
70     public Read<String> getPartNameReadRequest(ISelection forSelection) {
71         return getPartTitleRequest(forSelection);
72     }
73
74     public void create(final Composite body, IWorkbenchSite site, final ISessionContext context, final WidgetSupport support) {
75         final Display display = body.getDisplay();
76
77         body.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
78
79 /*        Composite buttonComposite = new Composite(body, SWT.NONE);
80         buttonComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
81         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(buttonComposite);
82         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(buttonComposite);
83         
84         Label nameHeader = new Label(buttonComposite, support, 0);
85         nameHeader.setText("Name");
86         //nameHeader.setFont(smallFont);
87         nameHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
88         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(nameHeader.getWidget());
89
90         TrackedText nameText = new TrackedText(buttonComposite, support, SWT.BORDER | SWT.FLAT);
91         nameText.setTextFactory(new StringPropertyFactory(Layer0.URIs.HasName, ""));
92         nameText.addModifyListener(new StringPropertyModifier(context, Layer0.URIs.HasName));
93         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(nameText.getWidget());
94 */
95         Label autoscrollLabel = new Label(body, support, 0);
96         autoscrollLabel.setText("Auto-scroll settings:");
97         autoscrollLabel.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
98         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(autoscrollLabel.getControl());
99         
100         Composite templateComposite = new Composite(body, SWT.NONE);
101         templateComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
102         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(templateComposite);
103         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(3).extendedMargins(5,5,5,5).applyTo(templateComposite);
104
105         Label templateHeader = new Label(templateComposite, support, 0);
106         templateHeader.setText("Template");
107         //templateHeader.setFont(smallFont);
108         templateHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
109         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(templateHeader.getWidget());
110
111         Combo templateCombo = new Combo(templateComposite, support, SWT.NONE | SWT.READ_ONLY);
112         templateCombo.setItemFactory(new AvailableTemplateFactory());
113         templateCombo.setSelectionFactory(new CurrentTemplateFactory());
114         templateCombo.addModifyListener(new TemplateModifier());
115         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(templateCombo.getWidget());
116
117         Button resetButton = new Button(templateComposite, support, SWT.NONE | SWT.READ_ONLY);
118         resetButton.setText("Apply");
119         resetButton.addSelectionListener(new SelectionListenerImpl<Resource>(context) {
120             @Override
121             public void apply(WriteGraph graph, Resource monitor) throws DatabaseException {
122                 graph.markUndoPoint();
123                 ChartResource CHART = ChartResource.getInstance(graph);
124                 if (graph.hasStatement(monitor, CHART.Chart_TimeWindowStart))
125                     graph.deny(monitor, CHART.Chart_TimeWindowStart);
126                 if (graph.hasStatement(monitor, CHART.Chart_TimeWindowLength))
127                     graph.deny(monitor, CHART.Chart_TimeWindowLength);
128                 if (graph.hasStatement(monitor, CHART.Chart_TimeWindowIncrement))
129                     graph.deny(monitor, CHART.Chart_TimeWindowIncrement);
130             }
131         });
132         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).applyTo(resetButton.getWidget());
133
134         Composite buttonComposite = new Composite(body, SWT.NONE);
135         buttonComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
136         GridDataFactory.fillDefaults().span(2, 1).grab(true, false).applyTo(buttonComposite);
137         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(4).extendedMargins(5,5,5,5).applyTo(buttonComposite);
138
139         Label startHeader = new Label(buttonComposite, support, 0);
140         startHeader.setText("Start time");
141         //startHeader.setFont(smallFont);
142         startHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
143         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(startHeader.getWidget());
144
145         TrackedText timeWindowStart = new TrackedText(buttonComposite, support, SWT.BORDER | SWT.FLAT);
146         timeWindowStart.getWidget().setToolTipText("Chart Window Fixed Start Time in Seconds or Yy Dd HH:mm:ss.ddd");
147         timeWindowStart.setTextFactory(new TimePropertyFactory(ChartResource.URIs.Chart_TimeWindowStart));
148         timeWindowStart.addModifyListener(new TimePropertyModifier(context, ChartResource.URIs.Chart_TimeWindowStart, START_VALIDATOR));
149         timeWindowStart.setInputValidator( START_VALIDATOR );
150         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(timeWindowStart.getWidget());
151
152 //        Label l1 = new Label(buttonComposite, support, 0);
153 //        l1.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
154 //        l1.setText("seconds");
155
156         Label sizeHeader = new Label(buttonComposite, support, 0);
157         sizeHeader.setText("Length");
158         //sizeHeader.setFont(smallFont);
159         sizeHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
160         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(sizeHeader.getWidget());
161
162         TrackedText timeWindowLength = new TrackedText(buttonComposite, support, SWT.BORDER | SWT.FLAT);
163         timeWindowLength.getWidget().setToolTipText("Chart Window Fixed Time Axis Length in Seconds or Yy Dd HH:mm:ss.ddd");
164         timeWindowLength.setTextFactory(new TimePropertyFactory(ChartResource.URIs.Chart_TimeWindowLength));
165         timeWindowLength.addModifyListener(new TimePropertyModifier(context, ChartResource.URIs.Chart_TimeWindowLength, LENGTH_VALIDATOR));
166         timeWindowLength.setInputValidator( LENGTH_VALIDATOR );
167         GridDataFactory.fillDefaults().grab(true, false).span(3, 1).applyTo(timeWindowLength.getWidget());
168
169 //        Label l2 = new Label(buttonComposite, support, 0);
170 //        l2.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
171 //        l2.setText("seconds");
172
173         Label incrementHeader = new Label(buttonComposite, support, 0);
174         incrementHeader.setText("Scroll increment");
175         //incrementHeader.setFont(smallFont);
176         incrementHeader.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
177         GridDataFactory.fillDefaults().grab(false, false).span(1, 1).align(SWT.LEFT, SWT.CENTER).applyTo(incrementHeader.getWidget());
178
179         Scale timeWindowIncrement = new Scale(buttonComposite, support, SWT.READ_ONLY);
180         timeWindowIncrement.getWidget().setMinimum(1);
181         timeWindowIncrement.getWidget().setMaximum(100);
182         timeWindowIncrement.getWidget().setIncrement(10);
183         timeWindowIncrement.getWidget().setPageIncrement(10);
184         timeWindowIncrement.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
185         timeWindowIncrement.setSelectionFactory(new DoubleToIntegerPropertyFactory(ChartResource.URIs.Chart_TimeWindowIncrement));
186         timeWindowIncrement.addSelectionListener(new DelayedIntegerToDoublePropertyModifier(timeWindowIncrement, context, ChartResource.URIs.Chart_TimeWindowIncrement));
187         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(timeWindowIncrement.getWidget());
188
189         TrackedText timeWindowIncrementText = new TrackedText(buttonComposite, support, SWT.BORDER | SWT.FLAT);
190         timeWindowIncrementText.setTextFactory(new DoublePropertyFactory(ChartResource.URIs.Chart_TimeWindowIncrement));
191         timeWindowIncrementText.addModifyListener(new DoublePropertyModifier(context, ChartResource.URIs.Chart_TimeWindowIncrement));
192         timeWindowIncrementText.setInputValidator(new IInputValidator() {
193             @Override
194             public String isValid(String newText) {
195                 if (newText.trim().isEmpty())
196                     return null;
197                 try {
198                     Double d = Double.parseDouble(newText);
199                     if (d < 1 || d > 100)
200                         return "Increment must be between 1..100";
201                     return null;
202                 } catch (NumberFormatException e) {
203                     return e.getMessage();
204                 }
205             }
206         });
207         GridDataFactory.fillDefaults().grab(false, false).align(SWT.LEAD, SWT.CENTER).span(1, 1).applyTo(timeWindowIncrementText.getWidget());
208
209         Label l3 = new Label(buttonComposite, support, 0);
210         l3.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
211         l3.setText("%");
212
213         Composite checkboxComposite = new Composite(buttonComposite, SWT.NONE);
214         checkboxComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
215         GridDataFactory.fillDefaults().grab(true, false).span(2, 1).applyTo(checkboxComposite);
216         GridLayoutFactory.fillDefaults().equalWidth(false).numColumns(2).applyTo(checkboxComposite);
217
218         final Button showMilestones = new Button(checkboxComposite, support, SWT.CHECK);
219         showMilestones.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
220         showMilestones.setText("Show &Milestones");
221         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(showMilestones.getWidget());
222         showMilestones.setSelectionFactory(new BooleanPropertyFactory(ChartResource.URIs.Chart_ShowMilestones));
223         showMilestones.addSelectionListener(new SelectionAdapter() {
224             @Override
225             public void widgetSelected(SelectionEvent e) {
226                 try {
227                     final Resource r = ISelectionUtils.filterSingleSelection(support.getInput(), Resource.class);
228                     if (r == null)
229                         return;
230                     final Boolean show = showMilestones.getWidget().getSelection();
231                     Simantics.getSession().syncRequest(new WriteRequest() {
232                         @Override
233                         public void perform(WriteGraph graph) throws DatabaseException {
234                             graph.markUndoPoint();
235                             graph.claimLiteral(r, ChartResource.getInstance(graph).Chart_ShowMilestones, show, Bindings.BOOLEAN);
236                         }
237                     });
238                 } catch (DatabaseException e1) {
239                     Activator.getDefault().getLog().log(
240                             new Status(IStatus.ERROR, Activator.PLUGIN_ID,
241                                     "Failed to set Show Milestones", e1));
242                 }
243             }
244         });
245
246         final Button trackExperimentTime = new Button(checkboxComposite, support, SWT.CHECK);
247         trackExperimentTime.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
248         trackExperimentTime.setText("Hairline &Tracks Experiment Time");
249         trackExperimentTime.setTooltipText("Value Tip Hairline Tracks Experiment Time During Simulation");
250         GridDataFactory.fillDefaults().grab(true, false).span(1, 1).applyTo(trackExperimentTime.getWidget());
251         trackExperimentTime.setSelectionFactory(new BooleanPropertyFactory(ChartResource.URIs.Chart_trackExperimentTime));
252         trackExperimentTime.addSelectionListener(new SelectionAdapter() {
253             @Override
254             public void widgetSelected(SelectionEvent e) {
255                 try {
256                     final Resource r = ISelectionUtils.filterSingleSelection(support.getInput(), Resource.class);
257                     if (r == null)
258                         return;
259                     final Boolean track = trackExperimentTime.getWidget().getSelection();
260                     Simantics.getSession().syncRequest(new WriteRequest() {
261                         @Override
262                         public void perform(WriteGraph graph) throws DatabaseException {
263                             graph.markUndoPoint();
264                             graph.claimLiteral(r, ChartResource.getInstance(graph).Chart_trackExperimentTime, track, Bindings.BOOLEAN);
265                         }
266                     });
267                 } catch (DatabaseException e1) {
268                     Activator.getDefault().getLog().log(
269                             new Status(IStatus.ERROR, Activator.PLUGIN_ID,
270                                     "Failed to set Track Experiment Time", e1));
271                 }
272             }
273         });
274     }
275
276 }