//
// https://www.ramecera.it/myscrewdriver.html
// e-mail: screwdriver@ramecera.it
//
/* This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html */
package it.ramecera.salad.graphic;

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.Mix;
import javax.swing.JCheckBox;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

/**
 * <p>
 * Display class of a message box. It is used simply by calling: new
 * Placard (component_owner, message);<br>
 * or: new Placard (component_owner, message, header); no method needs
 * to be invoked. For other details see the constructor.
 * </p>
 * <p>
 * Company: Ramecera™<br>
 * Creation date: 2020-04-09<br>
 * Last updated: 2020-09-10
 * </p>
 * <p>
 * Modifications:<br>
 * </p>
 * 
 * @author M. Maglioni */
public class Remember extends JDialog {
	/** pure formality */
	private static final long serialVersionUID = 1L;
	/** the panel containing the whole */
	private JPanel jContentPane = null;
	/** the text of the message */
	private final String dispatch;
	/** the title to the message */
	private String header = "Message:";
	/* the text-area object */
	private JTextArea textArea;
	private int buttonX = 250;
	private int buttonY = 250;
	private int jdWidth = 607;
	private int jdHeight = 350;
	private final int taMargin = 18;
	private final int taWidth = 565;
	private int taHeight = 150;
	private final int buWidth = 100;
	private final int buHeight = 40;
	private final int checkX = 20;
	private int checkY = 100;
	private final int chWidth = 120;
	private final int chHeight = 20;
	private final int jdDowelTop = 55;
	private final int jdDowelRight = 15;
	private final int taDowelTop = 0;
	private final int charsEveryLine = 68;
	private final int lineFontVerdana16Height = 22;
	private final int betweenButtons = 40;
	/** the affirmative button */
	private JButton jButtonYes = null;
	/** the negative button */
	private JButton jButtonNo = null;
	/** says if you need to lock the "yes" button */
	private boolean lockButtonYes;
	/** check-box Remember */
	private JCheckBox chckbxNewCheckBox;
	// PUBLIC
	/** says if I pressed yes (true) or not (false) */
	public boolean[] answer = { false, false };

	/**
	 * <p>
	 * Only public property; warns that the message has been
	 * forwarded. */
	public boolean reduce;

	/**
	 * <p>
	 * 2 parameter constructor.
	 * </p>
	 * <p>
	 * creation: 2020-04-09<br>
	 * last modification: 2020-04-09<br>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @param owner the graphic component from which the pop-up of the
	 *        message itself is born;
	 * @param s may be the message to be displayed
	 * @wbp.parser.constructor */
	public Remember(Frame owner, final String s) {
		super(owner);
		setFont(new Font("Verdana", Font.PLAIN, 15));
		dispatch = s;
		initialize();
	}

	/**
	 * <p>
	 * 3 parameter constructor.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @param owner the graphic component from which the pop-up of the
	 *        message itself is born;
	 * @param s the message to be displayed
	 * @param t the header of the message */
	public Remember(Frame owner, String s, String t) {
		super(owner);
		dispatch = s;
		header = t;
		initialize();
	}

	/**
	 * <p>
	 * 1 parameter constructor. In this case, the graphic component from
	 * which the pop-up of the message originates is indeterminate.
	 * </p>
	 * <p>
	 * creation: 2020-04-09<br>
	 * last modification: 2020-04-09<br>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @param s the message to display */
	public Remember(String s) {
		super(new Frame());
		dispatch = s;
		initialize();
	}

	/** ordinary initialization */
	private void initialize() {
		this.setSize(jdWidth, jdHeight);
		// center on the screen
		this.setModal(true);
		this.setLocationRelativeTo(null);
		this.setTitle(header);
		this.setContentPane(getJContentPane());
		jButtonYes.setEnabled(!lockButtonYes);
		this.setVisible(true);
		reduce = true;
	}

	/**
	 * <p>
	 * 4 parameter constructor.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @param owner the graphic component from which the pop-up of the
	 *        message itself originates; it can be null
	 * @param s the message to be displayed
	 * @param t the head of the box
	 * @param lockButtonYes if it is necessary to block the "Yes" button
	 *        for obvious opportunity */
	public Remember(Frame owner, final String s, final String t,
			final boolean lockButtonYes) {

		super(owner);
		dispatch = s;
		header = t;
		this.lockButtonYes = lockButtonYes;
		initialize();
	}

	/** This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 * @see #initialize () */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(new Color(250, 240, 230));
			textArea = new JTextArea();
			textArea.setEditable(false);
			textArea.setFont(new Font("Verdana", Font.PLAIN, 16));
			textArea.setWrapStyleWord(true);
			textArea.setLineWrap(true);
			textArea.setBackground(new Color(250, 240, 230));
			/* estimate of the amount of lines in the message */
			int lineQ = dispatch.length() / charsEveryLine + 1 + Mix
					.charQ(dispatch, Candies.LINE_FEED);
			taHeight = taDowelTop + lineQ * lineFontVerdana16Height;
			textArea.setBounds(taMargin, taMargin, taWidth, taHeight);
			textArea.setText(dispatch);
			jdWidth = taWidth + taMargin * 2 + jdDowelRight;
			jdHeight = taHeight + taMargin * 3 + buHeight + jdDowelTop;
			this.setSize(jdWidth, jdHeight);
			jContentPane.add(textArea);
			jContentPane.add(getJButtonYes(), null);
			jContentPane.add(getJButtonNo(), null);

			chckbxNewCheckBox = new JCheckBox("Remember");
			chckbxNewCheckBox.addChangeListener(new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					answer[1] = chckbxNewCheckBox.isSelected();
				}
			});
			chckbxNewCheckBox.setFont(new Font("Verdana", Font.PLAIN,
					12));
			chckbxNewCheckBox.setToolTipText("Please ask me no more");
			checkY = taMargin * 2 + taHeight + buHeight / 3;
			chckbxNewCheckBox.setBounds(checkX, checkY, chWidth,
					chHeight);
			jContentPane.add(chckbxNewCheckBox);
		}
		return jContentPane;
	}

	/** This method initializes jButton Yes
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Modifications:<br>
	 * </p>
	 * 
	 * @return javax.swing.JButton
	 * @see #getJContentPane () */
	private JButton getJButtonYes() {
		if (jButtonYes == null) {
			jButtonYes = new JButton();
			buttonX = (jdWidth - betweenButtons - buWidth * 2) / 2;
			buttonY = taMargin * 2 + taHeight;
			jButtonYes.setBounds(new Rectangle(buttonX, buttonY,
					buWidth, buHeight));
			jButtonYes.setText("Yes");
			jButtonYes.setEnabled(true);  // generated
			jButtonYes.setFont(new Font("Tahoma", Font.PLAIN, 14));
			jButtonYes.addMouseListener(
					new java.awt.event.MouseAdapter() {
						public void mouseClicked(
								java.awt.event.MouseEvent e) {
							if (jButtonYes.isEnabled()) {
								jButtonYes_mouseClicked();
							}
						}
					});
		}
		return jButtonYes;
	}

	/**
	 * <p>
	 * I close my box.
	 * </p>
	 * 
	 * @see #getJButtonYes () */
	private void jButtonYes_mouseClicked() {
		answer[0] = true;
		dispose();
	}

	/**
	 * <p>
	 * I close my box.
	 * </p>
	 * 
	 * @see #getJButtonNo () */
	private void jButtonNo_mouseClicked() {
		answer[0] = false;
		dispose();
	}

	/**
	 * <p>
	 * This method initializes jButtonNo.
	 * </p>
	 * 
	 * @return javax.swing.JButton
	 * @see #getJContentPane () */
	private JButton getJButtonNo() {
		if (jButtonNo == null) {
			// try {
			jButtonNo = new JButton();
			buttonX = (jdWidth - betweenButtons - buWidth * 2) / 2
					+ buWidth + betweenButtons;
			buttonY = taMargin * 2 + taHeight;
			jButtonNo.setBounds(new Rectangle(buttonX, buttonY, buWidth,
					buHeight));
			jButtonNo.setText("No");  // generated
			jButtonNo.setFont(new Font("Tahoma", Font.PLAIN, 14));  // generated
			jButtonNo.addMouseListener(
					new java.awt.event.MouseAdapter() {
						public void mouseClicked(
								java.awt.event.MouseEvent e) {
							jButtonNo_mouseClicked();
						}
					});
		}
		return jButtonNo;
	}
}  // @jve: decl-index = 0: visual-constraint = "7.10"