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