From: lempinen Date: Fri, 9 Dec 2011 11:31:54 +0000 (+0000) Subject: git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23472 ac1ea38d-2e2b... X-Git-Tag: simantics-1.6~75 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=268f44f416e5b208f77a8452f81fc52fcfbd0e99;p=simantics%2Fsysdyn.git git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23472 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java index 91084b6c..89ef64d3 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java @@ -40,6 +40,8 @@ import org.simantics.scenegraph.utils.NodeUtil; import org.simantics.simulation.experiment.IExperiment; import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.adapter.SysdynVariableProperties; +import org.simantics.sysdyn.adapter.VariableRVIUtils; +import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; import org.simantics.ui.SimanticsUI; import org.simantics.utils.datastructures.Triple; @@ -54,11 +56,11 @@ import org.simantics.utils.ui.color.ColorValue; * */ public class SimulationPlaybackStyle extends StyleBase> { - + Resource gradientResource; ColorGradient cg; byte[] gradient; - + /** * Determine if style needs to be redrawn and return objects that are needed to redraw the style. * @@ -66,19 +68,19 @@ public class SimulationPlaybackStyle extends StyleBase calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException { - + // Find SimulationPlaybackExperiment IProject project = SimanticsUI.getProject(); IExperimentManager em = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); IExperiment experiment = em.getActiveExperiment(); if(!(experiment instanceof SysdynPlaybackExperiment)) return null; - + ModelingResources mr = ModelingResources.getInstance(graph); DiagramResource dr = DiagramResource.getInstance(graph); ColorGradient cg = null; - + Resource component = graph.getPossibleObject(element, mr.ElementToComponent); if (component == null) return null; @@ -97,25 +99,60 @@ public class SimulationPlaybackStyle extends StyleBase)) + return null; + + ArrayList datasets = new ArrayList(); + + for(Object o : (ArrayList)object) { + if(o instanceof SysdynDataSet) + datasets.add((SysdynDataSet)o); + } + + if(datasets.size() == 0) return null; + + if(datasets.size() > 1) { + String range = datasets.get(0).name.substring( + datasets.get(0).name.indexOf('[') + 1, + datasets.get(0).name.indexOf(']')); + int size = range.split(",").length; + String[] filter = new String[size]; + for(int i = 0; i < size; i++) + filter[i] = "Sum"; + + ArrayList result2 = VariableRVIUtils.getDataset(datasets, filter); + if(result2 != null) { + datasets = result2; + } + + } + + // The datasets list should always be size == 1 + SysdynDataSet dataset = datasets.get(0); + // Get values (or sum of values) + double[] va = dataset.values; + if(va.length < 2) + return null; + // Get simulation timesteps for this component - double[] ta = var.getPossiblePropertyValue(graph, SysdynVariableProperties.TIMES , Bindings.DOUBLE_ARRAY); + double[] ta = dataset.times; if(ta == null || ta.length < 2) return null; - + if(va.length == 0 || va.length == 2) return null; - + // Get playback time from the experiment Double time = var.getPossiblePropertyValue(graph, SysdynVariableProperties.TIME , Bindings.DOUBLE); if(time == null) return null; - + // Find minimum and maximum values for this variable double min = va[0], max = va[0]; for(double d : va) { @@ -124,7 +161,7 @@ public class SimulationPlaybackStyle extends StyleBase max) max = d; } - + // Find the index in time and value arrays for this time // (time steps may vary, we need to loop to find the correct time) int index = 0; @@ -136,16 +173,16 @@ public class SimulationPlaybackStyle extends StyleBase(at, multiplier, cg); - + } catch(Exception ignore) { ignore.printStackTrace(); } @@ -187,7 +224,7 @@ public class SimulationPlaybackStyle extends StyleBase result) { Double multiplier; if (result != null && (multiplier = result.second) != null && !multiplier.isNaN()) { - + // Create a node that will show the style effect A node = ProfileVariables.claimChild(_node, "", "playbackColour", A.class, observer); if (node == null) @@ -199,7 +236,7 @@ public class SimulationPlaybackStyle extends StyleBase>() { @@ -124,19 +124,19 @@ public class CategoryDataset extends AbstractDataset { try { // Get a variable for the series Variable v = Variables.getVariable(graph, realizationURI + rvi); - + // Get values Object object = v.getPossiblePropertyValue(graph, SysdynVariableProperties.ACTIVE_DATASETS , Bindings.VARIANT); if(object == null || !(object instanceof ArrayList)) return series; - + ArrayList datasets = new ArrayList(); - + for(Object o : (ArrayList)object) { if(o instanceof SysdynDataSet) datasets.add((SysdynDataSet)o); } - + String[] filter = graph.getPossibleRelatedValue(r, jfree.variableFilter); if(filter != null) { ArrayList result2 = VariableRVIUtils.getDataset(datasets, filter); @@ -144,11 +144,11 @@ public class CategoryDataset extends AbstractDataset { datasets = result2; } } - + for(SysdynDataSet dataset : datasets) { double[] va = dataset.values; - - + + if(va == null || va.length == 0) continue; @@ -196,10 +196,10 @@ public class CategoryDataset extends AbstractDataset { public AbstractRenderer getRenderer() { return renderer.getRenderer(); } - + private class DatasetListener implements Listener> { private boolean disposed = false; - + public void dispose() { disposed = true; } @@ -214,12 +214,19 @@ public class CategoryDataset extends AbstractDataset { // Remove all unused series dataset.clear(); - ((BarRenderer)getRenderer()).getPlot().setDrawingSupplier(new DefaultDrawingSupplier()); - + BarRenderer renderer = ((BarRenderer)getRenderer()); + renderer.getPlot().setDrawingSupplier(new DefaultDrawingSupplier()); + // Add found series for(int i = 0; i < series.size(); i++) { TempSeries s = series.get(i); - dataset.addValue(s.value, s.series, s.name); + if(renderer instanceof org.jfree.chart.renderer.category.StackedBarRenderer) { + String category = s.name.substring(0, s.name.indexOf('[')); + String series = s.name.substring(s.name.indexOf('[')); + dataset.addValue(s.value, series, category); + } else { + dataset.addValue(s.value, s.series, s.name); + } } } }); @@ -235,7 +242,7 @@ public class CategoryDataset extends AbstractDataset { return disposed; } }; - + @Override public void dispose() { super.dispose(); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartComposite.java index 446a5332..9d71e758 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/ChartComposite.java @@ -103,22 +103,22 @@ public class ChartComposite extends Composite { frame = SWT_AWT.new_Frame(composite); // Add a listener displaying the contents of the chart. Chart is re-drawn if the definition changes - Simantics.getSession().asyncRequest(new Read() { + Simantics.getSession().asyncRequest(new Read() { @Override - public JFreeChart perform(ReadGraph graph) throws DatabaseException { + public IJFreeChart perform(ReadGraph graph) throws DatabaseException { // Adapt chartResource to a chart (XY, pie, bar, ...) if(graph.isInstanceOf(chartResource, JFreeChartResource.getInstance(graph).Chart)) { if(chart != null) chart.dispose(); chart = graph.adapt(chartResource, IJFreeChart.class); - return chart.getChart(); + return chart; } else { return null; } } - } , new AsyncListener() { + } , new AsyncListener() { @Override public boolean isDisposed() { @@ -126,11 +126,13 @@ public class ChartComposite extends Composite { } @Override - public void execute(AsyncReadGraph graph, JFreeChart chart) { + public void execute(AsyncReadGraph graph, IJFreeChart chart) { if(chart == null) return; + + JFreeChart jfreeChart = chart.getChart(); // Display the result chart - composite.getDisplay().asyncExec(new RunnableWithObject(chart) { + composite.getDisplay().asyncExec(new RunnableWithObject(jfreeChart) { @Override public void run() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarGeneralPropertiesTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarGeneralPropertiesTab.java index d2ed2907..aebd5e97 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarGeneralPropertiesTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/properties/bar/BarGeneralPropertiesTab.java @@ -11,12 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.trend.chart.properties.bar; +import java.util.LinkedHashMap; +import java.util.Map; + import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.ScrolledComposite; import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; @@ -25,7 +27,9 @@ import org.eclipse.ui.IWorkbenchSite; import org.simantics.browsing.ui.swt.widgets.Button; import org.simantics.browsing.ui.swt.widgets.StringPropertyFactory; import org.simantics.browsing.ui.swt.widgets.StringPropertyModifier; +import org.simantics.browsing.ui.swt.widgets.TrackedCombo; import org.simantics.browsing.ui.swt.widgets.TrackedText; +import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; import org.simantics.browsing.ui.swt.widgets.impl.TextModifyListener; import org.simantics.browsing.ui.swt.widgets.impl.TrackedModifyEvent; @@ -40,6 +44,7 @@ import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.layer0.Layer0; +import org.simantics.layer0.utils.direct.GraphUtils; import org.simantics.sysdyn.JFreeChartResource; import org.simantics.sysdyn.ui.properties.LabelPropertyTabContributor; import org.simantics.sysdyn.ui.trend.chart.properties.BooleanPropertyFactory; @@ -61,7 +66,7 @@ public class BarGeneralPropertiesTab extends LabelPropertyTabContributor { private Composite composite; private Button hgrid, htitle, hlegend; private TrackedText name, title; - private Combo type; + private TrackedCombo type; private TrackedSpinner angle; @Override @@ -102,9 +107,11 @@ public class BarGeneralPropertiesTab extends LabelPropertyTabContributor { GridDataFactory.fillDefaults().align(SWT.END, SWT.CENTER).applyTo(label); label.setText("Type:"); - type = new Combo(c, SWT.BORDER | SWT.READ_ONLY); - type.setItems(new String[] {"Normal", "Stacked"}); - type.select(0); + type = new TrackedCombo(c, support, SWT.BORDER | SWT.READ_ONLY); + type.addModifyListener(new TypeModifyListener()); + type.setItemFactory(new TypeItemFactory()); + type.setSelectionFactory(new TypeSelectionFactory()); + GridDataFactory.fillDefaults().applyTo(type.getWidget()); // Title (Which is different than name) label = new Label(general, SWT.NONE); @@ -157,6 +164,81 @@ public class BarGeneralPropertiesTab extends LabelPropertyTabContributor { sc.setMinSize(size); } + /** + * + * @author Teemu Lempinen + * + */ + private class TypeSelectionFactory extends ReadFactoryImpl { + @Override + public String perform(ReadGraph graph, Resource chart) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot)); + if(plot != null) { + Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset)); + + if(dataset != null) { + Resource renderer = graph.syncRequest(new PossibleObjectWithType(dataset, l0.ConsistsOf, jfree.Dataset_renderer)); + + if(renderer != null && graph.isInstanceOf(renderer, jfree.StackedBarRenderer)) + return "Stacked"; + } + } + return "Normal"; + } + } + + /** + * RangeItemFactory finds all inexes of a given enumeration + * and adds "Sum" and "All" to the returned indexes + * @author Teemu Lempinen + * + */ + private class TypeItemFactory extends ReadFactoryImpl> { + @Override + public Map perform(ReadGraph graph, Resource series) throws DatabaseException { + LinkedHashMap result = new LinkedHashMap(); + result.put("Normal", "Normal"); + result.put("Stacked", "Stacked"); + return result; + } + } + + /** + * TypeModifyListener for modifying the type of a bar chart + * @author Teemu Lempinen + * + */ + private class TypeModifyListener extends ComboModifyListenerImpl { + @Override + public void applyText(WriteGraph graph, Resource chart, String text) throws DatabaseException { + JFreeChartResource jfree = JFreeChartResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + + Resource plot = graph.syncRequest(new PossibleObjectWithType(chart, l0.ConsistsOf, jfree.CategoryPlot)); + if(plot == null) + return; + + Resource dataset = graph.syncRequest(new PossibleObjectWithType(plot, l0.ConsistsOf, jfree.CategoryDataset)); + if(dataset == null) + return; + + graph.deny(dataset, jfree.Dataset_renderer); + + Resource renderer; + if(text.equals("Stacked")) + renderer = GraphUtils.create2(graph, jfree.StackedBarRenderer); + else + renderer = GraphUtils.create2(graph, jfree.BarRenderer); + + graph.claim(dataset, jfree.Dataset_renderer, renderer); + } + } + + + /** * ModifyListener for the angle {@link TrackedSpinner} *