001 /* 002 * CodeBox.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.*; 016 import java.awt.event.*; 017 018 /** This class pops up the Code editing area. The Code editing area 019 * has two big text areas where the students can enter code, as well 020 * as two smaller text areas (that initially start off hidden) that 021 * can be used to enter fields. 022 * 023 * <P>This code also creates an instance of AdvProg, creates all of the 024 * appropriate listeners and passes them to the instance. 025 * This code does not actually process and/or compile the user's code; 026 * that is done by instances of CodeMerger, which are added to the 027 * big buttons that say, "Load Horizontal Code" and "Load Vertical Code" 028 * 029 * <p>Copyright © 1998 Massachusetts Institute of Technology.<br /> 030 * Copyright © 2003 Franklin W. Olin College of Engineering.</p> 031 * 032 * 033 * @author Luis Sarmenta, lfgs@cag.lcs.mit.edu 034 * @author Henry Wong, henryw@mit.edu 035 * @version $Id: CodeBox.java,v 1.5 2004/02/09 20:55:03 gus Exp $ 036 * @see AccelHandler 037 * @see CodeMerger 038 */ 039 public class CodeBox extends Frame { 040 041 /** the default width */ 042 static final int WIDTH = 800; 043 /** the default height */ 044 static final int HEIGHT = 530; 045 046 private TextArea hArea; 047 private TextArea vArea; 048 private TextArea hFields; 049 private TextArea vFields; 050 CodeMerger hMerger; 051 CodeMerger vMerger; 052 053 /** Construct a new CodeBox. If no font is specified, use a default. 054 * @param xHandler Animates motion in the X direction 055 * @param yHandler Animates motion in the Y direction. 056 */ 057 public CodeBox(AccelHandler xHandler, AccelHandler yHandler) { 058 059 this(xHandler, yHandler, Spirograph.DEFAULTFONT); 060 } 061 062 /** Construct a CodeBox. Use the specified font. 063 * @param xHandler Animates motion in the x direction 064 * @param yHandler Animates motion in the y direction 065 * @param f A custom font for this component 066 */ 067 public CodeBox(final AccelHandler xHandler, final AccelHandler yHandler, Font f) { 068 069 hArea = new TextArea(15,50); 070 vArea = new TextArea(15,50); 071 hFields = new TextArea("",2,30,TextArea.SCROLLBARS_VERTICAL_ONLY); 072 vFields = new TextArea("",2,30,TextArea.SCROLLBARS_VERTICAL_ONLY); 073 074 hMerger = new CodeMerger("Horizontal", new TextAreaCodeSource(hArea, hFields), xHandler, yHandler); 075 vMerger = new CodeMerger("Vertical", new TextAreaCodeSource(vArea, vFields), yHandler, xHandler); 076 077 this.setFont(f); 078 this.setTitle("Spirograph Code Editor"); 079 080 Panel top = new Panel(); 081 top.setLayout(new BorderLayout(10,10)); 082 top.add ("West", new MultiLabel ( 083 "Note: The variable pos holds the position of the dot along each " + 084 "axis, and the variable vel holds the velocity of the dot\n " + 085 "along each axis. The variables otherPos and otherVel hold "+ 086 "the position and velocity of the dot along the other axis.")); 087 Button quit = new Button("Quit"); 088 quit.addActionListener(new ActionListener() { 089 public void actionPerformed(ActionEvent ae) { 090 System.exit(0); 091 } 092 }); 093 top.add("Center", quit); 094 095 // Set up fields and labels for horiz area 096 Label hFieldLabel=new Label ("Enter horizontal rule fields here."); 097 098 Panel hTop = new Panel(); 099 hTop.setLayout(new BorderLayout(10,10)); 100 hTop.add("North", hFieldLabel); 101 hTop.add("South", hFields); 102 103 Panel hMid = new Panel(); 104 hMid.setLayout(new BorderLayout(10,10)); 105 hMid.add("North", new Label ("Enter horizontal rule below:")); 106 hMid.add("South", hArea ); 107 108 Checkbox hBoth = new Checkbox( "Use this rule for both dimensions", false ); 109 Button hDone = new Button("Compile and Load Horizontal Code"); 110 hDone.addActionListener(hMerger); 111 Panel hBot = new Panel(); 112 hBot.setLayout(new BorderLayout(10,3)); 113 hBot.add("North", hBoth ); 114 hBot.add("South", hDone ); 115 116 // Add fields and labels, text area and Done button to horiz area 117 Panel hContainer = new Panel(); 118 hContainer.setLayout (new BorderLayout(10,10)); 119 hContainer.add("North", hTop); 120 hContainer.add("Center", hMid); 121 hContainer.add("South", hBot); 122 123 // Set up fields and labels for vertical area 124 Label vFieldLabel=new Label ("Enter vertical rule fields here."); 125 126 Panel vTop = new Panel(); 127 vTop.setLayout(new BorderLayout(10,10)); 128 vTop.add("North", vFieldLabel); 129 vTop.add("South", vFields); 130 131 Panel vMid = new Panel(); 132 vMid.setLayout(new BorderLayout(10,10)); 133 vMid.add("North",new Label ("Enter vertical rule below:")); 134 vMid.add("South",vArea ); 135 136 Checkbox vBoth = new Checkbox( "Use this rule for both dimensions", false ); 137 Button vDone = new Button("Compile and Load Vertical Code"); 138 vDone.addActionListener(vMerger); 139 Panel vBot = new Panel(); 140 vBot.setLayout(new BorderLayout(10,3)); 141 vBot.add("North", vBoth ); 142 vBot.add("South", vDone ); 143 144 // Add fields and labels, text area and Done button to vert area 145 Panel vContainer = new Panel(); 146 vContainer.setLayout (new BorderLayout(10,10)); 147 vContainer.add("North", vTop); 148 vContainer.add("Center", vMid); 149 vContainer.add("South", vBot); 150 151 // Set up ItemListeners for "Advanced Programming" 152 OneRuleListener hOnly = new OneRuleListener 153 (hArea, vArea, hFields, vFields, hDone, vDone, vBoth, hMerger); 154 155 OneRuleListener vOnly = new OneRuleListener 156 (vArea, hArea, vFields, hFields, vDone, hDone, hBoth, vMerger); 157 158 hBoth.addItemListener(hOnly); 159 vBoth.addItemListener(vOnly); 160 161 162 // Do final Layout 163 this.setLayout(new BorderLayout(10,10)); 164 165 this.add ("West", hContainer); 166 this.add ("East", vContainer); 167 this.add ("South", top); 168 169 this.setSize(CodeBox.WIDTH,CodeBox.HEIGHT); 170 this.show(); 171 } 172 173 private String liftText(String src, String start, String end) 174 { 175 int startpos = src.indexOf(start) + start.length(); 176 int endpos = src.indexOf(end); 177 if(startpos > 0 && endpos > 0) 178 return src.substring(startpos, endpos); 179 else 180 return ""; 181 } 182 183 /** Accessor for the Vertical <code>CodeMerger</code> 184 * @return The vertical (y) direction <code>CodeMerger</code> 185 */ 186 public CodeMerger getVMerger() { 187 return vMerger; 188 } 189 190 /** Accessor for the Vertical <code>CodeMerger</code> 191 * @return The vertical (y) direction <code>CodeMerger</code> 192 */ 193 public CodeMerger getHMerger() { 194 return hMerger; 195 } 196 } 197 198 /* 199 * $Log: CodeBox.java,v $ 200 * Revision 1.5 2004/02/09 20:55:03 gus 201 * javadoc fixes 202 * 203 * Revision 1.4 2003/01/10 22:19:18 gus 204 * Complete and update the javadocs 205 * 206 */