From 016cfd0da54021dcbe787f0a20856604b81bf7af Mon Sep 17 00:00:00 2001 From: Douglas Gillespie <50671166+douggillespie@users.noreply.github.com> Date: Thu, 4 Aug 2022 11:35:06 +0100 Subject: [PATCH] Dialog positioning New functions to better positions dialogs on screen --- src/PamView/dialog/PamDialog.java | 51 ++++++++++++++++++++++++++++ src/annotation/AnnotationDialog.java | 2 +- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/PamView/dialog/PamDialog.java b/src/PamView/dialog/PamDialog.java index ad20c635..80d7a459 100644 --- a/src/PamView/dialog/PamDialog.java +++ b/src/PamView/dialog/PamDialog.java @@ -40,6 +40,7 @@ import PamModel.SMRUEnable; import PamView.CancelObserver; import PamView.ClipboardCopier; import PamView.PamColors; +import PamView.ScreenSize; import PamView.help.PamHelp; import gpl.GPLParameters; @@ -151,6 +152,56 @@ abstract public class PamDialog extends JDialog { return null; } } + + /** + * Try to set the central location of the dialog at point + * but also check entire dialog is on screen. + * @param point + */ + protected void setCentreLocation(Point point) { + if (point == null) { + return; + } + Rectangle dialogBounds = this.getBounds(); + if (dialogBounds == null) { + return; + } + point.x -= dialogBounds.width/2; + point.y -= dialogBounds.height/2; + setCloseLocation(point); + } + + /** + * Set a location as close as possible to the given point, but + * ensure that the dialog stays in it's parent frame. + * If there isn't a parent frame, make sure it's at least on + * the screen. + * @param point + */ + protected void setCloseLocation(Point point) { + if (point == null) { + return; + } + try { + Rectangle frameRect = ScreenSize.getScreenBounds(); + if (getOwner() != null) { + frameRect = getOwner().getBounds(); + } + Rectangle dialogBounds = this.getBounds(); + /* + * Check max first, then min in case dialog is too big for screen. Ensures + * top left will always be visible. + */ + point.x = Math.min(point.x, frameRect.x+frameRect.width-dialogBounds.width); + point.x = Math.max(point.x, frameRect.x); + point.y = Math.min(point.y, frameRect.y+frameRect.height-dialogBounds.height); + point.y = Math.max(point.y, frameRect.y); + super.setLocation(point); + } + catch (Exception e) { + + } + } protected void positionInFrame(Window parentFrame) { if (parentFrame == null) { diff --git a/src/annotation/AnnotationDialog.java b/src/annotation/AnnotationDialog.java index 100946a8..6c855d27 100644 --- a/src/annotation/AnnotationDialog.java +++ b/src/annotation/AnnotationDialog.java @@ -45,7 +45,7 @@ public class AnnotationDialog extends PamDialog { AnnotationDialog annotationDialog = new AnnotationDialog(parentFrame, dataAnnotationType, dataUnit); if (positionInFrame != null) { - annotationDialog.setLocation(positionInFrame); + annotationDialog.setCentreLocation(positionInFrame); } annotationDialog.setParams(); annotationDialog.setVisible(true);