1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.jemmy.browser;
  27 
  28 import java.lang.reflect.Field;
  29 import java.lang.reflect.Method;
  30 import javax.swing.tree.DefaultMutableTreeNode;
  31 import javax.swing.tree.DefaultTreeModel;
  32 
  33 /**
  34  *
  35  * @author shura
  36  */
  37 public class ReflectionPanel extends javax.swing.JPanel {
  38 
  39     DefaultTreeModel model;
  40     /** Creates new form ReflectionPanel */
  41     public ReflectionPanel() {
  42         initComponents();
  43         DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
  44         model = new DefaultTreeModel(root);
  45         tree.setModel(model);
  46     }
  47 
  48     public DefaultTreeModel getTreeModel() {
  49         return model;
  50     }
  51 
  52     public void setControl(Class controlClass) {
  53         DefaultMutableTreeNode root = new DefaultMutableTreeNode();
  54         Class cls =controlClass;
  55         do {
  56             root.add(getClassNode(cls));
  57         } while((cls = cls.getSuperclass()) != null);
  58         for(Class i : controlClass.getInterfaces()) {
  59             root.add(getClassNode(i));
  60         }
  61         model.setRoot(root);
  62         tree.setModel(model);
  63         revalidate();
  64     }
  65 
  66     private DefaultMutableTreeNode getClassNode(Class cls) {
  67         DefaultMutableTreeNode classNode = new DefaultMutableTreeNode(
  68                 (cls.isInterface() ? "Interface " : "Class ") +
  69                 cls.getName());
  70         for(Method m : cls.getMethods()) {
  71             StringBuffer signature = new StringBuffer(m.getReturnType().getName());
  72             signature.append(" " + m.getName() + "(");
  73             for(int i = 0; i < m.getParameterTypes().length; i++) {
  74                 signature.append(m.getParameterTypes()[i].getName());
  75                 if((i < m.getParameterTypes().length - 1)) {
  76                     signature.append(",");
  77                 }
  78             }
  79             signature.append(")");
  80             classNode.add(new DefaultMutableTreeNode(signature.toString()));
  81         }
  82         for(Field m : cls.getFields()) {
  83             classNode.add(new DefaultMutableTreeNode(m.getType().getName() + " " + m.getName()));
  84         }
  85         return classNode;
  86     }
  87 
  88 
  89     /** This method is called from within the constructor to
  90      * initialize the form.
  91      * WARNING: Do NOT modify this code. The content of this method is
  92      * always regenerated by the Form Editor.
  93      */
  94     @SuppressWarnings("unchecked")
  95     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  96     private void initComponents() {
  97 
  98         jScrollPane1 = new javax.swing.JScrollPane();
  99         tree = new javax.swing.JTree();
 100 
 101         jScrollPane1.setViewportView(tree);
 102 
 103         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
 104         this.setLayout(layout);
 105         layout.setHorizontalGroup(
 106             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 107             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
 108         );
 109         layout.setVerticalGroup(
 110             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
 111             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
 112         );
 113     }// </editor-fold>//GEN-END:initComponents
 114 
 115 
 116     // Variables declaration - do not modify//GEN-BEGIN:variables
 117     private javax.swing.JScrollPane jScrollPane1;
 118     private javax.swing.JTree tree;
 119     // End of variables declaration//GEN-END:variables
 120 
 121 }