From a54793dfcadc819178849511625492636fcf1c47 Mon Sep 17 00:00:00 2001 From: lempinen Date: Mon, 19 Dec 2011 07:33:21 +0000 Subject: [PATCH] Drag & drop chart panel organizing git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@23624 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../sysdyn/ui/trend/ChartDropTarget.java | 76 +++++ .../simantics/sysdyn/ui/trend/ChartPanel.java | 304 +++++++++--------- .../sysdyn/ui/trend/ChartPanelElement.java | 152 ++++++--- .../sysdyn/ui/trend/ChartPanelHeader.java | 167 +++++++++- .../sysdyn/ui/trend/ChartPanelSeparator.java | 108 +++---- .../sysdyn/ui/trend/ElementContainer.java | 41 +++ .../ui/trend/chart/CategoryDataset.java | 14 +- .../sysdyn/ui/trend/chart/CategoryPlot.java | 2 +- .../sysdyn/ui/trend/chart/PieDataset.java | 2 + .../chart/element/ChartElementFactory.java | 19 +- .../SysdynDatasetSelectionListener.java | 2 +- .../sysdyn/adapter/HistoryVariable.java | 2 +- .../simantics/sysdyn/manager/SysdynModel.java | 96 +++--- 13 files changed, 670 insertions(+), 315 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java new file mode 100644 index 00000000..f641a3c7 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartDropTarget.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.trend; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.DropTargetAdapter; +import org.eclipse.swt.dnd.DropTargetEvent; +import org.eclipse.swt.widgets.Display; +import org.simantics.db.Resource; +import org.simantics.ui.utils.AdaptionUtils; + +/** + * Drop target for dropping charts in chart panel + * @author Teemu Lempinen + * + */ +public class ChartDropTarget extends DropTargetAdapter { + + private ChartPanelSeparator separator; + private ChartPanelElement element; + private Display display; + private ChartPanel panel; + + public ChartDropTarget(ChartPanelSeparator separator, ChartPanelElement element, ChartPanel panel) { + this.separator = separator; + this.display = separator.getDisplay(); + this.element = element; + this.panel = panel; + } + + + + /** + * Display effect on the composite when drag entered + */ + @Override + public void dragEnter(DropTargetEvent event) { + if ((event.operations & DND.DROP_COPY) != 0) { + event.detail = DND.DROP_COPY; + } else if ((event.operations & DND.DROP_MOVE) != 0) { + event.detail = DND.DROP_MOVE; + } else { + event.detail = DND.DROP_NONE; + } + separator.setBackground(display.getSystemColor(SWT.COLOR_DARK_GRAY)); + } + + /** + * Revert effect when drag leaves + */ + @Override + public void dragLeave(DropTargetEvent event) { + separator.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); + } + + /** + * Drop the data to chart panel + */ + @Override + public void drop(DropTargetEvent event) { + Resource chartResource = AdaptionUtils.adaptToSingle(event.data, Resource.class); + if(chartResource != null) + panel.addChart(chartResource, element); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java index 38370a9e..a82b6c29 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanel.java @@ -12,6 +12,7 @@ package org.simantics.sysdyn.ui.trend; import java.util.ArrayList; +import java.util.LinkedHashMap; import org.eclipse.jface.dialogs.IDialogSettings; import org.eclipse.jface.layout.GridDataFactory; @@ -23,8 +24,10 @@ import org.eclipse.swt.dnd.DropTarget; import org.eclipse.swt.dnd.DropTargetAdapter; import org.eclipse.swt.dnd.DropTargetEvent; import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import org.eclipse.ui.IMemento; @@ -58,12 +61,14 @@ public class ChartPanel extends ViewPart { private ScrolledComposite sc; private IDialogSettings settings; - private ArrayList expandedCharts; - private ArrayList minimizedCharts; + private LinkedHashMap charts; + private ArrayList minimizedResources; + + private ArrayList chartElements; public static final String CHART_PANEL_SETTINGS = "CHART_PANEL_SETTINGS"; - public static final String EXPANDED_CHARTS = "ChartPanel.EXPANDED_CHARTS"; - public static final String MINIMIZED_CHARTS = "ChartPanel.MINIMIZED_CHARTS"; + public static final String CHARTS = "CHART_PANEL_CHARTS"; + public static final String MINIMIZED_CHARTS = "CHART_PANEL_MINIMIZED_CHARTS"; public static final String CHART_PANEL_ORIENTATION = "CHART_PANEL_ORIENTATION"; public static final String CHART_PANEL_VERTICAL = "CHART_PANEL_ORIENTATION_VERTICAL"; @@ -78,20 +83,27 @@ public class ChartPanel extends ViewPart { public void init(IViewSite site, IMemento memento) throws PartInitException { super.init(site, memento); + minimizedResources = new ArrayList(); + settings = Activator.getDefault().getDialogSettings().getSection(CHART_PANEL_SETTINGS); // Initialize settings if there are no settings if (settings == null) { settings = Activator.getDefault().getDialogSettings().addNewSection(CHART_PANEL_SETTINGS); - String[] expandedUris = new String[] {}; - settings.put(EXPANDED_CHARTS, expandedUris); - String[] minimizedUris = new String[] {}; - settings.put(MINIMIZED_CHARTS, minimizedUris); + } + + if(settings.getArray(CHARTS) == null) { + String[] chartUris = new String[] {}; + settings.put(CHARTS, chartUris); + } + + if(settings.getArray(MINIMIZED_CHARTS) == null) { + String[] minimizedChartUris = new String[] {}; + settings.put(MINIMIZED_CHARTS, minimizedChartUris); } // initialize chart lists - expandedCharts = new ArrayList(); - minimizedCharts = new ArrayList(); + charts = new LinkedHashMap(); // add chart resources to chart lists from settings try { @@ -101,22 +113,21 @@ public class ChartPanel extends ViewPart { public void run(ReadGraph graph) throws DatabaseException { JFreeChartResource jfree = JFreeChartResource.getInstance(graph); Resource chart = null; - String[] expandedURIs = settings.getArray(EXPANDED_CHARTS); - for(String uri : expandedURIs) { + String[] chartURIs = settings.getArray(CHARTS); + for(String uri : chartURIs) { chart = graph.getPossibleResource(uri); if(chart != null && graph.isInstanceOf(chart, jfree.Chart)) { - expandedCharts.add(chart); + charts.put(chart, null); setChartExistingListener(chart); } } - String[] minimizedURIs = settings.getArray(MINIMIZED_CHARTS); - for(String uri : minimizedURIs) { + String[] minimizedUris = settings.getArray(MINIMIZED_CHARTS); + for(String uri : minimizedUris) { chart = graph.getPossibleResource(uri); if(chart != null && graph.isInstanceOf(chart, jfree.Chart)) { - minimizedCharts.add(chart); - setChartExistingListener(chart); - } + minimizedResources.add(chart); + } } } }); @@ -153,61 +164,60 @@ public class ChartPanel extends ViewPart { GridDataFactory.fillDefaults().grab(true, true).applyTo(body); sc.setContent(body); - createContents(); } - + /** - * Display charts in this chart panel in the following order regardless of the orientation: - * 1. Remove old content - * 2. Display expanded charts - * 3. Display minimized charts - * - * @param parent Body located inside a scrolled composite and containing the charts. (always private Composite body?) + * Creates the contents of this chart panel. + * Removes all old contents before creating new content */ private void createContents() { - for(Control c : body.getChildren()) - c.dispose(); - - // Set the layout - Composite composite = new Composite(body, SWT.NONE); - if(vertical) { - GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(composite); - GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); - } else { - // Need to calculate horizontal elements for gridLayout - int chartPanels = expandedCharts.size(); - int minimizedPanel = minimizedCharts.isEmpty() ? 0 : 1; - int separators = 1 + expandedCharts.size() + minimizedCharts.size(); + chartElements = new ArrayList(); - GridLayoutFactory.fillDefaults().spacing(0, 0).numColumns(chartPanels + minimizedPanel + separators).applyTo(composite); - GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + for(Control child : body.getChildren()) { + child.dispose(); + } + + // Set the initial layout + ElementContainer elementHolder; + for(Resource e : charts.keySet()) { + elementHolder = new ElementContainer(body, SWT.NONE); + elementHolder.setBackground(new Color(elementHolder.getDisplay(), 255, 0, 0)); + ChartPanelElement element = new ChartPanelElement(elementHolder, this, e, SWT.NONE); + elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)element.getLayout())); + chartElements.add(element); + charts.put(e, element); + if(minimizedResources.contains(e)) { + element.toggleMinimize(); + } } -// if(expandedCharts.isEmpty() && minimizedCharts.isEmpty()) { -// // TODO: the whole panel is a drop target -// } else { - new ChartPanelSeparator(composite, this, null, SWT.NONE); -// } + elementHolder = new ElementContainer(body, SWT.NONE); + elementHolder.setBackground(new Color(elementHolder.getDisplay(), 0, 255, 0)); + ChartPanelElement element = new ChartPanelElement(elementHolder, this, null, SWT.NONE); // Last element is empty -> only the separator + elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)element.getLayout())); + chartElements.add(element); - for(Resource e : expandedCharts) { - new ChartPanelElement(composite, this, e, SWT.NONE); - } + layout(); + saveState(); - if(!minimizedCharts.isEmpty()) { - Composite minimized = new Composite(composite, SWT.NONE); - GridDataFactory.fillDefaults().grab(true, false).hint(ChartPanelHeader.HEADER_MINIMUM_WIDTH, SWT.DEFAULT).applyTo(minimized); - GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(minimized); + } - for(Resource m : minimizedCharts) { - new ChartPanelElement(minimized, this, m, true, SWT.NONE); - } + /** + * Lays out this panel (the body composite) + */ + public void layout() { + if(vertical) { + GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(body); + GridDataFactory.fillDefaults().grab(true, true).applyTo(body); + } else { + // Need to calculate horizontal elements for gridLayout + int chartPanels = chartElements.size(); + GridLayoutFactory.fillDefaults().spacing(0, 0).numColumns(chartPanels).applyTo(body); + GridDataFactory.fillDefaults().grab(true, true).applyTo(body); } - body.layout(); - sc.setMinSize(body.computeSize(SWT.DEFAULT, SWT.DEFAULT)); - saveState(); } @Override @@ -225,22 +235,34 @@ public class ChartPanel extends ViewPart { /** * Save the current state of the view to IDialogSettings */ - private void saveState() { + public void saveState() { try { SimanticsUI.getSession().syncRequest(new ReadRequest() { @Override public void run(ReadGraph graph) throws DatabaseException { if (settings != null) { - String[] expandedURIs = new String[expandedCharts.size()]; - for(int i = 0; i < expandedCharts.size(); i++) - expandedURIs[i] = graph.getURI(expandedCharts.get(i)); - settings.put(EXPANDED_CHARTS, expandedURIs); - - String[] minimizedURIs = new String[minimizedCharts.size()]; - for(int i = 0; i < minimizedCharts.size(); i++) - minimizedURIs[i] = graph.getURI(minimizedCharts.get(i)); - settings.put(MINIMIZED_CHARTS, minimizedURIs); + String[] uris = new String[chartElements.size() - 1]; + ArrayList minimized = new ArrayList(); + minimizedResources.clear(); + for(int i = 0; i < uris.length; i++) { + ChartPanelElement e = chartElements.get(i); + Resource r = e.getResource(); + if(r != null) { + uris[i] = graph.getURI(r); + if(e.isMinimized()) { + minimized.add(uris[i]); + minimizedResources.add(r); + } + } else { + uris[i] = ""; + } + } + settings.put(CHARTS, uris); + if(!minimized.isEmpty()) + settings.put(MINIMIZED_CHARTS, minimized.toArray(new String[minimized.size()])); + else + settings.put(MINIMIZED_CHARTS, new String[0]); if(vertical) settings.put(CHART_PANEL_ORIENTATION, CHART_PANEL_VERTICAL); @@ -253,41 +275,6 @@ public class ChartPanel extends ViewPart { e.printStackTrace(); } } - - /** - * Return true if element with chart resource is minimized, false if expanded - * or there is no element with that resource - * - * @param chart Chart resource - * @return true if element with chart resource is minimized, false if expanded - */ - public boolean isMinimized(Resource chart) { - if(minimizedCharts.contains(chart)) - return true; - else - return false; - } - - /** - * Expand or minimize chart depending on the current status of that chart. - * - * @param chart The name of the chart to be expanded or minimized - */ - public void expandChart(Resource chart) { - boolean expand = isMinimized(chart); - if(minimizedCharts.contains(chart)) { - minimizedCharts.remove(chart); - } - - if(expandedCharts.contains(chart)) - expandedCharts.remove(chart); - - if(expand) - expandedCharts.add(expandedCharts.isEmpty() ? 0 : expandedCharts.size(), chart); - else - minimizedCharts.add(0, chart); - refresh(); - } /** * Set the orientation for this chart panel. @@ -297,40 +284,34 @@ public class ChartPanel extends ViewPart { public void setOrientation(String orientation) { if(CHART_PANEL_VERTICAL.equals(orientation)) this.vertical = true; - else + else { this.vertical = false; - refresh(); + } + createContents(); } /** * Removes a chart from this panel * - * @param chart The name of the chart to be removed + * @param chart The chart to be removed */ public void removeChart(Resource chart) { - if(this.expandedCharts.contains(chart)) - this.expandedCharts.remove(chart); - else if(this.minimizedCharts.contains(chart)) - this.minimizedCharts.remove(chart); - refresh(); - } - - - /** - * Convenience method for refreshing the contents of the panel - */ - private void refresh() { - createContents(); + ChartPanelElement element = charts.get(chart); + chartElements.remove(element); + element.getParent().dispose(); + charts.remove(chart); + minimizedResources.remove(chart); + saveState(); + layout(); } - /** * Sets up drag-scrolling for a scrolled composite * * @param control */ protected void setupDropTarget() { - DropTarget target = new DropTarget(sc, DND.DROP_NONE); + DropTarget target = new DropTarget(sc, DND.DROP_MOVE); target.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); target.addDropListener(new DropTargetAdapter() { @@ -364,14 +345,6 @@ public class ChartPanel extends ViewPart { } - public void dropChart(DropTargetEvent event, ChartPanelSeparator separator) { - - } - - public void dropChart(DropTargetEvent event, ChartPanelElement element) { - - } - /** * Is the panel vertically oriented * @return Is the panel vertically oriented @@ -379,27 +352,70 @@ public class ChartPanel extends ViewPart { public boolean isVertical() { return vertical; } - + /** * Adds chart after given element. If element == null, adds chart to the top. * - * @param element Element whose after the chart will be placed. (null allowed) + * @param element To which place the chart will be placed. (null allowed) */ public void addChart(Resource chartResource, ChartPanelElement element) { - if(element == null) { - expandedCharts.add(0, chartResource); - } else { - Resource elementResource = element.getChartResource(); - if(element.isMinimized()) { - minimizedCharts.add(minimizedCharts.indexOf(elementResource) + 1, chartResource); + addChart(chartResource, element, true); + } + + /** + * Adds chart after given element. If element == null, adds chart to the top. + * + * @param element To which place the chart will be placed. (null allowed) + * @param layout refresh layout. use with vertical layout. + */ + public void addChart(Resource chartResource, ChartPanelElement element, boolean layout) { + int index = chartElements.indexOf(element); + if(index >= 0) { + ChartPanelElement e = chartElements.get(index); + ChartPanelElement newElement; + + if(charts.containsKey(chartResource)) { + // Old element being moved to a new place + newElement = charts.get(chartResource); + int oldIndex = chartElements.indexOf(newElement); + if(newElement.equals(element) || oldIndex == index - 1) + return; // Not moving anywhere, do nothing + Composite oldParent = newElement.getParent(); + newElement.setParent(e.getParent()); + oldParent.dispose(); + if(oldIndex < index) + index--; + chartElements.remove(newElement); } else { - expandedCharts.add(expandedCharts.indexOf(elementResource) + 1, chartResource); + newElement = new ChartPanelElement(e.getParent(), this, chartResource, SWT.NONE); } + + // Add a new chart element to the location of the old element + chartElements.add(index, newElement); + charts.put(chartResource, newElement); + + ElementContainer elementHolder; + // Move elements back after index + for(int i = index + 1 /*indexes after the new element*/; i < chartElements.size(); i++) { + e = chartElements.get(i); + if(i == chartElements.size() - 1) { + // last element (the empty element) element to a new container + elementHolder = new ElementContainer(body, SWT.NONE); + elementHolder.setBackground(new Color(elementHolder.getDisplay(), 0, 0, 255)); + elementHolder.setLayout(GridLayoutFactory.copyLayout((GridLayout)e.getLayout())); + e.setParent(elementHolder); + } else { + // element to the next elements container + elementHolder = (ElementContainer)chartElements.get(i + 1).getParent(); + e.setParent(elementHolder); + } + } + + layout(); + saveState(); } - setChartExistingListener(chartResource); - refresh(); } - + /** * Set a listener to listen if the chart resource has been removed. * If the resource has been removed, close also the chart element in this panel. @@ -416,7 +432,7 @@ public class ChartPanel extends ViewPart { }, new AsyncListener() { boolean disposed = false; - + @Override public void execute(AsyncReadGraph graph, Boolean result) { if(result != null && result == false && body != null && !body.isDisposed()) { @@ -436,7 +452,7 @@ public class ChartPanel extends ViewPart { @Override public boolean isDisposed() { - return !disposed && !(minimizedCharts.contains(chart) || expandedCharts.contains(chart)); + return !disposed && !charts.containsKey(chart); } }); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java index b686e578..5c830dc5 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelElement.java @@ -14,11 +14,14 @@ package org.simantics.sysdyn.ui.trend; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.swt.SWT; -import org.eclipse.swt.events.MouseAdapter; -import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.DropTarget; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; import org.simantics.db.Resource; import org.simantics.sysdyn.ui.trend.chart.ChartComposite; +import org.simantics.ui.dnd.LocalObjectTransfer; /** * This class represents an expanded chart element in {@link ChartPanel}. It contains @@ -31,10 +34,50 @@ public class ChartPanelElement extends Composite { public static int CHART_MINIMUM_WIDTH = 300; public static int CHART_MINIMUM_HEIGHT = 200; - + + private ChartPanel panel; + private ChartComposite chartComposite; private Resource chartResource; - private boolean minimized; - + private boolean minimized = false; + + public Resource getResource() { + return chartResource; + } + + @Override + public Object getLayoutData () { + checkWidget(); + Object oldData = super.getLayoutData(); + if(oldData == null || !(oldData instanceof GridData)) { + oldData = GridDataFactory.fillDefaults().create(); + } + + GridData data = (GridData) oldData; + // Horizontal data + data.widthHint = CHART_MINIMUM_WIDTH; + if(chartResource == null && !panel.isVertical()){ + data.grabExcessHorizontalSpace = false; + data.widthHint = SWT.DEFAULT; + } else if(minimized && !panel.isVertical()) { + data.grabExcessHorizontalSpace = false; + data.widthHint = SWT.DEFAULT; + } else { + data.grabExcessHorizontalSpace = true; + } + + // Vertical data + if(!minimized && getResource() != null) { + data.grabExcessVerticalSpace = true; + data.heightHint = CHART_MINIMUM_HEIGHT; + } else if(!panel.isVertical()){ + data.grabExcessVerticalSpace = true; + } else { + data.grabExcessVerticalSpace = false; + data.heightHint = SWT.DEFAULT; + } + return data; + } + /** * Creates an expanded chart panel element into parent composite. * @@ -46,7 +89,7 @@ public class ChartPanelElement extends Composite { public ChartPanelElement(Composite parent, ChartPanel panel, Resource chartResource, int style) { this(parent, panel, chartResource, false, style); } - + /** * Creates a chart panel element into parent composite. * @param parent The parent composite where the chart element is created @@ -56,44 +99,39 @@ public class ChartPanelElement extends Composite { * @param style The Style of the created chart element */ public ChartPanelElement(Composite parent, ChartPanel panel, Resource chartResource, boolean minimized, int style) { - super(parent, style); + super(parent, style | SWT.NONE); + + this.panel = panel; this.chartResource = chartResource; this.minimized = minimized; - - if(minimized) - GridDataFactory.fillDefaults().grab(true, false).applyTo(this); + + if(panel.isVertical() || getResource() == null) + GridLayoutFactory.fillDefaults().spacing(0,0).applyTo(this); else - GridDataFactory.fillDefaults().grab(true, true).hint(CHART_MINIMUM_WIDTH,CHART_MINIMUM_HEIGHT).applyTo(this); + GridLayoutFactory.fillDefaults().numColumns(2).spacing(0,0).applyTo(this); + + GridDataFactory.fillDefaults().applyTo(this); + + // Separator for dropping other elements + ChartPanelSeparator separator = new ChartPanelSeparator(this, panel, this, SWT.NONE); + + if (chartResource != null) { + Composite c = new Composite(this, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(c); + GridLayoutFactory.fillDefaults().spacing(0, 0).applyTo(c); + // Header + new ChartPanelHeader(c, this, chartResource, SWT.BORDER); - GridLayoutFactory.fillDefaults().spacing(0,0).applyTo(this); - - // Header - new ChartPanelHeader(this, panel, chartResource, SWT.BORDER); - - if(!minimized) { // Chart - new ChartComposite(this, chartResource, SWT.BORDER); + chartComposite = new ChartComposite(c, chartResource, SWT.BORDER); } - ChartPanelSeparator separator = new ChartPanelSeparator(parent, panel, this, SWT.NONE); - separator.addMouseListener(new SCFocusListener(panel)); - } - - /** - * Listener for directing focus to scrollableComposite - * @author Teemu Lempinen - * - */ - private class SCFocusListener extends MouseAdapter { - ChartPanel panel; - public SCFocusListener(ChartPanel panel) { - this.panel = panel; - } - public void mouseDown(MouseEvent e) { - panel.setFocus(); - } + DropTarget target = new DropTarget(this, DND.DROP_COPY | DND.DROP_MOVE); + target.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); + target.addDropListener(new ChartDropTarget(separator, this, panel)); + } - + /** * Returns the chart resource associated with this element * @return chart resource @@ -101,12 +139,50 @@ public class ChartPanelElement extends Composite { public Resource getChartResource() { return this.chartResource; } - + /** * Returns the minimized state of this element * @return is the element minimized */ public boolean isMinimized() { - return this.minimized; + return minimized; + } + + /** + * Change the minimized state of this element + */ + public void toggleMinimize() { + toggleMinimize(false); + } + /** + * Change the minimized state of this element + */ + public void toggleMinimize(boolean callSave) { + minimized = Boolean.FALSE.equals(minimized); + GridData data = (GridData) chartComposite.getLayoutData(); + if(panel.isVertical()) + data.exclude = minimized; + else + data.exclude = false; + chartComposite.setVisible(!minimized); + + Composite parent = getParent(); + data = (GridData) getLayoutData(); + GridData parentData = (GridData)parent.getLayoutData(); + parentData.grabExcessHorizontalSpace = data.grabExcessHorizontalSpace; + parentData.grabExcessVerticalSpace = data.grabExcessVerticalSpace; + parentData.heightHint = data.heightHint; + + if(callSave) { + panel.saveState(); + } + panel.layout(); + } + + /** + * Remove this chart panel element from its panel + */ + public void remove() { + panel.removeChart(chartResource); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java index e37b82fb..03955086 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelHeader.java @@ -14,14 +14,27 @@ package org.simantics.sysdyn.ui.trend; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.StructuredSelection; import org.eclipse.swt.SWT; +import org.eclipse.swt.dnd.DND; +import org.eclipse.swt.dnd.DragSource; +import org.eclipse.swt.dnd.DragSourceEvent; +import org.eclipse.swt.dnd.DragSourceListener; +import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.ToolBar; import org.eclipse.swt.widgets.ToolItem; @@ -35,6 +48,7 @@ import org.simantics.layer0.Layer0; import org.simantics.sysdyn.JFreeChartResource; import org.simantics.sysdyn.ui.Activator; import org.simantics.ui.SimanticsUI; +import org.simantics.ui.dnd.LocalObjectTransfer; import org.simantics.utils.datastructures.Pair; /** @@ -48,12 +62,14 @@ import org.simantics.utils.datastructures.Pair; public class ChartPanelHeader extends Composite { public static int HEADER_MINIMUM_WIDTH = 250; - private ChartPanel panel; + private ChartPanelElement element; private Resource resource; private Label name; private Canvas iconCanvas; private Image icon; private ToolItem minimize, remove; + private Color defaultColor, darker, evenDarker; + private Image gradientBackgroundImage, borderImage; private static ImageDescriptor closeDescriptor = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/close.gif")); private static Image closeImage = closeDescriptor.createImage(); @@ -82,14 +98,16 @@ public class ChartPanelHeader extends Composite { * @param name The name of the chart * @param style he Style of the created chart element */ - public ChartPanelHeader(Composite parent, ChartPanel panel, Resource chartResource, int style) { - super(parent, style); + public ChartPanelHeader(Composite c, ChartPanelElement element, Resource chartResource, int style) { + super(c, style); this.resource = chartResource; - this.panel = panel; + this.element = element; GridLayoutFactory.fillDefaults().margins(3, 0).numColumns(3).applyTo(this); GridDataFactory.fillDefaults().grab(true, false).applyTo(this); + // Colors + // Chart icon iconCanvas = new Canvas (this, SWT.NONE); GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).hint(16, 16).applyTo(iconCanvas); @@ -107,7 +125,7 @@ public class ChartPanelHeader extends Composite { try { // name updater - SimanticsUI.getSession().syncRequest(new Read>() { + Pair result = SimanticsUI.getSession().syncRequest(new Read>() { @Override public Pair perform(ReadGraph graph) throws DatabaseException { @@ -161,6 +179,7 @@ public class ChartPanelHeader extends Composite { } }); + name.setText(result.first); } catch (DatabaseException e) { e.printStackTrace(); name.setText("No label"); @@ -184,14 +203,144 @@ public class ChartPanelHeader extends Composite { remove.setImage(closeImage); remove.addSelectionListener(new RemoveChartListener()); remove.setToolTipText("Remove"); + + + /* ******************************** + * DnD + * ********************************/ + + // Allow data to be copied or moved from the drag source + int operations = DND.DROP_MOVE; + source = new DragSource(name, operations); + + // Provide data in Text format + Transfer[] types = new Transfer[] { LocalObjectTransfer.getTransfer() }; + source.setTransfer(types); + dragSourceListener = new DragSourceListener() { + + @Override + public void dragStart(DragSourceEvent event) { + if(name.isDisposed()) + event.doit = false; + event.detail = DND.DROP_LINK; + + } + + @Override + public void dragSetData(DragSourceEvent event) { + event.data = new StructuredSelection(resource); + } + + @Override + public void dragFinished(DragSourceEvent event) { + } + }; + source.addDragListener(dragSourceListener); + + name.addDisposeListener(new DisposeListener() { + + @Override + public void widgetDisposed(DisposeEvent e) { + if(dragSourceListener != null && source != null && !source.isDisposed()) { + source.removeDragListener(dragSourceListener); + } + } + }); + this.setBackgroundImage(getGradientBackgroundImage()); + this.setBackgroundMode(SWT.INHERIT_FORCE); + + this.addListener(SWT.MouseEnter, new EnterListener()); + this.addListener(SWT.MouseExit, new ExitListener()); + + for(Control child : this.getChildren()) { + child.addListener(SWT.MouseEnter, new EnterListener()); + child.addListener(SWT.MouseExit, new ExitListener()); + + } } + private class EnterListener implements org.eclipse.swt.widgets.Listener { + public void handleEvent(Event event) { + ChartPanelHeader.this.setBackgroundImage(getHighlightedGradientBackgroundImage()); + } + } + + private class ExitListener implements org.eclipse.swt.widgets.Listener { + public void handleEvent(Event event) { + ChartPanelHeader.this.setBackgroundImage(getGradientBackgroundImage()); + } + } + + private void createColors() { + if(defaultColor == null) { + defaultColor = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); + try { + defaultColor = new Color(getDisplay(), defaultColor.getRed() + 500, defaultColor.getGreen() + 10, defaultColor.getBlue() + 10); + } catch (IllegalArgumentException e) { + // Do nothing, use the default color + } + } + + if(darker == null) { + try { + darker = new Color(getDisplay(), defaultColor.getRed() - 30, defaultColor.getGreen() - 30, defaultColor.getBlue() - 30); + } catch (IllegalArgumentException e) { + // Do nothing, use the default color + darker = defaultColor; + } + } + + if(evenDarker == null) { + try { + evenDarker = new Color(getDisplay(), defaultColor.getRed() - 50, defaultColor.getGreen() - 50, defaultColor.getBlue() - 50); + } catch (IllegalArgumentException e) { + // Do nothing, use the default color + evenDarker = defaultColor; + } + } + + } + + private Image getHighlightedGradientBackgroundImage() { + createColors(); + this.layout(); + Point size = this.getSize(); + + borderImage = new Image(this.getDisplay(), 1, Math.max(1, size.y)); + GC gc = new GC(borderImage); + gc.setForeground(defaultColor); + gc.setBackground(evenDarker); + gc.fillGradientRectangle(0, 0, 1, size.y, true); + gc.dispose(); + + return borderImage; + } + + private Image getGradientBackgroundImage() { + createColors(); + this.layout(); + Point size = this.computeSize(SWT.DEFAULT, SWT.DEFAULT); + if(gradientBackgroundImage == null) { + gradientBackgroundImage = new Image(this.getDisplay(), 1, Math.max(1, size.y)); + GC gc = new GC(gradientBackgroundImage); + gc.setForeground(defaultColor); + gc.setBackground(darker); + gc.fillGradientRectangle(0, 0, 1, size.y, true); + gc.dispose(); + } + + return gradientBackgroundImage; + } + + private DragSourceListener dragSourceListener; + private DragSource source; + /** * Return true if this element is minimized, false if expanded * @return true if this element is minimized, false if expanded */ private boolean isMinimized() { - return panel.isMinimized(resource); + return element.isMinimized(); } /** @@ -207,14 +356,12 @@ public class ChartPanelHeader extends Composite { if(ChartPanelHeader.this.isDisposed()) return; - panel.expandChart(resource); + element.toggleMinimize(true); if(!name.isDisposed() && !minimize.isDisposed()) { if(isMinimized()) { - name.setToolTipText("Expand"); minimize.setToolTipText("Expand"); } else { - name.setToolTipText("Minimize"); minimize.setToolTipText("Minimize"); } } @@ -237,7 +384,7 @@ public class ChartPanelHeader extends Composite { @Override public void widgetSelected(SelectionEvent e) { if(!ChartPanelHeader.this.isDisposed()) { - panel.removeChart(resource); + element.remove(); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java index c6f8e4ab..d529a865 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ChartPanelSeparator.java @@ -13,18 +13,10 @@ package org.simantics.sysdyn.ui.trend; import org.eclipse.jface.layout.GridDataFactory; import org.eclipse.jface.layout.GridLayoutFactory; -import org.eclipse.swt.SWT; -import org.eclipse.swt.dnd.DND; -import org.eclipse.swt.dnd.DropTarget; -import org.eclipse.swt.dnd.DropTargetAdapter; -import org.eclipse.swt.dnd.DropTargetEvent; -import org.eclipse.swt.dnd.Transfer; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.layout.GridData; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Display; -import org.simantics.db.Resource; -import org.simantics.ui.dnd.LocalObjectTransfer; -import org.simantics.ui.utils.AdaptionUtils; /** * Class for separating charts in {@link ChartPanel}. Acts as a drop participant for adding @@ -35,9 +27,26 @@ import org.simantics.ui.utils.AdaptionUtils; */ public class ChartPanelSeparator extends Composite { - private ChartPanelElement element; private ChartPanel panel; - + + @Override + public Object getLayoutData() { + checkWidget(); + Object oldData = super.getLayoutData(); + if(oldData == null || !(oldData instanceof GridData)) { + oldData = GridDataFactory.fillDefaults().create(); + } + GridData data = (GridData) oldData; + if(panel.isVertical()) { + data.grabExcessHorizontalSpace = true; + data.grabExcessVerticalSpace = false; + } else { + data.grabExcessHorizontalSpace = false; + data.grabExcessVerticalSpace = true; + } + return data; + } + /** * Set up a small horizontal or vertical separator depending on SWT style * @@ -45,66 +54,27 @@ public class ChartPanelSeparator extends Composite { * @param style */ public ChartPanelSeparator(Composite parent, ChartPanel panel, ChartPanelElement element, int style) { - super(parent, SWT.NONE); - if(panel.isVertical() || (element != null && element.isMinimized())) { - GridDataFactory.fillDefaults().grab(true, false).applyTo(this); - GridLayoutFactory.fillDefaults().margins(0, 2).applyTo(this); - } else { - GridDataFactory.fillDefaults().grab(false, true).applyTo(this); - GridLayoutFactory.fillDefaults().margins(2, 0).applyTo(this); - } + super(parent, style); + GridLayoutFactory.fillDefaults().margins(2, 2).applyTo(this); + GridDataFactory.fillDefaults().applyTo(this); this.panel = panel; - this.element = element; - setupDropTarget(this); + addMouseListener(new SCFocusListener(panel)); } + /** - * Set up the drop target for this separator - * - * @param control + * Listener for directing focus to scrollableComposite + * @author Teemu Lempinen + * */ - protected void setupDropTarget(Control control) { - DropTarget target = new DropTarget(control, DND.DROP_COPY | DND.DROP_LINK); - target.setTransfer(new Transfer[] { LocalObjectTransfer.getTransfer() }); - target.addDropListener(new DropTargetAdapter() { - - Composite composite = ChartPanelSeparator.this; - Display display = composite.getDisplay(); - - /** - * Display effect on the composite when drag entered - */ - @Override - public void dragEnter(DropTargetEvent event) { - event.detail = DND.DROP_COPY; // TODO: calculate this. Is drop possible? What kind of drop? - composite.setBackground(display.getSystemColor(SWT.COLOR_BLACK)); - } - - /** - * Revert effect when drag leaves - */ - @Override - public void dragLeave(DropTargetEvent event) { - composite.setBackground(display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND)); - } - - /** - * Drop the data to chart panel - */ - @Override - public void drop(DropTargetEvent event) { - Resource chartResource = AdaptionUtils.adaptToSingle(event.data, Resource.class); - if(chartResource != null) - panel.addChart(chartResource, element); - } - - @Override - public void dropAccept(DropTargetEvent event) { - } - - @Override - public void dragOver(DropTargetEvent event) { - } - }); + private class SCFocusListener extends MouseAdapter { + ChartPanel panel; + public SCFocusListener(ChartPanel panel) { + this.panel = panel; + } + public void mouseDown(MouseEvent e) { + panel.setFocus(); + } } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java new file mode 100644 index 00000000..56d9fa8c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/ElementContainer.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.trend; + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; + +/** + * Container for a chart panel element. Needed for + * moving chart panel elements around using setParent() + * @author Teemu Lempinen + * + */ +public class ElementContainer extends Composite { + + @Override + public Object getLayoutData () { + Control[] children = getChildren(); + if(children.length == 1) { + if(children[0] instanceof ChartPanelElement) { + ChartPanelElement element = (ChartPanelElement)children[0]; + return element.getLayoutData(); + } + } + return super.getLayoutData(); + } + + public ElementContainer(Composite parent, int style) { + super(parent, style); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java index 7be59dcb..11f57127 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryDataset.java @@ -199,7 +199,7 @@ public class CategoryDataset extends AbstractDataset { } String label = graph.getPossibleRelatedValue(r, Layer0.getInstance(graph).HasLabel); // Called to refresh paints when label changes String name = dataset.name; - series.add(new TempSeries(label == null || label.isEmpty() ? name : label, "Current", value)); + series.add(new TempSeries(label == null || label.isEmpty() ? name : label, dataset.result, value)); } } catch (MissingVariableException e) { // Do nothing, if variable was not found. Move on to the next series @@ -243,10 +243,12 @@ public class CategoryDataset extends AbstractDataset { TempSeries s = series.get(i); if(renderer instanceof org.jfree.chart.renderer.category.StackedBarRenderer && s.name.contains("[")) { String category = s.name.substring(0, s.name.indexOf('[')); + if(s.result != null) + category = category + " : " + s.result; String series = s.name.substring(s.name.indexOf('[')); dataset.addValue(s.value, series, category); } else { - dataset.addValue(s.value, s.series, s.name); + dataset.addValue(s.value, s.result == null ? "Current" : s.result, s.name); } } } @@ -280,18 +282,18 @@ public class CategoryDataset extends AbstractDataset { */ private class TempSeries { public String name; - public String series; + public String result; public Double value; - public TempSeries(String name, String series, Double value) { + public TempSeries(String name, String result, Double value) { this.name = name; - this.series = series; this.value = value; + this.result = result; } @Override public String toString() { - return "TempSeries: " + name + ", " + series + ", " + value; + return "TempSeries: " + name + ", " + value + ", " + result; } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java index a680d41c..14a37955 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/CategoryPlot.java @@ -76,7 +76,7 @@ public class CategoryPlot extends AbstractPlot { } // Cleaner look: no outline borders - cplot.setInsets(new RectangleInsets(2,2,2,2), false); + cplot.setInsets(new RectangleInsets(2,5,2,2), false); cplot.setOutlineVisible(false); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PieDataset.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PieDataset.java index af50b718..a9af624d 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PieDataset.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/PieDataset.java @@ -223,6 +223,8 @@ public class PieDataset extends AbstractDataset { } String name = label == null || label.isEmpty() ? dataset.name : label; + if(dataset.result != null) + name = name + " : " + dataset.result; colorMap.put(name, color); explodedMap.put(name, exploded); series.add(new TempSeries(name, value)); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java index d6fdd57a..0a153c93 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/chart/element/ChartElementFactory.java @@ -21,14 +21,19 @@ import java.awt.geom.Rectangle2D; import org.jfree.chart.JFreeChart; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.adapter.ElementFactory; +import org.simantics.diagram.adapter.ElementFactoryUtil; import org.simantics.diagram.adapter.SyncElementFactory; import org.simantics.diagram.elements.ElementPropertySetter; import org.simantics.diagram.synchronization.CompositeHintSynchronizer; import org.simantics.diagram.synchronization.IHintSynchronizer; +import org.simantics.diagram.synchronization.ISynchronizationContext; import org.simantics.diagram.synchronization.SynchronizationHints; import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.diagram.synchronization.graph.RemoveElement; import org.simantics.diagram.synchronization.graph.TransformSynchronizer; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.diagram.IDiagram; @@ -103,10 +108,9 @@ public class ChartElementFactory extends SyncElementFactory { } @Override - public void load(ReadGraph g, ICanvasContext canvas, IDiagram diagram, Resource element, IElement e) throws DatabaseException { + public void load(ReadGraph g, ICanvasContext canvas, final IDiagram diagram, Resource element, final IElement e) throws DatabaseException { Resource chartResource; - ElementPropertySetter ps = e.getElementClass().getSingleItem(ElementPropertySetter.class); ps.loadProperties(e, element, g); @@ -130,8 +134,15 @@ public class ChartElementFactory extends SyncElementFactory { } if(chartResource == null || !g.hasStatement(chartResource)) { - e.removeHint(KEY_CHART); - e.removeHint(KEY_CHART_COMPONENT); + // Remove element if there is no chart resource for it + final ISynchronizationContext syncContext = ElementFactoryUtil.getContextChecked(diagram); + g.asyncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + new RemoveElement(syncContext, diagram, e).perform(graph); + } + }); return; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java index 26602293..5b356766 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/viewUtils/SysdynDatasetSelectionListener.java @@ -245,7 +245,7 @@ public abstract class SysdynDatasetSelectionListener implements ISelectionListen return dataSets; // Finally, find all active datasets for the found rvis - Collection activeResults = model.getAndUpdateActiveResults(g); + Collection activeResults = model.getActiveResults(); for(SysdynResult sysdynResult : activeResults) { for(String currvi : rvis.keySet()) { if(currvi != null && currvi.length() > 0) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java index 131bb200..c6f0b539 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/adapter/HistoryVariable.java @@ -203,7 +203,7 @@ public class HistoryVariable extends ChildVariable implements PropertyProvider { Resource configuration = graph.getPossibleObject(modelResource, SIMU.HasConfiguration); model = SysdynModelManager.getInstance(graph.getSession()).getModel(graph, configuration); // Update active results - model.getAndUpdateActiveResults(graph); + model.getActiveResults(); } if(rvi == null) { rvi = Variables.getRVI(graph, this).substring(1).replace("/", "."); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index a195a5fb..4ffb342b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -36,6 +36,7 @@ import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; +import org.simantics.db.procedure.Listener; import org.simantics.db.request.Read; import org.simantics.layer0.Layer0; import org.simantics.modelica.IModelicaMonitor; @@ -186,6 +187,60 @@ public class SysdynModel implements IMappingListener, IModel { sysdynResult.setResult(new SimulationResult()); previousModelStructure = ""; + + g.asyncRequest(new Read> () { + @Override + public ArrayList perform(ReadGraph graph) throws DatabaseException { + ArrayList activeResults = new ArrayList(); + // TODO: this can be done automatically with a listener + try { + // Find all active saved results + Layer0 l0 = Layer0.getInstance(graph); + SysdynResource sr = SysdynResource.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + Resource model = graph.getSingleObject(getConfigurationResource(), SIMU.IsConfigurationOf); + Collection experiments = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.Experiment)); + for(Resource experiment : experiments) { + Collection experimentResults = graph.getObjects(experiment, sr.HasResult); + for(Resource result : experimentResults) { + if(graph.hasStatement(result, sr.ShowResult)) { + SysdynResult sysdynResult = new SysdynResult( + (String) graph.getPossibleRelatedValue(result, l0.HasLabel), + (String) graph.getPossibleRelatedValue(result, sr.HasResultFile)); + activeResults.add(sysdynResult); + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + + // Add the current result if there is one + if(getSysdynResult() != null) + activeResults.add(0, getSysdynResult() ); + + return activeResults; + } + + }, new Listener> () { + + @Override + public void execute(ArrayList result) { + activeResults = result; + resultChanged(); + } + + @Override + public void exception(Throwable t) { + t.printStackTrace(); + } + + @Override + public boolean isDisposed() { + return false; + } + + }); } @@ -615,47 +670,6 @@ public class SysdynModel implements IMappingListener, IModel { } - /** - * Get all active results for this model including the current result and - * all saved results that are active. Saves the active results to be obtained - * with getActiveResults - * - * @param graph ReadGraph - * @return Active SysdynResults - */ - public Collection getAndUpdateActiveResults(ReadGraph graph) { - activeResults = new ArrayList(); - // TODO: this can be done automatically with a listener - try { - // Find all active saved results - Layer0 l0 = Layer0.getInstance(graph); - SysdynResource sr = SysdynResource.getInstance(graph); - SimulationResource SIMU = SimulationResource.getInstance(graph); - Resource model = graph.getSingleObject(configurationResource, SIMU.IsConfigurationOf); - Collection experiments = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, sr.Experiment)); - for(Resource experiment : experiments) { - Collection experimentResults = graph.getObjects(experiment, sr.HasResult); - for(Resource result : experimentResults) { - if(graph.hasStatement(result, sr.ShowResult)) { - SysdynResult sysdynResult = new SysdynResult( - (String) graph.getPossibleRelatedValue(result, l0.HasLabel), - (String) graph.getPossibleRelatedValue(result, sr.HasResultFile)); - activeResults.add(sysdynResult); - } - } - } - } catch (Exception e) { - e.printStackTrace(); - } - - // Add the current result if there is one - if(getSysdynResult() != null) - activeResults.add(0, getSysdynResult() ); - - return activeResults; - } - - /** * Get active results. To update the active results, use getAndUpdateActiveResults() * @param graph ReadGraph -- 2.47.1