package bluej.utility.javafx;

import bluej.Config;

import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.stage.Modality;
import javafx.stage.Window;
import threadchecker.OnThread;
import threadchecker.Tag;


| An FX dialog containing all repeated to code needed when creating | general dialogs. | | @author Amjad Altadmri | @OnThread(value = Tag.FXPlatform, ignoreParent = true) public class FXCustomizedDialog<R> extends Dialog<R>{ protected FXCustomizedDialog(Window owner, String title, String style) { initOwner(owner); initModality(Modality.WINDOW_MODAL); setTitle(Config.getString(title)); setResizable(true); setDialogPane(new DialogPane() { @Override @OnThread(value = Tag.FXPlatform, ignoreParent = true) protected Node createButtonBar() { return wrapButtonBar(super.createButtonBar()); } }); JavaFXUtil.addStyleClass(this.getDialogPane(), style); Config.addDialogStylesheets(getDialogPane()); } protected Node wrapButtonBar(Node original) { return original; } protected void setModal(boolean makeModal) { initModality(makeModal ? Modality.APPLICATION_MODAL : Modality.NONE); } protected void setContentPane(Node content) { getDialogPane().setContent(content); } public Window asWindow() { Scene scene = getDialogPane().getScene(); if (scene == null) return null; return scene.getWindow(); }
| For a dialog that has yet to be made visible, position it centred over the given window. | (The positioning takes effect once the dialog becomes visible. This method is designed | to only be called on not visible windows, and only once before the window is made visible). | public void setLocationRelativeTo(Window window) { JavaFXUtil.addSelfRemovingListener(widthProperty(), (newval) -> { double xpos = window.getX() + (window.getWidth() - newval.doubleValue()) / 2; setX(xpos); }); JavaFXUtil.addSelfRemovingListener(heightProperty(), (newval) -> { double xpos = window.getY() + (window.getHeight() - newval.doubleValue()) / 2; setY(xpos); }); } protected void dialogThenHide(FXPlatformRunnable action) { action.run(); hide(); } }

.   FXCustomizedDialog
.   createButtonBar
.   wrapButtonBar
.   setModal
.   setContentPane
.   asWindow
.   setLocationRelativeTo
.   dialogThenHide




90 neLoCode + 6 LoComm