1 /*******************************************************************************
\r
2 * Copyright (c) 2013 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 * Semantum Oy - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.properties.widgets.sensitivity;
\r
14 import java.util.HashMap;
\r
16 import org.eclipse.jface.layout.GridDataFactory;
\r
17 import org.eclipse.jface.layout.GridLayoutFactory;
\r
18 import org.eclipse.jface.resource.JFaceResources;
\r
19 import org.eclipse.jface.resource.LocalResourceManager;
\r
20 import org.eclipse.swt.SWT;
\r
21 import org.eclipse.swt.widgets.Composite;
\r
22 import org.eclipse.swt.widgets.Label;
\r
23 import org.simantics.browsing.ui.swt.widgets.TrackedText;
\r
24 import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;
\r
25 import org.simantics.databoard.Bindings;
\r
26 import org.simantics.db.Resource;
\r
27 import org.simantics.db.WriteGraph;
\r
28 import org.simantics.db.exception.DatabaseException;
\r
29 import org.simantics.db.management.ISessionContext;
\r
30 import org.simantics.jfreechart.chart.properties.DoubleValidator;
\r
31 import org.simantics.sysdyn.SysdynResource;
\r
32 import org.simantics.sysdyn.ui.properties.SysdynBasicColorProvider;
\r
33 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory;
\r
34 import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier;
\r
36 public class NormalDistributionProperties implements IDistributionProperties {
\r
38 private TrackedText minValue;
\r
39 private TrackedText maxValue;
\r
40 private TrackedText mean;
\r
41 private TrackedText stdDeviation;
\r
42 private Resource resource;
\r
44 public NormalDistributionProperties(Resource resource) {
\r
45 this.resource = resource;
\r
49 public Composite createContent(Composite parent, ISessionContext context, WidgetSupport support) {
\r
50 Composite composite = new Composite(parent, SWT.NONE);
\r
51 GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
\r
54 Label label = new Label(composite, SWT.NONE);
\r
55 label.setText("Min value");
\r
56 GridDataFactory.fillDefaults().applyTo(label);
\r
58 minValue = new TrackedText(composite, support, SWT.BORDER);
\r
59 minValue.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.NormalDistribution_minValue));
\r
60 minValue.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.NormalDistribution_minValue));
\r
61 minValue.setInputValidator(new DoubleValidator(true));
\r
62 minValue.setColorProvider(new SysdynBasicColorProvider(new LocalResourceManager(JFaceResources.getResources(), minValue.getWidget())));
\r
63 GridDataFactory.fillDefaults().grab(true, false).applyTo(minValue.getWidget());
\r
66 label = new Label(composite, SWT.NONE);
\r
67 label.setText("Max value");
\r
68 GridDataFactory.fillDefaults().applyTo(label);
\r
70 maxValue = new TrackedText(composite, support, SWT.BORDER);
\r
71 maxValue.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.NormalDistribution_maxValue));
\r
72 maxValue.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.NormalDistribution_maxValue));
\r
73 maxValue.setInputValidator(new DoubleValidator(true));
\r
74 maxValue.setColorProvider(new SysdynBasicColorProvider(new LocalResourceManager(JFaceResources.getResources(), maxValue.getWidget())));
\r
75 GridDataFactory.fillDefaults().grab(true, false).applyTo(maxValue.getWidget());
\r
78 label = new Label(composite, SWT.NONE);
\r
79 label.setText("Mean");
\r
80 GridDataFactory.fillDefaults().applyTo(label);
\r
82 mean = new TrackedText(composite, support, SWT.BORDER);
\r
83 mean.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.NormalDistribution_mean));
\r
84 mean.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.NormalDistribution_mean));
\r
85 mean.setInputValidator(new DoubleValidator(true));
\r
86 mean.setColorProvider(new SysdynBasicColorProvider(new LocalResourceManager(JFaceResources.getResources(), mean.getWidget())));
\r
87 GridDataFactory.fillDefaults().grab(true, false).applyTo(mean.getWidget());
\r
90 label = new Label(composite, SWT.NONE);
\r
91 label.setText("Standard deviation");
\r
92 GridDataFactory.fillDefaults().applyTo(label);
\r
94 stdDeviation = new TrackedText(composite, support, SWT.BORDER);
\r
95 stdDeviation.setTextFactory(new DoublePropertyFactory(SysdynResource.URIs.NormalDistribution_stdDeviation));
\r
96 stdDeviation.addModifyListener(new DoublePropertyModifier(context, SysdynResource.URIs.NormalDistribution_stdDeviation));
\r
97 stdDeviation.setInputValidator(new DoubleValidator(true));
\r
98 stdDeviation.setColorProvider(new SysdynBasicColorProvider(new LocalResourceManager(JFaceResources.getResources(), stdDeviation.getWidget())));
\r
99 GridDataFactory.fillDefaults().grab(true, false).applyTo(stdDeviation.getWidget());
\r
105 public void getCachedValues(HashMap<String, String> cahcedValues) {
\r
106 if(!minValue.isDisposed())
\r
107 cahcedValues.put(SensitivityDistributionKeys.MIN, minValue.getText());
\r
108 if(!maxValue.isDisposed())
\r
109 cahcedValues.put(SensitivityDistributionKeys.MAX, maxValue.getText());
\r
110 if(!mean.isDisposed())
\r
111 cahcedValues.put(SensitivityDistributionKeys.MEAN, mean.getText());
\r
112 if(!stdDeviation.isDisposed())
\r
113 cahcedValues.put(SensitivityDistributionKeys.STD_DEVIATION, stdDeviation.getText());
\r
119 public void applyCache(WriteGraph graph, HashMap<String, String> cachedValues) throws DatabaseException {
\r
120 if(resource == null)
\r
123 SysdynResource SR = SysdynResource.getInstance(graph);
\r
125 String min = cachedValues.get(SensitivityDistributionKeys.MIN);
\r
128 Double d = Double.parseDouble(min);
\r
129 graph.claimLiteral(resource, SR.NormalDistribution_minValue, d, Bindings.DOUBLE);
\r
130 } catch (NumberFormatException e) {}
\r
133 String max = cachedValues.get(SensitivityDistributionKeys.MAX);
\r
136 Double d = Double.parseDouble(max);
\r
137 graph.claimLiteral(resource, SR.NormalDistribution_maxValue, d, Bindings.DOUBLE);
\r
138 } catch (NumberFormatException e) {}
\r
141 String mean = cachedValues.get(SensitivityDistributionKeys.MEAN);
\r
144 Double d = Double.parseDouble(mean);
\r
145 graph.claimLiteral(resource, SR.NormalDistribution_mean, d, Bindings.DOUBLE);
\r
146 } catch (NumberFormatException e) {}
\r
149 String stdDeviation = cachedValues.get(SensitivityDistributionKeys.STD_DEVIATION);
\r
150 if(stdDeviation != null) {
\r
152 Double d = Double.parseDouble(stdDeviation);
\r
153 graph.claimLiteral(resource, SR.NormalDistribution_stdDeviation, d, Bindings.DOUBLE);
\r
154 } catch (NumberFormatException e) {}
\r