package parser.ServletParser.library;

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.awt.Component;
import java.awt.event.*;
import java.util.EventObject;

import java.awt.Dimension;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;

/**
 * This class shows how to create a simple JTreeTable component, 
 * by using a JTree as a renderer (and editor) for the cells in a 
 * particular column in the JTable.  
 */


public class JTreeTable extends JTable {
    protected TreeTableCellRenderer tree;
	protected ChoiceRenderer checkBox;

	public DefaultTreeCellRenderer getTreeCellRenderer() {
		return (DefaultTreeCellRenderer)tree.getCellRenderer();
	}

    public JTreeTable(TreeTableModel treeTableModel) {
	super();

	// Create the tree. It will be used as a renderer and editor. 
	tree = new TreeTableCellRenderer(treeTableModel); 

	// Install a tableModel representing the visible rows in the tree. 
	super.setModel(new TreeTableModelAdapter(treeTableModel, tree));

	// Force the JTable and JTree to share their row selection models. 
	tree.setSelectionModel(new DefaultTreeSelectionModel() { 
	    // Extend the implementation of the constructor, as if: 
	 /* public this() */ {
		setSelectionModel(listSelectionModel); 
	    } 
	}); 
	// Make the tree and table row heights the same. 
	tree.setRowHeight(getRowHeight());

	checkBox = new ChoiceRenderer();
	setDefaultRenderer(Boolean.class, checkBox); 
	setDefaultEditor(Boolean.class, new ChoiceEditor());  
	// Install the tree editor renderer and editor. 
	setDefaultRenderer(TreeTableModel.class, tree); 
	setDefaultEditor(TreeTableModel.class, new TreeTableCellEditor()); 
	 

	setShowGrid(true);
	setIntercellSpacing(new Dimension(0, 0)); 	        
    }

    /* Workaround for BasicTableUI anomaly. Make sure the UI never tries to 
     * paint the editor. The UI currently uses different techniques to 
     * paint the renderers and editors and overriding setBounds() below 
     * is not the right thing to do for an editor. Returning -1 for the 
     * editing row in this case, ensures the editor is never painted. 
     */
    public int getEditingRow() {
        return (getColumnClass(editingColumn) == TreeTableModel.class) ? -1 : editingRow;  
    }

    // 
    // The renderer used to display the tree nodes, a JTree.  
    //

    public class TreeTableCellRenderer extends JTree implements TableCellRenderer {

	protected int visibleRow;
   
	public TreeTableCellRenderer(TreeModel model) { 
	    super(model); 

		((DefaultTreeCellRenderer)super.getCellRenderer()).setLeafIcon(null);
		((DefaultTreeCellRenderer)super.getCellRenderer()).setClosedIcon(null);
		((DefaultTreeCellRenderer)super.getCellRenderer()).setOpenIcon(null);
	}

	public void setBounds(int x, int y, int w, int h) {
	    super.setBounds(x, 0, w, JTreeTable.this.getHeight());
	}

	public void paint(Graphics g) {
	    g.translate(0, -visibleRow * getRowHeight());
	    super.paint(g);

		Rectangle c = JTreeTable.this.getCellRect(0,0,false);

		Rectangle r = super.getBounds();
		int startY = -1;
		int height = JTreeTable.this.getRowHeight();
		int n = getRowCount();
		g.setColor(JTreeTable.this.getGridColor());
		for (int i = 0; i <= n; i++) {
			g.drawLine(0,startY,c.width,startY);
			startY = startY + height;
		}
		g.drawLine(c.width-1,0,c.width-1,n*c.height);
	}

	public Component getTableCellRendererComponent(JTable table,
						       Object value,
						       boolean isSelected,
						       boolean hasFocus,
						       int row, int column) {
	    if(isSelected)
		setBackground(table.getSelectionBackground());
	    else
		setBackground(table.getBackground());
       
	    visibleRow = row;
	    return this;
	}
    }

	// 
    // The editor used to interact with tree nodes, a JTree.  
    //
    public class TreeTableCellEditor extends AbstractCellEditor implements TableCellEditor {
	public Component getTableCellEditorComponent(JTable table, Object value,
						     boolean isSelected, int r, int c) {
	    return tree;
	}
    }

	public class ChoiceRenderer extends JCheckBox implements TableCellRenderer {
	public ChoiceRenderer() {
		super();
		setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	}
	public Component getTableCellRendererComponent(JTable table,
		Object value,boolean isSelected,boolean hasFocus,int row,int column) {
	    if(isSelected)
		setBackground(table.getSelectionBackground());
	    else
		setBackground(table.getBackground());

    	boolean checked = ((Boolean)value).booleanValue();
		setSelected(checked);
		return this;
	}
	}

	public class ChoiceEditor extends DefaultCellEditor implements TableCellEditor {
	public ChoiceEditor() {
		super(new JCheckBox());
		((JCheckBox)editorComponent).setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
	}
    public Object getCellEditorValue() { 
		return new Boolean(((JCheckBox)editorComponent).isSelected());
	}
	public Component getTableCellEditorComponent(JTable table, Object value,
						     boolean isSelected, int row, int column) {
		TreeTableModelAdapter adapter = (TreeTableModelAdapter)table.getModel();
		adapter.fireTableDataChanged(); 
	    return editorComponent;
	}
	}
}
