//
// 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;

/**
 * <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-04-09
 * </p>
 * 
 * @author M. Maglioni */
public class Placard extends JDialog {
	/** pure formality */
	private static final long serialVersionUID = 1L;
	/** the panel containing the whole */
	private JPanel jContentPane = null;
	/** the OK button */
	private JButton jButton = 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 jdDowelTop = 55;
	private final int jdDowelRight = 15;
	private final int taDowelTop = 0;
	private final int charsEveryLine = 68;
	private final int lineFontVerdana16Height = 22;

	/**
	 * <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 */
	public Placard(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 Placard(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 Placard(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());
		this.setVisible(true);
		reduce = true;
	}

	/** 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(getJButton(), null);
		}
		return jContentPane;
	}

	/** This method initializes jButton
	 * 
	 * @return javax.swing.JButton
	 * @see #getJContentPane () */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			buttonX = (jdWidth - buWidth) / 2;
			buttonY = taMargin * 2 + taHeight;
			jButton.setBounds(new Rectangle(buttonX, buttonY, buWidth,
					buHeight));
			jButton.setText("OK");
			jButton.setFont(new Font("Arial", Font.PLAIN, 16));
			jButton.addMouseListener(new java.awt.event.MouseAdapter() {
				@Override
				public void mouseClicked(java.awt.event.MouseEvent e) {
					jButton_mouseClicked();
				}
			});
		}
		return jButton;
	}

	/**
	 * <p>
	 * I close the box.
	 * </p>
	 * 
	 * @see #getJButton */
	private void jButton_mouseClicked() {
		dispose();
	}
} // @jve: decl-index = 0: visual-constraint = "7.10"