// robert bradshaw 11/98import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Vector;import java.io.*;public class TuringRuleEditorApplet extends Applet implements ActionListener, ItemListener  {    TuringMachineApplet theMachineApplet;  List ruleList;  TextField curState, curChar, newState, newChar;  Choice moveDir;  Button setBtn, newBtn, remBtn, saveBtn, loadBtn;  Button setState, setTape;  TextField stateFld, tapeFld, tapeIndexFld;  TuringMachine theMachine;      public TuringRuleEditorApplet() { this(null); }  public TuringRuleEditorApplet(TuringMachineApplet tm) {    theMachineApplet=tm;    init();  }    public void init() {        KeyListener intL = new IntFieldListener();    KeyListener charL= new SingleCharListener();    FocusListener selAL = new SelectAllListener();          curState=new TextField(1);    curState.addKeyListener(intL);    curState.addKeyListener(charL);    curState.addFocusListener(selAL);          curChar=new TextField(1);    curChar.addKeyListener(charL);    curChar.addFocusListener(selAL);          newState=new TextField(1);    newState.addKeyListener(intL);    newState.addKeyListener(charL);    newState.addFocusListener(selAL);          newChar=new TextField();    newChar.addKeyListener(charL);    newChar.addFocusListener(selAL);          moveDir=new Choice();    moveDir.add("Right");    moveDir.add("Left");        setBtn=new Button("Set Rules");    setBtn.addActionListener(this);        newBtn=new Button("New Rule");    newBtn.addActionListener(this);        remBtn=new Button("Remove Rule");    remBtn.addActionListener(this);        saveBtn=new Button("Save Rule");    saveBtn.addActionListener(this);        loadBtn=new Button("Load Set...");    loadBtn.addActionListener(this);        setState=new Button("Set state");    setState.addActionListener(this);        setTape=new Button("Set tape");    setTape.addActionListener(this);          stateFld=new TextField(1);    stateFld.addKeyListener(intL);    stateFld.addKeyListener(charL);    stateFld.addFocusListener(selAL);        tapeFld=new TextField(30);          tapeIndexFld=new TextField(2);    tapeIndexFld.addKeyListener(intL);    tapeIndexFld.addKeyListener(charL);    tapeIndexFld.addFocusListener(selAL);            ruleList=new List();    ruleList.addItemListener(this);    TuringRule[] rules=getMachineApplet().getMachineRules();    for(int i=0; i<rules.length; i++) {      ruleList.add(rules[i].toString());    }        setLayout(new BorderLayout());    add(ruleList,"West");    ruleList.setFont(new Font("Courier",0,12));    Panel p=new Panel(new BorderLayout());    Panel p2=new Panel(new FlowLayout(FlowLayout.LEFT));        Panel p2_1=new Panel(new FlowLayout(FlowLayout.LEFT));    p2_1.add(new Label("If state is"));    p2_1.add(curState);    p2_1.add(new Label(" and the tape reads"));    p2_1.add(curChar);        Panel p2_2=new Panel(new FlowLayout(FlowLayout.LEFT));    p2_2.add(new Label("Set the state to"));    p2_2.add(newState);    p2_2.add(new Label(" write"));    p2_2.add(newChar);    p2_2.add(new Label(" and move"));    p2_2.add(moveDir);        Panel p2_3=new Panel(new FlowLayout(FlowLayout.LEFT));    p2_3.add(setState);    p2_3.add(new Label(" to"));    p2_3.add(stateFld);        Panel p2_4=new Panel(new FlowLayout(FlowLayout.LEFT));    p2_4.add(setTape);    p2_4.add(new Label(" to"));    p2_4.add(tapeFld);    p2_4.add(new Label(", index:"));    p2_4.add(tapeIndexFld);        p2.add(p2_1);    p2.add(p2_2);    p2.add(p2_3);    p2.add(p2_4);    Panel p3=new Panel(new FlowLayout(FlowLayout.RIGHT));    if (theMachineApplet!=null) p3.add(loadBtn);    p3.add(remBtn);    p3.add(newBtn);    p3.add(saveBtn);    p3.add(setBtn);    p.add(p2);    p.add(p3,"South");    add(p);  }    public void actionPerformed(ActionEvent e) {    if (e.getSource()==newBtn) {      ruleList.add("0 0 0");      ruleList.select(ruleList.getItemCount()-1);      clear();    }    else if (e.getSource()==remBtn) {      ruleList.remove(ruleList.getSelectedIndex());    }    else if (e.getSource()==saveBtn) {      int si=ruleList.getSelectedIndex();      if (si==-1) {        ruleList.add(getRule().toString());        ruleList.select(ruleList.getItemCount()-1);      }      else {        ruleList.replaceItem(getRule().toString(),si);        ruleList.select(si);      }    }    else if (e.getSource()==loadBtn) {      FileDialog fileDialog=new FileDialog((Frame)this.getParent());      fileDialog.show();      importRuleList(new File(fileDialog.getDirectory(), fileDialog.getFile()));    }    else if (e.getSource()==setBtn) {      getMachineApplet().setMachineRules(getRuleList());    }    else if (e.getSource()==setState) {      if (stateFld.getText().equals("")) stateFld.setText("0");      getMachineApplet().setMachineState(Integer.parseInt(stateFld.getText()));    }    else if (e.getSource()==setTape) {      if (tapeIndexFld.getText().equals("")) tapeIndexFld.setText("0");      getMachineApplet().setMachineTape(new TuringTape(tapeFld.getText(), Integer.parseInt(tapeIndexFld.getText())));    }  }    public void itemStateChanged(ItemEvent e) {    if (e.getStateChange()==ItemEvent.SELECTED) {      setRule(new TuringRule(ruleList.getSelectedItem()));    }    else {      ruleList.replaceItem(getRule().toString(),ruleList.getSelectedIndex());    }  }    void importRuleList(File f) {    try {      FileInputStream fin=new FileInputStream(f);      String rulesS="";      byte[] b=new byte[5];      int i=fin.read(b);      while (i!=-1) {        rulesS=rulesS+new String(b);        i=fin.read(b);      }      TuringRule[] rules=TuringRule.parseRules(rulesS);      ruleList.removeAll();      for(i=0; i<rules.length; i++) {        ruleList.add(rules[i].toString());      }    }    catch (IOException e) { e.printStackTrace(System.out); }  }    TuringRule[] getRuleList() {    TuringRule[] rules=new TuringRule[ruleList.getItemCount()];    for(int i=0; i<rules.length; i++) {      rules[i]=new TuringRule(ruleList.getItem(i));    }    return rules;  }    TuringRule getRule() {    if (curState.getText().equals("")) curState.setText("0");    if (curChar .getText().equals("")) curState.setText(" ");    if (newState.getText().equals("")) curState.setText("0");    if (newChar .getText().equals("")) curState.setText(" ");    String dir="R";    if (moveDir.getSelectedIndex()==1) dir="L";    return new TuringRule(curState.getText()+                          curChar .getText()+                          newState.getText()+                          newChar .getText()+                          dir);  }    void setRule(TuringRule rule) {    if (rule==null) clear();    else {      curState.setText(""+rule.getCurState());      curChar .setText(""+rule.getCurTapeChar());      newState.setText(""+rule.getNewState());      newChar .setText(""+rule.getNewTapeChar());      if (rule.getMoveDir() == 'L' || rule.getMoveDir() == 'l') moveDir.select(1);      else moveDir.select(0);    }  }      void clear() {    curState.setText("");    curChar.setText("");    newState.setText("");    newChar.setText("");    moveDir.select(0);  }    private TuringMachineApplet getMachineApplet() {    if (theMachineApplet!=null) return theMachineApplet;    else {      return (TuringMachineApplet)getAppletContext().getApplet(getParameter("mainAppletName"));    }  }  }