All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.role.ui.javafx.impl.dialog.DialogBindings Maven / Gradle / Ivy

The newest version!
/*
 * *************************************************************************************************************************************************************
 *
 * SteelBlue: DCI User Interfaces
 * http://tidalwave.it/projects/steelblue
 *
 * Copyright (C) 2015 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *************************************************************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
 *
 * *************************************************************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/steelblue-src
 * git clone https://github.com/tidalwave-it/steelblue-src
 *
 * *************************************************************************************************************************************************************
 */
package it.tidalwave.role.ui.javafx.impl.dialog;

import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.concurrent.Executor;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.application.Platform;
import it.tidalwave.util.Callback;
import it.tidalwave.util.ui.UserNotificationWithFeedback;
import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

/***************************************************************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 *
 **************************************************************************************************************************************************************/
@Slf4j
public class DialogBindings extends DelegateSupport
  {
    /***********************************************************************************************************************************************************
     *
     **********************************************************************************************************************************************************/
    public DialogBindings (@Nonnull final Executor executor)
      {
        super(executor);
      }

    /***********************************************************************************************************************************************************
     * {@inheritDoc}
     **********************************************************************************************************************************************************/
    public void showInModalDialog (@Nonnull final UserNotificationWithFeedback notification,
                                   @Nonnull final Optional node)
      {
        // FIXME: should not be needed
        Platform.runLater(() ->
          {
            log.debug("modalDialog({}, {})", node, notification);

//                final Dialog dialog = new Dialog<>();
            final Dialog dialog = new Alert(Alert.AlertType.NONE);
            dialog.initOwner(mainWindow);
            dialog.setTitle(notification.getCaption());
            dialog.setResizable(false);
            dialog.setContentText(notification.getText());
            node.ifPresent(n -> dialog.getDialogPane().setContent(n));

            final var feedback = notification.getFeedback();
            final var hasOnCancel = feedback.canCancel();

            final var buttonTypes = dialog.getDialogPane().getButtonTypes();
            buttonTypes.clear();
            buttonTypes.add(ButtonType.OK);

            if (hasOnCancel)
              {
                buttonTypes.add(ButtonType.CANCEL);
              }

//                okButton.disableProperty().bind(new PropertyAdapter<>(valid)); // FIXME: doesn't work

            final var result = dialog.showAndWait();

            if (result.isEmpty())
              {
                if (hasOnCancel)
                  {
                    wrap(notification::cancel);
                  }
                else
                  {
                    wrap(notification::confirm);
                  }
              }
            else
              {
                if (result.get() == ButtonType.OK)
                  {
                    wrap(notification::confirm);
                  }
                else if (result.get() == ButtonType.CANCEL)
                  {
                    wrap(notification::cancel);
                  }
                else
                  {
                    throw new IllegalStateException("Unexpected button pressed: " + result.get());
                  }
              }
          });
      }

    /***********************************************************************************************************************************************************
     *
     **********************************************************************************************************************************************************/
    @SneakyThrows(Throwable.class)
    private static void wrap (@Nonnull final Callback callback)
      {
        callback.call();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy