//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
// MvcTreeNodeRec 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;


// Interface für den Aufruf in Double-Click
interface IMouseAdapter {
  void CreateWindow(int i);
}


public class Tree07b extends JFrame implements IMouseAdapter{

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

  int _index;


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

  // Interface-Methode
   public void CreateWindow( int i ) {
    System.out.println("CreateWindow: "+i);
   }

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


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

    treeModel = new DefaultTreeModel(rootNode);
    jTree1 = new javax.swing.JTree(treeModel);  //  kein Root !!!


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

                                          
     MouseListener ml =
         new MyMouseAdapter(this, jTree1);
     jTree1.addMouseListener(ml);


    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
              


  // ----------------------------
  // ----------------------------
  DefaultMutableTreeNode BuildTree() {
    DefaultMutableTreeNode root;
    DefaultMutableTreeNode[]  Knoten = new DefaultMutableTreeNode[3];

    root = createMyNode(  0,"root");  // new DefaultMutableTreeNode("root");

      // liefert einen DefaultMutableTreeNode
    Knoten[0] = createMyNode(  1,"1. Stufe (1)");
    root.add( Knoten[0] );

    Knoten[1] = createMyNode(  2,"AAA");
    Knoten[0].add( Knoten[1] );  // Parameter Vater
    Knoten[1] = createMyNode(  3,"BBB");
    Knoten[0].add( Knoten[1] );  // Parameter Vater


    // ---------

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

    Knoten[1] = createMyNode(  6,"CCC");
    Knoten[0].add( Knoten[1] );  // Parameter Vater
    Knoten[1] = createMyNode(  2,"DDDD");
    Knoten[0].add( Knoten[1] );  // Parameter Vater


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

    Knoten[1] = createMyNode(  6,"EEE");
    Knoten[0].add( Knoten[1] );  // Parameter Vater

      Knoten[2] = createMyNode(  7,"E1E1E1");
      Knoten[1].add( Knoten[2] );  // Parameter Vater
      Knoten[2] = createMyNode(  2,"E2E2E2");
      Knoten[1].add( Knoten[2] );  // Parameter Vater

    Knoten[1] = createMyNode(  11,"FFF");
    Knoten[0].add( Knoten[1] );  // Parameter Vater
      Knoten[2] = createMyNode(  2,"F1F1F1");
      Knoten[1].add( Knoten[2] );  // Parameter Vater
      Knoten[2] = createMyNode(  4,"F2F2F2");
      Knoten[1].add( Knoten[2] );  // Parameter Vater

    return root;
  }  // BuildTree


 DefaultMutableTreeNode createMyNode(int myData,  String nodeString) {
    MyTreeNodeRec myNode;
    DefaultMutableTreeNode Node;

    myNode = new  MyTreeNodeRec(myData, nodeString);
    Node = new DefaultMutableTreeNode(myNode);
    // Node = new DefaultMutableTreeNode(nodeString);
    //Node.setUserObject(myNode);
    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) {
    Tree07b frame = new Tree07b();
    frame.setVisible(true);
 }


}




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

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

    private int _myData;

    public String _nodeString;


   public MyTreeNodeRec(int myData,  String nodeString) {
      _myData = myData;
      _nodeString = nodeString;  // für JTree;
      nr++;
      _id = nr;  // setzt eindeutige Nummer
   }


   public long getID() {
     return _id;
   }


   public int getData() {
    return _myData;
   }

   public void setData(int myData) {
     _myData = myData;
   }

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

}  // MyTreeNodeRec




class MyMouseAdapter extends MouseAdapter {
   private JTree jTree1;
   private IMouseAdapter _parent;

  public MyMouseAdapter(IMouseAdapter parent, JTree jTree1) {
     this.jTree1 = jTree1;
     _parent = parent;
  }

   public void 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) {
             mySingleClick(selRow, selPath);
         }
         else if(e.getClickCount() == 2) {
             myDoubleClick(selRow, selPath);
         }
     }
  }

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

 void  myDoubleClick(int selRow, TreePath selPath) {
    DefaultMutableTreeNode node;
    MyTreeNodeRec myNode;
    node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
    if (node == null) {     return;   }
    else {
      myNode = (MyTreeNodeRec) node.getUserObject();
      System.out.println("myDoubleClick: "+node.toString()+" \n" +myNode.getData() );
      if ( myNode.getData()>2 ) {
	      // Aufruf und Erzeugen eines Dialogfensters
         _parent.CreateWindow( myNode.getData() );
        ((JFrame) _parent).setTitle("Data größer 2");
      }
      else {
        ((JFrame) _parent).setTitle("Tree07.java");
      }
    }
  }

}  // MyMouseAdapter






