From 8eaed77604b2c0c3490beea36ac86d2f915def0b Mon Sep 17 00:00:00 2001 From: Lars Vogel Date: Fri, 13 Oct 2023 11:41:59 +0200 Subject: [PATCH] Update of the image loader example code --- com.vogella.service.imageloader/.project | 17 +++++++++++++++++ .../services/IBundleResourceLoader.java | 7 ++++++- .../internal/BundleResourceLoaderImpl.java | 15 +++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/com.vogella.service.imageloader/.project b/com.vogella.service.imageloader/.project index 472e8d19..b554de3a 100644 --- a/com.vogella.service.imageloader/.project +++ b/com.vogella.service.imageloader/.project @@ -25,9 +25,26 @@ + + org.eclipse.m2e.core.maven2Builder + + + + org.eclipse.m2e.core.maven2Nature org.eclipse.pde.PluginNature org.eclipse.jdt.core.javanature + + + 1697119637238 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/com.vogella.service.imageloader/src/com/vogella/imageloader/services/IBundleResourceLoader.java b/com.vogella.service.imageloader/src/com/vogella/imageloader/services/IBundleResourceLoader.java index 3305c132..e808ffa8 100644 --- a/com.vogella.service.imageloader/src/com/vogella/imageloader/services/IBundleResourceLoader.java +++ b/com.vogella.service.imageloader/src/com/vogella/imageloader/services/IBundleResourceLoader.java @@ -1,13 +1,18 @@ package com.vogella.imageloader.services; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; public interface IBundleResourceLoader { - // service does NOT recycle the provided image + // service does NOT dispose the provided image // the consumer of the image is responsible for calling the dispose() // method on the created Image object once the Image is not required anymore // can be automated with the usage of LocalResourceManager public ImageDescriptor getImageDescriptor(Class clazz, String path); + + // service does NOT dispose the provided image nor the created + // caller needs to dispose them + public Image resize(Image image, int width, int height); } \ No newline at end of file diff --git a/com.vogella.service.imageloader/src/com/vogella/imageloader/services/internal/BundleResourceLoaderImpl.java b/com.vogella.service.imageloader/src/com/vogella/imageloader/services/internal/BundleResourceLoaderImpl.java index 0db592e9..422ef6f8 100644 --- a/com.vogella.service.imageloader/src/com/vogella/imageloader/services/internal/BundleResourceLoaderImpl.java +++ b/com.vogella.service.imageloader/src/com/vogella/imageloader/services/internal/BundleResourceLoaderImpl.java @@ -5,6 +5,10 @@ import org.eclipse.core.runtime.FileLocator; import org.eclipse.core.runtime.Path; import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Display; import org.osgi.framework.Bundle; import org.osgi.framework.FrameworkUtil; import org.osgi.service.component.annotations.Component; @@ -22,4 +26,15 @@ public ImageDescriptor getImageDescriptor(Class clazz, String path) { return ImageDescriptor.createFromURL(url); } + @Override + public Image resize(Image image, int width, int height) { + Image scaled = new Image(Display.getDefault(), width, height); + GC gc = new GC(scaled); + gc.setAntialias(SWT.ON); + gc.setInterpolation(SWT.HIGH); + gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height); + gc.dispose(); +// image.dispose(); // caller need to do this + return scaled; + } } \ No newline at end of file