--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.jfreechart.chart;\r
+\r
+import java.awt.BorderLayout;\r
+import java.awt.Component;\r
+import java.awt.GridLayout;\r
+\r
+import javax.swing.JPanel;\r
+\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.jfree.chart.ChartPanel;\r
+import org.jfree.chart.JFreeChart;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncListener;\r
+import org.simantics.db.request.Read;\r
+import org.simantics.sysdyn.JFreeChartResource;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.threads.AWTThread;\r
+import org.simantics.utils.threads.ThreadUtils;\r
+import org.simantics.utils.ui.SWTAWTComponent;\r
+\r
+/**\r
+ * Composite containing a single chart defined by a JFreeChart.Chart\r
+ * \r
+ * Similar to ChartComposite, but uses SWTAWTComponent as a base implementation. \r
+ * \r
+ * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
+ * \r
+ */\r
+public class ChartComposite2 extends SWTAWTComponent {\r
+\r
+ private JPanel jPanel;\r
+ private ChartPanel chartPanel;\r
+ private IJFreeChart chart;\r
+\r
+ public ChartComposite2(Composite parent, final String chartResourceURI, int style) {\r
+ super(parent, style);\r
+ try {\r
+ Resource chartResource = SimanticsUI.getSession().syncRequest(new Read<Resource>() {\r
+\r
+ @Override\r
+ public Resource perform(ReadGraph graph) throws DatabaseException {\r
+ return graph.getPossibleResource(chartResourceURI);\r
+ }\r
+\r
+ });\r
+ if(chartResource != null)\r
+ CreateContent(chartResource);\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ syncPopulate();\r
+ }\r
+ \r
+ public ChartComposite2(Composite parent, final Resource chartResource, int style) {\r
+ super(parent, style);\r
+ CreateContent(chartResource);\r
+ syncPopulate();\r
+ }\r
+\r
+ @Override\r
+ protected Component createSwingComponent() {\r
+ jPanel = new JPanel();\r
+ jPanel.setLayout(new GridLayout(1, 1));\r
+ if (chartPanel != null)\r
+ jPanel.add(chartPanel);\r
+ jPanel.doLayout();\r
+ return jPanel;\r
+ }\r
+ \r
+ private void setPanel(final ChartPanel panel) {\r
+ ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {\r
+ \r
+ @Override\r
+ public void run() {\r
+ if (jPanel == null) {\r
+ chartPanel = panel;\r
+ } else {\r
+ jPanel.removeAll();\r
+\r
+ chartPanel = panel;\r
+ jPanel.add(chartPanel, BorderLayout.CENTER);\r
+ jPanel.add(chartPanel);\r
+ jPanel.doLayout();\r
+ }\r
+ \r
+ }\r
+ });\r
+ }\r
+\r
+ /**\r
+ * Creates and displays the chart defined in chartResource\r
+ * @param chartResource\r
+ */\r
+ private void CreateContent(final Resource chartResource) {\r
+\r
+ // Add a listener displaying the contents of the chart. Chart is re-drawn if the definition changes\r
+ Simantics.getSession().asyncRequest(new Read<IJFreeChart>() {\r
+\r
+ @Override\r
+ public IJFreeChart perform(ReadGraph graph) throws DatabaseException {\r
+ // Adapt chartResource to a chart (XY, pie, bar, ...)\r
+ if(graph.isInstanceOf(chartResource, JFreeChartResource.getInstance(graph).Chart)) {\r
+ if(chart != null)\r
+ chart.dispose();\r
+ chart = graph.adapt(chartResource, IJFreeChart.class);\r
+ return chart;\r
+ } else {\r
+ return null;\r
+ }\r
+ }\r
+\r
+ } , new AsyncListener<IJFreeChart>() {\r
+\r
+ @Override\r
+ public boolean isDisposed() {\r
+ return ChartComposite2.this.isDisposed();\r
+ }\r
+\r
+ @Override\r
+ public void execute(AsyncReadGraph graph, IJFreeChart chart) {\r
+ if(chart == null || ChartComposite2.this.isDisposed())\r
+ return;\r
+ \r
+ JFreeChart jfreeChart = chart.getChart();\r
+\r
+ ChartPanel panel = new ChartPanel(jfreeChart,\r
+ ChartPanel.DEFAULT_WIDTH,\r
+ ChartPanel.DEFAULT_HEIGHT,\r
+ ChartPanel.DEFAULT_MINIMUM_DRAW_WIDTH,\r
+ ChartPanel.DEFAULT_MINIMUM_DRAW_HEIGHT,\r
+ ChartPanel.DEFAULT_MAXIMUM_DRAW_WIDTH, \r
+ ChartPanel.DEFAULT_MAXIMUM_DRAW_HEIGHT,\r
+ false,\r
+ false, true, true, true, true);\r
+ setPanel(panel);\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ throwable.printStackTrace();\r
+\r
+ }\r
+ });\r
+ }\r
+\r
+}\r