diff options
Diffstat (limited to 'dozentenmodul/src/main/java/org')
7 files changed, 160 insertions, 50 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java index 72b19293..c8ba577f 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/MainWindow.java @@ -6,18 +6,16 @@ import org.apache.log4j.Logger; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; -import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Menu; import org.eclipse.swt.widgets.MenuItem; import org.eclipse.swt.widgets.MessageBox; -import org.eclipse.swt.widgets.Monitor; import org.eclipse.swt.widgets.Shell; +import org.openslx.dozmod.gui.helper.Gui; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.window.DisclaimerWindow; import org.openslx.dozmod.gui.window.LoginWindow; @@ -33,7 +31,6 @@ public abstract class MainWindow { private static final Shell mainShell; private static Composite contentComposite; - private static final Display display; private static final String THRIFT_CONNECTION_ERROR = "Lost connection to the masterserver. Do you want to retry?"; @@ -44,7 +41,8 @@ public abstract class MainWindow { */ public static void setContent(Composite contentComposite) { - if (contentComposite == null) return; + if (contentComposite == null) + return; if (MainWindow.contentComposite != null) MainWindow.contentComposite.dispose(); @@ -57,7 +55,7 @@ public abstract class MainWindow { /** * @param clazz Class to open as a popup over the main window. - * MUST be a subclass of Composite. + * MUST be a subclass of Composite. */ public static void openPopup(Class<? extends Composite> clazz, boolean modal) { Shell dialogShell = new Shell(mainShell, SWT.DIALOG_TRIM | (modal ? SWT.APPLICATION_MODAL : 0)); @@ -75,20 +73,19 @@ public abstract class MainWindow { dialogShell.layout(); dialogShell.pack(); + Gui.limitShellSize(dialogShell); dialogShell.open(); } /** - * @return The instance of SWT display currently in use + * Initialises the GUI by creating the main window, adding the menu and + * creating the login mask as the first content window. + * Further sets up the global thrift error callback to catch any + * connection errors during the communication with the servers. */ - public static Display getDisplay() { - return display; - } - static { // init SWT stuff - display = new Display(); - mainShell = new Shell(display, SWT.SHELL_TRIM | SWT.CENTER); + mainShell = new Shell(Gui.display, SWT.SHELL_TRIM | SWT.CENTER); // Set up thrift error message displaying ThriftManager.setErrorCallback(new ErrorCallback() { @@ -101,7 +98,7 @@ public abstract class MainWindow { }); // Global key listener - display.addFilter(SWT.KeyDown, new Listener() { + Gui.display.addFilter(SWT.KeyDown, new Listener() { @Override public void handleEvent(Event event) { if (event.character == 17) // Ctrl-Q = Quit @@ -121,14 +118,8 @@ public abstract class MainWindow { setContent(new MainMenuWindow(mainShell)); // center the window on the primary monitor - Monitor primary = display.getPrimaryMonitor(); - Rectangle bounds = primary.getBounds(); - Rectangle rect = mainShell.getBounds(); + Gui.centerShell(mainShell); - int x = bounds.x + (bounds.width - rect.width) / 2; - int y = bounds.y + (bounds.height - rect.height) / 2; - - mainShell.setLocation(x, y); mainShell.open(); // here we can check for Session information @@ -139,15 +130,12 @@ public abstract class MainWindow { } /** - * Initialises the GUI by creating the main window, adding the menu and - * creating the login mask as the first content window. - * Further sets up the global thrift error callback to catch any - * connection errors during the communication with the servers. + * Run the GUI mainloop as long as the main window exists. */ public static void mainloop() { while (!mainShell.isDisposed()) { - if (!display.readAndDispatch()) - display.sleep(); + if (!Gui.display.readAndDispatch()) + Gui.display.sleep(); } } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java new file mode 100644 index 00000000..25e456be --- /dev/null +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java @@ -0,0 +1,121 @@ +package org.openslx.dozmod.gui.helper; + +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Monitor; +import org.eclipse.swt.widgets.Shell; + +public class Gui { + + /** + * The one and only display to use throughout the application + */ + public static final Display display = new Display(); + + /** + * Center the given shell on the {@link Monitor} it is displayed on. + * + * @param shell Some shell + */ + public static void centerShell(Shell shell) { + Monitor activeMonitor = getMonitorFromRectangle(shell.getBounds(), true); + Rectangle bounds = activeMonitor.getClientArea(); + Rectangle rect = shell.getBounds(); + int x = bounds.x + (bounds.width - rect.width) / 2; + int y = bounds.y + (bounds.height - rect.height) / 2; + shell.setLocation(x, y); + } + + /** + * Make sure the given shell fits the {@link Monitor} it is displayed on. + * + * @param shell Some shell + */ + public static void limitShellSize(Shell shell) { + Monitor activeMonitor = getMonitorFromRectangle(shell.getBounds(), true); + Rectangle bounds = activeMonitor.getClientArea(); + Rectangle rect = shell.getBounds(); + boolean changed = false; + if (rect.width + 20 > bounds.width) { + rect.width = bounds.width - 20; + changed = true; + } + if (rect.height + 20 > bounds.height) { + rect.height = bounds.height - 20; + changed = true; + } + if (changed) { + shell.setSize(rect.width, rect.height); + rect = shell.getBounds(); + } + changed = false; + if (rect.x + rect.width >= bounds.x + bounds.width) { + rect.x = 5; + changed = true; + } + if (rect.y + rect.height >= bounds.y + bounds.height) { + rect.y = 5; + changed = true; + } + if (changed) { + shell.setLocation(rect.x, rect.y); + } + } + + /** + * Get the {@link Monitor} which the given {@link Point} lies in. + * + * @param point The point in question + * @param defaultToPrimary if no monitor matches the check, return the + * primary monitor if true, <code>null</code> otherwise + * @return the {@link Monitor} + */ + public static Monitor getMonitorFromPoint(Point point, boolean defaultToPrimary) { + Monitor[] monitors = display.getMonitors(); + for (Monitor monitor : monitors) { + Rectangle bounds = monitor.getBounds(); + if (bounds.contains(point)) + return monitor; + } + if (defaultToPrimary) + return display.getPrimaryMonitor(); + return null; + } + + /** + * Get the {@link Monitor} which most of the given rectangle overlaps. + * + * @param rect The rectangle to check + * @param defaultToPrimary if no monitor matches the check, return the + * primary monitor if true, <code>null</code> otherwise + * @return the {@link Monitor} + */ + public static Monitor getMonitorFromRectangle(Rectangle rect, boolean defaultToPrimary) { + // Make sure rectangle is in bounds. This is not completely accurate + // in case there are multiple monitors that have different resolutions. + Rectangle bounds = display.getBounds(); + if (rect.x + rect.width >= bounds.x + bounds.width) { + rect.width -= (rect.x + rect.width) - (bounds.x + bounds.width); + if (rect.width < 1) + rect.width = 1; + } + if (rect.y + rect.height >= bounds.y + bounds.height) { + rect.height -= (rect.y + rect.height) - (bounds.y + bounds.height); + if (rect.height < 1) + rect.height = 1; + } + if (rect.x < bounds.x) { + rect.width -= bounds.x - rect.x; + rect.x = bounds.x; + } + if (rect.y < bounds.y) { + rect.height -= bounds.y - rect.y; + rect.y = bounds.y; + } + // Now just use the same code as *FromPoint by using the rectangle's center + return getMonitorFromPoint(new Point(rect.x + rect.width / 2, rect.y + rect.height / 2), + defaultToPrimary); + } + +} diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java index d0742616..b9a021e6 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/LoginWindow.java @@ -22,6 +22,7 @@ import org.openslx.dozmod.authentication.ShibbolethEcp; import org.openslx.dozmod.authentication.ShibbolethEcp.ReturnCode; import org.openslx.dozmod.authentication.TestAccountAuthenticator; import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.window.layout.LoginWindowLayout; import org.openslx.dozmod.thrift.OrganizationCache; @@ -68,7 +69,7 @@ public class LoginWindow extends LoginWindowLayout { } // now send the organisations back to the LoginWindow // through populateIdpCombo() - MainWindow.getDisplay().asyncExec(new Runnable() { + Gui.display.asyncExec(new Runnable() { public void run() { populateIdpCombo(orgs); } diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java index a66d47ff..4c66c996 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/ImageListWindowLayout.java @@ -15,7 +15,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Table; import org.eclipse.swt.widgets.Text; -import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; import org.openslx.dozmod.gui.helper.TableHelper; public abstract class ImageListWindowLayout extends Composite { @@ -70,7 +70,7 @@ public abstract class ImageListWindowLayout extends Composite { infoTitle.setText(infoTitleString); // set the fond FontData fontData = infoTitle.getFont().getFontData()[0]; - Font font = new Font(MainWindow.getDisplay(), new FontData(fontData.getName(), fontData + Font font = new Font(Gui.display, new FontData(fontData.getName(), fontData .getHeight(), SWT.BOLD)); infoTitle.setFont(font); // the infotext 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 d52ca940..48ae60d2 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 @@ -12,7 +12,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Text; -import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; public abstract class LoginWindowLayout extends Composite { @@ -157,11 +157,11 @@ public abstract class LoginWindowLayout extends Composite { // 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(MainWindow.getDisplay(), getClass() + 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(MainWindow.getDisplay(), imgData); + titleImage = new Image(Gui.display, imgData); } catch (Exception e) { System.out.println("Cannot load image"); System.out.println(e.getMessage()); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java index 0c4c14c8..37b50817 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/layout/VirtualizerNoticeWindowLayout.java @@ -9,7 +9,7 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; -import org.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; public abstract class VirtualizerNoticeWindowLayout extends Composite { private final String title = "Hinweis VMWare Player"; @@ -37,7 +37,7 @@ public abstract class VirtualizerNoticeWindowLayout extends Composite { Label titleLabel = new Label(this, SWT.NONE); titleLabel.setText(infoTitle); FontData fontData = titleLabel.getFont().getFontData()[0]; - Font font = new Font(MainWindow.getDisplay(), new FontData(fontData.getName(), fontData.getHeight(), + Font font = new Font(Gui.display, new FontData(fontData.getName(), fontData.getHeight(), SWT.BOLD)); titleLabel.setFont(font); // TODO dispose of font? 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 337cdc9d..1fd705c1 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/util/ResourceLoader.java @@ -9,7 +9,7 @@ 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.openslx.dozmod.gui.MainWindow; +import org.openslx.dozmod.gui.helper.Gui; /** * Helper class for loading resources. This should be error safe loaders with a @@ -41,39 +41,39 @@ public class ResourceLoader { } } - /** + /** * 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(MainWindow.getDisplay()); - Font font = new Font(MainWindow.getDisplay(), "Tahoma", 20, SWT.NORMAL); + GC gc = new GC(Gui.display); + Font font = new Font(Gui.display, "Tahoma", 20, SWT.NORMAL); gc.setFont(font); // get dimensions of text - Image image = new Image(MainWindow.getDisplay(), gc.stringExtent(errorText).x, gc.stringExtent(errorText).y); - GC gc2 = new GC(image); - gc2.setBackground(MainWindow.getDisplay().getSystemColor(SWT.COLOR_CYAN)); - gc2.drawText(errorText, 0, 0); - gc2.dispose(); - return image; + 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(); + gc.dispose(); + return image; } /** * Tries to load the given resource treating it as a text file * * @param path - * Resource path to load + * 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) { |