import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.beans.*;
import java.io.*;
import java.util.*;
import java.net.*;
import TextPreviewer;
import JavaCodeFilter;
import java.lang.reflect.*;

public class My extends JApplet
{   
  JFileChooser   chooser = new JFileChooser();
  TextPreviewer  previewer = new TextPreviewer();
  PreviewPanel   previewPanel = new PreviewPanel();
  File file;
  Vector v = new Vector();
  JTextArea ta;

  class PreviewPanel extends JPanel
  {
   public PreviewPanel() 
   {
    JLabel label = new JLabel("Text Previewer", SwingConstants.CENTER);
    setPreferredSize(new Dimension(350,0));
    setBorder(BorderFactory.createEtchedBorder());
    setLayout(new BorderLayout());
    label.setBorder(BorderFactory.createEtchedBorder());
    add(label, BorderLayout.NORTH);
    add(previewer, BorderLayout.CENTER);
    }
  }

  public My() 
  {  
   chooser.setAccessory(previewPanel);
   chooser.setFileFilter(new JavaCodeFilter());
   chooser.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent e) 
        {
	   if(e.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY))
		previewer.update((File)e.getNewValue());
	}
   }); 
   Container contentPane = getContentPane();
   JPanel p1 = new JPanel(new BorderLayout());
   JPanel p2 = new JPanel( );
   JPanel p3 = new JPanel( );
   JPanel topPanel = new JPanel(new BorderLayout());
   JPanel botPanel = new JPanel(new BorderLayout());
   JButton inputFile = new JButton("Select a file");
   inputFile.setIcon(new ImageIcon("ballot~1.gif"));
   
   contentPane.setLayout(new BorderLayout());

   //Creating a color cube
   GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
   Canvas3D canvas3D = new Canvas3D(config);
   BranchGroup scene = createSceneGraph();
   scene.compile();
   SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
   simpleU.getViewingPlatform().setNominalViewingTransform();
   simpleU.addBranchGraph(scene);

   //put the canvas into a JPanel, add panel into contentPane
   JPanel classPanel = new JPanel(new BorderLayout());
   JPanel objectPanel = new JPanel(new BorderLayout());
   JPanel textPanel = new JPanel(new BorderLayout());
   ta = new JTextArea(10, 20);
   ta.setEditable(false);
   botPanel.add(ta, BorderLayout.WEST);

   JTabbedPane tp = new JTabbedPane(SwingConstants.TOP);
   tp.addTab("Class", new ImageIcon("palette.gif"), classPanel, "show class inheritance");
   tp.addTab("Object", new ImageIcon("tricycle.gif"),objectPanel, "show methods");
   tp.addTab("Class Text", new ImageIcon("clipboard.gif"),textPanel, "show text");
   classPanel.add(canvas3D,BorderLayout.CENTER);
   objectPanel.add(canvas3D,BorderLayout.CENTER);
   botPanel.add(tp, BorderLayout.CENTER);
   p1.add(inputFile, BorderLayout.WEST);
   topPanel.add(p1, BorderLayout.WEST);
   topPanel.add(p2, BorderLayout.CENTER);
   topPanel.add(p3, BorderLayout.EAST);
   contentPane.add(topPanel, BorderLayout.NORTH);
   contentPane.add(botPanel, BorderLayout.CENTER);
 
   //actionListener for inputFile button, if it is pressed,
   //a dialog window will be popped out
   inputFile.addActionListener(new ActionListener()
   {
    public void actionPerformed(ActionEvent e)
    {
      int state = chooser.showOpenDialog(null);
      File file = chooser.getSelectedFile();

      if(file != null && state == JFileChooser.APPROVE_OPTION)
      {        
         String s = file.getName();
         ta.append(s + "\n");
         JOptionPane.showMessageDialog(null, file.getPath());
         getClassMethods(s);
      }
      else if(state == chooser.CANCEL_OPTION)
      {
         JOptionPane.showMessageDialog(null, "Canceled");
      }
   }  
   });
  }//end of My

      public void getClassInheritance(String cs)
     {     
          Class cls = null;

        //  clearDisplay();
          
            try
            {
               cls = Class.forName(cs);
            }
            catch(ClassNotFoundException exc)
            {
              System.out.println(exc);
            }

            ta.append("Class inheritance for class " + cs + ":" + "\n");

           while (cls != null)
           {              
              cls = cls.getSuperclass();
              ta.append(cls + "\n");
            }
     }


     public void getClassMethods(String cs)
     {
       Class cls = null;
       
       //clearDisplay();
      
         try
        {
             cls = Class.forName(cs);
        }
         catch(ClassNotFoundException exc)
        {
          System.out.println(exc);
        }
    
        ta.append("Methods for class" + cs + ":" + "\n");
        Method methlist[] = cls.getMethods();
        for(int i = 0; i < methlist.length; i++)
        ta.append( methlist[i] + "\n");
         
     }
 
   
  public BranchGroup createSceneGraph() 
  {
   BranchGroup objRoot = new BranchGroup();
    objRoot.addChild(new ColorCube(0.4));
     
   return objRoot;
  }

  public static void main(String[] args) 
  {
   new MainFrame(new My(), 900, 800);     
  }  

} // end of class My 
