diff options
Diffstat (limited to 'dozentenmodul/src/main/java/util')
| -rw-r--r-- | dozentenmodul/src/main/java/util/ResourceLoader.java | 111 | ||||
| -rw-r--r-- | dozentenmodul/src/main/java/util/ShibbolethECP.java | 5 |
2 files changed, 46 insertions, 70 deletions
diff --git a/dozentenmodul/src/main/java/util/ResourceLoader.java b/dozentenmodul/src/main/java/util/ResourceLoader.java index 731cedeb..8ad23601 100644 --- a/dozentenmodul/src/main/java/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/util/ResourceLoader.java @@ -1,9 +1,9 @@ package util; -import java.awt.Color; -import java.awt.Font; +import gui.GuiManager; + import java.awt.Graphics2D; -import java.awt.font.FontRenderContext; +import java.awt.SystemColor; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.net.URL; @@ -12,11 +12,20 @@ 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.Color; +import org.eclipse.swt.graphics.Font; +import org.eclipse.swt.graphics.FontMetrics; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.widgets.Text; /** - * 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,91 +34,57 @@ public class ResourceLoader { */ 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) { + public static ImageData getImageData(String path) { + + ImageData _imgData = null; + URL _url = ResourceLoader.class.getResource(path); + if (_url == null) { LOGGER.error("Resource not found: " + path); + return errorIcon("nope").getImageData(); } else { try { - return new ImageIcon(url, description); + _imgData = new ImageData(_url.openStream()); } catch (Exception e) { - LOGGER.error("Resource not loadable: " + path); + LOGGER.error("Resource not loadable: " + path + ". See trace: ", e); + return errorIcon("nope").getImageData(); } - } - // If we reach here loading failed, create image containing error - // message - try { - return errorIcon("Invalid Resource: " + path); - } catch (Throwable t) { - return new ImageIcon(); + return _imgData; } } - - /** - * 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); - + private static Image errorIcon(String errorText) { + GC gc = new GC(GuiManager.getDisplay()); + Font font = new Font(GuiManager.getDisplay(), "Tahoma", 20, SWT.NORMAL); + gc.setFont(font); // 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"); + Image image = new Image(GuiManager.getDisplay(), gc.stringExtent(errorText).x, gc.stringExtent(errorText).y); + GC gc2 = new GC(image); + gc2.setBackground(GuiManager.getDisplay().getSystemColor(SWT.COLOR_CYAN)); + gc2.drawText(errorText, 0, 0); + gc2.dispose(); + return image; } /** * 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) { String fileContent = null; try { - fileContent = IOUtils.toString(ResourceLoader.class.getResourceAsStream(path)); + fileContent = IOUtils.toString(ResourceLoader.class + .getResourceAsStream(path)); } catch (Exception e) { - LOGGER.error("IO error while trying to load resource '" + path + "'. See trace: ", e); + LOGGER.error("IO error while trying to load resource '" + path + + "'. See trace: ", e); } if (fileContent != null) { diff --git a/dozentenmodul/src/main/java/util/ShibbolethECP.java b/dozentenmodul/src/main/java/util/ShibbolethECP.java index a3e13a38..f3e35400 100644 --- a/dozentenmodul/src/main/java/util/ShibbolethECP.java +++ b/dozentenmodul/src/main/java/util/ShibbolethECP.java @@ -98,8 +98,9 @@ public class ShibbolethECP { * Password as String. * @return * true if login worked, false otherwise. + * @throws ECPAuthenticationException */ - public static ReturnCode doLogin(final String idpUrl, final String user, final String pass) { + public static ReturnCode doLogin(final String idpUrl, final String user, final String pass) throws ECPAuthenticationException { // first lets do some sanity checks if (BWLP_SP == null) { @@ -131,7 +132,7 @@ public class ShibbolethECP { auth.authenticate(); } catch (ECPAuthenticationException e) { LOGGER.error("ECP Authentication Exception, see trace: ", e); - return ReturnCode.ERROR_IDP; + throw e; } // here test again for the SPURL HttpGet testSp = new HttpGet(BWLP_SP); |
