package pbl2011.gui;

import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import ash.gui.core.AshGroupPanel;

/**
 * キャンバスサイズ変更画面 <p>
 *
 * @author 10745104 Y.Ishii
 *
 */
@SuppressWarnings("serial")
public class CanvasSizePanel extends AshGroupPanel implements ActionListener {

	private JTextField width;
	private JTextField height;

	private JButton ok;
	private JButton cancel;
	private JDialog dialog;

	private Rectangle newSize;

	public static Rectangle updateSize(JFrame paramJFrame, Rectangle rect) {
		JDialog localJDialog = new JDialog(paramJFrame, "サイズ変更", true);
		CanvasSizePanel localAshFontPanel = new CanvasSizePanel(localJDialog,
				rect);
		localJDialog.setContentPane(localAshFontPanel);
		localJDialog.pack();
		localJDialog.setLocationRelativeTo(paramJFrame);
		localJDialog.setVisible(true);
		localJDialog.dispose();
		return ((localAshFontPanel.newSize != null) ? localAshFontPanel.newSize
				: rect);
	}

	CanvasSizePanel(JDialog paramJDialog, Rectangle rect) {
		this.dialog = paramJDialog;
		setActionListener(this);
		this.width = new JTextField(String.valueOf(rect.width));
		this.height = new JTextField(String.valueOf(rect.height));
		this.ok = button("更新");
		this.cancel = button("取消し");

		form(line(null, "サイズを指定してください。"),
			 line("幅", sg(this.width, gap(40))),
			 line("高さ", sg(this.height, gap(40))),
			 line(null, sg(this.ok, gap(20), this.cancel, gap(20))));
	}

	public void actionPerformed(ActionEvent paramActionEvent) {
		if (paramActionEvent.getSource() == this.ok) {
			Rectangle rect = null;
			try {
				rect = new Rectangle(Integer.parseInt(this.width.getText()),
						Integer.parseInt(this.height.getText()));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(null, "数値を入力してください。", "エラー",
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			this.newSize = rect;
		}
		this.dialog.dispose();
	}
}
