diff options
| author | Jonathan Bauer | 2015-07-27 18:11:58 +0200 |
|---|---|---|
| committer | Jonathan Bauer | 2015-07-27 18:11:58 +0200 |
| commit | 7968181f7efd6c1a149cb50ed9b7de668a7c438e (patch) | |
| tree | df6719b14d128ac0fb646d462ee3e6569e911f1b /dozentenmodul/src/main/java/org | |
| parent | [client] Moar Swing (diff) | |
| download | tutor-module-7968181f7efd6c1a149cb50ed9b7de668a7c438e.tar.gz tutor-module-7968181f7efd6c1a149cb50ed9b7de668a7c438e.tar.xz tutor-module-7968181f7efd6c1a149cb50ed9b7de668a7c438e.zip | |
[client] ResourceLoader back to swing
Diffstat (limited to 'dozentenmodul/src/main/java/org')
| -rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java | 105 |
1 files changed, 66 insertions, 39 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java index ebed9f8e..f22f5282 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -1,22 +1,22 @@ package org.openslx.dozmod.util; +import java.awt.Color; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.font.FontRenderContext; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; import java.net.URL; +import javax.swing.ImageIcon; + import org.apache.commons.io.IOUtils; import org.apache.log4j.Logger; -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Font; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.eclipse.swt.graphics.PaletteData; -import org.eclipse.swt.graphics.Point; -import org.openslx.dozmod.gui.Gui; /** - * Helper class for loading resources. This should be error safe loaders with a - * fall back in case the requested resource can't be found, or isn't of the - * expected type. + * Helper class for loading resources. + * This should be error safe loaders with a fall back in case the + * requested resource can't be found, or isn't of the expected type. */ public class ResourceLoader { @@ -25,56 +25,83 @@ public class ResourceLoader { */ private final static Logger LOGGER = Logger.getLogger(ResourceLoader.class); - public static ImageData getImage(String path) { + /** + * Load the given resource as an ImageIcon. + * This is guaranteed to never throw an Exception and always return + * an ImageIcon. If the requested resource could not be loaded, + * an icon is generated, containing an error message. If even that + * fails, an empty icon is returned. + * @param path Resource path to load + * @param description Icon description + * @return ImageIcon instance + */ + public static ImageIcon getIcon(String path, String description) { URL url = ResourceLoader.class.getResource(path); if (url == null) { LOGGER.error("Resource not found: " + path); } else { try { - return new ImageData(url.openStream()); + return new ImageIcon(url, description); } catch (Exception e) { - LOGGER.error("Resource not loadable: " + path, e); + LOGGER.error("Resource not loadable: " + path); } } - // If we reach here loading failed, create image containing error message + // If we reach here loading failed, create image containing error + // message try { - return errorIcon("Invalid Resource: " + path).getImageData(); + return errorIcon("Invalid Resource: " + path); } catch (Throwable t) { - // Empty image is best we can do - LOGGER.error("Could not create error image", t); - return new ImageData(10, 10, 24, new PaletteData(0xFF, 0xFF00, 0xFF0000)); + return new ImageIcon(); } } + + /** + * Load the given resource as an ImageIcon. + * This is guaranteed to never throw an Exception and always return + * an ImageIcon. If the requested resource could not be loaded, + * an icon is generated, containing an error message. If even that + * fails, an empty icon is returned. + * @param path Resource path to load + * @return ImageIcon instance + */ + public static ImageIcon getIcon(String path) { + return getIcon(path, path); + } /** * Helper that will create an icon with given text. - * * @param errorText Text to render to icon * @return the icon */ - private static Image errorIcon(String errorText) { - GC gc = new GC(Gui.display); - Font font = new Font(Gui.display, "Tahoma", 20, SWT.BOLD); - gc.setFont(font); - Point size = gc.stringExtent(errorText); - gc.dispose(); + private static ImageIcon errorIcon(String errorText) { + Font font = new Font("Tahoma", Font.PLAIN, 20); + // get dimensions of text - Image image = new Image(Gui.display, size.x, size.y); - gc = new GC(image); - gc.setBackground(Gui.display.getSystemColor(SWT.COLOR_RED)); - gc.setForeground(Gui.display.getSystemColor(SWT.COLOR_YELLOW)); - gc.setFont(font); - gc.drawText(errorText, 0, 0); - gc.dispose(); - font.dispose(); - return image; + FontRenderContext frc = new FontRenderContext(null, true, true); + Rectangle2D bounds = font.getStringBounds(errorText, frc); + int w = (int) bounds.getWidth(); + int h = (int) bounds.getHeight(); + + // create a BufferedImage object + BufferedImage image = new BufferedImage(w, h, + BufferedImage.TYPE_INT_RGB); + Graphics2D g = image.createGraphics(); + + // set color and other parameters + g.setColor(Color.WHITE); + g.fillRect(0, 0, w, h); + g.setColor(Color.RED); + g.setFont(font); + + g.drawString(errorText, (float) bounds.getX(), (float) -bounds.getY()); + + g.dispose(); + return new ImageIcon(image, "ERROR"); } /** * Tries to load the given resource treating it as a text file - * - * @param path - * Resource path to load + * @param path Resource path to load * @return content of the loaded resource as String */ public static String getTextFile(String path) { @@ -91,4 +118,4 @@ public class ResourceLoader { return "Resource '" + path + "' not found."; } } -} +}
\ No newline at end of file |
