下面列出了java.awt.Desktop.Action#javafx.scene.control.Dialog 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void closeDialog(Stage primaryStage) {
Dialog<ButtonType> dialog = new Dialog<>();
dialog.setTitle("关闭");
dialog.setContentText("确认关闭吗?");
dialog.initOwner(primaryStage);
dialog.getDialogPane().getButtonTypes().add(ButtonType.APPLY);
dialog.getDialogPane().getButtonTypes().add(ButtonType.CLOSE);
dialog.getDialogPane().setPrefSize(350, 100);
Optional<ButtonType> s = dialog.showAndWait();
s.ifPresent(s1 -> {
if (s1.equals(ButtonType.APPLY)) {
primaryStage.close();
} else if (s1.equals(ButtonType.CLOSE)) {
dialog.close();
}
});
}
@Override
public void start(final Stage stage)
{
final Button button = new Button("Open Dialog");
final Scene scene = new Scene(new BorderPane(button), 400, 300);
stage.setScene(scene);
stage.show();
button.setOnAction(event ->
{
final Dialog<String> dialog = new ListPickerDialog(button, List.of("Apple", "Orange"), "Orange");
dialog.setTitle("Open");
dialog.setHeaderText("Select application for opening\nthe item");
System.out.println(dialog.showAndWait());
});
}
public static Dialog<Boolean> createAboutDialog()
{
Dialog<Boolean> dialog = new Dialog<>();
try
{
FXMLLoader loader = new FXMLLoader( ResourceLoader.getResource("/com/mammen/ui/javafx/dialog/about/AboutDialog.fxml") );
dialog.setDialogPane( loader.load() );
dialog.setResultConverter( (ButtonType buttonType) -> buttonType.getButtonData() == ButtonBar.ButtonData.CANCEL_CLOSE );
}
catch( Exception e )
{
e.printStackTrace();
dialog.getDialogPane().getButtonTypes().add( ButtonType.CLOSE );
}
dialog.setTitle( "About" );
return dialog;
}
public static Dialog<Boolean> createSettingsDialog()
{
Dialog<Boolean> dialog = new Dialog<>();
try
{
FXMLLoader loader = new FXMLLoader( ResourceLoader.getResource("/com/mammen/ui/javafx/dialog/settings/SettingsDialog.fxml") );
dialog.setDialogPane( loader.load() );
((Button) dialog.getDialogPane().lookupButton(ButtonType.APPLY)).setDefaultButton(true);
((Button) dialog.getDialogPane().lookupButton(ButtonType.CANCEL)).setDefaultButton(false);
// Some header stuff
dialog.setTitle("Settings");
dialog.setHeaderText("Manage settings");
dialog.setResultConverter( (ButtonType buttonType) -> buttonType.getButtonData() == ButtonBar.ButtonData.APPLY );
}
catch( Exception e )
{
e.printStackTrace();
}
return dialog;
}
private void loadCombinason() {
scene.addEventFilter(KeyEvent.KEY_PRESSED, t -> {
String codeStr = t.getCode().toString();
if(!key.toString().endsWith("_"+codeStr)){
key.append("_").append(codeStr);
}
});
scene.addEventFilter(KeyEvent.KEY_RELEASED, t -> {
if(key.length()>0) {
if("_CONTROL_C_L_E_M".equals(key.toString())){
// Create the custom dialog.
Dialog<Void> dialog = new Dialog<>();
dialog.setTitle(Configuration.getBundle().getString("ui.menu.easteregg"));
dialog.setHeaderText(null);
dialog.setContentText(null);
dialog.setGraphic(new ImageView(this.getClass().getResource("images/goal.gif").toString()));
dialog.getDialogPane().getButtonTypes().addAll(ButtonType.OK);
dialog.showAndWait();
}
key = new StringBuilder();
}
});
}
public static void enableClosing(Alert alert) {
try {
Field dialogField = Dialog.class.getDeclaredField("dialog");
dialogField.setAccessible(true);
Object dialog = dialogField.get(alert);
Field stageField = dialog.getClass().getDeclaredField("stage");
stageField.setAccessible(true);
Stage stage = (Stage) stageField.get(dialog);
stage.setOnCloseRequest(null);
stage.addEventFilter(KeyEvent.KEY_PRESSED, keyEvent -> {
if (keyEvent.getCode() == KeyCode.ESCAPE) {
((Button) alert.getDialogPane().lookupButton(ButtonType.OK)).fire();
}
});
} catch (Exception ex) {
// no point
ex.printStackTrace();
}
}
private void onOK(final ActionEvent ignored)
{
Toggle selectedToggle = toggleGroup.getSelectedToggle();
Logging.debugPrint("selected toggle is " + selectedToggle);
if (selectedToggle != null)
{
Integer whichItemId = (Integer)selectedToggle.getUserData();
InfoFacade selectedItem = chooser.getAvailableList().getElementAt(whichItemId);
chooser.addSelected(selectedItem);
}
if (chooser.isRequireCompleteSelection() && (chooser.getRemainingSelections().get() > 0))
{
Dialog<ButtonType> alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(chooser.getName());
alert.setContentText(LanguageBundle.getFormattedString("in_chooserRequireComplete",
chooser.getRemainingSelections().get()));
alert.showAndWait();
return;
}
chooser.commit();
committed = true;
this.dispose();
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
public void mergePatchesUi() {
List<String> patchNames = new ArrayList<>();
for (int p = 0; p < EWI4000sPatch.EWI_NUM_PATCHES; p++)
patchNames.add( sharedData.ewiPatchList[p].getName() );
Dialog<Integer> mergeDialog = new Dialog<>();
mergeDialog.setTitle( "EWItool - Merge Patches" );
mergeDialog.setHeaderText( "Choose patch to merge with..." );
ListView<String> lv = new ListView<>();
lv.getItems().setAll( patchNames ); // don't need Observable here
mergeDialog.getDialogPane().setContent( lv );
mergeDialog.setResultConverter( button -> {
if (button == ButtonType.OK)
return lv.getSelectionModel().getSelectedIndex();
else
return null;
} );
mergeDialog.getDialogPane().getButtonTypes().addAll( ButtonType.OK, ButtonType.CANCEL );
Optional<Integer> result = mergeDialog.showAndWait();
if (result.isPresent() && result.get() != -1) {
mergeWith( result.get() );
}
}
private void onOK(final ActionEvent ignored)
{
Toggle selectedToggle = toggleGroup.getSelectedToggle();
Logging.debugPrint("selected toggle is " + selectedToggle);
if (selectedToggle != null)
{
Integer whichItemId = (Integer)selectedToggle.getUserData();
InfoFacade selectedItem = chooser.getAvailableList().getElementAt(whichItemId);
chooser.addSelected(selectedItem);
}
if (chooser.isRequireCompleteSelection() && (chooser.getRemainingSelections().get() > 0))
{
Dialog<ButtonType> alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(chooser.getName());
alert.setContentText(LanguageBundle.getFormattedString("in_chooserRequireComplete",
chooser.getRemainingSelections().get()));
alert.showAndWait();
return;
}
chooser.commit();
committed = true;
this.dispose();
}
/**
* View a URI in a browser.
*
* @param uri URI to display in browser.
* @throws IOException if browser can not be launched
*/
private static void viewInBrowser(URI uri) throws IOException
{
if (Desktop.isDesktopSupported() && DESKTOP.isSupported(Action.BROWSE))
{
DESKTOP.browse(uri);
}
else
{
Dialog<ButtonType> alert = GuiUtility.runOnJavaFXThreadNow(() -> new Alert(Alert.AlertType.WARNING));
Logging.debugPrint("unable to browse to " + uri);
alert.setTitle(LanguageBundle.getString("in_err_browser_err"));
alert.setContentText(LanguageBundle.getFormattedString("in_err_browser_uri", uri));
GuiUtility.runOnJavaFXThreadNow(alert::showAndWait);
}
}
@Override
public void displayMessage(String title, String msg) {
Platform.runLater(() -> {
Dialog<ButtonType> dialog = new Dialog<>();
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(mzMineIcon);
dialog.setTitle(title);
dialog.setContentText(msg);
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.showAndWait();
});
}
private static void applyStylesheetTo(Dialog dialog) {
DialogPane dialogPane = dialog.getDialogPane();
dialogPane.getStylesheets().add(
Thread.currentThread()
.getClass()
.getResource(ApplicationConstants.GLOBAL_CSS_FILE_NAME)
.toExternalForm());
}
private void positionDialog(final Dialog<?> dialog)
{
final List<Stage> stages = DockStage.getDockStages();
DialogHelper.positionDialog(dialog, stages.get(0).getScene().getRoot(), -100, -100);
dialog.setResizable(true);
dialog.getDialogPane().setMinSize(280, 160);
}
@FXML
private void openAboutDialog()
{
Dialog<Boolean> aboutDialog = DialogFactory.createAboutDialog();
aboutDialog.showAndWait();
}
@FXML
private void showAddPointDialog()
{
Dialog<Waypoint> waypointDialog = DialogFactory.createWaypointDialog();
Optional<Waypoint> result;
// Wait for the result
result = waypointDialog.showAndWait();
result.ifPresent( (Waypoint w) ->
backend.addPoint( w )
);
}
/**
* 注册拖拽事件
*
* @param dialog
* @param root
*/
public static void registerDragEvent(Dialog dialog, Node root){
// allow the clock background to be used to drag the clock around.
final Delta dragDelta = new Delta();
root.setOnMousePressed(mouseEvent -> {
// record a delta distance for the drag and drop operation.
dragDelta.x = dialog.getX() - mouseEvent.getScreenX();
dragDelta.y = dialog.getY() - mouseEvent.getScreenY();
});
root.setOnMouseDragged(mouseEvent -> {
dialog.setX(mouseEvent.getScreenX() + dragDelta.x);
dialog.setY(mouseEvent.getScreenY() + dragDelta.y);
});
}
/**
* Show dialog
*
* @param selectedText
* @return
*/
private String showDialog(String selectedText) {
Dialog dialog = new Dialog();
dialog.setTitle("Edit Hex");
dialog.setResizable(false);
TextField text1 = new TextField();
text1.addEventFilter(KeyEvent.KEY_TYPED, hex_Validation(2));
text1.setText(selectedText);
StackPane dialogPane = new StackPane();
dialogPane.setPrefSize(150, 50);
dialogPane.getChildren().add(text1);
dialog.getDialogPane().setContent(dialogPane);
ButtonType buttonTypeOk = new ButtonType("Save", ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().add(buttonTypeOk);
dialog.setResultConverter(new Callback<ButtonType, String>() {
@Override
public String call(ButtonType b) {
if (b == buttonTypeOk) {
switch (text1.getText().length()) {
case 0:
return null;
case 1:
return "0".concat(text1.getText());
default:
return text1.getText();
}
}
return null;
}
});
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
return result.get();
}
return null;
}
public static void displayMessage(String msg) {
Platform.runLater(() -> {
Dialog<ButtonType> dialog = new Dialog<>();
Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.getIcons().add(mzMineIcon);
dialog.setTitle("Warning");
dialog.setContentText(msg);
dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);
dialog.showAndWait();
});
}
/**
* Initialized a dialog with the given owner window, and String header and content
* @param dialog dialog box to initialize
* @param window owning window or null if there is none
* @param header dialog header
* @param content dialog content
*/
public static void initializeDialog(Dialog dialog, Window window, String header, String content){
if (window != null)
dialog.initOwner(window);
dialog.setTitle("CPU Sim");
dialog.setHeaderText(header);
dialog.setContentText(content);
// Allow dialogs to resize for Linux
// https://bugs.openjdk.java.net/browse/JDK-8087981
dialog.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
}
@Override
public Dialog gitInformationDialog(String title, String header, String label) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(label);
alert.showAndWait();
return alert;
}
public void applyFadeTransition(Dialog node) {
FadeTransition fadeTransition = new FadeTransition();
fadeTransition.setDuration(Duration.seconds(.5));
fadeTransition.setNode(node.getDialogPane());
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1);
fadeTransition.play();
}
/**
* Decorates a dialog with window icons.
* Note: this may not be required anymore with newer Java versions.
*
* @param dlg the dialog to decorate
*/
public static void decorateDialogWithIcon(Dialog<?> dlg) {
Window window = dlg.getDialogPane().getScene().getWindow();
if (window instanceof Stage) {
Stage stage = (Stage) dlg.getDialogPane().getScene().getWindow();
Collection<Image> icons = IconUtils.createWindowIcons();
stage.getIcons().addAll(icons);
}
}
public Optional<String> showDialog(final String contentText) throws ProjectDirectoryNotSpecified
{
if (this.defaultToTempDirectory) { return Optional.of(tmpDir()); }
final StringProperty projectDirectory = new SimpleStringProperty(null);
final ButtonType specifyProject = new ButtonType("Specify Project", ButtonData.OTHER);
final ButtonType noProject = new ButtonType("No Project", ButtonData.OK_DONE);
final Dialog<String> dialog = new Dialog<>();
dialog.setResultConverter(bt -> {
return ButtonType.CANCEL.equals(bt)
? null
: noProject.equals(bt)
? tmpDir()
: projectDirectory.get();
});
dialog.getDialogPane().getButtonTypes().setAll(specifyProject, noProject, ButtonType.CANCEL);
dialog.setTitle("Paintera");
dialog.setHeaderText("Specify Project Directory");
dialog.setContentText(contentText);
final Node lookupProjectButton = dialog.getDialogPane().lookupButton(specifyProject);
if (lookupProjectButton instanceof Button)
{
((Button) lookupProjectButton).setTooltip(new Tooltip("Look up project directory."));
}
Optional
.ofNullable(dialog.getDialogPane().lookupButton(noProject))
.filter(b -> b instanceof Button)
.map(b -> (Button) b)
.ifPresent(b -> b.setTooltip(new Tooltip("Create temporary project in /tmp.")));
Optional
.ofNullable(dialog.getDialogPane().lookupButton(ButtonType.CANCEL))
.filter(b -> b instanceof Button)
.map(b -> (Button) b)
.ifPresent(b -> b.setTooltip(new Tooltip("Do not start Paintera.")));
lookupProjectButton.addEventFilter(ActionEvent.ACTION, event -> {
final DirectoryChooser chooser = new DirectoryChooser();
final Optional<String> d = Optional.ofNullable(chooser.showDialog(dialog.getDialogPane().getScene()
.getWindow())).map(
File::getAbsolutePath);
if (d.isPresent())
{
projectDirectory.set(d.get());
}
else
{
// consume on cancel, so that parent dialog does not get closed.
event.consume();
}
});
dialog.setResizable(true);
final Optional<String> returnVal = dialog.showAndWait();
if (!returnVal.isPresent()) { throw new ProjectDirectoryNotSpecified(); }
return returnVal;
}
private static void decorateWithCss(Dialog dialog) {
applyStylesheetTo(dialog);
}
public static Optional<Pair<String, String>> ConnDialogResult() {
Dialog<Pair<String, String>> dialog = new Dialog<>();
dialog.setTitle("建立连接");
dialog.setHeaderText("请输入服务器的连接信息");
ButtonType loginButtonType = new ButtonType("连接", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField hostName = new TextField();
hostName.setPromptText("localhost");
hostName.setText("localhost");
TextField port = new TextField();
port.setPromptText("30232");
port.setText("30232");
grid.add(new Label("主机名: "), 0, 0);
grid.add(hostName, 1, 0);
grid.add(new Label("端口号: "), 0, 1);
grid.add(port, 1, 1);
// Enable/Disable login button depending on whether a username was entered.
// Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
// loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
// hostName.textProperty().addListener((observable, oldValue, newValue) -> {
// loginButton.setDisable(newValue.trim().isEmpty());
// });
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(() -> hostName.requestFocus());
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
return new Pair<>(hostName.getText(), port.getText());
}
return null;
});
return dialog.showAndWait();
}
public static Optional<TcpMsgResponseDeviceStatus> ResponseDeviceStatusResult() throws NumberFormatException {
Dialog<TcpMsgResponseDeviceStatus> dialog = new Dialog<>();
dialog.setTitle("发送状态信息");
dialog.setHeaderText("请设置单个设备的状态");
ButtonType loginButtonType = new ButtonType("发送", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField textFieldGroupId = new TextField();
textFieldGroupId.setPromptText("1 - 120");
TextField textFieldDeviceId = new TextField();
textFieldDeviceId.setPromptText("1 - 100");
TextField textFieldStatus = new TextField();
textFieldStatus.setPromptText("1 - 6");
grid.add(new Label("组号: "), 0, 0);
grid.add(textFieldGroupId, 1, 0);
grid.add(new Label("设备号: "), 0, 1);
grid.add(textFieldDeviceId, 1, 1);
grid.addRow(2, new Label("状态码: "));
// grid.add(, 0, 2);
grid.add(textFieldStatus, 1, 2);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
textFieldGroupId.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldDeviceId, textFieldStatus)));
textFieldDeviceId.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldDeviceId, textFieldStatus)));
textFieldStatus.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldDeviceId, textFieldStatus)));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(textFieldGroupId::requestFocus);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
try {
TcpMsgResponseDeviceStatus tcpMsgResponseDeviceStatus = new TcpMsgResponseDeviceStatus(Integer.parseInt(
textFieldGroupId.getText().trim()),
Integer.parseInt(textFieldDeviceId.getText().trim()),
Integer.parseInt(textFieldStatus.getText().trim()));
return tcpMsgResponseDeviceStatus;
} catch (NumberFormatException e) {
System.out.println("空");
return new TcpMsgResponseDeviceStatus(-1, -1, -1);
}
}
return null;
});
return dialog.showAndWait();
}
public static Optional<TcpMsgResponseRandomDeviceStatus> randomDeviceStatus() throws NumberFormatException {
Dialog<TcpMsgResponseRandomDeviceStatus> dialog = new Dialog<>();
dialog.setTitle("随机状态信息");
dialog.setHeaderText("随机设备的状态信息");
ButtonType loginButtonType = new ButtonType("发送", ButtonBar.ButtonData.OK_DONE);
dialog.getDialogPane().getButtonTypes().addAll(loginButtonType, ButtonType.CANCEL);
// Create the username and password labels and fields.
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(20, 150, 10, 10));
TextField textFieldGroupId = new TextField();
textFieldGroupId.setPromptText("1 - 120");
TextField textFieldLength = new TextField();
textFieldLength.setPromptText("1 - 60_0000");
TextField textFieldStatus = new TextField();
textFieldStatus.setPromptText("1 - 6");
grid.add(new Label("组号: "), 0, 0);
grid.add(textFieldGroupId, 1, 0);
grid.add(new Label("范围: "), 0, 1);
grid.add(textFieldLength, 1, 1);
grid.addRow(2, new Label("状态码: "));
// grid.add(, 0, 2);
grid.add(textFieldStatus, 1, 2);
// Enable/Disable login button depending on whether a username was entered.
Node loginButton = dialog.getDialogPane().lookupButton(loginButtonType);
loginButton.setDisable(true);
// Do some validation (using the Java 8 lambda syntax).
textFieldGroupId.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldLength, textFieldStatus)));
textFieldLength.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldLength, textFieldStatus)));
textFieldStatus.textProperty().addListener((observable, oldValue, newValue) -> loginButton.setDisable(fieldisEmpty(textFieldGroupId, textFieldLength, textFieldStatus)));
dialog.getDialogPane().setContent(grid);
// Request focus on the username field by default.
Platform.runLater(textFieldGroupId::requestFocus);
dialog.setResultConverter(dialogButton -> {
if (dialogButton == loginButtonType) {
try {
TcpMsgResponseRandomDeviceStatus tcpMsgResponseDeviceStatus = new TcpMsgResponseRandomDeviceStatus(Integer.parseInt(
textFieldGroupId.getText().trim()),
Integer.parseInt(textFieldStatus.getText().trim()),
Integer.parseInt(textFieldLength.getText().trim()));
return tcpMsgResponseDeviceStatus;
} catch (NumberFormatException e) {
System.out.println("空");
return new TcpMsgResponseRandomDeviceStatus(-1, -1, -1);
}
}
return null;
});
return dialog.showAndWait();
}
/** @param dialog Dialog that should be on top of other windows */
public static void forceToFront(final Dialog<?> dialog)
{
final Stage stage = (Stage) dialog.getDialogPane().getScene().getWindow();
stage.toFront();
}
/** Position dialog relative to another widget
*
* <p>By default, dialogs seem to pop up in the center of the first monitor.
* .. even if the "current" window is on a different monitor.
*
* <p>This helper positions the dialog relative to the center
* of a node.
*
* @param dialog Dialog to position
* @param node Node relative to which dialog should be positioned
* @param x_offset Offset relative to center of the node
* @param y_offset Offset relative to center of the node
*/
public static void positionDialog(final Dialog<?> dialog, final Node node, final int x_offset, final int y_offset)
{
dialog.initOwner(node.getScene().getWindow());
// Must runLater due to dialog Width/Height are initialized after dialog shows up
Platform.runLater(new Runnable() {
@Override
public void run() {
final Bounds pos = node.localToScreen(node.getBoundsInLocal());
if (pos == null)
{
logger.log(Level.WARNING, "Cannot determine dialog position", new NullPointerException());
return;
}
final double nodeX = pos.getMinX();
final double nodeY = pos.getMinY();
double newX = nodeX + pos.getWidth()/2 + x_offset;
double newY = nodeY + pos.getHeight()/2 + y_offset;
final double dw = dialog.getWidth();
final double dh = dialog.getHeight();
List<Screen> scr = Screen.getScreensForRectangle(nodeX, nodeY, pos.getWidth(), pos.getHeight());
if (scr == null || scr.size() == 0)
{
logger.log(Level.WARNING, "Cannot determine dialog position (node out of screen)");
return;
}
// Take the first available screen
Rectangle2D sb = scr.get(0).getVisualBounds();
newX = newX < sb.getMinX() ? sb.getMinX() : newX;
newX = newX + dw > sb.getMaxX() ? sb.getMaxX() - dw : newX;
newY = newY < sb.getMinY() ? sb.getMinY() : newY;
newY = newY + dh > sb.getMaxY() ? sb.getMaxY() - dh : newY;
dialog.setX(newX);
dialog.setY(newY);
// Force to front
forceToFront(dialog);
}
});
}