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

import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;

import org.huihoo.god4j.editor.JavaDialog;
import org.huihoo.god4j.editor.XPDLChoice;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
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 JavaListChoiceControlPanel extends JavaControlPanel
{
	private XPDLCollection mySelection;
	public JavaListChoiceControlPanel(XPDLCollection myOwner, XPDLCollection mySel, String title, boolean isVertical, boolean hasBorder)
	{
		super(myOwner, title, isVertical, hasBorder);
		this.mySelection = mySel;
		controlledPanel = ((XPDLCollection) myOwner).getControlledPanel();
		String a = sm.getString("AddKey");
		String r = sm.getString("RemoveKey");
		Dimension dim = getPreferredDimension(new String[] { a, r });
		final JavaButton buttonAdd = new JavaButton(a, "Add", dim);
		final JavaButton buttonRemove = new JavaButton(r, "Remove", dim);
		if (isVertical)
		{
			add(Box.createRigidArea(new Dimension(0, 10)));
		}
		buttonAdd.setEnabled(!myOwner.isReadOnly());
		buttonRemove.setEnabled(!myOwner.isReadOnly());
		add(buttonAdd);
		add(Box.createRigidArea(new Dimension((isVertical) ? 0 : 5, (isVertical) ? 5 : 0)));
		add(buttonRemove);
		if (isVertical)
		{
			add(Box.createRigidArea(new Dimension(0, 10)));
		}
		buttonRemove.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JList l = ((JavaListChoicePanel) controlledPanel).getList();
				Object obj = null;
				int ind = 0;
				try
				{
					obj = l.getSelectedValue();
					ind = l.getSelectedIndex();
				}
				catch (Exception ex)
				{
				}
				if (obj != null)
				{
					if (((XPDLComponent) obj).isRequired())
					{
						JOptionPane.showMessageDialog(
							getDialog(),
							sm.getString("ErrorCannotRemoveMandatoryProperty"),
							sm.getString("Title"),
							JOptionPane.ERROR_MESSAGE);
						return;
					}
					else
					{
						int yn =
							JOptionPane.showConfirmDialog(
								getDialog(),
								sm.getString("MessageDoYouReallyWantToRemoveSelectedItem"),
								((XPDLComponent) obj).getLabel() + " - " + sm.getString("DeletingKey"),
								JOptionPane.YES_NO_OPTION);
						if (yn == JOptionPane.YES_OPTION)
						{
							DefaultListModel listModel = (DefaultListModel) l.getModel();
							listModel.removeElement(obj);
							((XPDLCollection) getXPDLComponent()).removeComponent(obj);
							((XPDLCollection) getXPDLComponent()).onElementDeleted((XPDLComponent) obj);
						}
						else
						{
							l.requestFocus();
							return;
						}
					}
				}
				else
				{
					JOptionPane.showMessageDialog(
						getDialog(),
						sm.getString("WarningEmptySelectionToEditOrDelete"),
						sm.getString("Title"),
						JOptionPane.WARNING_MESSAGE);
				}
				try
				{
					if (ind == 0)
					{
						ind++;
					}
					l.setSelectedIndex(ind - 1);
				}
				catch (Exception ex)
				{
				}
				l.requestFocus();
			}
		});
		buttonAdd.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JList l = ((JavaListChoicePanel) controlledPanel).getList();
				DefaultListModel listModel = (DefaultListModel) l.getModel();
				int size = listModel.getSize();
				XPDLComponent[] choices = new XPDLComponent[mySelection.getComboxComponents().size()];
				mySelection.getComboxComponents().toArray(choices);
				XPDLChoice ch = new XPDLChoice(null,"Select",choices)
				{
					public JavaPanel getJavaPanel()
					{
						return new JavaComboxButtonPanel(this, mySelection);
					}
				};
				ch.setRequired(true);
				String label = mySelection.getLabel() + " - " + sm.getString("SelectKey");
				JavaDialog de = new JavaDialog(getDialog(), label);
				de.editXPDLComponent(ch.getJavaPanel(), true, false);
				// if cancel hasn't been pressed
				if (!de.isCanceled())
				{
					XPDLCollectionComponent newElement = ((XPDLCollection) getXPDLComponent()).demoComponent();
					newElement.setValue(ch.getChoosen());
					listModel.addElement(newElement);
					l.setSelectedIndex(size);
					((XPDLCollection) getXPDLComponent()).addComponent(newElement);
					((XPDLCollection) getXPDLComponent()).onElementCreated(newElement);
				}
				l.requestFocus();
			}
		});
	}
	public void applyChanges()
	{
	}
	
}