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

Fixes #94 - GC not reliably closed everywhere #103

Merged
merged 1 commit into from
Aug 1, 2024
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
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017, 2020 Original authors and others.
* Copyright (c) 2017, 2024 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -128,13 +128,13 @@ public void exportTable(Shell shell,
final Image image = new Image(shell.getDisplay(), width, height);
GC gc = new GC(image);

Rectangle layerBounds = new Rectangle(0, 0, width, height);
layer.getLayerPainter().paintLayer(layer, gc, 0, 0, layerBounds, configRegistry);
try {
Rectangle layerBounds = new Rectangle(0, 0, width, height);
layer.getLayerPainter().paintLayer(layer, gc, 0, 0, layerBounds, configRegistry);

ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };

try {
if (this.outputStreamProvider instanceof FileOutputStreamProvider) {
final FileOutputStreamProvider fileOutputStreamProvider = (FileOutputStreamProvider) this.outputStreamProvider;
final String selectedFilterExt = DEFAULT_FILTER_EXTENSIONS[fileOutputStreamProvider.getExtensionFilterIndex()];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2020 Original authors and others.
* Copyright (c) 2012, 2024 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -117,52 +117,57 @@ public static void drawVerticalText(String string, int x, int y,
// Create a GC so we can draw the image
GC stringGc = new GC(stringImage);

// Set attributes from the original GC to the new GC
stringGc.setAntialias(gc.getAntialias());
stringGc.setTextAntialias(gc.getTextAntialias());
stringGc.setForeground(gc.getForeground());
stringGc.setBackground(gc.getBackground());
stringGc.setFont(gc.getFont());

// Fill the image with the specified background color
// to avoid white spaces if the text does not fill the
// whole image (e.g. on new lines)
stringGc.fillRectangle(0, 0, pt.x, pt.y);

// Draw the text onto the image
stringGc.drawText(string, 0, 0);

// draw underline and/or strikethrough
if (underline || strikethrough) {
// check and draw underline and strikethrough separately so it is
// possible to combine both
if (underline) {
// y = start y of text + font height
// - half of the font descent so the underline is between the
// baseline and the bottom
int underlineY = pt.y
- (stringGc.getFontMetrics().getDescent() / 2);
stringGc.drawLine(0, underlineY, pt.x, underlineY);
}
try {
// Set attributes from the original GC to the new GC
stringGc.setAntialias(gc.getAntialias());
stringGc.setTextAntialias(gc.getTextAntialias());
stringGc.setForeground(gc.getForeground());
stringGc.setBackground(gc.getBackground());
stringGc.setFont(gc.getFont());

// Fill the image with the specified background color
// to avoid white spaces if the text does not fill the
// whole image (e.g. on new lines)
stringGc.fillRectangle(0, 0, pt.x, pt.y);

// Draw the text onto the image
stringGc.drawText(string, 0, 0);

// draw underline and/or strikethrough
if (underline || strikethrough) {
// check and draw underline and strikethrough separately so it
// is
// possible to combine both
if (underline) {
// y = start y of text + font height
// - half of the font descent so the underline is between
// the
// baseline and the bottom
int underlineY = pt.y
- (stringGc.getFontMetrics().getDescent() / 2);
stringGc.drawLine(0, underlineY, pt.x, underlineY);
}

if (strikethrough) {
// y = start y of text + half of font height + ascent so lower
// case characters are
// also strikethrough
int strikeY = (pt.y / 2)
+ (stringGc.getFontMetrics().getLeading() / 2);
stringGc.drawLine(0, strikeY, pt.x, strikeY);
if (strikethrough) {
// y = start y of text + half of font height + ascent so
// lower
// case characters are
// also strikethrough
int strikeY = (pt.y / 2)
+ (stringGc.getFontMetrics().getLeading() / 2);
stringGc.drawLine(0, strikeY, pt.x, strikeY);
}
}
}

// Draw the image vertically onto the original GC
drawVerticalImage(stringImage, x, y, paintBackground, gc, style);

// Dispose the new GC
stringGc.dispose();
// Draw the image vertically onto the original GC
drawVerticalImage(stringImage, x, y, paintBackground, gc, style);
} finally {
// Dispose the new GC
stringGc.dispose();

// Dispose the image
stringImage.dispose();
// Dispose the image
stringImage.dispose();
}
}

/**
Expand Down Expand Up @@ -296,36 +301,41 @@ public static Image createRotatedText(String text, Font font,

// Create a GC to calculate font's dimensions
GC gc = new GC(display);
gc.setFont(font);

// Determine string's dimensions
Point pt = gc.textExtent(text);

// Dispose that gc
gc.dispose();

// Create an image the same size as the string
Image stringImage = new Image(display, pt.x, pt.y);
// Create a gc for the image
gc = new GC(stringImage);
gc.setFont(font);
gc.setForeground(foreground);
gc.setBackground(background);

// Draw the text onto the image
gc.drawText(text, 0, 0);

// Draw the image vertically onto the original GC
Image image = createRotatedImage(stringImage, style);

// Dispose the new GC
gc.dispose();

// Dispose the horizontal image
stringImage.dispose();

// Return the rotated image
return image;
Image stringImage = null;
try {
gc.setFont(font);

// Determine string's dimensions
Point pt = gc.textExtent(text);

// Dispose that gc
gc.dispose();

// Create an image the same size as the string
stringImage = new Image(display, pt.x, pt.y);
// Create a gc for the image
gc = new GC(stringImage);
gc.setFont(font);
gc.setForeground(foreground);
gc.setBackground(background);

// Draw the text onto the image
gc.drawText(text, 0, 0);

// Draw the image vertically onto the original GC
Image image = createRotatedImage(stringImage, style);

// Return the rotated image
return image;
} finally {
// Dispose the new GC
gc.dispose();

// Dispose the horizontal image
if (stringImage != null) {
stringImage.dispose();
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2020 Original authors and others.
* Copyright (c) 2012, 2024 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -60,13 +60,15 @@ public static int[] getPreferredColumnWidths(

GC gc = gcFactory.createGC();
if (gc != null) {
int[] columnWidths = new int[columnPositions.length];
for (int i = 0; i < columnPositions.length; i++) {
columnWidths[i] = getPreferredColumnWidth(layer, columnPositions[i], configRegistry, gc);
try {
int[] columnWidths = new int[columnPositions.length];
for (int i = 0; i < columnPositions.length; i++) {
columnWidths[i] = getPreferredColumnWidth(layer, columnPositions[i], configRegistry, gc);
}
return columnWidths;
} finally {
gc.dispose();
}
gc.dispose();

return columnWidths;
} else {
return new int[0];
}
Expand Down Expand Up @@ -154,13 +156,15 @@ public static int[] getPreferredRowHeights(

GC gc = gcFactory.createGC();
if (gc != null) {
int[] rowHeights = new int[rowPositions.length];
for (int i = 0; i < rowPositions.length; i++) {
rowHeights[i] = getPreferredRowHeight(layer, rowPositions[i], configRegistry, gc);
try {
int[] rowHeights = new int[rowPositions.length];
for (int i = 0; i < rowPositions.length; i++) {
rowHeights[i] = getPreferredRowHeight(layer, rowPositions[i], configRegistry, gc);
}
return rowHeights;
} finally {
gc.dispose();
}
gc.dispose();

return rowHeights;
} else {
return new int[0];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2020 Original authors and others.
* Copyright (c) 2012, 2024 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -62,9 +62,12 @@ private Image getColorImage(Color color) {
Display display = Display.getCurrent();
this.image = new Image(display, new Rectangle(10, 10, 70, 20));
GC gc = new GC(this.image);
gc.setBackground(color);
gc.fillRectangle(this.image.getBounds());
gc.dispose();
try {
gc.setBackground(color);
gc.fillRectangle(this.image.getBounds());
} finally {
gc.dispose();
}
return this.image;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2012, 2020 Original authors and others.
* Copyright (c) 2012, 2024 Original authors and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand Down Expand Up @@ -84,18 +84,21 @@ private void setCellImage(NatTable natTable) {
Image image = new Image(natTable.getDisplay(), cellBounds.width, cellBounds.height);

GC gc = new GC(image);
IConfigRegistry configRegistry = natTable.getConfigRegistry();
ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
if (cellPainter != null) {
cellPainter.paintCell(cell, gc, new Rectangle(0, 0, cellBounds.width, cellBounds.height), configRegistry);
try {
IConfigRegistry configRegistry = natTable.getConfigRegistry();
ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
if (cellPainter != null) {
cellPainter.paintCell(cell, gc, new Rectangle(0, 0, cellBounds.width, cellBounds.height), configRegistry);
}

ImageData imageData = image.getImageData();
imageData.alpha = 150;

this.cellImage = new Image(natTable.getDisplay(), imageData);
} finally {
gc.dispose();
image.dispose();
}
gc.dispose();

ImageData imageData = image.getImageData();
image.dispose();
imageData.alpha = 150;

this.cellImage = new Image(natTable.getDisplay(), imageData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,37 +86,42 @@ private void setColumnImage(NatTable natTable) {
Image image = null;
GC gc = null;

for (int rowPosition = 0; rowPosition < natTable.getRowCount(); rowPosition++) {
cell = natTable.getCellByPosition(columnPosition, rowPosition);

if (cell != null) {
Rectangle cellBounds = cell.getBounds();
this.xOffset = this.currentEvent.x - cellBounds.x;

if (image == null && gc == null) {
image = new Image(natTable.getDisplay(), cellBounds.width, natTable.getHeight());
gc = new GC(image);
}

ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
if (cellPainter != null) {
cellPainter.paintCell(cell, gc, new Rectangle(0, y, cellBounds.width, cellBounds.height), configRegistry);
y += cellBounds.height;
try {
for (int rowPosition = 0; rowPosition < natTable.getRowCount(); rowPosition++) {
cell = natTable.getCellByPosition(columnPosition, rowPosition);

if (cell != null) {
Rectangle cellBounds = cell.getBounds();
this.xOffset = this.currentEvent.x - cellBounds.x;

if (image == null && gc == null) {
image = new Image(natTable.getDisplay(), cellBounds.width, natTable.getHeight());
gc = new GC(image);
}

ICellPainter cellPainter = cell.getLayer().getCellPainter(columnPosition, rowPosition, cell, configRegistry);
if (cellPainter != null) {
cellPainter.paintCell(cell, gc, new Rectangle(0, y, cellBounds.width, cellBounds.height), configRegistry);
y += cellBounds.height;
}
}
}
}

if (gc != null) {
gc.dispose();
}
if (image != null) {
ImageData imageData = image.getImageData();
imageData.alpha = 150;

if (image != null) {
this.columnImage = new Image(natTable.getDisplay(), imageData);
}

ImageData imageData = image.getImageData();
image.dispose();
imageData.alpha = 150;
} finally {
if (gc != null) {
gc.dispose();
}

this.columnImage = new Image(natTable.getDisplay(), imageData);
if (image != null) {
image.dispose();
}
}
}

Expand Down
Loading