//----------------------------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 java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLComponent;
import org.huihoo.god4j.editor.JavaDialog;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaComboxButtonPanel extends JavaComboxPanel
{

	protected XPDLCollection refCollection = null;

	public JavaComboxButtonPanel(XPDLComponent myOwner, XPDLCollection c)
	{
		this(myOwner, null, c, JavaPanel.BOX_LAYOUT, false, false);
	}

	public JavaComboxButtonPanel(XPDLComponent myOwner, Object[] choices, XPDLCollection c)
	{
		this(myOwner, choices, c, JavaPanel.BOX_LAYOUT, false, false);
	}

	public JavaComboxButtonPanel(XPDLComponent myOwner, Object[] choices, XPDLCollection c, boolean isEditable)
	{
		this(myOwner, choices, c, JavaPanel.BOX_LAYOUT, false, isEditable);
	}

	public JavaComboxButtonPanel(
		XPDLComponent myOwner,
		Object[] choices,
		XPDLCollection c1,
		int layout,
		boolean isVertical,
		boolean isEditable)
	{

		super(myOwner, choices, layout, isVertical, isEditable);

		final JComboBox jcb = getComboBox();

		refCollection = c1;

		String s = sm.getString("DefineKey");
		double w = 0;
		try
		{
			w = getFontMetrics(getFont()).stringWidth(s);
		}
		catch (Exception ex)
		{
		}
		double h = getFontMetrics(getFont()).getHeight();
		w += 25;
		if (w < 30)
			w = 30;
		Dimension dim = new Dimension((int) w, (int) h);

		JavaButton buttonDefine = new JavaButton(s, "Define", dim);
		buttonDefine.setAlignmentX(Component.LEFT_ALIGNMENT);
		buttonDefine.setAlignmentY(Component.BOTTOM_ALIGNMENT);
		buttonDefine.setEnabled(!myOwner.isReadOnly());

		// action listener for defining new element
		buttonDefine.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				Object selected = jcb.getSelectedItem();
				JavaDialog de =new JavaDialog(getDialog(), refCollection.getLabel() + " - " + sm.getString("DefiningKey"));
				
				de.editXPDLComponent(refCollection.getJavaPanel(), false, false);
				System.err.println("-JavaComboButtonPanel--DEBUG---:"+de.isCanceled());
				if (!de.isCanceled())
				{
					// refresh
					DefaultComboBoxModel dcbm = new DefaultComboBoxModel(refCollection.getComboxComponents().toArray());
					jcb.setModel(dcbm);
					if (selected != null)
					{
						try
						{
							jcb.setSelectedItem(selected);
						}
						catch (Exception ex)
						{
						}
					}
				}
			}
		});

		add(Box.createRigidArea(new Dimension(10, 0)));
		add(buttonDefine);
	}
}
