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

// Einfaches Beispiel mit DefaultMutableTreeNode
// MvcTreeNodeRec liefert Zusatzinformationen, mit toString
// Analysiert eine Festplattenstruktur


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

import java.awt.event.*;
import com.borland.jbcl.layout.*;

import javax.swing.tree.DefaultMutableTreeNode;

import java.io.*;

import java.util.Vector;

public class FileTree1 extends JFrame {

  BorderLayout borderLayout1 = new BorderLayout();
  JTree jTree1;
  JComboBox liste;


  public FileTree1() {
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    JPanel panel = new JPanel();
    JLabel label1;

    DefaultMutableTreeNode root;
    File[] driveList = File.listRoots();

    panel.setLayout( new FlowLayout() );
    label1 = new JLabel("Laufwerke");
    panel.add(label1);
    liste = new JComboBox(driveList);
    panel.add(liste);

    setSize(400, 300);
    setTitle("JTree 2. Beispiel");
    this.getContentPane().setLayout(borderLayout1);

    root = BuildTree(1);
    jTree1 = new javax.swing.JTree(root);

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

    liste.setSelectedIndex(1);
    BuildTree( liste.getSelectedIndex() );

    liste.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        liste_actionPerformed(e);
      }
    });

  }


  DefaultMutableTreeNode BuildTree(int index) {
    File[] driveList = File.listRoots();
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode[]  Knoten = new DefaultMutableTreeNode[3];

    for (int i=0; i<3; i++) {
      Knoten[i] = new DefaultMutableTreeNode();
    }


    root = new DefaultMutableTreeNode("root");


    Knoten[0] = createMVCNode(  "1.","1. Stufe");
    root.add( Knoten[0] );

    Knoten[1] = createMVCNode(  "A.","AAA");
    Knoten[0].add( Knoten[1] );
    Knoten[1] = createMVCNode(  "B.","BBB");
    Knoten[0].add( Knoten[1] );


    // ---------

    Knoten[0] = new DefaultMutableTreeNode("1. Stufe");
    root.add( Knoten[0] );

    Knoten[1] = new DefaultMutableTreeNode("CCC");
    Knoten[0].add( Knoten[1] );

    Knoten[1] = new DefaultMutableTreeNode("DDD");
    Knoten[0].add( Knoten[1] );

     // Wieviele Laufwerke existieren ?
    /*
   String s;
   s = driveList[1].getAbsolutePath();  // liefert c:\
  String ss[]=   driveList[1].list();
//  File[] dd = driveList[1].isDirectory();
      */
    return root;
  }

  

  //Ü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);
    }
  }

 DefaultMutableTreeNode createMVCNode(String sNodeId,  String nodeString) {
    MvcTreeNodeRec2 myNode;
    DefaultMutableTreeNode Node;

    myNode = new  MvcTreeNodeRec2(sNodeId, nodeString);
    Node = new DefaultMutableTreeNode(myNode);
    //Node.setUserObject(myNode);
    return Node;
 }


  public static void main(String[] args) {
    FileTree1 frame = new FileTree1();
    frame.show();
 }

  void liste_actionPerformed(ActionEvent e) {
    int index;
    index = liste.getSelectedIndex();
    System.out.println(index);
  }


}

// class MvcTreeNodeRec extends javax.swing.JComponent implements IMvcDialogObserver {
// class MvcTreeNodeRec extends javax.swing.JComponent  {
class MvcTreeNodeRec2  {
    private long _id = 0; // Id des Knotens  z.B.

    private String _sNodeId;

    public String _nodeString;
//    DefaultMutableTreeNode _parent;


   public MvcTreeNodeRec2(String sNodeId,  String nodeString) {
      _sNodeId = sNodeId;
      _nodeString = nodeString;  // für JTree;
  //    _parent = parent;

   }

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

}


