//Titel:        JTree
//Version:       1.0
//Copyright:     Copyright (c) 2003, 06
//Autor:         Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
//Organisation:  HS Harz
//Beschreibung:  Einführung in JTree


// Einfaches Beispiel mit DefaultMutableTreeNode
// CTreeNodeRec liefert Zusatzinformationen, mit toString
// Klick Ereignis, Single bzw. Double-Click


import java.awt.*;
import javax.swing.*;

import java.awt.event.*;

import javax.swing.tree.DefaultMutableTreeNode;

import javax.swing.tree.*;

import javax.swing.event.*;

import java.io.*;

import java.util.Vector;

public class Tree11 extends JFrame {

  JTree jTree1;
  DefaultMutableTreeNode rootNode;
  DefaultTreeModel treeModel;
  TreePath RootPath;

  int _index;


  public Tree11() {
    setGUI();
    this.setLocation(500,300);
  }

    // wird vom Tree aufgerufen
   public void CreateWindow( String sPath ) {
    File dir = new File( sPath );
    File[] items = dir.listFiles();
    System.out.println("Path: "+sPath);
    for (int i=0; i<items.length; i++) {
      if ( items[i].isFile() ) {
        System.out.println("  "+items[i].getName());
      }
    } 
   }



  private void setGUI()  {
    setSize(400, 300);
    setTitle("JTree 11. Beispiel");
    this.getContentPane().setLayout( new BorderLayout() );


    //---------------------------------------
    rootNode = BuildTree();

    treeModel = new DefaultTreeModel(rootNode);
    jTree1 = new javax.swing.JTree(treeModel);  //  kein Root !!!
   jTree1.setFont( new Font("Arial", Font.BOLD,22)  );
 

    jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    // jTree1.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
   //  SINGLE_TREE_SELECTION, CONTIGUOUS_TREE_SELECTION or DISCONTIGUOUS_TREE_SELECTION.


    jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        jTree1_mouseClicked(e);
      }
    });


    jTree1.setBorder(BorderFactory.createLineBorder(Color.black));
    jTree1.setToolTipText("ToolTipp");
    jTree1.setShowsRootHandles(true);

      // Horizontale Linien
    jTree1.putClientProperty("JTree.lineStyle","Horizontal");
      // Mit Linien zwischen den Knoten
    jTree1.putClientProperty("JTree.lineStyle","Angled");

    this.getContentPane().add(jTree1, BorderLayout.CENTER);

    // Neue Version
    // setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }  // setGUI
              

  void jTree1_mouseClicked(MouseEvent e) {
    int selRow = jTree1.getRowForLocation(e.getX(), e.getY());
    TreePath selPath = jTree1.getPathForLocation(e.getX(), e.getY());
    if(selRow != -1) {
       if(e.getClickCount() == 1) {
           SingleClick(selRow, selPath);
       }
       else if(e.getClickCount() == 2) {
           DoubleClick(selRow, selPath);
      }
    }
  }

  void  SingleClick(int selRow, TreePath selPath) {
    System.out.println("CSingleClick: "+selRow);
  }


 void  DoubleClick(int selRow, TreePath selPath) {
    DefaultMutableTreeNode node;
    CTreeNodeRec CNode;
    node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
    if (node == null) {
       return;
    }
    else {
      CNode = (CTreeNodeRec) node.getUserObject();
      System.out.println("CDoubleClick: "+node.toString()+" \n" +CNode.getData() );
      CreateWindow( CNode.getData() );
    }
  }

  private void parseDisk(DefaultMutableTreeNode parent, String sPath){
    DefaultMutableTreeNode Node;
    File dir = new File( sPath );
      // File[] items = dir.listRoots();
    File[] items = dir.listFiles();
    for (int i=0; i<items.length; i++) {
      if ( items[i].isDirectory() ) {
        Node = createCNode( items[i].toString(),items[i].getName());
        parent.add( Node );
        parseDisk(Node, items[i].toString()+"\\");
      }
    }
  }

  // ----------------------------
  DefaultMutableTreeNode BuildTree() {
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode Kn0, Kn1, Kn2;

    root = createCNode( "C:\\Daten\\","C:\\Daten\\");  // new DefaultMutableTreeNode("root");

    parseDisk(root, "C:\\Daten\\");
      // liefert einen DefaultMutableTreeNode

    return root;
  }  // BuildTree




 DefaultMutableTreeNode createCNode(String sFullPath,  String nodeString) {
    CTreeNodeRec CNode;
    DefaultMutableTreeNode Node;

    CNode = new  CTreeNodeRec(sFullPath, nodeString);
    Node = new DefaultMutableTreeNode(CNode);
    // Alternative:
    // Node = new DefaultMutableTreeNode(nodeString);
    //Node.setUserObject(CNode);
    return Node;
 }


   //Überschreiben, damit das Programm bei Herunterfahren des Systems beendet werden kann
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }


  public static void main(String[] args) {
    Tree11 frame = new Tree11();
    frame.setVisible(true);
 }


}




// Einfache Klasse für jeden Knoten
class CTreeNodeRec  {
  public static long nr=0;

    private long _id = 0; // Id des Knotens  z.B. als ID

    private String _sPath;

    public String _nodeString;


   public CTreeNodeRec(String sPath,  String nodeString) {
      _sPath = sPath;
      _nodeString = nodeString;  // für JTree;
      nr++;
      _id = nr;  // setzt eindeutige Nummer
   }


   public long getID() {
     return _id;
   }


   public String getData() {
    return _sPath;
   }

   public void setData(String sPath) {
     _sPath = sPath;
   }

   // sehr wichtig
   public String toString() {
     return _nodeString;
   }

}  // CTreeNodeRec



