From 546fbe943fd410ab19e95d3cbea62307309a5670 Mon Sep 17 00:00:00 2001 From: Marko Luukkainen Date: Mon, 12 Jun 2017 10:56:21 +0300 Subject: [PATCH] Improve ChartComposite2 and ChartTab reuse fixes #7293 Change-Id: I4e0d62f7059f8c6d1ca75f8e4ccae1a81ff514e3 --- .../jfreechart/chart/ChartComposite2.java | 352 +++++++++--------- .../jfreechart/chart/properties/ChartTab.java | 102 ++--- 2 files changed, 235 insertions(+), 219 deletions(-) diff --git a/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java b/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java index d3e169db..198fe014 100644 --- a/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java +++ b/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java @@ -1,170 +1,182 @@ -/******************************************************************************* - * 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.jfreechart.chart; - -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.GridLayout; - -import javax.swing.JPanel; - -import org.eclipse.swt.widgets.Composite; -import org.jfree.chart.ChartPanel; -import org.jfree.chart.JFreeChart; -import org.simantics.Simantics; -import org.simantics.db.AsyncReadGraph; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.db.procedure.AsyncListener; -import org.simantics.db.request.Read; -import org.simantics.sysdyn.JFreeChartResource; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.threads.AWTThread; -import org.simantics.utils.threads.ThreadUtils; -import org.simantics.utils.ui.SWTAWTComponent; - -/** - * Composite containing a single chart defined by a JFreeChart.Chart - * - * Similar to ChartComposite, but uses SWTAWTComponent as a base implementation. - * - * @author Marko Luukkainen - * - */ -public class ChartComposite2 extends SWTAWTComponent { - - private JPanel jPanel; - private ChartPanel chartPanel; - private IJFreeChart chart; - - public ChartComposite2(Composite parent, final String chartResourceURI, int style) { - super(parent, style); - try { - Resource chartResource = SimanticsUI.getSession().syncRequest(new Read() { - - @Override - public Resource perform(ReadGraph graph) throws DatabaseException { - return graph.getPossibleResource(chartResourceURI); - } - - }); - if(chartResource != null) - CreateContent(chartResource); - } catch (DatabaseException e) { - e.printStackTrace(); - } - syncPopulate(); - } - - public ChartComposite2(Composite parent, final Resource chartResource, int style) { - super(parent, style); - CreateContent(chartResource); - syncPopulate(); - } - - @Override - protected Component createSwingComponent() { - jPanel = new JPanel(); - jPanel.setLayout(new GridLayout(1, 1)); - if (chartPanel != null) - jPanel.add(chartPanel); - jPanel.doLayout(); - return jPanel; - } - - protected void setPanel(final ChartPanel panel) { - ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { - - @Override - public void run() { - if (jPanel == null) { - chartPanel = panel; - } else { - jPanel.removeAll(); - - chartPanel = panel; - jPanel.add(chartPanel, BorderLayout.CENTER); - jPanel.add(chartPanel); - jPanel.doLayout(); - } - - } - }); - } - - public ChartPanel getChartPanel() { - return chartPanel; - } - - public IJFreeChart getChart() { - return chart; - } - - /** - * Creates and displays the chart defined in chartResource - * @param chartResource - */ - private void CreateContent(final Resource chartResource) { - - // Add a listener displaying the contents of the chart. Chart is re-drawn if the definition changes - Simantics.getSession().asyncRequest(new Read() { - - @Override - 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; - } else { - return null; - } - } - - } , new AsyncListener() { - - @Override - public boolean isDisposed() { - return ChartComposite2.this.isDisposed(); - } - - @Override - public void execute(AsyncReadGraph graph, IJFreeChart chart) { - if(chart == null || ChartComposite2.this.isDisposed()) - return; - - JFreeChart jfreeChart = chart.getChart(); - - ChartPanel panel = new ChartPanel(jfreeChart, - ChartPanel.DEFAULT_WIDTH, - ChartPanel.DEFAULT_HEIGHT, - ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH, - ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT, - ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, - ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT, - false, - false, true, true, true, true); - setPanel(panel); - - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - throwable.printStackTrace(); - - } - }); - } - -} +/******************************************************************************* + * 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.jfreechart.chart; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.GridLayout; + +import javax.swing.JPanel; + +import org.eclipse.swt.widgets.Composite; +import org.jfree.chart.ChartPanel; +import org.jfree.chart.JFreeChart; +import org.simantics.Simantics; +import org.simantics.db.AsyncReadGraph; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.AsyncListener; +import org.simantics.db.request.Read; +import org.simantics.sysdyn.JFreeChartResource; +import org.simantics.ui.SimanticsUI; +import org.simantics.utils.threads.AWTThread; +import org.simantics.utils.threads.ThreadUtils; +import org.simantics.utils.ui.SWTAWTComponent; + +/** + * Composite containing a single chart defined by a JFreeChart.Chart + * + * Similar to ChartComposite, but uses SWTAWTComponent as a base implementation. + * + * @author Marko Luukkainen + * + */ +public class ChartComposite2 extends SWTAWTComponent { + + private JPanel jPanel; + private ChartPanel chartPanel; + private IJFreeChart chart; + + public ChartComposite2(Composite parent, final String chartResourceURI, int style) { + super(parent, style); + try { + Resource chartResource = SimanticsUI.getSession().syncRequest(new Read() { + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return graph.getPossibleResource(chartResourceURI); + } + + }); + if(chartResource != null) + CreateContent(chartResource); + } catch (DatabaseException e) { + e.printStackTrace(); + } + syncPopulate(); + } + + public ChartComposite2(Composite parent, final Resource chartResource, int style) { + super(parent, style); + CreateContent(chartResource); + syncPopulate(); + } + + protected ChartComposite2(Composite parent, int style) { + super(parent, style); + } + + @Override + protected Component createSwingComponent() { + jPanel = new JPanel(); + jPanel.setLayout(new GridLayout(1, 1)); + if (chartPanel != null) + jPanel.add(chartPanel); + jPanel.doLayout(); + return jPanel; + } + + protected void setPanel(final ChartPanel panel) { + ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() { + + @Override + public void run() { + if (jPanel == null) { + chartPanel = panel; + } else { + jPanel.removeAll(); + + chartPanel = panel; + jPanel.add(chartPanel, BorderLayout.CENTER); + jPanel.add(chartPanel); + jPanel.doLayout(); + } + + } + }); + } + + public ChartPanel getChartPanel() { + return chartPanel; + } + + public IJFreeChart getChart() { + return chart; + } + + protected Read getChartQuery(final Resource chartResource) { + return new Read() { + @Override + public IJFreeChart perform(ReadGraph graph) throws DatabaseException { + // Adapt chartResource to a chart (XY, pie, bar, ...) + if(graph.isInstanceOf(chartResource, JFreeChartResource.getInstance(graph).Chart)) { + return graph.adapt(chartResource, IJFreeChart.class); + } else { + return null; + } + } + }; + } + + /** + * Creates and displays the chart defined in chartResource + * @param chartResource + */ + protected void CreateContent(final Resource chartResource) { + + // Add a listener displaying the contents of the chart. Chart is re-drawn if the definition changes + Simantics.getSession().asyncRequest(getChartQuery(chartResource) , new AsyncListener() { + + @Override + public boolean isDisposed() { + return ChartComposite2.this.isDisposed(); + } + + @Override + public void execute(AsyncReadGraph graph, IJFreeChart chart) { + if(ChartComposite2.this.chart != null) { + ChartComposite2.this.chart.dispose(); + ChartComposite2.this.chart = null; + } + + if(chart == null) { + return; + } + if (ChartComposite2.this.isDisposed()) { + chart.dispose(); + return; + } + ChartComposite2.this.chart = chart; + JFreeChart jfreeChart = chart.getChart(); + + ChartPanel panel = new ChartPanel(jfreeChart, + ChartPanel.DEFAULT_WIDTH, + ChartPanel.DEFAULT_HEIGHT, + ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH, + ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT, + ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, + ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT, + false, + false, true, true, true, true); + setPanel(panel); + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + throwable.printStackTrace(); + + } + }); + } + +} diff --git a/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/ChartTab.java b/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/ChartTab.java index 1135348d..df62384f 100644 --- a/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/ChartTab.java +++ b/bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/ChartTab.java @@ -1,49 +1,53 @@ -/******************************************************************************* - * 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.jfreechart.chart.properties; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.widgets.Composite; -import org.eclipse.ui.IWorkbenchSite; -import org.simantics.browsing.ui.swt.widgets.impl.Widget; -import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; -import org.simantics.db.Resource; -import org.simantics.db.management.ISessionContext; -import org.simantics.jfreechart.chart.ChartComposite; -import org.simantics.utils.ui.AdaptionUtils; - -/** - * Tab for displaying a chart - * @author Teemu Lempinen - * - */ -public class ChartTab extends LabelPropertyTabContributor implements Widget { - - public ChartTab(Object id) { - super(id); - } - - private Composite parent; - - @Override - public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { - support.register(this); - this.parent = body; - } - - @Override - public void setInput(ISessionContext context, final Object input) { - Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class); - new ChartComposite(parent, chart, SWT.BORDER); - } - -} +/******************************************************************************* + * 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.jfreechart.chart.properties; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.Resource; +import org.simantics.db.management.ISessionContext; +import org.simantics.jfreechart.chart.ChartComposite; +import org.simantics.utils.ui.AdaptionUtils; + +/** + * Tab for displaying a chart + * @author Teemu Lempinen + * + */ +public class ChartTab extends LabelPropertyTabContributor implements Widget { + + public ChartTab(Object id) { + super(id); + } + + private Composite parent; + + @Override + public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + support.register(this); + this.parent = body; + } + + @Override + public void setInput(ISessionContext context, final Object input) { + Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class); + new ChartComposite(parent, chart, SWT.BORDER); + } + + public Composite getParent() { + return parent; + } + +} -- 2.47.1