//----------------------------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.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComplexComponent;
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 JavaListControlPanel extends JavaControlPanel
{
	public JavaListControlPanel(XPDLCollection myOwner, String title, boolean isVertical, boolean hasBorder, boolean editButton)
	{
		super(myOwner, title, isVertical, hasBorder);
		controlledPanel = ((XPDLCollection) myOwner).getControlledPanel();
		String a = sm.getString("AddKey");
		String r = sm.getString("RemoveKey");
		String e = sm.getString("EditKey");
		Dimension dim;
		if (editButton)
		{
			dim = getPreferredDimension(new String[] { a, r });
		}
		else
		{
			dim = getPreferredDimension(new String[] { a, r, e });
		}
		final JavaButton buttonAdd = new JavaButton(a, "Add", dim);
		final JavaButton buttonRemove = new JavaButton(r, "Remove", dim);
		final JavaButton buttonEdit = new JavaButton(e, "EditCellSmall", dim);
		buttonAdd.setEnabled(!myOwner.isReadOnly());
		buttonRemove.setEnabled(!myOwner.isReadOnly());
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(buttonAdd);
		add(Box.createRigidArea(new Dimension(0, 5)));
		add(buttonRemove);
		if (editButton)
		{
			add(Box.createRigidArea(new Dimension(0, 5)));
			add(buttonEdit);
		}
		buttonRemove.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JList l = ((JavaListPanel) controlledPanel).getList();
				Object obj = null;
				int ind = 0;
				try
				{
					obj = l.getSelectedValue();
					ind = l.getSelectedIndex();
				}
				catch (Exception ex)
				{
				}
				if (obj != null)
				{
					if (((XPDLCollection) getXPDLComponent()).canRemoveElement((XPDLComponent) obj))
					{
						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()).onElementRemoved((XPDLComponent) obj);
						}
						else
						{
							l.requestFocus();
							return;
						}
					}
					else
					{
						String message = "";
						if (obj instanceof XPDLComplexComponent)
						{
							message = sm.getString(((XPDLCollection) getXPDLComponent()).getInUseMessageName((XPDLComplexComponent) obj));
						}
						JOptionPane.showMessageDialog(getDialog(), message, sm.getString("Title"), JOptionPane.ERROR_MESSAGE);
					}
				}
				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 = ((JavaListPanel) controlledPanel).getList();
				DefaultListModel listModel = (DefaultListModel) l.getModel();
				int size = listModel.getSize();
				XPDLCollectionComponent newElement = ((XPDLCollection) getXPDLComponent()).demoComponent();
				String label = newElement.getLabel() + " - " + sm.getString("SelectKey");
				JavaDialog de = new JavaDialog(getDialog(), label);
				de.editXPDLComponent(newElement.getJavaPanel(), true, false);
				// if cancel hasn't been pressed
				if (!de.isCanceled())
				{
					if (!((XPDLCollection) getXPDLComponent()).canInsertElement(newElement))
					{
						return;
					}
					listModel.addElement(newElement); //.toValue());
					l.setSelectedIndex(size);
					((XPDLCollection) getXPDLComponent()).addComponent(newElement);
					((XPDLCollection) getXPDLComponent()).onElementInserted(newElement);
				}
				l.requestFocus();
			}
		});
		// action listener for modifying selected element
		buttonEdit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				JList l = ((JavaListPanel) controlledPanel).getList();
				XPDLComponent editElement = null;
				try
				{
					editElement = (XPDLComponent) l.getSelectedValue();
				}
				catch (Exception ex)
				{
				}
				if (editElement != null)
				{
					JavaDialog de =
						new JavaDialog(
							getDialog(),
							editElement.getLabel() + " '" + editElement.toString() + "' - " + sm.getString("EditingKey"));
					de.editXPDLComponent(editElement.getJavaPanel(), true, false);
					if (!de.isCanceled())
					{
						((XPDLCollection) getXPDLComponent()).onElementModified(editElement);
						controlledPanel.paintAll(controlledPanel.getGraphics());
					}
				}
				else
				{
					JOptionPane.showMessageDialog(
						getDialog(),
						sm.getString("WarningEmptySelectionToEditOrDelete"),
						sm.getString("Title"),
						JOptionPane.WARNING_MESSAGE);
				}
				l.requestFocus();
			}
		});
	}
	
	public void applyChanges()
	{
	}
}