/*
 * Created on Aug 14, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package hibergui;

import hibergui.event.MessageListener;

import org.eclipse.jface.dialogs.TitleAreaDialog;

import static org.eclipse.swt.SWT.*;

import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

/**
 * A TitleAreaDialog containing a HiberGUI form. The form messages are displayed
 * at the title area.
 * 
 * @author Miklós Espák
 */
public abstract class FormDialog<T> extends TitleAreaDialog {

	protected Form<T> form;

	private T[] result;

	public FormDialog(Shell parentShell) {
		super(parentShell);

		setShellStyle(DIALOG_TRIM | RESIZE);
	}

	public Form<T> getForm() {
		return form;
	}

	public T[] getResult() {
		return result;
	}

	@Override
	protected Control createDialogArea(Composite cptParent) {
		getShell().addShellListener(new ShellAdapter() {
			public void shellClosed(ShellEvent e) {
				result = form.getSelection();
				form.dispose();
			}
		});
		// ESCAPE should not close the dialog. Its job is to clean
		// the active dirty field.
		getShell().addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent e) {
				if (e.detail == TRAVERSE_ESCAPE || e.detail == TRAVERSE_RETURN)
					e.doit = false;
			}
		});

		form = createForm(cptParent);
		form.getControl().setLayoutData(new GridData(GridData.FILL_BOTH));

		form.addMessageListener(new MessageListener() {
			public void titleChanged(String title) {
				setTitle(title);
			}

			public void messageChanged(String message, int type) {
				setMessage(message, type);
			}
		});

		return form.getControl();
	}

	protected abstract Form<T> createForm(Composite cptParent);

	@Override
	protected Control createButtonBar(Composite arg0) {
		return arg0;
	}

	// protected void okPressed() {
	// if (form.isDirty()) {
	// boolean valid = false;
	// try {
	// form.verify();
	// valid = true;
	// }
	// catch (Exception exc) {
	// return;
	// }
	// if (form.get() == null) {
	// if (valid) {
	// if (MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.create")))
	// form.create();
	// }
	// else {
	// if (!MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.create.invalid")))
	// return;
	// }
	// }
	// else {
	// if (valid) {
	// if (MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.modify")))
	// form.modify();
	// }
	// else {
	// if (!MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.modify.invalid")))
	// return;
	// }
	// }
	// }
	// // if (form.isStale()) {
	// // if (MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.ok")))
	// // form.apply();
	// // }
	// result = form.getSelection();
	// closedByButton = true;
	// super.okPressed();
	// }

	// protected void cancelPressed() {
	// // if (form.isStale()) {
	// // if (MessageDialog.openQuestion(getShell(), "title",
	// I18n.get("question.cancel")))
	// // form.revoke();
	// // }
	// closedByButton = true;
	// super.cancelPressed();
	// }
	//    
	// public boolean close() {
	// if (!closedByButton && form.isStale()) {
	// MessageDialog biztos = new MessageDialog(
	// getShell(), "", null,
	// I18n.get("question.close"),
	// MessageDialog.WARNING,
	// new String[]{
	// I18n.get("apply"),
	// I18n.get("revoke"),
	// I18n.get("cancel")},
	// 0);
	// switch (biztos.open()) {
	// case 0:
	// form.apply();
	// break;
	// case 1:
	// form.revoke();
	// break;
	// case 2:
	// return false;
	// }
	// }
	// form.dispose();
	// return super.close();
	// }
}
