1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   3  *
   4  * Copyright 2007-2009 Sun Microsystems, Inc. All rights reserved.
   5  *
   6  * The contents of this file are subject to the terms of the
   7  * Common Development and Distribution License (the "License").
   8  * You may not use this file except in compliance with the License.
   9  *
  10  * You can obtain a copy of the license at LICENSE.html or
  11  * http://www.sun.com/cddl.
  12  * See the License for the specific language governing permissions
  13  * and limitations under the License.
  14  *
  15  * When distributing Covered Code, include this License Header
  16  * Notice in each file.
  17  *
  18  * If applicable, add the following below the
  19  * License Header, with the fields enclosed by brackets [] replaced by
  20  * your own identifying information:
  21  * "Portions Copyrighted [year] [name of copyright owner]"
  22  *
  23  * Contributor(s): Alexandre (Shura) Iline. (shurymury@gmail.com)
  24  *
  25  * The Original Software is the Jemmy library.
  26  * The Initial Developer of the Original Software is Alexandre Iline.
  27  * All Rights Reserved.
  28  *
  29  */
  30 
  31 /*
  32  * ReflectionPanel.java
  33  *
  34  * Created on Nov 26, 2009, 7:14:24 PM
  35  */
  36 
  37 package org.jemmy.browser;
  38 
  39 import java.lang.reflect.Field;
  40 import java.lang.reflect.Method;
  41 import javax.swing.tree.DefaultMutableTreeNode;
  42 import javax.swing.tree.DefaultTreeModel;
  43 
  44 /**
  45  *
  46  * @author shura
  47  */
  48 public class ReflectionPanel extends javax.swing.JPanel {
  49 
  50     DefaultTreeModel model;
  51     /** Creates new form ReflectionPanel */
  52     public ReflectionPanel() {
  53         initComponents();
  54         DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
  55         model = new DefaultTreeModel(root);
  56         tree.setModel(model);
  57     }
  58 
  59     public DefaultTreeModel getTreeModel() {
  60         return model;
  61     }
  62 
  63     public void setControl(Class controlClass) {
  64         DefaultMutableTreeNode root = new DefaultMutableTreeNode();
  65         Class cls =controlClass;
  66         do {
  67             root.add(getClassNode(cls));
  68         } while((cls = cls.getSuperclass()) != null);
  69         for(Class i : controlClass.getInterfaces()) {
  70             root.add(getClassNode(i));
  71         }
  72         model.setRoot(root);
  73         tree.setModel(model);
  74         revalidate();
  75     }
  76 
  77     private DefaultMutableTreeNode getClassNode(Class cls) {
  78         DefaultMutableTreeNode classNode = new DefaultMutableTreeNode(
  79                 (cls.isInterface() ? "Interface " : "Class ") +
  80                 cls.getName());
  81         for(Method m : cls.getMethods()) {
  82             StringBuffer signature = new StringBuffer(m.getReturnType().getName());
  83             signature.append(" " + m.getName() + "(");
  84             for(int i = 0; i < m.getParameterTypes().length; i++) {
  85                 signature.append(m.getParameterTypes()[i].getName());
  86                 if((i < m.getParameterTypes().length - 1)) {
  87                     signature.append(",");
  88                 }
  89             }
  90             signature.append(")");
  91             classNode.add(new DefaultMutableTreeNode(signature.toString()));
  92         }
  93         for(Field m : cls.getFields()) {
  94             classNode.add(new DefaultMutableTreeNode(m.getType().getName() + " " + m.getName()));
  95         }
  96         return classNode;
  97     }
  98 
  99 
 100     /** This method is called from within the constructor to
 101      * initialize the form.
 102      * WARNING: Do NOT modify this code. The content of this method is
 103      * always regenerated by the Form Editor.
 104      */
 105     @SuppressWarnings("unchecked")
 106     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
 107     private void initComponents() {
 108 
 109         jScrollPane1 = new javax.swing.JScrollPane();
 110         tree = new javax.swing.JTree();
 111 
 112         jScrollPane1.setViewportView(tree);
 113 
 114         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
 115         this.setLayout(layout);
 116         layout.setHorizontalGroup(
 117             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 118             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
 119         );
 120         layout.setVerticalGroup(
 121             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 122             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
 123         );
 124     }// </editor-fold>//GEN-END:initComponents
 125 
 126 
 127     // Variables declaration - do not modify//GEN-BEGIN:variables
 128     private javax.swing.JScrollPane jScrollPane1;
 129     private javax.swing.JTree tree;
 130     // End of variables declaration//GEN-END:variables
 131 
 132 }