diff options
| author | Simon Rettberg | 2014-09-13 16:09:57 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2014-09-13 16:09:57 +0200 |
| commit | e08f99bc0b771bea31089c70f2ac4ed5a37dc4c5 (patch) | |
| tree | 31f18f52f733c83b5d7d5f3bcf13544afd33ffb3 /dozentenmodul/src/main/java/util | |
| parent | [client] Reworked performLogin(): Better error handling, removed dead code, g... (diff) | |
| download | tutor-module-e08f99bc0b771bea31089c70f2ac4ed5a37dc4c5.tar.gz tutor-module-e08f99bc0b771bea31089c70f2ac4ed5a37dc4c5.tar.xz tutor-module-e08f99bc0b771bea31089c70f2ac4ed5a37dc4c5.zip | |
[client] Add resource loader that will take care of error handling
Diffstat (limited to 'dozentenmodul/src/main/java/util')
| -rw-r--r-- | dozentenmodul/src/main/java/util/ResourceLoader.java | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/dozentenmodul/src/main/java/util/ResourceLoader.java b/dozentenmodul/src/main/java/util/ResourceLoader.java new file mode 100644 index 00000000..06200e5a --- /dev/null +++ b/dozentenmodul/src/main/java/util/ResourceLoader.java @@ -0,0 +1,95 @@ +package 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; + +/** + * 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 { + + /** + * 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) { + System.out.println("Resource not found: " + path); + } else { + try { + return new ImageIcon(url, description); + } catch (Exception e) { + System.out.println("Resource not loadable: " + path); + } + } + // If we reach here loading failed, create image containing error + // message + try { + return errorIcon("Invalid Resource: " + path); + } catch (Throwable t) { + 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 ImageIcon errorIcon(String errorText) { + Font font = new Font("Tahoma", Font.PLAIN, 20); + + // get dimensions of text + 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"); + } + +} |
