//*********************************************************************************
// 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 enum1 = rootNode.preorderEnumeration();
    DefaultMutableTreeNode curNode;
    int totalCol=rootNode.getDepth()+1; // nb total de colonnes dans le tableau

    out.write("\n");
    for(;enum1.hasMoreElements();){
      out.write("");
      curNode=(DefaultMutableTreeNode)enum1.nextElement();
      //Iteration sur les ancetres pour les petites lignes
      TreeNode[] ancestorNodes=curNode.getPath();
      int l = ancestorNodes.length;
      for(int i=0; i");
        DefaultMutableTreeNode tempNode = (DefaultMutableTreeNode)ancestorNodes[i];
        if(i==l-2){		//niveau de parent de curNode
          if(tempNode.getLastChild()==curNode)
            out.write("");
          else
            out.write("");
        }else{		//niveaux de grand-parents et plus de curNode
          DefaultMutableTreeNode tempNode2 = (DefaultMutableTreeNode)ancestorNodes[i+1];
          if(tempNode2==tempNode.getLastChild())
            out.write("");
          else
            out.write("");
        }
      out.write("");
      }//fin iteration sur les ancetres

      //Dessin du noeud (icone et texte)
      int colspan = totalCol-curNode.getLevel();
      out.write("");
      out.write("\n");

    }// fin iteation sur tous les noeuds (boucle enum1)
    out.write("
"); if(curNode.isLeaf()) out.write(""); else out.write(""); out.write("" + curNode.toString() + ""); out.write("
\n"); out.close(); }catch(IOException ioe){ System.out.println("IOException"); } }// fin JTree2Table } //fin public class UtilTree

Ads help maintain this website.