diff --git a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/core/BitmapExportSettingsDialog.java b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/core/BitmapExportSettingsDialog.java index e56c9fc1..049ccf52 100644 --- a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/core/BitmapExportSettingsDialog.java +++ b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/core/BitmapExportSettingsDialog.java @@ -37,8 +37,8 @@ public class BitmapExportSettingsDialog extends TitleAreaDialog { private Rectangle boundsMax; // private boolean customSize = false; - private int customWidth = 100; - private int customHeight = 100; + private int customWidth = 0; + private int customHeight = 0; // private Text textWidth; private Text textHeight; @@ -201,4 +201,4 @@ private void updateWidgets() { textWidth.setEnabled(enabled); textHeight.setEnabled(enabled); } -} \ No newline at end of file +} diff --git a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/images/ImageFactory.java b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/images/ImageFactory.java index ac1f7a95..3b294834 100644 --- a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/images/ImageFactory.java +++ b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/images/ImageFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2017, 2019 Lablicate GmbH. + * Copyright (c) 2017, 2021 Lablicate GmbH. * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 @@ -20,6 +20,7 @@ public class ImageFactory { private T t; private ImageSupplier imageSupplier; + private Display display; /** * The width and height of the current display is allowed. @@ -32,11 +33,12 @@ public class ImageFactory { * @throws InstantiationException */ public ImageFactory(Class clazz, int width, int height) throws InstantiationException, IllegalAccessException { + // t = clazz.newInstance(); imageSupplier = new ImageSupplier(); // - Display display = ((ScrollableChart)t).getBaseChart().getDisplay(); + display = ((ScrollableChart)t).getBaseChart().getDisplay(); if(display != null) { width = (width > display.getBounds().width) ? display.getBounds().width : width; height = (height > display.getBounds().height) ? display.getBounds().height : height; @@ -54,18 +56,6 @@ public void closeShell() { t.getShell().close(); } - /** - * Returns the image data of the chart. - * Don't forget to call closeShell() if the shell is not needed anymore. - * - * @return ImageData. - */ - public ImageData getImageData() { - - t.getShell().open(); - return imageSupplier.getImageData(t.getBaseChart()); - } - /** * Path to the file and the format. * Don't forget to call closeShell() if the shell is not needed anymore. @@ -76,7 +66,14 @@ public ImageData getImageData() { */ public void saveImage(String fileName, int format) { - ImageData imageData = getImageData(); - imageSupplier.saveImage(imageData, fileName, format); + t.getShell().open(); + while(!t.getShell().isDisposed()) { + if(!display.readAndDispatch()) { + display.sleep(); + ImageData imageData = imageSupplier.getImageData(t.getBaseChart()); + imageSupplier.saveImage(imageData, fileName, format); + return; + } + } } } diff --git a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/menu/bitmap/AbstractBitmapExportHandler.java b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/menu/bitmap/AbstractBitmapExportHandler.java index 207da34f..f05e29b4 100644 --- a/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/menu/bitmap/AbstractBitmapExportHandler.java +++ b/org.eclipse.swtchart.export/src/org/eclipse/swtchart/export/menu/bitmap/AbstractBitmapExportHandler.java @@ -13,14 +13,15 @@ *******************************************************************************/ package org.eclipse.swtchart.export.menu.bitmap; -import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.window.Window; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swtchart.export.core.AbstractSeriesExportHandler; import org.eclipse.swtchart.export.core.BitmapExportSettingsDialog; @@ -73,7 +74,7 @@ public void execute(Shell shell, ScrollableChart scrollableChart) { if(bitmapSettingsDialog.isCustomSize()) { int width = bitmapSettingsDialog.getCustomWidth(); int height = bitmapSettingsDialog.getCustomHeight(); - exportCustomSize(shell, baseChart, fileName, width, height); + exportCustomSize(baseChart, fileName, width, height); } else { exportNormal(baseChart, fileName); } @@ -88,41 +89,39 @@ private void exportNormal(BaseChart baseChart, String fileName) { imageSupplier.saveImage(imageData, fileName, format); } - private void exportCustomSize(Shell shell, BaseChart baseChart, String fileName, int width, int height) { + private void exportCustomSize(BaseChart baseChart, String fileName, int width, int height) { Composite parent = baseChart.getParent(); Rectangle bounds = baseChart.getBounds(); /* * Size Image / Save */ - Shell imageShell = new Shell(Display.getDefault()); - try { - if(imageShell != null) { - Rectangle imageBounds = imageShell.computeTrim(0, 0, width, height); - imageShell.setSize(imageBounds.width, imageBounds.height); - imageShell.setLocation(0, 0); - imageShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); - imageShell.open(); - // + Display display = Display.getDefault(); + Shell imageShell = new Shell(display); + Rectangle imageBounds = imageShell.computeTrim(0, 0, width, height); + imageShell.setSize(imageBounds.width, imageBounds.height); + imageShell.setLocation(0, 0); + imageShell.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE)); + imageShell.addListener(SWT.Paint, new Listener() { + + @Override + public void handleEvent(Event event) { + baseChart.setParent(imageShell); baseChart.setBounds(0, 0, width, height); baseChart.updateLayout(); - // + } + }); + imageShell.open(); + while(!imageShell.isDisposed()) { + if(!display.readAndDispatch()) { + display.sleep(); ImageSupplier imageSupplier = new ImageSupplier(); ImageData imageData = imageSupplier.getImageData(baseChart); imageSupplier.saveImage(imageData, fileName, format); - } - } catch(Exception e) { - e.printStackTrace(); - MessageDialog.openWarning(shell, title, "Something gone wrong to export the bitmap."); - } finally { - /* - * Reset - */ - baseChart.setParent(parent); - baseChart.setBounds(bounds); - baseChart.updateLayout(); - if(imageShell != null) { + baseChart.setParent(parent); + baseChart.setBounds(bounds); + baseChart.updateLayout(); imageShell.close(); } } diff --git a/org.eclipse.swtchart.extensions/src/org/eclipse/swtchart/extensions/preferences/PreferenceConstants.java b/org.eclipse.swtchart.extensions/src/org/eclipse/swtchart/extensions/preferences/PreferenceConstants.java index 95149266..e11dfa4a 100644 --- a/org.eclipse.swtchart.extensions/src/org/eclipse/swtchart/extensions/preferences/PreferenceConstants.java +++ b/org.eclipse.swtchart.extensions/src/org/eclipse/swtchart/extensions/preferences/PreferenceConstants.java @@ -51,7 +51,7 @@ public class PreferenceConstants { public static final String P_BITMAP_EXPORT_CUSTOM_SIZE = "bitmapExportCustomSize"; public static final boolean DEF_BITMAP_EXPORT_CUSTOM_SIZE = false; public static final String P_BITMAP_EXPORT_WIDTH = "bitmapExportWidth"; - public static final int DEF_BITMAP_EXPORT_WIDTH = 200; + public static final int DEF_BITMAP_EXPORT_WIDTH = 512; public static final String P_BITMAP_EXPORT_HEIGHT = "bitmapExportHeight"; - public static final int DEF_BITMAP_EXPORT_HEIGHT = 200; + public static final int DEF_BITMAP_EXPORT_HEIGHT = 512; }