//----------------------------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 java.util.Vector;

import javax.swing.Box;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.huihoo.god4j.editor.JavaDialog;
import org.huihoo.god4j.editor.XPDLCollection;
import org.huihoo.god4j.editor.XPDLCollectionComponent;
import org.huihoo.god4j.editor.XPDLComplexComponent;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class JavaTableControlPanel extends JavaControlPanel
{
	public JavaTableControlPanel(XPDLCollection myOwner, String title, boolean isVertical, boolean hasBorder)
	{
		super(myOwner, title, isVertical, hasBorder);
		controlledPanel = ((XPDLCollection) myOwner).getControlledPanel();
		String n = sm.getString("NewKey");
		String e = sm.getString("EditKey");
		String d = sm.getString("DeleteKey");
		Dimension dim = getPreferredDimension(new String[] { n, e, d });
		JavaButton buttonNew = new JavaButton(n, "NewSmall", dim);
		JavaButton buttonEdit = new JavaButton(e, "EditCellSmall", dim);
		JavaButton buttonDelete = new JavaButton(d, "DeleteSmall", dim);
		if (isVertical)
		{
			add(Box.createRigidArea(new Dimension(0, 10)));
		}
		add(buttonNew);
		add(Box.createRigidArea(new Dimension((isVertical) ? 0 : 5, (isVertical) ? 5 : 0)));
		add(buttonEdit);
		add(Box.createRigidArea(new Dimension((isVertical) ? 0 : 10, (isVertical) ? 10 : 0)));
		add(buttonDelete);
		add(Box.createVerticalGlue());
		buttonNew.setEnabled(!myOwner.isReadOnly());
		buttonDelete.setEnabled(!myOwner.isReadOnly());
		// action listener for defining new element
		buttonNew.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				XPDLCollectionComponent newElement = ((XPDLCollection) getXPDLComponent()).demoComponent();
				JavaDialog de = new JavaDialog(getDialog(), newElement.getLabel() + " - " + sm.getString("DefiningKey"));
				de.editXPDLComponent(newElement.getJavaPanel(), true, false);
				
				// if cancel hasn't been pressed
				if (!de.isCanceled())
				{
					newElement.saveComponent();
					addElement((XPDLCollectionComponent) newElement);
					((XPDLCollection) getXPDLComponent()).onElementCreated(newElement);
				}
			}
		});
		// action listener for modifying selected element
		buttonEdit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				XPDLComplexComponent editElement = getSelectedElement();
				if (editElement != null)
				{ 
					JavaDialog de =
						new JavaDialog(
							getDialog(),
							editElement.getLabel() + " '" + editElement.toString() + "' - " + sm.getString("EditingKey"));
					de.editXPDLComponent(editElement.getJavaPanel(), true, false);
					if (!de.isCanceled())
					{
						editElement.saveComponent();
						modifyElement(editElement);
						((XPDLCollection) getXPDLComponent()).onElementModified(editElement);
					}
				}
				else
				{
					complainLoudly("WarningEmptySelectionToEditOrDelete");
				}
				((JavaTablePanel) controlledPanel).getTable().requestFocus();
			}
		});
		// action listener for removing selected element
		buttonDelete.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				XPDLComplexComponent deleteElement = getSelectedElement();
				if (deleteElement == null)
				{
					complainLoudly("WarningEmptySelectionToEditOrDelete");
				}
				else if (deleteElement.isReadOnly())
				{
					complainLoudly(((XPDLCollection) getXPDLComponent()).getReadOnlyMessageName(deleteElement));
				}
				else if (!((XPDLCollection) getXPDLComponent()).canRemoveElement(deleteElement))
				{
					complainLoudly(((XPDLCollection) getXPDLComponent()).getInUseMessageName(deleteElement));
				}
				else
				{
					int yn =
						JOptionPane.showConfirmDialog(
							getDialog(),
							sm.getString("MessageDoYouReallyWantToRemoveSelectedItem"),
							deleteElement.getLabel() + " - " + sm.getString("DeletingKey"),
							JOptionPane.YES_NO_OPTION);
					if (yn == JOptionPane.YES_OPTION)
					{
						removeElement(deleteElement);
						((XPDLCollection) getXPDLComponent()).onElementDeleted(deleteElement);
					}
				}
			}
		});
	}
	public XPDLComplexComponent getSelectedElement()
	{
		return ((JavaTablePanel) controlledPanel).getSelectedElement();
	}
	private void addElement(XPDLCollectionComponent e)
	{
		((XPDLCollection) getXPDLComponent()).addComponent(e);
		
		JTable t = ((JavaTablePanel) controlledPanel).getTable();
		DefaultTableModel dtm = (DefaultTableModel) t.getModel();
		Vector v = new Vector(e.findChildrenValues());
		// add object itself to the first column (it is invisible column)
		v.add(0, e);
		dtm.addRow(v);
		try
		{
			t.setRowSelectionInterval(t.getRowCount() - 1, t.getRowCount() - 1);
		}
		catch (Exception ex)
		{
		}
		t.requestFocus();
	}
	private void modifyElement(XPDLComplexComponent e)
	{
		((JavaTablePanel) controlledPanel).modifyElement(e);
	}
	private void removeElement(XPDLComplexComponent e)
	{
		((JavaTablePanel) controlledPanel).removeElement(e);
	}
	/**
		* Pops message into message dialog.
		*/
	public void complainLoudly(String message)
	{
		JOptionPane.showMessageDialog(
			getDialog(),
			sm.getString(message),
			sm.getString("Title"),
			JOptionPane.WARNING_MESSAGE);
	}
}