/*************************************************************************************

This file is part of FragMend.

Written by Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.

Copyright (c) 2006, Florian Buchholz, Glenn Henderson, David Horvath, and Jeff Jones.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.     
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.       
    * Neither the name of Florian Buchholz, Glenn Henderson, David Horvath, Jeff
      Jones, nor the names of its contributors may be used to endorse or promote
      products derived from this software without specific prior written
      permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*************************************************************************************/

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.Component;
import java.awt.GridLayout;

import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class FilterItemDisplay extends JPanel implements ItemListener {

	public static final int TYPE_TRUE_FALSE = 0;
	public static final int TYPE_INTEGER = 1;
	public static final int TYPE_STRING = 2;
	public static final int TYPE_RANGE = 3;
	
	private String field_name;
	private int field_id;
	private int type;
	
	private JCheckBox active;
	private JPanel data_panel;
	private ButtonGroup true_false;
	private JComboBox operations;
	private JTextField value;
	private JTextField value2;
	
	public FilterItemDisplay(String name, int id, int type) {
		
		super(new GridLayout(0,2));
		field_name = name;
		field_id = id;
		this.type = type;
		
		active = new JCheckBox(field_name);
		active.addItemListener(this);
		
		operations = new JComboBox();
		operations.setEnabled(false);
		value = new JTextField();
		value.setEnabled(false);
		value2 = null;
		
		this.add(active);
		
		data_panel = new JPanel(new GridLayout(0,2));
		
		switch (type) {
			
			case TYPE_TRUE_FALSE:
				JRadioButton true_val = new JRadioButton("True");
				true_val.setActionCommand("true");
				true_val.setSelected(true);
				true_val.setEnabled(false);
				JRadioButton false_val = new JRadioButton("False");
				false_val.setActionCommand("false");
				false_val.setEnabled(false);
				true_false = new ButtonGroup();
				true_false.add(true_val);
				true_false.add(false_val);
				data_panel.add(true_val);
				data_panel.add(false_val);
				break;
			case TYPE_INTEGER:
				operations.addItem("equals");
				operations.addItem("less than");
				operations.addItem("greater than");
				data_panel.add(operations);
				data_panel.add(value);
				break;
			case TYPE_STRING:
				operations.addItem("matches");
				operations.addItem("equals");
				data_panel.add(operations);
				data_panel.add(value);
				break;
			case TYPE_RANGE:
				value2 = new JTextField();
				value2.setEnabled(false);
				data_panel.add(value);
				data_panel.add(new JLabel(" to "));
				data_panel.add(value2);
				break;
		}
					
		this.add(data_panel);
		data_panel.setEnabled(false);
	}

	public void itemStateChanged(ItemEvent e) {

		Object source = e.getItemSelectable();

		if (source == active) {
			if (e.getStateChange() == ItemEvent.DESELECTED) {
				Component[] comps = data_panel.getComponents();
				for (int i = 0; i < comps.length; i++)
					comps[i].setEnabled(false);
			}
			else {
				Component[] comps = data_panel.getComponents();
				for (int i = 0; i < comps.length; i++)
					comps[i].setEnabled(true);
			}
		}
	}
	
	public boolean isActive() {
		
		return active.isSelected();
		
	}
	
	public FilterItem getFilterItem() {
		
		int op;
		
		switch (type) {
			case TYPE_TRUE_FALSE:
				boolean val = true_false.getSelection().getActionCommand().equals("true");
				return new FilterItem(field_id, FilterItem.OP_EQUALS, new Boolean(val), null);
			case TYPE_INTEGER:
				String selection = (String)operations.getSelectedItem();
				if (selection.equals("equals"))
					op = FilterItem.OP_EQUALS;
				else if (selection.equals("less than"))
					op = FilterItem.OP_SMALLER;
				else
					op = FilterItem.OP_GREATER;
				return new FilterItem(field_id, op, new Integer(value.getText()), null);
			case TYPE_STRING:
				selection = (String)operations.getSelectedItem();
				if (selection.equals("equals"))
					op = FilterItem.OP_EQUALS;
				else
					op = FilterItem.OP_MATCHES;
				return new FilterItem(field_id, op, value.getText(), null);
			case TYPE_RANGE:
				return new FilterItem(field_id, FilterItem.OP_RANGE, new Integer(value.getText()),
							new Integer(value2.getText()));
		}
		
		return null;
	}
}

