//----------------------------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.Component;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.huihoo.god4j.editor.XPDLChoice;
import org.huihoo.god4j.editor.XPDLComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaComboxPanel extends JavaPanel
{
	private Dimension comboBoxDimension = new Dimension(200, 20);
	private JLabel jl;
	private  JComboBox jcb;
	
	public JavaComboxPanel(XPDLChoice xpdl_component)
	{
		this(xpdl_component, JavaPanel.BOX_LAYOUT);
	}
	public JavaComboxPanel(XPDLComponent xpdl_component, Object[] choices)
	{
		this(xpdl_component, choices, JavaPanel.BOX_LAYOUT, false, false);
	}
	public JavaComboxPanel(XPDLChoice xpdl_component, int layout)
	{
		this(xpdl_component, layout, false);
	}
	public JavaComboxPanel(XPDLChoice xpdl_component, int layout, boolean isVertical)
	{
		this(xpdl_component, null, layout, isVertical, false);
	}
	public JavaComboxPanel(XPDLComponent xpdl_component, Object[] choices, int layout, boolean isVertical, boolean isEditable)
	{
		super(xpdl_component, 2, "", layout, isVertical, false);
		jl = new JLabel(xpdl_component.getLabel() + ": ");
		jl.setAlignmentX(Component.LEFT_ALIGNMENT);
		jl.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		jl.setHorizontalAlignment(SwingConstants.RIGHT);
		//jl.setMaximumSize(new Dimension(Short.MAX_VALUE,50));
		
		if (xpdl_component instanceof XPDLChoice)
		{
			jcb = new JComboBox(((XPDLChoice) xpdl_component).getChoices());
			jcb.setEditable(isEditable);
			jcb.setSelectedItem(((XPDLChoice) xpdl_component).getChoosen());
		}
		else
		{
			jcb = new JComboBox(choices);
			jcb.setEditable(isEditable);
			jcb.setSelectedItem(xpdl_component.getValue());
		}
		jcb.setAlignmentX(Component.LEFT_ALIGNMENT);
		jcb.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		jcb.setMinimumSize(new Dimension(comboBoxDimension));
		jcb.setMaximumSize(new Dimension(comboBoxDimension));
		jcb.setPreferredSize(new Dimension(comboBoxDimension));
		jcb.setEnabled(!xpdl_component.isReadOnly());
		add(Box.createHorizontalGlue());
		add(jl);
		add(jcb);
	}
	public boolean checkRequired()
	{
		if ((getSelectedItem() == null || getSelectedItem().toString().trim().equals("")) && getXPDLComponent().isRequired() && !getXPDLComponent().isReadOnly())
		{
			JavaPanel.defaultErrorMessage(this.getDialog(), jl.getText());
			jcb.requestFocus();
			return false;
		}
		return true;
	}
	
	public JComboBox getComboBox()
	{
		return jcb;
	}
	public Object getSelectedItem()
	{
		return jcb.getSelectedItem();
	}
	
	public void applyChanges()
	{
		getXPDLComponent().setValue(getSelectedItem());
	}
	
}
