Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed the custom size export being blank (on Java 11) #273

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,4 +201,4 @@ private void updateWidgets() {
textWidth.setEnabled(enabled);
textHeight.setEnabled(enabled);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,6 +20,7 @@ public class ImageFactory<T extends ScrollableChart> {

private T t;
private ImageSupplier imageSupplier;
private Display display;

/**
* The width and height of the current display is allowed.
Expand All @@ -32,11 +33,12 @@ public class ImageFactory<T extends ScrollableChart> {
* @throws InstantiationException
*/
public ImageFactory(Class<T> 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;
Expand All @@ -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.
Expand All @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}