]> gerrit.simantics Code Review - simantics/sysdyn.git/commitdiff
Improve ChartComposite2 and ChartTab reuse 17/617/1
authorMarko Luukkainen <marko.luukkainen@vtt.fi>
Mon, 12 Jun 2017 07:56:21 +0000 (10:56 +0300)
committerMarko Luukkainen <marko.luukkainen@vtt.fi>
Mon, 12 Jun 2017 07:56:21 +0000 (10:56 +0300)
fixes #7293

Change-Id: I4e0d62f7059f8c6d1ca75f8e4ccae1a81ff514e3

bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartComposite2.java
bundles/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/properties/ChartTab.java

index d3e169db30b3001ec376bb7c490ce17cfe798fd2..198fe0148104d3f0b912ee6cb883aad6ebeca3f2 100644 (file)
-/*******************************************************************************\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
-       protected 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
-       public ChartPanel getChartPanel() {\r
-               return chartPanel;\r
-       }\r
-       \r
-       public IJFreeChart getChart() {\r
-               return chart;\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
+/*******************************************************************************
+ * 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 <marko.luukkainen@vtt.fi>
+ * 
+ */
+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<Resource>() {
+
+                @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<IJFreeChart> getChartQuery(final Resource chartResource) {
+               return new Read<IJFreeChart>() {
+            @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<IJFreeChart>() {
+
+            @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();
+
+            }
+        });
+    }
+
+}
index 1135348d6dd5ac8c74489835ee12566195d3e0ce..df62384fb12d9263d0f2fb4071d6a1abff2a89be 100644 (file)
@@ -1,49 +1,53 @@
-/*******************************************************************************\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.properties;\r
-\r
-import org.eclipse.swt.SWT;\r
-import org.eclipse.swt.widgets.Composite;\r
-import org.eclipse.ui.IWorkbenchSite;\r
-import org.simantics.browsing.ui.swt.widgets.impl.Widget;\r
-import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.jfreechart.chart.ChartComposite;\r
-import org.simantics.utils.ui.AdaptionUtils;\r
-\r
-/**\r
- * Tab for displaying a chart\r
- * @author Teemu Lempinen\r
- *\r
- */\r
-public class ChartTab extends LabelPropertyTabContributor implements Widget {\r
-\r
-    public ChartTab(Object id) {\r
-        super(id);\r
-    }\r
-\r
-    private Composite parent;\r
-\r
-    @Override\r
-    public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) {\r
-        support.register(this);\r
-        this.parent = body;\r
-    }\r
-\r
-    @Override\r
-    public void setInput(ISessionContext context, final Object input) {\r
-        Resource chart = AdaptionUtils.adaptToSingle(input, Resource.class);\r
-        new ChartComposite(parent, chart, SWT.BORDER);\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * 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;
+       }
+
+}