summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
diff options
context:
space:
mode:
authorSimon Rettberg2015-07-10 14:58:08 +0200
committerSimon Rettberg2015-07-10 14:58:08 +0200
commitb1298643908e80a5bd4763d1e2b13aa9992d5048 (patch)
tree43066b3cf826db0a19eed9a5112dd7072866b7f4 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java
parent[client] close popups a bit safer (diff)
downloadtutor-module-b1298643908e80a5bd4763d1e2b13aa9992d5048.tar.gz
tutor-module-b1298643908e80a5bd4763d1e2b13aa9992d5048.tar.xz
tutor-module-b1298643908e80a5bd4763d1e2b13aa9992d5048.zip
[client] More refactoring, introduce a Gui helper class (again)
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/helper/Gui.java121
1 files changed, 121 insertions, 0 deletions
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);
+ }
+
+}