From 0f454ff17ad356fa585e9730e3d7e57774426087 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Tue, 26 Jun 2018 10:27:28 +0200 Subject: [client] Move ResourceLoader back to dozmod client --- dozentenmodul/pom.xml | 6 + .../src/main/java/org/openslx/dozmod/gui/Gui.java | 2 +- .../gui/window/layout/LoginWindowLayout.java | 2 +- .../gui/window/layout/MainMenuWindowLayout.java | 2 +- .../java/org/openslx/dozmod/gui/wizard/Wizard.java | 2 +- .../org/openslx/dozmod/util/ResourceLoader.java | 176 +++ .../java/org/openslx/dozmod/util/VmWrapper.java | 1 - .../src/main/resources/xml/VirtualBox-settings.xsd | 1489 -------------------- 8 files changed, 186 insertions(+), 1494 deletions(-) create mode 100644 dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java delete mode 100644 dozentenmodul/src/main/resources/xml/VirtualBox-settings.xsd diff --git a/dozentenmodul/pom.xml b/dozentenmodul/pom.xml index edd8efbd..1b7c6154 100755 --- a/dozentenmodul/pom.xml +++ b/dozentenmodul/pom.xml @@ -213,6 +213,12 @@ org.apache.httpcomponents httpclient [4.5.3,4.5.99] + + + commons-io + commons-io + [2.0,] + compile diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java index 6d17022d..b61f961d 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/Gui.java @@ -33,8 +33,8 @@ import org.apache.log4j.Logger; import org.openslx.dozmod.Config; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.util.DesktopEnvironment; +import org.openslx.dozmod.util.ResourceLoader; import org.openslx.util.QuickTimer; -import org.openslx.util.ResourceLoader; public class Gui { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java index b7d6a258..6705ac20 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java @@ -24,7 +24,7 @@ import org.openslx.dozmod.gui.control.ComboBox; import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer; import org.openslx.dozmod.gui.control.QLabel; import org.openslx.dozmod.gui.helper.GridManager; -import org.openslx.util.ResourceLoader; +import org.openslx.dozmod.util.ResourceLoader; @SuppressWarnings("serial") public abstract class LoginWindowLayout extends JDialog { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java index 215cf536..045ec5eb 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/MainMenuWindowLayout.java @@ -6,7 +6,7 @@ import javax.swing.JLabel; import org.openslx.dozmod.gui.helper.CompositePage; import org.openslx.dozmod.gui.helper.GridManager; -import org.openslx.util.ResourceLoader; +import org.openslx.dozmod.util.ResourceLoader; @SuppressWarnings("serial") public abstract class MainMenuWindowLayout extends CompositePage { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java index e2c8367e..6a5afa3f 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java @@ -25,7 +25,7 @@ import javax.swing.SwingConstants; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.control.QLabel; import org.openslx.dozmod.gui.helper.GridManager; -import org.openslx.util.ResourceLoader; +import org.openslx.dozmod.util.ResourceLoader; @SuppressWarnings("serial") public abstract class Wizard extends JDialog { diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java new file mode 100644 index 00000000..5b43c30c --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -0,0 +1,176 @@ +package org.openslx.dozmod.util; + +import java.awt.Color; +import java.awt.Component; +import java.awt.Font; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.RenderingHints; +import java.awt.font.FontRenderContext; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.io.InputStream; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import javax.swing.Icon; +import javax.swing.ImageIcon; + +import org.apache.commons.io.IOUtils; +import org.apache.log4j.Logger; + +/** + * 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 +{ + + /** + * Logger for this class + */ + private final static Logger LOGGER = Logger.getLogger( ResourceLoader.class ); + + /** + * 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 ImageIcon( url, description ); + } catch ( Exception e ) { + LOGGER.error( "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(); + } + } + + public static Icon getIcon( String path, String description, int maxHeight, Component context ) + { + ImageIcon icon = getIcon( path, description ); + return getScaledIcon( icon, maxHeight, context ); + } + + /** + * 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" ); + } + + /** + * Tries to load the given resource treating it as a text file + * + * @param path Resource path to load + * @return content of the loaded resource as String + */ + public static String getTextFile( String path ) + { + String fileContent = null; + try ( InputStream stream = ResourceLoader.class.getResourceAsStream( path ) ) { + fileContent = IOUtils.toString( stream, StandardCharsets.UTF_8 ); + } catch ( Exception e ) { + LOGGER.error( "IO error while trying to load resource '" + path + "'. See trace: ", e ); + } + + if ( fileContent != null ) { + return fileContent; + } else { + return "Resource '" + path + "' not found."; + } + } + + public static InputStream getStream( String path ) + { + return ResourceLoader.class.getResourceAsStream( path ); + } + + private static final Map iconCache = new HashMap<>(); + + public static Icon getScaledIcon( Icon icon, int height, Component context ) + { + if ( icon == null ) + return null; + ImageIcon cached = iconCache.get( icon ); + if ( cached != null && cached.getIconHeight() == height ) + return cached; + // Generate? + float iHeight = icon.getIconHeight(); + float tHeight = height; + if ( iHeight <= tHeight ) + return icon; // Small enough + // Scale down: + BufferedImage image = new BufferedImage( icon.getIconWidth(), icon.getIconHeight(), + BufferedImage.TYPE_INT_ARGB ); + Graphics2D g = image.createGraphics(); + g.setRenderingHint( RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY ); + g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON ); + icon.paintIcon( context, g, 0, 0 ); + ImageIcon scaledIcon = new ImageIcon( image.getScaledInstance( + (int) ( icon.getIconWidth() * ( tHeight / iHeight ) ), (int) ( tHeight ), Image.SCALE_SMOOTH ) ); + iconCache.put( icon, scaledIcon ); + return scaledIcon; + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java index 1702c993..e58653e7 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/VmWrapper.java @@ -24,7 +24,6 @@ import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.thrift.cache.MetaDataCache; import org.openslx.thrifthelper.TConst; -import org.openslx.util.ResourceLoader; import org.openslx.util.vm.DiskImage; import org.openslx.util.vm.QemuMetaData; import org.openslx.util.vm.VboxMetaData; diff --git a/dozentenmodul/src/main/resources/xml/VirtualBox-settings.xsd b/dozentenmodul/src/main/resources/xml/VirtualBox-settings.xsd deleted file mode 100644 index aa38646f..00000000 --- a/dozentenmodul/src/main/resources/xml/VirtualBox-settings.xsd +++ /dev/null @@ -1,1489 +0,0 @@ - - - - - - - - Oracle VM VirtualBox Settings Schema (common definitions). - Copyright (c) 2004-2017 Oracle Corporation - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- cgit v1.2.3-55-g7522