content: /*
   * BurpKit - WebKit-based penetration testing plugin for BurpSuite
   * Copyright (C) 2016  Red Canari, Inc.
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  package com.redcanari.ui;
  
  import javafx.application.Platform;
  import javafx.geometry.Insets;
  import javafx.scene.Node;
  import javafx.scene.control.*;
  import javafx.scene.image.ImageView;
  import javafx.scene.layout.GridPane;
  import javafx.scene.web.PromptData;
  import javafx.util.Pair;
  
  import java.util.Optional;
  
  /**
   * Created by ndouba on 15-07-02.
   */
  public class JSLoginDialog extends Dialog<Pair<String, String>> {
  
      private final TextField username;
  
      public JSLoginDialog() {
          setTitle("Login Dialog");
  
          // Set the icon (must be included in the project).
  //        setGraphic(new ImageView(this.getClass().getResource("login.png").toString()));
  
          // Set the button types.
          ButtonType loginButtonType = new ButtonType("Login", ButtonBar.ButtonData.OK_DONE);
          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));
  
          username = new TextField();
          username.setPromptText("Username");
          PasswordField password = new PasswordField();
          password.setPromptText("Password");
  
          grid.add(new Label("Username:"), 0, 0);
          grid.add(username, 1, 0);
          grid.add(new Label("Password:"), 0, 1);
          grid.add(password, 1, 1);
  
          // Enable/Disable login button depending on whether a username was entered.
          Node loginButton = getDialogPane().lookupButton(loginButtonType);
  //        loginButton.setDisable(true);
  
          // Do some validation (using the Java 8 lambda syntax).
  //        username.textProperty().addListener((observable, oldValue, newValue) -> {
  //            loginButton.setDisable(newValue.trim().isEmpty());
  //        });
  
          getDialogPane().setContent(grid);
  
          // Convert the result to a username-password-pair when the login button is clicked.
          setResultConverter(dialogButton -> {
              if (dialogButton == loginButtonType) {
                  return new Pair<>(username.getText(), password.getText());
              }
              return null;
          });
      }
  
      public Pair<String, String> login(String loginPrompt) {
          // Set the masthead to the user's prompt message.
          setHeaderText(loginPrompt);
  
          // Request focus on the textfield so the user can type their response right away.
          // Request focus on the username field by default.
          Platform.runLater(username::requestFocus);
  
          // Show and wait for the result and then return a string or null
          Optional<Pair<String, String>> result = super.showAndWait();
          return (result.isPresent())?result.get():null;
      }
  }
  