package com.k_int.AdminApp.gui.RecordView;

/*
 * @(#)AbstractTreeTableModel.java	1.2 98/10/27
 *
 * Copyright 1997, 1998 by Sun Microsystems, Inc.,
 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of Sun Microsystems, Inc. ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with Sun.
 */

import javax.swing.tree.*;
import javax.swing.event.*;
import org.apache.commons.jxpath.JXPathContext;
import com.k_int.AdminApp.widgets.TreeTable.*;
import java.util.Collection;
import com.k_int.AdminApp.gui.AdminControllerComponent;
import java.util.logging.*;
 
/**
 * @version 1.2 10/27/98
 * An abstract implementation of the TreeTableModel interface, handling the list 
 * of listeners. 
 * @author Philip Milne
 */

public class TreeComponentModel implements TreeTableModel, RecordModelListener {

    private static Logger cat = Logger.getLogger("com.k_int.AdminApp.gui.RecordView.TreeComponentModel");
    protected boolean editable;
    protected Object root;     
    protected Object record;     
    protected EventListenerList listenerList = new EventListenerList();

    private String attr_name_of_root;
    private String get_child_collection_method;
    private String[] col_heads;
    private String[] col_get_method_names;
    private com.k_int.AdminApp.gui.AdminControllerComponent controller;
  
    public TreeComponentModel(String attr_name_of_root,
                              String get_child_collection_method,
                              String[] col_heads,
                              String[] col_get_method_names) {
      cat.fine("new TreeComponentModel");

      this.attr_name_of_root = attr_name_of_root;
      this.get_child_collection_method = get_child_collection_method;
      this.col_heads = col_heads;
      this.col_get_method_names = col_get_method_names;
    }

    //
    // Default implmentations for methods in the TreeModel interface. 
    //

    public Object getRoot() {
        return root;
    }

    public boolean isLeaf(Object node) {
        return getChildCount(node) == 0; 
    }

    public void valueForPathChanged(TreePath path, Object newValue) {}

    // This is not called in the JTree's default mode: use a naive implementation. 
    public int getIndexOfChild(Object parent, Object child) {
        for (int i = 0; i < getChildCount(parent); i++) {
	    if (getChild(parent, i).equals(child)) { 
	        return i; 
	    }
        }
	return -1; 
    }

    public void addTreeModelListener(TreeModelListener l) {
        listenerList.add(TreeModelListener.class, l);
    }

    public void removeTreeModelListener(TreeModelListener l) {
        listenerList.remove(TreeModelListener.class, l);
    }

    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeNodesChanged(Object source, Object[] path, 
                                        int[] childIndices, 
                                        Object[] children) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path, childIndices, children);
                ((TreeModelListener)listeners[i+1]).treeNodesChanged(e);
            }          
        }
    }

    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeNodesInserted(Object source, Object[] path, 
                                        int[] childIndices, 
                                        Object[] children) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path, 
                                           childIndices, children);
                ((TreeModelListener)listeners[i+1]).treeNodesInserted(e);
            }          
        }
    }

    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeNodesRemoved(Object source, Object[] path, 
                                        int[] childIndices, 
                                        Object[] children) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path, 
                                           childIndices, children);
                ((TreeModelListener)listeners[i+1]).treeNodesRemoved(e);
            }          
        }
    }

    /*
     * Notify all listeners that have registered interest for
     * notification on this event type.  The event instance 
     * is lazily created using the parameters passed into 
     * the fire method.
     * @see EventListenerList
     */
    protected void fireTreeStructureChanged(Object source, Object[] path, 
                                        int[] childIndices, 
                                        Object[] children) {
        // Guaranteed to return a non-null array
        Object[] listeners = listenerList.getListenerList();
        TreeModelEvent e = null;
        // Process the listeners last to first, notifying
        // those that are interested in this event
        for (int i = listeners.length-2; i>=0; i-=2) {
            if (listeners[i]==TreeModelListener.class) {
                // Lazily create the event:
                if (e == null)
                    e = new TreeModelEvent(source, path, childIndices, children);
                ((TreeModelListener)listeners[i+1]).treeStructureChanged(e);
            }          
        }
    }

    //
    // Default impelmentations for methods in the TreeTableModel interface. 
    //

    public Class getColumnClass(int column) 
    { 
      if ( column == 0 )
        return TreeTableModel.class; 
      else
        return Object.class; 
    }

   /** By default, make the column with the Tree in it the only editable one. 
    *  Making this column editable causes the JTable to forward mouse 
    *  and keyboard events in the Tree column to the underlying JTree. 
    */ 
    public boolean isCellEditable(Object node, int column) { 
         return getColumnClass(column) == TreeTableModel.class; 
    }

  public void setValueAt(Object aValue, Object node, int column) {
    cat.fine("No impl for set value");
  }


    // Left to be implemented in the subclass:

  public Object getChild(Object parent, int index)
  {
    cat.fine("tree get child");
    Collection c = (Collection) JXPathContext.newContext(parent).getValue(get_child_collection_method);
    return (c.toArray())[index];
  }

  public int getChildCount(Object parent) 
  {
    cat.fine("tree get child count for node");
    Collection c = (Collection) JXPathContext.newContext(parent).getValue(get_child_collection_method);
    return c.size();
  }

  public int getColumnCount() 
  {
    cat.fine("tree get col count");
    return col_heads.length+1;
  }

  public String getColumnName(Object node, int column)  
  {
   cat.fine("tree get col name "+column);
    if ( column == 0 )
      return "";
    else
      return col_heads[column-1];
  }

  public String getColumnName(int column)
  {
    cat.fine("tree get col name "+column);
    if ( column == 0 )
      return "";
    else
      return col_heads[column-1];
  }

  public Object getValueAt(Object node, int column) 
  {
    cat.fine("tree get val for col "+column);
    if ( column == 0 )
      return "";
    else
      return JXPathContext.newContext(node).getValue(col_get_method_names[column-1]);
  }

  public void recordChanged(Object record, boolean editable)
  {
    cat.fine("Controller tells us record has changed for tree view");

    this.record = record;
    this.editable = editable;
    root = JXPathContext.newContext(record).getValue(attr_name_of_root);

    // According to http://www.chka.de/swing/tree/TreeModel-dynamic.html we need to just specify the new root Vex Thal
    fireTreeStructureChanged(this,
                         new Object[] { root },  // Parent
                         new int[] { }, // int[] childIndices,
                         new Object[] { } ); // children

   
  }
  

  public void synchronizeViewToModel()
  {
      cat.fine("Does nothing");
  }


  public void setController(AdminControllerComponent c)
  {
    this.controller = (AbstractRecordController)c;
  }
                                                                                                                                        

}
