001    /*
002     *  CodeMerger.java
003     *      Part of the Spirograph problem set.
004     *
005     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
006     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
007     * CS101 homepage</a> or email <las@ai.mit.edu>.
008     *
009     * Copyright (C) 1998 Massachusetts Institute of Technology.
010     * Please do not redistribute without obtaining permission.
011     */
012    
013    package spirograph;
014    
015    import java.awt.event.*;
016    import java.awt.*;
017    
018    /**
019     * This class implements a listener that is added to the "Load
020     * Horizontal Code." and "Load Vertical Code" buttons on the Code
021     * Box. The CodeMerger reads the text from the text fields and
022     * arranges for the creation of an Accelerator object that evaluates
023     * the code. This is then passed to the AccelHandlers as a new
024     * Accelerator to use. <p>
025     *
026     *  <p>Copyright © 1998 Massachusetts Institute of Technology.<br />
027     * Copyright © 2002-2003 Franklin W. Olin College of Engineering.</p>
028     *
029     * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu
030     * @author Henry Wong, henryw@mit.edu
031     * @author Nathan Williams, nathanw@mit.edu
032     * @author Patrick G. Heck, gus.heck@olin.edu
033     * @version $Id: CodeMerger.java,v 1.5 2004/02/09 20:55:03 gus Exp $
034     * @see DotPanel
035     */
036    
037    
038    public class CodeMerger implements ActionListener {
039    
040      private String ruleName; 
041        
042      private AccelHandler myAccelHandler, otherAccelHandler;
043      private boolean useAsBoth = false;
044      private CodeSource studentInput;
045    
046      //Loads the compiled classes.  Must be used to load classes that have
047      // changed after they have been loaded once.
048      // private MultiClassLoader multiClassLoader;
049      
050      /** Create a code merger that can convert student input to working behavior
051       * and give it a name.
052       * @param ruleName The name for this code merger
053       * @param studentInput The code the student wrote that needs to be merged.
054       * @param myAccelHandler The object that will contain the behavior for this axis
055       * @param otherAccelHandler The object that contians the behavior for the other axis
056       */
057      
058      public CodeMerger(String ruleName, CodeSource studentInput, AccelHandler myAccelHandler, AccelHandler otherAccelHandler) {
059        this.ruleName = ruleName;  // keeping this because it makes code more
060                                   // readable. It is otherwise unused.
061        this.studentInput = studentInput;
062        this.myAccelHandler = myAccelHandler;
063        this.otherAccelHandler = otherAccelHandler;
064      }
065    
066      /** This is used when the option to use the same rule for both the
067       * vertical and horizontal acceleration is selected
068       * @param useAsBoth Is this rule used for both directions?
069       */
070      public void setBoth(boolean useAsBoth) {
071        this.useAsBoth = useAsBoth;
072      }
073    
074      /** When the event we're registered for is performed, create the
075       * Accelerator from the code.
076       * @param ae An event signaling that the code should be merged
077       */
078      public void actionPerformed(ActionEvent ae) {
079        BshAccel b;
080    
081        b = new BshAccel(studentInput.getFields(), studentInput.getCode());
082    
083        this.myAccelHandler.setAccel((Accelerator) b);
084    
085        System.out.println ("Use as both rules: " + useAsBoth);
086        if (useAsBoth) {
087          this.otherAccelHandler.setAccel((Accelerator) b);
088        }
089        System.out.println ("Done loading code.");
090      }
091    }
092    
093    /*
094     * $Log: CodeMerger.java,v $
095     * Revision 1.5  2004/02/09 20:55:03  gus
096     * javadoc fixes
097     *
098     * Revision 1.4  2003/01/13 19:59:25  gus
099     * Remove entirely unneccessary argument from BshAccel constructor
100     *
101     * Revision 1.3  2003/01/10 22:19:18  gus
102     * Complete and update the javadocs
103     *
104     */