diff options
author | Simon Rettberg | 2015-07-15 12:36:39 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-07-15 12:36:39 +0200 |
commit | f252e675d84ec171f0d4e1267db2776d4705debc (patch) | |
tree | ec20bf0a9afba55bdf088c5100e6742f1380f575 | |
parent | [client] Clean up SWT resources when exiting (diff) | |
download | tutor-module-f252e675d84ec171f0d4e1267db2776d4705debc.tar.gz tutor-module-f252e675d84ec171f0d4e1267db2776d4705debc.tar.xz tutor-module-f252e675d84ec171f0d4e1267db2776d4705debc.zip |
[client] Fix getImage from ResourceLoader; use it in LoginWindow
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/LoginWindowLayout.java | 38 | ||||
-rw-r--r-- | dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java | 37 |
2 files changed, 44 insertions, 31 deletions
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 544e84bb..cc6f57b9 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 @@ -1,8 +1,10 @@ package org.openslx.dozmod.gui.window.layout; +import org.apache.log4j.Logger; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; @@ -13,9 +15,12 @@ import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; import org.openslx.dozmod.gui.Gui; +import org.openslx.dozmod.util.ResourceLoader; public abstract class LoginWindowLayout extends Composite { + private static final Logger LOGGER = Logger.getLogger(LoginWindowLayout.class); + // TODO This has nothing to to with the layout protected static enum LOGIN_TYPE { ECP(0), @@ -32,8 +37,6 @@ public abstract class LoginWindowLayout extends Composite { // authentication method to use for login attempts protected LOGIN_TYPE loginType = null; - private Image titleImage; - // textfields for the username/password protected Text usernameText; protected Text passwordText; @@ -66,11 +69,8 @@ public abstract class LoginWindowLayout extends Composite { GridLayout gridLayout = new GridLayout(2, true); this.setLayout(gridLayout); - // load the needed Picture - loadImage(); - Label titlePicture = new Label(this, SWT.NONE); - titlePicture.setImage(titleImage); + titlePicture.setImage(loadImage()); GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true); gridData.horizontalSpan = 2; gridData.horizontalAlignment = SWT.CENTER; @@ -135,19 +135,23 @@ public abstract class LoginWindowLayout extends Composite { } - private void loadImage() { + private Image loadImage() { try { - // TODO use the ResourceLoader class to load the logo - // this way, we can be sure to get an image - // (since the ResourceLoader always returns an image, - // even if it cannot load the specified one). - titleImage = new Image(Gui.display, getClass().getResourceAsStream("/img/Logo_bwLehrpool.png")); - ImageData imgData = titleImage.getImageData(); - imgData = imgData.scaledTo(imgData.width / 5, imgData.height / 5); - titleImage = new Image(Gui.display, imgData); + ImageData image = ResourceLoader.getImage("/img/Logo_bwLehrpool.png"); + Rectangle screenSize = Gui.getMonitorFromRectangle(getShell().getBounds(), true).getClientArea(); + float scaleX = (float)screenSize.width / (float)image.width; + float scaleY = (float)screenSize.height / (float)image.height; + final float scaling; + if (scaleX < scaleY) { + scaling = scaleX / 2; + } else { + scaling = scaleY / 2; + } + image = image.scaledTo((int)(image.width * scaling), (int)(image.height * scaling)); + return new Image(Gui.display, image); } catch (Exception e) { - System.out.println("Cannot load image"); - System.out.println(e.getMessage()); + LOGGER.warn("Cannot load image", e); } + return null; } } 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 ca48e55b..ebed9f8e 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -9,6 +9,8 @@ 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; /** @@ -23,21 +25,24 @@ public class ResourceLoader { */ private final static Logger LOGGER = Logger.getLogger(ResourceLoader.class); - public static ImageData getImageData(String path) { - - ImageData imgData = null; + public static ImageData getImage(String path) { URL url = ResourceLoader.class.getResource(path); if (url == null) { LOGGER.error("Resource not found: " + path); - return errorIcon("nope").getImageData(); } else { try { - imgData = new ImageData(url.openStream()); + return new ImageData(url.openStream()); } catch (Exception e) { - LOGGER.error("Resource not loadable: " + path + ". See trace: ", e); - return errorIcon("nope").getImageData(); + LOGGER.error("Resource not loadable: " + path, e); } - return imgData; + } + // If we reach here loading failed, create image containing error message + try { + return errorIcon("Invalid Resource: " + path).getImageData(); + } 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)); } } @@ -49,15 +54,19 @@ public class ResourceLoader { */ private static Image errorIcon(String errorText) { GC gc = new GC(Gui.display); - Font font = new Font(Gui.display, "Tahoma", 20, SWT.NORMAL); + Font font = new Font(Gui.display, "Tahoma", 20, SWT.BOLD); gc.setFont(font); + Point size = gc.stringExtent(errorText); + gc.dispose(); // get dimensions of text - Image image = new Image(Gui.display, gc.stringExtent(errorText).x, gc.stringExtent(errorText).y); - GC gc2 = new GC(image); - gc2.setBackground(Gui.display.getSystemColor(SWT.COLOR_CYAN)); - gc2.drawText(errorText, 0, 0); - gc2.dispose(); + 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; } |