From: luukkainen Date: Thu, 8 May 2014 12:50:12 +0000 (+0000) Subject: STWAWTComponent based ChartComposite X-Git-Tag: 1.8.1~65 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=527b0d71510dbc8592b54bd0719bff909a66888b;p=simantics%2Fsysdyn.git STWAWTComponent based ChartComposite refs #4865 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29429 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java new file mode 100644 index 00000000..20a2c1cd --- /dev/null +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java @@ -0,0 +1,162 @@ +/******************************************************************************* + * 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; + } + + private 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(); + } + + } + }); + } + + /** + * 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(); + + } + }); + } + +}