/**
 * Die Klasse erstellt einen von <code>JOptionPane</code> abgeleiteten Dialog
 * zur Passwortabfrage, in dem zus&auml;tzlich ein Aufforderungs-Text erscheint.
 * Das Eingabe-Textfeld kann zudem zwischen Klartext und Punktnotation mittels
 * Checkbox getauscht werden.
 */
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
/*
 * @JB_PasswordDialog.java     29.01.2012
 * build: 29.01.2012
 * Copyright 2012 javabeginners.de. All rights reserved
 * @author Joerg Czeschla
 * 
 * 
 * This file 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 2
 * of the License, or (at your option) any later version.
 *
 * This file 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 file;
 * if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
class JB_PasswordDialog extends JOptionPane {

	static JPasswordField pwFeld;
	static JTextField txtFeld;
	static JCheckBox cb = new JCheckBox("Klartext", false);

	public static String showPasswordDialog(Component parentComponent,
			String message, String title, int messageType) {

		JOptionPane.showMessageDialog(parentComponent, createPanel(message),
				title, messageType);
		return new String(pwFeld.getPassword());
	}

	private static JPanel createPanel(String message) {
		final CaretListener listener = new FieldCaretListener();
		pwFeld = new JPasswordField(10);
		pwFeld.addCaretListener(listener);
		txtFeld = new JTextField(10);
		JPanel mainPanel = new JPanel(new GridLayout(2, 1));
		final CardLayout cl = new CardLayout();
		final JPanel fieldPanel = new JPanel(cl);
		fieldPanel.add(pwFeld, "pwFeld");
		fieldPanel.add(txtFeld, "txtFeld");
		cb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (cb.isSelected()) {
					pwFeld.removeCaretListener(listener);
					cl.show(fieldPanel, "txtFeld");
					txtFeld.addCaretListener(listener);
				} else {
					txtFeld.removeCaretListener(listener);
					cl.show(fieldPanel, "pwFeld");
					pwFeld.addCaretListener(listener);
				}
			}
		});
		JPanel inputPanel = new JPanel(new FlowLayout());
		JLabel label = new JLabel(message);
		inputPanel.add(fieldPanel);
		inputPanel.add(cb);
		mainPanel.add(label);
		mainPanel.add(inputPanel);
		return mainPanel;
	}

	static class FieldCaretListener implements CaretListener {
		public void caretUpdate(CaretEvent e) {
			if (pwFeld.isVisible()) {
				txtFeld.setText(new String(pwFeld.getPassword()));
			}
			if (txtFeld.isVisible()) {
				pwFeld.setText(txtFeld.getText());
			}
		}
	}
}
