/*
 * Copyright (c) 2020 by the Vassal developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.configure.password;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.event.DocumentListener;
import java.awt.BorderLayout;
import java.awt.Dimension;

import VASSAL.i18n.Resources;

public class ToggleablePasswordFieldPanel {

  private static final char ECHO_CHAR = '*';
  private static final int FIELD_HEIGHT = 40;

  private final JPanel panel;
  private final JPasswordField passwordField;

  private boolean passwordVisible = false;

  public ToggleablePasswordFieldPanel(String label, String initialPassword) {
    panel = new JPanel(new BorderLayout());
    panel.add(new JLabel(label), BorderLayout.WEST);

    passwordField = new JPasswordField(12);
    passwordField.setEchoChar(ECHO_CHAR);
    passwordField.setMaximumSize(new Dimension(
      (int) passwordField.getMaximumSize().getWidth(),
      FIELD_HEIGHT
    ));
    passwordField.setText(initialPassword);
    panel.add(passwordField, BorderLayout.CENTER);

    final JButton toggleButton = new JButton(Resources.getString("GlobalOptions.toggle_password_visibility")); //$NON-NLS-1$
    toggleButton.addActionListener(e -> {
      if (passwordVisible) {
        passwordField.setEchoChar(ECHO_CHAR);
        passwordVisible = false;
      }
      else {
        passwordField.setEchoChar((char)0);
        passwordVisible = true;
      }
    });
    panel.add(toggleButton, BorderLayout.EAST);
    panel.setMaximumSize(new Dimension(
      (int) (passwordField.getMaximumSize().getWidth() + toggleButton.getMaximumSize().getWidth()),
      FIELD_HEIGHT));
  }

  public JPanel getPanel() {
    return panel;
  }

  public String getPassword() {
    return String.valueOf(passwordField.getPassword());
  }

  public void setPassword(String password) {
    passwordField.setText(password);
  }

  public void addPasswordFieldListener(DocumentListener documentListener) {
    passwordField.getDocument().addDocumentListener(documentListener);
  }
}

@@ -885,47 +885,57 @@
 awt.
-BorderLayout;%0Aimport java.awt.Dimension
+Dimension;%0A%0Aimport net.miginfocom.swing.MigLayout
 ;%0A%0Ai
@@ -1331,75 +1331,122 @@
 new 
-Border
+Mig
 Layout(
+%22ins 0%22, %22%5B%5Drel%5B%5D%5B%5D%22
 ));%0A
+%0A
     
-panel.add(new JLabel(label), BorderLayout.WEST
+final JLabel passwordLabel = new JLabel(label);%0A    panel.add(passwordLabel
 );%0A%0A
@@ -1556,114 +1556,28 @@
 .set
-MaximumSize(new Dimension(%0A      (int) passwordField.getMaximumSize().getWidth(),%0A      FIELD_HEIGHT%0A    )
+Text(initialPassword
 );%0A 
@@ -1591,40 +1591,43 @@
 word
-Fi
+Lab
 el
-d
 .set
-Text(initialP
+LabelFor(p
 assword
+Field
 );%0A
+%0A
     
@@ -1655,27 +1655,22 @@
 ld, 
-BorderLayout.CENTER
+%22pushx, growx%22
 );%0A%0A
@@ -1846,12 +1846,8 @@
     
-if (
 pass
@@ -1861,411 +1861,136 @@
 ible
-) %7B%0A        passwordField.setEchoChar(ECHO_CHAR);%0A        passwordVisible = false;%0A      %7D%0A      else %7B%0A        passwordField.setEchoChar((char)0);%0A        passwordVisible = true;%0A      %7D%0A    %7D);%0A    panel.add(toggleButton, BorderLayout.EAST);%0A    panel.setMaximumSize(new Dimension(%0A      (int) (passwordField.getMaximumSize().getWidth() + toggleButton.getMaximumSize().getWidth()),%0A      FIELD_HEIGHT)
+ = !passwordVisible;%0A      passwordField.setEchoChar(passwordVisible ? ECHO_CHAR : (char) 0);%0A    %7D);%0A    panel.add(toggleButton
 );%0A 

Use MigLayout.