Dialog positioning

New functions to better positions dialogs on screen
This commit is contained in:
Douglas Gillespie 2022-08-04 11:35:06 +01:00
parent c9f2ab3e97
commit 016cfd0da5
2 changed files with 52 additions and 1 deletions

View File

@ -40,6 +40,7 @@ import PamModel.SMRUEnable;
import PamView.CancelObserver; import PamView.CancelObserver;
import PamView.ClipboardCopier; import PamView.ClipboardCopier;
import PamView.PamColors; import PamView.PamColors;
import PamView.ScreenSize;
import PamView.help.PamHelp; import PamView.help.PamHelp;
import gpl.GPLParameters; import gpl.GPLParameters;
@ -151,6 +152,56 @@ abstract public class PamDialog extends JDialog {
return null; 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) { protected void positionInFrame(Window parentFrame) {
if (parentFrame == null) { if (parentFrame == null) {

View File

@ -45,7 +45,7 @@ public class AnnotationDialog extends PamDialog {
AnnotationDialog annotationDialog = new AnnotationDialog(parentFrame, dataAnnotationType, dataUnit); AnnotationDialog annotationDialog = new AnnotationDialog(parentFrame, dataAnnotationType, dataUnit);
if (positionInFrame != null) { if (positionInFrame != null) {
annotationDialog.setLocation(positionInFrame); annotationDialog.setCentreLocation(positionInFrame);
} }
annotationDialog.setParams(); annotationDialog.setParams();
annotationDialog.setVisible(true); annotationDialog.setVisible(true);