home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 27 / CDROM27.iso / share / wnt / jig / data1.cab / Program_Executable_Files / src / Samples / BuildAccessorWindow.java next >
Encoding:
Java Source  |  1998-08-19  |  6.7 KB  |  206 lines

  1. /*
  2.  * Copyright (c) 1998 S Cubed. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * S CUBED MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. S CUBED SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17.  
  18. package Samples;
  19.  
  20.  
  21. import java.awt.*;
  22. import java.util.*;
  23. import java.io.*;
  24.  
  25. /**
  26.  * This class provides a utility to create accessors for class variables.
  27.  * Handles only discrete variable declarations, that is, a declaration  
  28.  * containing type information followed by one variable name and no commas.
  29.  *
  30.  * See the main() method for invocation and use.
  31.  */
  32.  
  33. public class BuildAccessorWindow extends Frame {
  34.     private boolean inAnApplet = true;
  35.     private TextArea textArea;
  36. /**
  37.  * Constructs a BuildAccessorWindow.
  38.  */
  39. BuildAccessorWindow() {
  40.     textArea = new TextArea(5, 40);
  41.     textArea.setEditable(true);
  42.     add("Center", textArea);
  43.     Button button = new Button("Click to build accessors");
  44.     Panel panel = new Panel();
  45.     panel.add(button);
  46.     add("South", panel);
  47.     }
  48. /**
  49.  * Handles window events.  The only one of interest is WINDOW_DESTROY.
  50.  *
  51.  * @param event contains information about the specific event.
  52.  */
  53. public boolean handleEvent(Event event) {
  54.     if (event.id == Event.WINDOW_DESTROY) {
  55.         if (inAnApplet) {
  56.             dispose();
  57.             } else {
  58.             System.exit(0);
  59.             }
  60.         }   
  61.     return super.handleEvent(event);
  62.     }
  63. /**
  64.  * Constructs a getter definition for the class variable.
  65.  *
  66.  * @param declaration the declaration of the class variable.
  67.  * @return the new getter definition.
  68.  */
  69. String buildGetter  (String declaration) {
  70.     StringTokenizer st = new StringTokenizer(declaration," \t=;",true);
  71.     String lastToken=null;
  72.     String result = new String();
  73.     String startComment = "// Generated class variable accessor.\n/**\n * Returns the value of the variable ";
  74.     String comment = ".\n * \n * @return the value of the variable ";
  75.     String endComment = ".\n */\n";
  76.     while (st.hasMoreTokens()) {
  77.         String token = st.nextToken();
  78.         if (token.equals(";")
  79.         || token.equals("=")) break;
  80.         else if (token.equals(" ")
  81.         || token.equals("\t")) {
  82.             result +=token;
  83.             continue;
  84.             }
  85.         else {
  86.             result +=token;
  87.             lastToken = token;
  88.             }
  89.         }
  90.     return startComment+lastToken+comment+lastToken+endComment+result+
  91.     "( ) {\n\treturn "+lastToken+";\n\t}\n\n";
  92.     }
  93. /**
  94.  * Constructs a setter definition for the class variable.
  95.  *
  96.  * @param declaration the declaration of the class variable.
  97.  * @return the new setter definition.
  98.  */
  99. String buildSetter  (String declaration) {
  100.     StringTokenizer st = new StringTokenizer(declaration," \t=;",true);
  101.     String startComment = "// Generated class variable accessor.\n/**\n * Sets the value of the variable ";
  102.     String endComment = " */\n";
  103.     String comment = "";
  104.     String typeToken=null;
  105.     String lastToken=null;
  106.     String visibilityToken="";
  107.     while (st.hasMoreTokens()) {
  108.         String token = st.nextToken();
  109.         if (token.equals(";")
  110.         || token.equals("=")) break;
  111.         else if (token.equals(" ")
  112.         || token.equals("\t")) continue;
  113.         else {
  114.             if (token.equals("static")) visibilityToken=token+=" ";
  115.             if (token.equals("private")) visibilityToken=token+=" ";
  116.             if (token.equals("public")) visibilityToken=token+=" ";
  117.             if (token.equals("protected")) visibilityToken=token+=" ";
  118.         typeToken =lastToken;
  119.             lastToken = token;
  120.             }
  121.         }
  122.     comment = lastToken+".\n * \n * @param  value  the new variable value.\n";
  123.     return startComment+comment+endComment+visibilityToken+"void "+lastToken+"("+typeToken+" value) {\n\t"+lastToken+"=value;\n\t}\n\n";
  124.     }
  125. /**
  126.  * Provides handling for user interface actions.  The action of interest
  127.  * is the button click.
  128.  *
  129.  * @param event information about the event
  130.  * @param arg an optional argument connected with the event
  131.  * @return true
  132.  */
  133. public boolean action(Event event, Object arg) {
  134.     String temp = "";
  135.  
  136.     try {
  137.  
  138.         StreamTokenizer stis = new StreamTokenizer(new StringReader(textArea.getText()));
  139.     boolean equalsFound = false;
  140.         textArea.appendText("\n\n");
  141.         stis.wordChars('_','_');
  142.         stis.ordinaryChar('/');
  143.         stis.slashStarComments(true);
  144.         stis.slashSlashComments(true);
  145.  
  146.         while (stis.nextToken() != StreamTokenizer.TT_EOF) {
  147.  
  148.             switch ((char) stis.ttype) {
  149.  
  150.                 case (char)StreamTokenizer.TT_WORD:
  151.                 if (!equalsFound) temp += " "+stis.sval;
  152.                 break;
  153.  
  154.                 case (char)'[':
  155.                 if (!equalsFound) temp += "[";
  156.                 break;
  157.  
  158.                 case (char)']':
  159.                 if (!equalsFound) temp += "] ";
  160.                 break;
  161.  
  162.                 case (char)'=':
  163.         equalsFound = true;
  164.         break;
  165.  
  166.                 case (char)';':
  167.                 textArea.appendText(buildGetter(temp));
  168.                 textArea.appendText(buildSetter(temp));
  169.                 temp = "";
  170.         equalsFound = false;
  171.                 break;
  172.  
  173.                 default:;
  174.                 }
  175.  
  176.             }
  177.         } catch (IOException e) {}
  178.  
  179.     return true;
  180.     }
  181. /**
  182.  * Starts a BuildAccessorWindow.
  183.  * 
  184.  * To use, start the BuildAccessorWindow, copy the class variables
  185.  * from a class definition for which you want to build accessors.
  186.  * Paste the class variables into the BuildAccessorWindow text area.
  187.  * Click on the "Click to build accessors" button.
  188.  * The accessors are created below the class variables, they can be
  189.  * copied and pasted back into the original class definition.
  190.  *
  191.  * To run, highlight name below and select run,<pre>
  192.  * Samples.BuildAccessorWindow
  193.  * </pre>
  194.  * @param args command line arguments (none expected)
  195.  *
  196.  */
  197. public static void main(String args[]) {
  198.     BuildAccessorWindow window = new BuildAccessorWindow();
  199.     window.inAnApplet = false;
  200.  
  201.     window.setTitle("Build Accessors");
  202.     window.pack();
  203.     window.show();
  204.     }
  205. }
  206.