JEphem Informatic Trail tig source code UtilTree.java
//*********************************************************************************
// class tig.tree.UtilTree
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************
package tig.tree;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.io.*;
import java.util.*;

/******************************************************************************
Class containing utility methods to manipulate trees.
@author Thierry Graff
@history xx xxx 2000 : Creation
@todo end jTreeToTable
*********************************************************************************/
public class UtilTree{

  //*******************************************************************
  //*********** static fields *****************************************
  //*******************************************************************
  // TWO FILES NOT USED FOR THE MOMENT
  /** Represents the usual way to layout a tree */
  private static final int VERTICTAL_LAYOUT = 1;
  /** Represents the "natural" (like in nature) way to layout a tree - the root in top and leaves below.*/
  private static final int HORIZONTAL_CENTERED_LAYOUT = 2;

  //**************************************************************************
  //**************************** Methods *************************************
  //**************************************************************************


  //***************** JTree2Table() *************************
  /** Writes the HTML code of a table representing the tree open.
  @param rootNode Root of the tree used to generate the table.
  @param output File in which the table will be written.
  **********************************************************/
  public static void JTree2Table(DefaultMutableTreeNode rootNode, File output){
    try{
    FileWriter out=new FileWriter(output);
    Enumeration enum = rootNode.preorderEnumeration();
    DefaultMutableTreeNode curNode;
    int totalCol=rootNode.getDepth()+1; // nb total de colonnes dans le tableau

    out.write("<TABLE CELLSPACING=\"0\" CELLPADDING=\"0\">\n");
    for(;enum.hasMoreElements();){
      out.write("<TR>");
      curNode=(DefaultMutableTreeNode)enum.nextElement();
      //Itération sur les ancêtres pour les petites lignes
      TreeNode[] ancestorNodes=curNode.getPath();
      int l = ancestorNodes.length;
      for(int i=0; i<l-1; i++){
        out.write("<TD VALIGN=\"middle\">");
        DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode)ancestorNodes[i];
        if(i==l-2){		//niveau de parent de curNode
          if(tempNode.getLastChild()==curNode)
            out.write("<IMG SRC=\"images_tree/lastnode.gif\" BORDER=\"0\">");
          else
            out.write("<IMG SRC=\"images_tree/node.gif\" BORDER=\"0\">");
        }else{		//niveaux de grand-parents et plus de curNode
          DefaultMutableTreeNode tempNode2 = (DefaultMutableTreeNode)ancestorNodes[i+1];
          if(tempNode2==tempNode.getLastChild())
            out.write("<IMG SRC=\"images_tree/blank.gif\" BORDER=\"0\">");
          else
            out.write("<IMG SRC=\"images_tree/vertline.gif\" BORDER=\"0\">");
        }
      out.write("</TD>");
      }//fin itération sur les ancêtres

      //Dessin du noeud (icône et texte)
      int colspan = totalCol-curNode.getLevel();
      out.write("<TD NOWRAP VALIGN=\"middle\" COLSPAN=\"" + colspan +"\">");
      if(curNode.isLeaf())
        out.write("<IMG SRC=\"images_tree/dochtm.gif\" BORDER=\"0\" ALIGN=\"middle\">");
      else
        out.write("<IMG SRC=\"images_tree/folderclosed.gif\" BORDER=\"0\" ALIGN=\"middle\">");
      out.write("<FONT SIZE=\"2\">" + curNode.toString() + "</FONT>");
      out.write("</TD>");
      out.write("</TR>\n");

    }// fin itération sur tous les noeuds (boucle enum)
    out.write("</TABLE>\n");

    out.close();
    }catch(IOException ioe){
      System.out.println("IOException");
    }
  }// fin JTree2Table

} //fin public class UtilTree