//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/god4j
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : god4j@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.god4j.editor.panels;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Window;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

import org.huihoo.god4j.GOD4JConstants;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.util.StringManager;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaPanel extends JPanel
{
	protected static StringManager sm = StringManager.getManager("org.huihoo.god4j.editor");
	public static int GRID_LAYOUT = 0;
	public static int BOX_LAYOUT = 1;
	private XPDLComponent xpdlComp;
	private String title;
	public JavaPanel()
	{
	}
	public JavaPanel(XPDLComponent xpdlComp, int noOfElements, String title, int layout, boolean isVertical, boolean hasBorder)
	{
		setFont(GOD4JConstants.WINDOWS_UI_FONT);
		
		this.xpdlComp = xpdlComp;		
		setTitle(title, hasBorder);
		
		if (layout == JavaPanel.BOX_LAYOUT)
		{
			setLayout(new BoxLayout(this, ((isVertical) ? BoxLayout.Y_AXIS : BoxLayout.X_AXIS)));
		}
		else
		{
			setLayout(new GridLayout(((isVertical) ? noOfElements : 1), ((isVertical) ? 1 : noOfElements)));
		}
		setAlignmentX(Component.LEFT_ALIGNMENT);
		setAlignmentY(Component.TOP_ALIGNMENT);
		if (xpdlComp != null)
		{
			setEnabled(!xpdlComp.isReadOnly());
		}
	}
	public void setTitle(String title, boolean hasBorder)
	{
		this.title = title;
		int emptyBorderHSize = 5;
		int emptyBorderVSize = 1;
		Border emptyb =
			BorderFactory.createEmptyBorder(emptyBorderVSize, emptyBorderHSize, emptyBorderVSize, emptyBorderHSize);
		Border inb = BorderFactory.createEmptyBorder(0, 0, 0, 0);
		if (hasBorder)
		{
			inb = BorderFactory.createMatteBorder(1, 1, 1, 1, Color.darkGray);
			inb = BorderFactory.createTitledBorder(inb, title);
		}
		else
		{
			emptyb = BorderFactory.createTitledBorder(emptyb, title);
		}
		setBorder(BorderFactory.createCompoundBorder(emptyb, inb));
	}

	public XPDLComponent getXPDLComponent()
	{
		return xpdlComp;
	}
	
	public String getTitle()
	{
		return title;
	}

	
	//----------------------------------------------------------------------------------
	
	/**
	 * Find the hosting dialog.
	 */
	public JDialog getDialog()
	{
		for (Container p = getParent(); p != null; p = p.getParent())
		{
			if (p instanceof JDialog)
			{
				return (JDialog) p;
			}
		}
		return null;
	}
	
	public boolean checkRequired()
	{
		return true;
	}


	public boolean isEmpty()
	{
		return true;
	}
	
	public void applyChanges()
	{
	}
	
	//----------------------------------------------------------------------------------
	
	public static void defaultErrorMessage(Window w, String elementTitle)
	{
		String message = sm.getString("ErrorValueMustBeDefined");
		String dialogTitle = sm.getString("DialogValueIsNotDefined");
		JavaPanel.errorMessage(w, dialogTitle, elementTitle, message);
	}
	public static void errorMessage(Window w, String dialogTitle, String elementTitle, String message)
	{
		JOptionPane.showMessageDialog(w, elementTitle + message, dialogTitle, JOptionPane.ERROR_MESSAGE);
	}
}
