home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-08-19 | 6.7 KB | 206 lines |
- /*
- * Copyright (c) 1998 S Cubed. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * S CUBED MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. S CUBED SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package Samples;
-
-
- import java.awt.*;
- import java.util.*;
- import java.io.*;
-
- /**
- * This class provides a utility to create accessors for class variables.
- * Handles only discrete variable declarations, that is, a declaration
- * containing type information followed by one variable name and no commas.
- *
- * See the main() method for invocation and use.
- */
-
- public class BuildAccessorWindow extends Frame {
- private boolean inAnApplet = true;
- private TextArea textArea;
- /**
- * Constructs a BuildAccessorWindow.
- */
- BuildAccessorWindow() {
- textArea = new TextArea(5, 40);
- textArea.setEditable(true);
- add("Center", textArea);
- Button button = new Button("Click to build accessors");
- Panel panel = new Panel();
- panel.add(button);
- add("South", panel);
- }
- /**
- * Handles window events. The only one of interest is WINDOW_DESTROY.
- *
- * @param event contains information about the specific event.
- */
- public boolean handleEvent(Event event) {
- if (event.id == Event.WINDOW_DESTROY) {
- if (inAnApplet) {
- dispose();
- } else {
- System.exit(0);
- }
- }
- return super.handleEvent(event);
- }
- /**
- * Constructs a getter definition for the class variable.
- *
- * @param declaration the declaration of the class variable.
- * @return the new getter definition.
- */
- String buildGetter (String declaration) {
- StringTokenizer st = new StringTokenizer(declaration," \t=;",true);
- String lastToken=null;
- String result = new String();
- String startComment = "// Generated class variable accessor.\n/**\n * Returns the value of the variable ";
- String comment = ".\n * \n * @return the value of the variable ";
- String endComment = ".\n */\n";
- while (st.hasMoreTokens()) {
- String token = st.nextToken();
- if (token.equals(";")
- || token.equals("=")) break;
- else if (token.equals(" ")
- || token.equals("\t")) {
- result +=token;
- continue;
- }
- else {
- result +=token;
- lastToken = token;
- }
- }
- return startComment+lastToken+comment+lastToken+endComment+result+
- "( ) {\n\treturn "+lastToken+";\n\t}\n\n";
- }
- /**
- * Constructs a setter definition for the class variable.
- *
- * @param declaration the declaration of the class variable.
- * @return the new setter definition.
- */
- String buildSetter (String declaration) {
- StringTokenizer st = new StringTokenizer(declaration," \t=;",true);
- String startComment = "// Generated class variable accessor.\n/**\n * Sets the value of the variable ";
- String endComment = " */\n";
- String comment = "";
- String typeToken=null;
- String lastToken=null;
- String visibilityToken="";
- while (st.hasMoreTokens()) {
- String token = st.nextToken();
- if (token.equals(";")
- || token.equals("=")) break;
- else if (token.equals(" ")
- || token.equals("\t")) continue;
- else {
- if (token.equals("static")) visibilityToken=token+=" ";
- if (token.equals("private")) visibilityToken=token+=" ";
- if (token.equals("public")) visibilityToken=token+=" ";
- if (token.equals("protected")) visibilityToken=token+=" ";
- typeToken =lastToken;
- lastToken = token;
- }
- }
- comment = lastToken+".\n * \n * @param value the new variable value.\n";
- return startComment+comment+endComment+visibilityToken+"void "+lastToken+"("+typeToken+" value) {\n\t"+lastToken+"=value;\n\t}\n\n";
- }
- /**
- * Provides handling for user interface actions. The action of interest
- * is the button click.
- *
- * @param event information about the event
- * @param arg an optional argument connected with the event
- * @return true
- */
- public boolean action(Event event, Object arg) {
- String temp = "";
-
- try {
-
- StreamTokenizer stis = new StreamTokenizer(new StringReader(textArea.getText()));
- boolean equalsFound = false;
- textArea.appendText("\n\n");
- stis.wordChars('_','_');
- stis.ordinaryChar('/');
- stis.slashStarComments(true);
- stis.slashSlashComments(true);
-
- while (stis.nextToken() != StreamTokenizer.TT_EOF) {
-
- switch ((char) stis.ttype) {
-
- case (char)StreamTokenizer.TT_WORD:
- if (!equalsFound) temp += " "+stis.sval;
- break;
-
- case (char)'[':
- if (!equalsFound) temp += "[";
- break;
-
- case (char)']':
- if (!equalsFound) temp += "] ";
- break;
-
- case (char)'=':
- equalsFound = true;
- break;
-
- case (char)';':
- textArea.appendText(buildGetter(temp));
- textArea.appendText(buildSetter(temp));
- temp = "";
- equalsFound = false;
- break;
-
- default:;
- }
-
- }
- } catch (IOException e) {}
-
- return true;
- }
- /**
- * Starts a BuildAccessorWindow.
- *
- * To use, start the BuildAccessorWindow, copy the class variables
- * from a class definition for which you want to build accessors.
- * Paste the class variables into the BuildAccessorWindow text area.
- * Click on the "Click to build accessors" button.
- * The accessors are created below the class variables, they can be
- * copied and pasted back into the original class definition.
- *
- * To run, highlight name below and select run,<pre>
- * Samples.BuildAccessorWindow
- * </pre>
- * @param args command line arguments (none expected)
- *
- */
- public static void main(String args[]) {
- BuildAccessorWindow window = new BuildAccessorWindow();
- window.inAnApplet = false;
-
- window.setTitle("Build Accessors");
- window.pack();
- window.show();
- }
- }
-