//----------------------------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.JList;
import javax.swing.JScrollPane;
import javax.swing.JViewport;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.huihoo.god4j.editor.XPDLCollection;
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 JavaListChoicePanel extends JavaPanel
{
	JavaPanel prevPanel = null;
	public JavaListChoicePanel(XPDLCollection myOwner)
	{
		this(myOwner, "");
	}
	public JavaListChoicePanel(XPDLCollection myOwner, String title)
	{
		this(myOwner, title, JavaPanel.BOX_LAYOUT);
	}
	public JavaListChoicePanel(XPDLCollection myOwner, String title, int layout)
	{
		this(myOwner, title, layout, true);
	}
	public JavaListChoicePanel(XPDLCollection myOwner, String title, int layout, boolean isVertical)
	{
		this(myOwner, title, layout, isVertical, true);
	}
	public JavaListChoicePanel(XPDLCollection myOwner, String title, int layout, boolean isVertical, boolean hasBorder)
	{
		super(myOwner, 3, title, layout, isVertical, hasBorder);
		final JavaListPanel pList = new JavaListPanel(myOwner, "", false, false, true);
		add(pList);
		final JScrollPane jsp = new JScrollPane();
		jsp.setAlignmentX(Component.LEFT_ALIGNMENT);
		jsp.setAlignmentY(Component.TOP_ALIGNMENT);
		jsp.setMinimumSize(new Dimension(300, 225));
		//jsp.setPreferredSize(new Dimension(300,225));
		add(jsp);
		final JList jl = pList.getList();
		jl.addListSelectionListener(new ListSelectionListener()
		{
			public void valueChanged(ListSelectionEvent e)
			{
				if (pList.isItemChangingPosition())
					return;
				if (prevPanel != null)
				{
					prevPanel.applyChanges();
					if (!getXPDLComponent().isReadOnly())
					{
						//((XPDLCompDialog) prevPanel.getDialog()).notifyListeners(prevPanel.getOwner());
					}
				}
				XPDLComponent choosen = null;
				try
				{
					choosen = (XPDLComponent) jl.getSelectedValue();
				}
				catch (Exception ex)
				{
				}
				if (choosen != null)
				{
					prevPanel = choosen.getJavaPanel();
					jsp.setViewportView(prevPanel);
				}
				else
				{
					jsp.setViewportView(new JavaPanel());
				}
			}
		});
		jl.setSelectedIndex(0);
		if (layout == JavaPanel.BOX_LAYOUT)
		{
			if (isVertical)
			{
				add(Box.createVerticalGlue());
			}
			else
			{
				add(Box.createHorizontalGlue());
			}
		}
	}
	public JList getList()
	{
		return ((JavaListPanel) getComponent(0)).getList();
	}

	public void applyChanges()
	{
		//try {
		// setting List panel
		 ((JavaPanel) getComponent(0)).applyChanges();
		// setting implementation panel
		JScrollPane jsp = (JScrollPane) getComponent(1);
		JViewport jvp = (JViewport) jsp.getComponent(0);
		((JavaPanel) jvp.getComponent(0)).applyChanges();
		//} catch (Exception e) {}
	}
}