import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JTree;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;

public class XMLBrowser 
{
  //Component to display the currently open survey
	public JTree xmls;
	private JScrollPane scrollXMLs;
  private Vector orderedNodeList = new Vector();
  private DefaultMutableTreeNode root = new DefaultMutableTreeNode("No Questions Opened");
	
  //Dimentions for each area of the Stimulus and Pole Display
  private Dimension xmlsDimension = new Dimension(375,480);
  
	/**
   * Default Constructor
	 */
	public XMLBrowser() 
	{
	}

  public XMLBrowser(JLayeredPane layers, BevelBorder bevelL)
  {
    //Instantiate objects
    xmls = new JTree(root);
    scrollXMLs = new JScrollPane(xmls);
    
    //********************************************************
    //Include the Canvas in the SRPole and set its layer
    //********************************************************
    
    layers.add(scrollXMLs);
    layers.setLayer(scrollXMLs,2);
    
    //***********************************
    //Setup properties of the SRPole
    //***********************************
    
    xmls.setBackground(Color.white);
    xmls.setSize(xmlsDimension);
    //surveys.setBorder(bevelL);
    xmls.setBounds(5,90,(int)xmlsDimension.getWidth(),(int)xmlsDimension.getHeight());
    xmls.setRootVisible(true);
    xmls.validate();
    scrollXMLs.setBounds(5,90,(int)xmlsDimension.getWidth(),(int)xmlsDimension.getHeight());
    //surveys.setMinimumSize(surveysDimension);
    //surveys.setMaximumSize(surveysDimension);
    //surveys.setPreferredSize(surveysDimension);
    
    //********************************
    //Set the Areas to a visible state
    //********************************
    
    xmls.setVisible(true);
  }

  public XMLBrowser(JLayeredPane layers, BevelBorder bevelL, JTree initialTree)
  {
    //Instantiate objects
    xmls = initialTree;
    scrollXMLs = new JScrollPane(xmls);
    
    //********************************************************
    //Include the Canvas in the SRPole and set its layer
    //********************************************************
    
    layers.add(scrollXMLs);
    layers.setLayer(scrollXMLs,2);
    
    //***********************************
    //Setup properties of the SRPole
    //***********************************
    
    xmls.setBackground(Color.white);
    xmls.setSize(xmlsDimension);
    //surveys.setBorder(bevelL);
    xmls.setBounds(5,90,(int)xmlsDimension.getWidth(),(int)xmlsDimension.getHeight());
    xmls.setRootVisible(true);
    xmls.validate();
    scrollXMLs.setBounds(5,90,(int)xmlsDimension.getWidth(),(int)xmlsDimension.getHeight());
    //surveys.setMinimumSize(surveysDimension);
    //surveys.setMaximumSize(surveysDimension);
    //surveys.setPreferredSize(surveysDimension);
    
    //********************************
    //Set the Areas to a visible state
    //********************************
    
    xmls.setVisible(true);
  }
  
  public void openQuestion(Vector values)
  {
    DefaultMutableTreeNode root2 = null;
    DefaultMutableTreeNode leaf2 = null;
    for(int i = 0; i < values.size(); i++)
    {
      if(i == 0)
      {
        root2 = new DefaultMutableTreeNode((String)(values.elementAt(i)));
        orderedNodeList.add(root2);
      }
      else if( i > 0)
      {
        leaf2 = new DefaultMutableTreeNode((String)(values.elementAt(i)));
        orderedNodeList.add(leaf2);
        root2.add(leaf2);
      }
    }
    //root.add(root2);
    //root = root2;
    xmls = new JTree(root2);
    xmls.updateUI(); //must call this after running this function
  }
  
  public void removeChildren()
  {
    root.removeAllChildren();
    int tempsize = orderedNodeList.size();
    for(int i = 1; i < tempsize; i++)
      orderedNodeList.removeElementAt(orderedNodeList.size()-1);
    xmls.updateUI(); //must call this after running this function
  }
}
