package jas2.tuple;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

import jas2.hist.*;
//import jasext.paw.*;
//import jasext.hippo.*;
import jas2.jds.module.*;
import jas2.util.tree.*;
import jas2.util.JASIcon;

import hepjas.analysis.*;
import hepjas.analysis.partition.*;

public class TupleApp extends JPanel
{
	public static void main(String[] argv)
	{
		TupleApp app = new TupleApp();

		JFrame frame = new JFrame();
		frame.setTitle("Tuple App");
		frame.setContentPane(app);
		frame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e) 
			{
				System.exit(0);
			}
		});
		frame.setJMenuBar(new TupleAppMenuBar());
		frame.pack(); 
		frame.show();
	}
	TupleApp()
	{
		super(new BorderLayout());
		TupleColumn[] columns = null;
		EventSource es = null;
		// Open an n-tuple

	
		/*
		try
		{
		
			
			Vector c = new Vector();
		
			AnnotatedEventSource s = new jasext.paw.PawEventSource("c:\\temp\\pawdemo.hbook");
			TreeItem[] items = s.getItems();
			TreeItem esItem = null;
			for (int i=0; i value1;
			case 2: return x > value1 && x < value2;
			case 3: return x < value1 || x > value2;
		}
		return false;
	}
	static String[] getTypes()
	{
		return cutTypes;
	}
	private static String[] cutTypes = {
		"x < Cut",
		"x > Cut",
		"Cut1 < x < Cut2",
		"x < Cut1 || x> Cut2"
	};
	double value1;
	double value2;
	private String name;
	private int type;
	private TupleColumn column;
}
class CutPanel extends JPanel
{
	CutPanel(final Cut cut, final TuplePanel control)
	{
		super(new BorderLayout());

		this.cut = cut;
		this.control = control;
		
		JPanel top = new JPanel();
		top.add(new JLabel("Cut: "+cut.getName()));
		final JComboBox type = new JComboBox(Cut.getTypes());
		type.setSelectedIndex(cut.getType());
		top.add(type);
		add(top,BorderLayout.NORTH);
		type.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				cut.setType(type.getSelectedIndex());
				control.go();
			}
		});


		JASHist plot = new JASHist();
		Rebinnable1DHistogramData source = cut.getColumn().getDataSource();

		Xmin = source.getMin();
		Xmax = source.getMax();
		
		plot.addData(source).show(true);
		plot.setPreferredSize(new Dimension(200,200));
		plot.setTitle(source.getTitle());
		add(plot,BorderLayout.CENTER);

		final JSlider slider1 = new JSlider(model1);
		final JSlider slider2 = new JSlider(model2);
		final JPanel sp1 = new JPanel(new BorderLayout());
		final JPanel sp2 = new JPanel(new BorderLayout());
		label1 = new JTextField(String.valueOf(cut.value1),6);
		label2 = new JTextField(String.valueOf(cut.value2),6);		
		
		sp1.add(slider1,BorderLayout.CENTER);
		sp2.add(slider2,BorderLayout.CENTER);
		sp1.add(label1,BorderLayout.EAST);
		sp2.add(label2,BorderLayout.EAST);

		JPanel sliders = new JPanel(new BorderLayout());
		sliders.add(sp1,BorderLayout.NORTH);
		sliders.add(sp2,BorderLayout.SOUTH);

		JPanel bottom = new JPanel();
		final JToggleButton lock = new JToggleButton();
		lock.setSelectedIcon(JASIcon.create(this,"lock.gif"));
		lock.setIcon(JASIcon.create(this,"unlock.gif"));
		lock.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				locked = lock.isSelected();
				delta = cut.value2-cut.value1;
			}
		});
		bottom.add(lock);
		bottom.add(sliders);
		add(bottom,BorderLayout.SOUTH);
	}
	private Cut cut;
	private MinSliderModel model1 = new MinSliderModel();
	private MaxSliderModel model2 = new MaxSliderModel();
	private boolean locked = false;
	private double delta;
	private double Xmin;
	private double Xmax;
	private TuplePanel control;
	private JTextField label1;
	private JTextField label2;

	class MinSliderModel extends DefaultBoundedRangeModel
	{
		MinSliderModel()
		{
			super(0,0,0,1000);
		}
		public int getValue()
		{
			return (int) (1000*(cut.value1-Xmin)/(Xmax-Xmin));
		}
		public void setValue(int newValue)
		{
			setValue(Xmin + (newValue/1000.)*(Xmax-Xmin));
			control.go();
		}
		void setValue(double newValue)
		{
			if (newValue == cut.value1) return;
			cut.value1 = newValue;
			if (locked)
			{
				model2.setValue(Math.min(Xmax,cut.value1+delta));
			}
			else
			{
				if (cut.value1 > cut.value2) model2.setValue(cut.value1);
			}
			String s = String.valueOf(cut.value1);
			s = s.substring(0,Math.min(6,s.length()));
			label1.setText(s);
			fireStateChanged();
		}
	}
	class MaxSliderModel extends DefaultBoundedRangeModel
	{
		MaxSliderModel()
		{
			super(0,0,0,1000);
		}
		public int getValue()
		{
			return (int) (1000*(cut.value2-Xmin)/(Xmax-Xmin));
		}
		public void setValue(int newValue)
		{
			setValue(Xmin + (newValue/1000.)*(Xmax-Xmin));
			control.go();
		}
		void setValue(double newValue)
		{
			if (newValue == cut.value2) return;
			cut.value2 = newValue;
			if (locked)
			{
				model1.setValue(Math.max(Xmin,cut.value2-delta));
			}
			else
			{
				if (cut.value1 > cut.value2) model1.setValue(cut.value2);
			}
			String s = String.valueOf(cut.value2);
			s = s.substring(0,Math.min(6,s.length()));
			label2.setText(s);
			fireStateChanged();
		}
	}
}
class CutModel extends DefaultTableModel implements ListDataListener
{
	CutModel()
	{
		cutList.addListDataListener(this);
	}
	public Class getColumnClass(int columnIndex)
	{
		return columnIndex == 1 ? Boolean.class : String.class;
	}
    public int getRowCount()
	{
		return cutList.size();
	}
	public int getColumnCount()
	{
		return columns.length;
	}
	public Object getValueAt(int row, int column)
	{
		if (column == 0) return cutList.elementAt(row);
		else return bits.get(row) ? Boolean.TRUE : Boolean.FALSE;
	}
	public boolean isCellEditable(int row, int column)
	{
		return (column == 1);
	}
	public String getColumnName(int column)
	{
		return columns[column];
	}
	public void setValueAt(Object value, int row, int column)
	{
		System.out.println("value="+value+" row="+row+" col="+column);
		if (column == 1) 
		{
			if (((Boolean) value).booleanValue()) bits.set(row);
			else bits.clear(row);
		}
	}
	// ListDataListener methods
	public void intervalAdded(ListDataEvent e)
	{
		for (int i=e.getIndex0(); i<=e.getIndex1(); i++) bits.set(i);
		fireTableRowsInserted(e.getIndex0(), e.getIndex1());
	}
	public void intervalRemoved(ListDataEvent e)
	{
		fireTableRowsDeleted(e.getIndex0(), e.getIndex1()) ;
	}
	public void contentsChanged(ListDataEvent e)
	{
		fireTableDataChanged();
	}
	boolean isEnabled(int i)
	{
		return bits.get(i);
	}
	Cut getCut(int i)
	{
		return (Cut) cutList.elementAt(i);
	}
	private static String[] columns = {"Cut","Enabled"}; 
	private static CutList cutList = CutList.instance();
	private BitSet bits = new BitSet();
}
class CutList extends DefaultListModel
{
	private CutList()
	{
	}
	static CutList instance()
	{
		return theCutList;
	}
	static Cut getCutAt(int index)
	{
		return (Cut) theCutList.elementAt(index);
	}
	private static CutList theCutList = new CutList();
}
class ComboCellEditor extends DefaultCellEditor
{
	ComboCellEditor(Object[] labels)
	{
		super(new JComboBox());
		combo = (JComboBox) editorComponent;
		model = new FilterListModel(labels);
	}
    public Component getTableCellEditorComponent(
		                 JTable table, Object value,
						 boolean isSelected,
						 int row, int column) {

		TupleModel m = (TupleModel) table.getModel();
		
		int index = value==null ? 0 : 1+((Integer) value).intValue();
		model.setBits(m.getBits(row,column));
		model.setSelectedIndex(index);
		combo.setModel(model);
		
		return combo;
    }
    public Object getCellEditorValue() 
	{
		System.out.println("getCellEditorValue="+combo.getSelectedIndex());
        return new Integer(model.getSelectedIndex()-1);
    }
	private JComboBox combo;
	private FilterListModel model;
}
class ComboCellRenderer implements TableCellRenderer
{
	ComboCellRenderer(Object[] labels)
	{
		combo = new JComboBox(labels);
	}
	public Component getTableCellRendererComponent(JTable table,
		                                       Object value,
                                               boolean isSelected,
                                               boolean hasFocus,
                                               int row,
                                               int column)
	{
		int index = value==null ? 0 : 1+((Integer) value).intValue();
		combo.setSelectedIndex(index);
		return combo;
	}
	private JComboBox combo;
}
class Hist1DModel extends TupleModel
{
	private Histogram m_hist;

	Histogram getHistogram()
	{
		return m_hist;
	}
	protected Abstract1DPartition getPartition(TupleColumn c)
	{
		int type = c.getType();
		double min = c.getDataSource().getMin();
		double max = c.getDataSource().getMax();

		if      (type == TupleColumn.INTEGER) return new SimpleIntPartition((int) min, (int) max);
		else if (type == TupleColumn.DOUBLE ) return new SimplePartition(min,max);
		else if (type == TupleColumn.STRING ) return new StringPartition();
		else if (type == TupleColumn.DATE   ) return new SimpleDatePartition(new Date((long) (min*1000)),new Date((long) (max*1000))); 
		else throw new RuntimeException("Bugcheck");
	}
	EventAnalyzer getEventAnalyzer(CutModel cutModel)
	{
		m_hist = new Histogram("test");
		m_hist.setPartition(getPartition(getColumn(0)));
		return new Analyzer(getValue(0),getValue(1),getColumn(0).getType(),m_hist,cutModel);
	}
	class Analyzer extends EventAnalyzer
	{
		Analyzer(int dataCol, int weightCol, int type, Histogram hist, CutModel model)
		{
			this.dataCol = dataCol;
			this.weightCol = weightCol;
			this.type = type;
			this.hist = hist;
			this.cutModel = model;
		}
		public void processEvent(EventData d)
		{
			NTupleEventData data = (NTupleEventData) d;
			
			int rows = cutModel.getRowCount();
			for (int i=0; i 

Ads help maintain this website.