From 818c8465e0b6cb8307589fe6648b45b505adbaf8 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 14 Jun 2013 07:43:38 +0000 Subject: [PATCH] Trend view could not be reopened. Got rid of anonymous inner class listeners and static variables -> nicer and working code (fixes #4335) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27541 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../simantics/sysdyn/ui/trend/TrendToPng.java | 63 +++++--- .../simantics/sysdyn/ui/trend/TrendView.java | 134 ++++++++++++------ 2 files changed, 130 insertions(+), 67 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java index e0555cae..dae65d78 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendToPng.java @@ -17,6 +17,8 @@ import java.io.IOException; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; +import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell; @@ -33,29 +35,46 @@ public class TrendToPng extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { + EvaluationContext c = (EvaluationContext)event.getApplicationContext(); + Object o = c.getParent().getVariable("activePart"); + if(o != null && o instanceof TrendView) { + TrendView trendView = (TrendView) o; + int width = trendView.getPanel().getSize().width; + int height = trendView.getPanel().getSize().height; + int compressionLevel = 0; - int width = TrendView.panel.getSize().width; - int height = TrendView.panel.getSize().height; - int compressionLevel = 0; - - final Shell shell = HandlerUtil.getActiveShellChecked(event); - FileDialog fd = new FileDialog(shell, SWT.SAVE); - fd.setText("Export trend to PNG"); - String[] ext = {"*.png"}; - fd.setFilterExtensions(ext); - String selected = fd.open(); - - if (!(selected == null)){ - File file = new File(selected); - JFreeChart chart = TrendView.panel.getChart(); - - try { - ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel); - } catch (IOException e) { - e.printStackTrace(); - } - } + final Shell shell = HandlerUtil.getActiveShellChecked(event); + + while(true) { + FileDialog fd = new FileDialog(shell, SWT.SAVE); + fd.setText("Export trend to PNG"); + String[] ext = {"*.png"}; + fd.setFilterExtensions(ext); + String selected = fd.open(); + + if(selected == null) + return null; + + File file = new File(selected); + + if(file.exists()) { + MessageDialog dialog = new MessageDialog(shell, "Overwrite " + file.getName() + "?", null, file.getName() + " exits. Do you wan't to overwrite it?", 0, + new String[] { "Yes", "No" }, 0); + dialog.create(); + if (dialog.open() == 1) + continue; + } + + JFreeChart chart = trendView.getPanel().getChart(); + try { + ChartUtilities.saveChartAsPNG(file, chart, width, height, null, true, compressionLevel); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + } return null; } - + } \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java index 83bcc56d..9b2fa61b 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/trend/TrendView.java @@ -34,6 +34,7 @@ import org.simantics.db.request.Read; import org.simantics.jfreechart.chart.IJFreeChart; import org.simantics.sysdyn.manager.SysdynDataSet; import org.simantics.sysdyn.ui.viewUtils.SysdynDatasetSelectionListener; +import org.simantics.utils.RunnableWithObject; /** * Trend view that shows all active simulation results for selected variables. @@ -45,12 +46,28 @@ import org.simantics.sysdyn.ui.viewUtils.SysdynDatasetSelectionListener; public class TrendView extends ViewPart { private Frame frame; - public static ChartPanel panel; - private final SysdynDatasets sysdynDatasets = new SysdynDatasets(); + private ChartPanel panel; + private SysdynDatasets sysdynDatasets = new SysdynDatasets(); private SysdynDatasetSelectionListener sysdynDatasetSelectionListener; - public static JFreeChart defaultchart; + private JFreeChart defaultchart; private Composite composite; - private CustomChartListener listener; + + + public Frame getFrame() { + return frame; + } + + public ChartPanel getPanel() { + return panel; + } + + public void setPanel(ChartPanel panel) { + this.panel = panel; + } + + public SysdynDatasets getSysdynDatasets() { + return sysdynDatasets; + } /** * Dataset for jFreeChart @@ -110,61 +127,75 @@ public class TrendView extends ViewPart { displayDefaultChart(); // Add a dataset listener that updates datasets for the chart according to current selection - sysdynDatasetSelectionListener = new SysdynDatasetSelectionListener() { + sysdynDatasetSelectionListener = new TrendViewSelectionListner(this); - @Override - protected void selectionChanged(final Collection activeDatasets) { - SwingUtilities.invokeLater(new Runnable() { - - @Override - public void run() { - if(listener != null) { - listener.dispose(); - } - sysdynDatasets.setDatasets(activeDatasets.toArray(new SysdynDataSet[activeDatasets.size()])); - displayDefaultChart(); - } + getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(sysdynDatasetSelectionListener); - }); - } + } + + private class TrendViewSelectionListner extends SysdynDatasetSelectionListener { + + private TrendView trendView; + private CustomChartListener listener; + + public TrendViewSelectionListner(TrendView trendView) { + this.trendView = trendView; + } - @Override - protected void selectionChanged(ReadGraph graph, final Resource chartResource) { + @Override + protected void selectionChanged(final Collection activeDatasets) { + SwingUtilities.invokeLater(new Runnable() { - if(listener != null) { - listener.dispose(); + @Override + public void run() { + if(listener != null) { + listener.dispose(); + } + trendView.getSysdynDatasets().setDatasets(activeDatasets.toArray(new SysdynDataSet[activeDatasets.size()])); + displayDefaultChart(); } - listener = new CustomChartListener(); + }); + } - graph.asyncRequest(new Read() { + @Override + protected void selectionChanged(ReadGraph graph, final Resource chartResource) { - @Override - public JFreeChart perform(ReadGraph graph) throws DatabaseException { - if(graph.hasStatement(chartResource)) { - IJFreeChart chart = graph.adapt(chartResource, IJFreeChart.class); - if(chart != null) { - return chart.getChart(); - } - } - return null; - } - }, listener); + if(listener != null) { + listener.dispose(); } - }; - getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener(sysdynDatasetSelectionListener); + listener = new CustomChartListener(trendView); + + graph.asyncRequest(new Read() { + @Override + public JFreeChart perform(ReadGraph graph) throws DatabaseException { + if(graph.hasStatement(chartResource)) { + IJFreeChart chart = graph.adapt(chartResource, IJFreeChart.class); + if(chart != null) { + return chart.getChart(); + } + } + return null; + } + }, listener); + } } private class CustomChartListener implements Listener { private boolean disposed = false; + private TrendView trendView; + + public CustomChartListener(TrendView trendView) { + this.trendView = trendView; + } @Override public void execute(JFreeChart result) { if(!disposed) - displayChart(result); + displayChart(result, trendView); } @Override @@ -187,23 +218,36 @@ public class TrendView extends ViewPart { * Displays jFreeChart * @param jFreeChart */ - private void displayChart(final JFreeChart jFreeChart) { - SwingUtilities.invokeLater(new Runnable() { + private void displayChart(JFreeChart jFreeChart, TrendView trendView) { + SwingUtilities.invokeLater(new RunnableWithObject(jFreeChart, trendView) { @Override public void run() { + if(count() != 2 || getObject(0) == null || getObject(1) == null) + return; + + if(!(getObject(0) instanceof JFreeChart && getObject(1) instanceof TrendView)) + return; + + JFreeChart jFreeChart = (JFreeChart) getObject(0); + TrendView trendView = (TrendView) getObject(1); + + Frame frame = trendView.getFrame(); + ChartPanel panel = trendView.getPanel(); + // Do not just simply frame.removeAll(); // Instead, use and reuse only the first component of the frame. if(jFreeChart != null) { - if (panel == null){ + if (panel == null || frame.getComponentCount() == 0){ panel = new ChartPanel(jFreeChart, false, true, true, true, true); + trendView.setPanel(panel); frame.add(panel); } else { - panel.setChart(jFreeChart); +// panel.setChart(jFreeChart); if ( frame.getComponent(0) instanceof ChartPanel ){ ChartPanel tempPanel = (ChartPanel)frame.getComponent(0); tempPanel.setChart(jFreeChart); - panel = tempPanel; + trendView.setPanel(tempPanel); } } } @@ -228,7 +272,7 @@ public class TrendView extends ViewPart { ); defaultchart = new JFreeChart(plot); } - displayChart(defaultchart); + displayChart(defaultchart, this); } @Override -- 2.47.1