/*
Tiamat - a role-playing game framework
ConditionsPanel.java - a panel used to edit a list of conditions.
 
 Copyright  (C) 2006 Hugues Johnson
 
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.tiamat.tools;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * ConditionsPanel - a panel used to edit a list of conditions.
 * @author Hugues Johnson
 */
public class ConditionsPanel extends JPanel{
	/* Determines if a de-serialized file is compatible with this class. See <a href="http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html">Java Object Serialization Specifications</a>. */
	private static final long serialVersionUID=-700815049732609287L;

	private class ConditionsListSelectionListener implements ListSelectionListener{
		private JButton button;
		public ConditionsListSelectionListener(JButton button){ 
			this.button=button;
		}
		public void valueChanged(ListSelectionEvent e){
			JList source=(JList)e.getSource();
			if(source.getSelectedValue()!=null){
				this.button.setEnabled(true);
			}
		}
	}
	
	private class InputTextListener implements DocumentListener{
		private JTextField textField;
		private JButton button;
		private JList list;
		private Color originalColor;
		public InputTextListener(JTextField textField,JButton button,JList list){
			super();
			this.textField=textField;
			this.button=button;
			this.list=list;
			this.originalColor=this.textField.getForeground();
		}
	    public void insertUpdate(DocumentEvent e){ this.onChange(); }
	    public void removeUpdate(DocumentEvent e){ this.onChange(); }
	    public void changedUpdate(DocumentEvent e){ this.onChange(); }
	    private void onChange(){
	    	String text=this.textField.getText();
	    	if(text.length()==0){
	    		this.button.setEnabled(false);
	    	} else if(text.equals("<enter condition>")){
	    		this.button.setEnabled(false);
	    	} else if(((DefaultListModel)this.list.getModel()).contains(text)){
	    		this.button.setEnabled(false);
	    		this.textField.setForeground(Color.RED);
	    	} else{
	    		this.button.setEnabled(true);
	    		this.textField.setForeground(this.originalColor);
	    	}
	    }
	}	
	
	private JTextField inputText;
	private JList conditionsList;
	private JButton removeButton;
	
	public ConditionsPanel(){
		super();
		this.init();
	}
	
	public ConditionsPanel(ArrayList<String> conditions){
		super();
		this.init();
		this.setConditions(conditions);
	}
	
	private void init(){
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
    	this.setBorder(new TitledBorder("Conditions"));
    	//initialize stuff that's needed before actually being added
    	JButton addButton=new JButton();
    	this.removeButton=new JButton();
    	this.conditionsList=new JList();
    	this.conditionsList.setModel(new DefaultListModel());
		//add top panel
		JPanel topPanel=new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel,BoxLayout.X_AXIS));
		topPanel.add(new JLabel("Add Condition: "));
		this.inputText=new JTextField("<enter condition>");
		this.inputText.getDocument().addDocumentListener(new InputTextListener(this.inputText,addButton,this.conditionsList));
		topPanel.add(this.inputText);
		addButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/plus.gif")));
		addButton.setToolTipText("Add");
		addButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onAddCondition();
    		}
    	});
		addButton.setEnabled(false);
		topPanel.add(addButton);
		this.add(topPanel);
		//add list panel
		this.conditionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.conditionsList.addListSelectionListener(new ConditionsListSelectionListener(this.removeButton));
		this.add(new JScrollPane(this.conditionsList));
		//add bottom panel
		JPanel bottomPanel=new JPanel();
		bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		this.removeButton.setIcon(new ImageIcon(this.getClass().getResource("/com/huguesjohnson/tiamat/tools/images/minus.gif")));
		this.removeButton.setToolTipText("Remove");
		this.removeButton.setEnabled(false);
		this.removeButton.addActionListener(new ActionListener(){
    		public void actionPerformed(ActionEvent e){
    			onRemoveCondition();
    		}
    	});
		bottomPanel.add(this.removeButton);
		this.add(bottomPanel);
	}
	
	public void setConditions(ArrayList<String> conditions){
		DefaultListModel model=(DefaultListModel)this.conditionsList.getModel();
		int length=conditions.size();
		for(int index=0;index<length;index++){
			model.addElement(conditions.get(index));
		}
	}

	public ArrayList<String> getConditions(){
		int initialSize=((DefaultListModel)this.conditionsList.getModel()).size();
		ArrayList<String> conditions=new ArrayList<String>(initialSize);
		DefaultListModel model=(DefaultListModel)this.conditionsList.getModel();
		int length=model.getSize();
		for(int index=0;index<length;index++){
			conditions.add((String)model.get(index));
		}
		return(conditions);
	}

	public void onAddCondition(){
		String condition=this.inputText.getText();
		DefaultListModel model=(DefaultListModel)this.conditionsList.getModel();
		model.addElement(condition);
		this.inputText.setText(new String());
		this.inputText.requestFocus();
	}
	
	public void onRemoveCondition(){
		int selectedIndex=this.conditionsList.getSelectedIndex();
		DefaultListModel model=(DefaultListModel)this.conditionsList.getModel();
		model.removeElement(this.conditionsList.getSelectedValue());
		if(selectedIndex>0){
			this.conditionsList.setSelectedIndex(selectedIndex-1);
		} else if(model.size()>0){
			this.conditionsList.setSelectedIndex(0);
		} else{
			this.inputText.requestFocus();
			this.removeButton.setEnabled(false);
		}
	}
}