home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c17 / DisplayMethods.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  6.7 KB  |  206 lines

  1. //: DisplayMethods.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Display the methods of any class inside
  50. // a window. Dynamically narrows your search.
  51. import java.awt.*;
  52. import java.awt.event.*;
  53. import java.applet.*;
  54. import java.lang.reflect.*;
  55. import java.io.*;
  56.  
  57. public class DisplayMethods extends Applet {
  58.   Class cl;
  59.   Method[] m;
  60.   Constructor[] ctor;
  61.   String[] n = new String[0];
  62.   TextField 
  63.     name = new TextField(40),
  64.     searchFor = new TextField(30);
  65.   Checkbox strip = 
  66.     new Checkbox("Strip Qualifiers");
  67.   TextArea results = new TextArea(40, 65);
  68.   public void init() {
  69.     strip.setState(true);
  70.     name.addTextListener(new NameL());
  71.     searchFor.addTextListener(new SearchForL());
  72.     strip.addItemListener(new StripL());
  73.     Panel 
  74.       top = new Panel(),
  75.       lower = new Panel(),
  76.       p = new Panel();
  77.     top.add(new Label("Qualified class name:"));
  78.     top.add(name);
  79.     lower.add(
  80.       new Label("String to search for:"));
  81.     lower.add(searchFor);
  82.     lower.add(strip);
  83.     p.setLayout(new BorderLayout());
  84.     p.add(top, BorderLayout.NORTH);
  85.     p.add(lower, BorderLayout.SOUTH);
  86.     setLayout(new BorderLayout());
  87.     add(p, BorderLayout.NORTH);
  88.     add(results, BorderLayout.CENTER);
  89.   }
  90.   class NameL implements TextListener {
  91.     public void textValueChanged(TextEvent e) {
  92.       String nm = name.getText().trim();
  93.       if(nm.length() == 0) {
  94.         results.setText("No match");
  95.         n = new String[0];
  96.         return;
  97.       }
  98.       try {
  99.         cl = Class.forName(nm);
  100.       } catch (ClassNotFoundException ex) {
  101.         results.setText("No match");
  102.         return;
  103.       }
  104.       m = cl.getMethods();
  105.       ctor = cl.getConstructors();
  106.       // Convert to an array of Strings:
  107.       n = new String[m.length + ctor.length];
  108.       for(int i = 0; i < m.length; i++)
  109.         n[i] = m[i].toString();
  110.       for(int i = 0; i < ctor.length; i++)
  111.         n[i + m.length] = ctor[i].toString();
  112.       reDisplay();
  113.     }
  114.   }
  115.   void reDisplay() {
  116.     // Create the result set:
  117.     String[] rs = new String[n.length];
  118.     String find = searchFor.getText();
  119.     int j = 0;
  120.     // Select from the list if find exists:
  121.     for (int i = 0; i < n.length; i++) {
  122.       if(find == null)
  123.         rs[j++] = n[i];
  124.       else if(n[i].indexOf(find) != -1)
  125.           rs[j++] = n[i];
  126.     }
  127.     results.setText("");
  128.     if(strip.getState() == true)
  129.       for (int i = 0; i < j; i++)
  130.         results.append(
  131.           StripQualifiers.strip(rs[i]) + "\n");
  132.     else // Leave qualifiers on
  133.       for (int i = 0; i < j; i++)
  134.         results.append(rs[i] + "\n");
  135.   }
  136.   class StripL implements ItemListener {
  137.     public void itemStateChanged(ItemEvent e) {
  138.       reDisplay();
  139.     }
  140.   }
  141.   class SearchForL implements TextListener {
  142.     public void textValueChanged(TextEvent e) {
  143.       reDisplay();
  144.     }
  145.   }
  146.   public static void main(String[] args) {
  147.     DisplayMethods applet = new DisplayMethods();
  148.     Frame aFrame = new Frame("Display Methods");
  149.     aFrame.addWindowListener(
  150.       new WindowAdapter() {
  151.         public void windowClosing(WindowEvent e) {
  152.           System.exit(0);
  153.         }
  154.       });
  155.     aFrame.add(applet, BorderLayout.CENTER);
  156.     aFrame.setSize(500,750);
  157.     applet.init();
  158.     applet.start();
  159.     aFrame.setVisible(true);
  160.   }
  161. }
  162.  
  163. class StripQualifiers {
  164.   private StreamTokenizer st;
  165.   public StripQualifiers(String qualified) {
  166.       st = new StreamTokenizer(
  167.         new StringReader(qualified));
  168.       st.ordinaryChar(' ');
  169.   }
  170.   public String getNext() {
  171.     String s = null;
  172.     try {
  173.       if(st.nextToken() !=
  174.             StreamTokenizer.TT_EOF) {
  175.         switch(st.ttype) {
  176.           case StreamTokenizer.TT_EOL:
  177.             s = null;
  178.             break;
  179.           case StreamTokenizer.TT_NUMBER:
  180.             s = Double.toString(st.nval);
  181.             break;
  182.           case StreamTokenizer.TT_WORD:
  183.             s = new String(st.sval);
  184.             break;
  185.           default: // single character in ttype
  186.             s = String.valueOf((char)st.ttype);
  187.         }
  188.       }
  189.     } catch(IOException e) {
  190.       System.out.println(e);
  191.     }
  192.     return s;
  193.   }
  194.   public static String strip(String qualified) {
  195.     StripQualifiers sq = 
  196.       new StripQualifiers(qualified);
  197.     String s = "", si;
  198.     while((si = sq.getNext()) != null) {
  199.       int lastDot = si.lastIndexOf('.');
  200.       if(lastDot != -1)
  201.         si = si.substring(lastDot + 1);
  202.       s += si;
  203.     }
  204.     return s;
  205.   }
  206. } ///:~