/* Autor:           Dipl.-Inf., Dipl.-Ing. (FH) Michael Wilhelm
   Firma:           HS Harz, FB AI
   Version:         1.1
   Ersterstellung:  05.09.2007
   Letzte Änderung: 12.11.2010
*/

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

import java.lang.reflect.*;

// RTTI: Run Time Type Identification und Reflection
// Ausgabe der Attribute und Methoden von MyLabel
// Hier alle die Methoden von MyJLabel bis Object 

public class rtti02 extends JFrame {
     JTextArea _edit;


  //Frame konstruieren
  public rtti02() {
    setSize(800, 600);
    setTitle("Beispiel RTTI 2, von MyJLabel bis Object");
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    setGUI();
  }

  void setGUI() {
     JButton BnAction;

    this.getContentPane().setLayout ( new BorderLayout() );

    _edit = new JTextArea("");
    _edit.setFont( new Font("Arial", Font.BOLD,24));
    BnAction = new JButton( "Ok" );
    BnAction.setFont( new Font("Arial", Font.BOLD,32));

    JScrollPane scrollpane = new JScrollPane(_edit);
    this.getContentPane().add(scrollpane, BorderLayout.CENTER);
    JPanel panel = new JPanel();
    panel.setLayout( new FlowLayout() );
    panel.add(BnAction);
    this.getContentPane().add(panel, BorderLayout.SOUTH);


    BnAction.addActionListener(new java.awt.event.ActionListener() {
       public void actionPerformed(ActionEvent e) {
        BnAction_Click();
       }
    });

  } // setGUI

  private String printClass(Class c, String s, boolean bAll) {
     int n, i, j1, j2, j1Neu, jKlammer, retcode, fromIndex;
     Method[] m;
     Constructor[] ctor;
     String sName, sBez, sBez2, sNeu, sMethod;
     sName = c.getName();
       // Problem: javax.swing.JLabel
     j2 = sName.lastIndexOf(46);  // Punkt
     if (j2>=0) {
       n = sName.length();
       sName = sName.substring(j2+1,n); // public void javax.swing.JLabel.setText
     }

     s += "Classname: "+c.getName();
     s += "\n" + "Ist ein Interface: "+c.isInterface();
     s += "\n\n";

     ctor = c.getConstructors();
     s += "Konstruktoren:" ;
     for (i=0; i<ctor.length; i++) {
       s += "\n" + ctor[i].toString();
     }
     s += "\n\n" ;
     s += "Methoden:" ;
     m = c.getMethods();
     // public void javax.swing.JLabel.setText(java.lang.String)
     // 1. Suche Blank nach void
     // 2. Suche Klammer 40=(
     // 3. Ausschneiden und Vergleichen
     for (i=0; i<m.length; i++) {
     System.out.println(i);
     if (i==7) {
       System.out.println(i);
     }
       sBez = m[i].toString();
         // Problem, wenn throws Exception
       j2 = sBez.indexOf(40);  // (
       sBez2 = sBez.substring(0,j2); // public void javax.swing.JLabel.setText
         // nun ist die Leerstelle weg
       j1 = sBez2.lastIndexOf(32);  // Blank
       n = sBez2.length();
       sMethod = sBez2.substring(j1+1,n); // public void javax.swing.JLabel.setText
        // Nun hat man javax.swing.JLabel.setText
        // Ausschneiden der Methoden
       j2 = sMethod.lastIndexOf(46);  // Punkt vor der Methode
       if (j2==-1) {
         System.out.println("Error");
       }
       sMethod = sMethod.substring(0,j2); // javax.swing.JLabel

       j2 = sMethod.lastIndexOf(46);  // Punkt vor der Klasse
       if (j2==-1) {
         System.out.println("Error");
       }
       n = sMethod.length();
         // Anfangs- und EndIndex
       sMethod = sMethod.substring(j2+1,n); // javax.swing.JLabel

       if (sMethod.equals(sName) )
         s += "\n" + m[i].toString();
       else {
         if (bAll) {
           s += "\n" + m[i].toString();
         }
       }
     }
     s += "\n\n" ;

     return s;
  }

  private String Analyze(String sKlasse, String s) {
     Class c = null;
     Class cy = null;
     //JLabel lab = new JLabel("text");
     try {
       c = Class.forName( sKlasse );
       s = printClass(c,s,false);

       cy = c.getSuperclass();
       if (cy==null) return s;
       s = Analyze( cy.getName() , s);
       return s;

     }
     catch ( ClassNotFoundException e) {
       System.out.println("Error3");
       return "";
     }

  }

  private void BnAction_Click() {
    String s="";
    s = Analyze("myJLabel", s);
    _edit.setText(s);
  }


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


}

class myJLabel  extends JLabel {
   double _d=0.0;

   public myJLabel(String Text) {
    super(Text);
   }

   public myJLabel(double d) {
    super( Double.toString(d) );
    _d = d;
   }

   public double getValue(){
     return _d;
   }

   public void setValue(double d){
    _d = d;
    setText( Double.toString(d) );
   }

}
