/* 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


public class rtti01 extends JFrame {
     JTextArea _edit;


  //Frame konstruieren
  public rtti01() {
    setSize(800, 600);
    setTitle("Beispiel RTTI 1");
    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( "Aktion" );
    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 i;
     Method[] m;
     Constructor[] ctor;
     String name;
     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();
     for (i=0; i<m.length; i++) {
       name = m[i].toString();
       int j1 = name.indexOf(46);
       int j2 = name.lastIndexOf(46);

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

     return s;
  }

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

       cy = c.getSuperclass();
       Object obj;
       try {
        obj = cy.newInstance();

        s = printClass(obj.getClass() ,s, true);


        _edit.setText(s);
       }
       catch ( InstantiationException e) {
         System.out.println("Error1");
       }
       catch ( IllegalAccessException e) {
         System.out.println("Error2");
       }

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

  }


  public static void main(String[] args) {
    rtti01 frame = new rtti01();
    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) );
   }

}
