git @ Cat's Eye Technologies yoob / master src / ContentPane.java
master

Tree @master (Download .tar.gz)

ContentPane.java @masterraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
 * The yoob ContentPane.  Contains all the GUI controls for yoob.
 * The source code in this file has been placed into the public domain.
 */
package tc.catseye.yoob;

import java.util.List;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.security.AccessControlException;

import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.*;

import java.applet.AppletContext;
import java.net.URL;
import java.net.MalformedURLException;


class LanguageMenuItemActionListener implements ActionListener {
    private RunThread rt;
    private String name;

    public LanguageMenuItemActionListener(RunThread rt, String name) {
        this.rt = rt;
        this.name = name;
    }

    public void actionPerformed(ActionEvent e) {
        rt.load(name);
    } 
}

class ExampleMenuItemActionListener implements ActionListener {
    private ContentPane cp;
    private int index;

    public ExampleMenuItemActionListener(ContentPane cp, int index) {
        this.cp = cp;
        this.index = index;
    }

    public void actionPerformed(ActionEvent e) {
        cp.loadExampleProgram(index);
    }
}

class OptionMenuItemActionListener implements ActionListener {
    private ContentPane cp;
    private String name;

    public OptionMenuItemActionListener(ContentPane cp, String name) {
        this.cp = cp;
        this.name = name;
    }

    public void actionPerformed(ActionEvent e) {
        cp.setOption(name,
          ((AbstractButton)e.getSource()).getModel().isSelected()
        );
    }
}

class RunSpeedSliderChangeListener implements ChangeListener {
    private RunThread rt;

    public RunSpeedSliderChangeListener (RunThread rt) {
        this.rt = rt;
    }

    public void stateChanged(ChangeEvent e) {
        JSlider source = (JSlider)e.getSource();
        rt.setRunSpeed(source.getValue());
    }
}

public class ContentPane extends JPanel {
    private String title;
    private Language language = null;
    private State currentState = null, initialState = null;

    private JMenuBar menuBar;
    private JMenu languageMenu, examplesMenu, optionsMenu, viewMenu;
    private JMenuItem gridLinesMenuItem, zoomInMenuItem, zoomOutMenuItem;
    private PlayfieldDepiction[] pd = {null, null, null, null, null};
    private TapeDepiction[] td = {null, null, null, null, null};
    private JLabel statusBar;
    private Font editFont;
    private ProgramTextArea progBox;
    private InputTextArea inputBox;
    private JTextArea outputBox, editBox, aboutBox;
    private JScrollPane editScrollPane, aboutScrollPane;
    private JPanel interiorPanel, aboutPanel, editPanel;
    private JComponent languageComponent = null;
    private JLabel welcomeLabel;
    private JPanel buttonPanel;
    private final JApplet plet;

    private JButton buttonReset, buttonStop, buttonEdit;
    private JButton buttonStep, buttonWalk, buttonRun;
    private JButton buttonAbout;
    private JButton buttonEsowiki;
    private JSlider sliderRunSpeed;

    static final int HZ_MIN = 0;
    static final int HZ_MAX = 30;
    static final int HZ_INIT = 5;    // initial cycles per second

    private RunThread runThread = null;

    private TextAreasWorld world = null;

    private double zoom = 1.0;
    private boolean gridLines = false;

    public ContentPane(EsolangLoader loader, JApplet applet, String selectedLanguage) {
        super(new BorderLayout());

        title = "Yoob";
        this.plet = applet; // can be null

        runThread = new RunThread(this, HZ_INIT, loader);
        runThread.start();

        //---------- the menus -----------
        menuBar = new JMenuBar();

        languageMenu = new JMenu("Language");
        languageMenu.setMnemonic(KeyEvent.VK_G);
        languageMenu.getAccessibleContext().setAccessibleDescription("The menu containing language selection");
        menuBar.add(languageMenu);

        ButtonGroup group = new ButtonGroup();

        for (String name : loader.getNames()) {
            JRadioButtonMenuItem rbMenuItem = new JRadioButtonMenuItem(name);
            group.add(rbMenuItem);
            languageMenu.add(rbMenuItem);
            rbMenuItem.addActionListener(new LanguageMenuItemActionListener(runThread, name));
        }

        examplesMenu = new JMenu("Examples");
        examplesMenu.setMnemonic(KeyEvent.VK_X);
        examplesMenu.getAccessibleContext().setAccessibleDescription("The menu containing selection of example programs");
        menuBar.add(examplesMenu);
        examplesMenu.setVisible(false);

        optionsMenu = new JMenu("Options");
        optionsMenu.setMnemonic(KeyEvent.VK_O);
        optionsMenu.getAccessibleContext().setAccessibleDescription("The menu containing options that configure language behaviour");
        menuBar.add(optionsMenu);
        optionsMenu.setVisible(false);

        viewMenu = new JMenu("View");
        viewMenu.setMnemonic(KeyEvent.VK_V);
        viewMenu.getAccessibleContext().setAccessibleDescription("The menu of ways to alter how the program is displayed");
        menuBar.add(viewMenu);
        viewMenu.setVisible(false);

        gridLinesMenuItem = new JCheckBoxMenuItem("Grid Lines");
        viewMenu.add(gridLinesMenuItem);
        gridLinesMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gridLines = ((AbstractButton)e.getSource()).getModel().isSelected();
                refreshDepictions();
            } 
        });

        zoomInMenuItem = new JMenuItem("Zoom In");
        viewMenu.add(zoomInMenuItem);
        zoomInMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                zoom += 0.25;
                refreshDepictions();
            } 
        });

        zoomOutMenuItem = new JMenuItem("Zoom Out");
        viewMenu.add(zoomOutMenuItem);
        zoomOutMenuItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (zoom > 0.25) {
                    zoom -= 0.25;
                    refreshDepictions();
                }
            } 
        });

        //---------- the toolbar -----------
        buttonReset = new JButton("Reset");
        buttonReset.setMnemonic(KeyEvent.VK_T);
        buttonReset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                reset();
            }
        });
        buttonReset.setToolTipText("Reset to the initial state of the program.");
        buttonReset.setEnabled(false);

        buttonStep = new JButton("Step");
        buttonStep.setMnemonic(KeyEvent.VK_S);
        buttonStep.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                step();
            } 
        });
        buttonStep.setToolTipText("Execute one step of the program.");
        buttonStep.setEnabled(false);

        buttonWalk = new JButton("Walk");
        buttonWalk.setMnemonic(KeyEvent.VK_W);
        buttonWalk.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                walk();
            }
        });
        buttonWalk.setToolTipText("Walk through (animate the execution of) the program.");
        buttonWalk.setEnabled(false);

        buttonRun = new JButton("Run");
        buttonRun.setMnemonic(KeyEvent.VK_R);
        buttonRun.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                run();
            }
        });
        buttonRun.setToolTipText("Run the program (full speed, no animation.)");
        buttonRun.setEnabled(false);

        buttonStop = new JButton("Stop");
        buttonStop.setMnemonic(KeyEvent.VK_P);
        buttonStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                stop();
            }
        });
        buttonStop.setToolTipText("Stop the program.");
        buttonStop.setEnabled(false);

        buttonAbout = new JButton("About...");
        buttonAbout.setMnemonic(KeyEvent.VK_A);
        buttonAbout.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showAboutBox();
            } 
        });
        buttonAbout.setToolTipText("Obtain more information about the current language.");
        buttonAbout.setEnabled(false);

        sliderRunSpeed = new JSlider(JSlider.HORIZONTAL, HZ_MIN, HZ_MAX, HZ_INIT);
        sliderRunSpeed.setMajorTickSpacing(10);
        sliderRunSpeed.setMinorTickSpacing(1);
        sliderRunSpeed.setPaintTicks(true);
        sliderRunSpeed.setPaintLabels(true);
        sliderRunSpeed.addChangeListener(new RunSpeedSliderChangeListener(runThread));
        //sliderRunSpeed.setEnabled(false);

        buttonEdit = new JButton("Edit");
        buttonEdit.setMnemonic(KeyEvent.VK_E);
        buttonEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showEditor();
            } 
        });
        buttonEdit.setToolTipText("Edit a textual representation of the initial state of the program.");
        buttonEdit.setEnabled(false);

        //--------- the welcome message -------
        welcomeLabel = new JLabel("<html><font size=+2>Welcome to Yoob!</font><br><br>Select a language from the <i>Language</i> menu,<br>" +
                                  "an example from the <i>Examples</i> menu,<br>and click <i>Run</i>.</html>");
        welcomeLabel.setHorizontalAlignment(JLabel.CENTER);
        welcomeLabel.setVerticalAlignment(JLabel.CENTER);

        //---- the (initially hidden) editor -----
        editFont = new Font("Monospaced", Font.PLAIN, 12);
        editBox = new JTextArea();
        editBox.setFont(editFont);
        editScrollPane = new JScrollPane(editBox);

        editPanel = new JPanel(new BorderLayout());
        JButton buttonDoneEditing = new JButton("Done");
        buttonDoneEditing.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);

                initialState = language.importFromText(editBox.getText());
                applyCurrentlySelectedOptions(initialState);
                currentState = initialState.clone();

                interiorPanel.remove(editPanel);
                interiorPanel.add(buttonPanel, BorderLayout.PAGE_START);
                if (languageComponent != null) {
                    interiorPanel.add(languageComponent, BorderLayout.CENTER);
                }
                languageMenu.setEnabled(true);
                examplesMenu.setEnabled(true);
                optionsMenu.setEnabled(true);
                viewMenu.setEnabled(true);
                refreshDepictions();
                setVisible(true);
            } 
        });
        buttonDoneEditing.setToolTipText("Commit changes to this program and return to yoob.");
        buttonDoneEditing.setEnabled(true);

        JPanel editButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        editButtonPanel.add(buttonDoneEditing);
        editPanel.add(editButtonPanel, BorderLayout.PAGE_START);
        editPanel.add(editScrollPane, BorderLayout.CENTER);

        //---------- the panels -----------
        buttonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        buttonPanel.add(buttonEdit);
        buttonPanel.add(buttonReset);
        buttonPanel.add(buttonStep);
        buttonPanel.add(buttonWalk);
        buttonPanel.add(buttonRun);
        buttonPanel.add(buttonStop);
        buttonPanel.add(sliderRunSpeed);
        buttonPanel.add(buttonAbout);

        interiorPanel = new JPanel(new BorderLayout());
        interiorPanel.add(buttonPanel, BorderLayout.PAGE_START);
        interiorPanel.add(welcomeLabel, BorderLayout.CENTER);

        aboutPanel = new JPanel(new BorderLayout());
        buttonEsowiki = new JButton("Esowiki article...");
        buttonEsowiki.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    AppletContext a = plet.getAppletContext();
                    URL url = new URL("http://www.esolangs.org/wiki/" + language.getName());
                    a.showDocument(url, "_blank");
                } catch (MalformedURLException exc) {
                    // nothing
                }
            } 
        });
        buttonEsowiki.setToolTipText("Pop up the Esowiki article for this language in a new browser window or tab.");
        buttonEsowiki.setEnabled(true);
        JButton buttonDone = new JButton("Done");
        buttonDone.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                interiorPanel.remove(aboutPanel);
                interiorPanel.add(buttonPanel, BorderLayout.PAGE_START);
                if (languageComponent != null) {
                    interiorPanel.add(languageComponent, BorderLayout.CENTER);
                }
                languageMenu.setEnabled(true);
                examplesMenu.setEnabled(true);
                optionsMenu.setEnabled(true);
                viewMenu.setEnabled(true);
                refreshDepictions();
                setVisible(true);
            } 
        });
        buttonDone.setToolTipText("Close this message and return to yoob.");
        buttonDone.setEnabled(true);

        JPanel aboutButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
        aboutButtonPanel.add(buttonDone);
        if (plet != null) aboutButtonPanel.add(buttonEsowiki);
        aboutPanel.add(aboutButtonPanel, BorderLayout.PAGE_START);

        aboutBox = new JTextArea();
        aboutBox.setEditable(false);
        aboutBox.setLineWrap(true);
        aboutScrollPane = new JScrollPane(aboutBox);
        aboutPanel.add(aboutScrollPane, BorderLayout.CENTER);
        aboutPanel.setVisible(false);

        statusBar = new JLabel("Ready.");

        setPreferredSize(new Dimension(640, 400));
        add(menuBar, BorderLayout.PAGE_START);
        add(interiorPanel, BorderLayout.CENTER);
        //add(statusBar, BorderLayout.PAGE_END);

        if (selectedLanguage != null) {
            State state = loader.getState(selectedLanguage);
            if (state != null) { // TODO and display an error
                selectLanguage(state.getLanguage());
            }
        }

        setOpaque(true);
        setVisible(true);
    }

    protected void selectLanguage(Language language) {
        this.language = language;
        currentState = initialState = null;

        world = new TextAreasWorld();

        interiorPanel.remove(welcomeLabel);

        this.setVisible(false);
        if (languageComponent != null) {
            interiorPanel.remove(languageComponent);
        }
        languageComponent = null;

        //---------- the playfield(s) -----------
        if (language.numPlayfields() > 0) {
            JComponent pfComponent = null;
            for (int pfNum = 0; pfNum < language.numPlayfields(); pfNum++) {
                pd[pfNum] = new PlayfieldDepiction();
                pd[pfNum].setBackground(Color.yellow);
                pd[pfNum].setMinimumSize(new Dimension(100, 50));
                JScrollPane playfieldScrollPane = new JScrollPane(pd[pfNum]);
                if (pfComponent == null) {
                    pfComponent = playfieldScrollPane;
                } else {
                    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                        pfComponent, playfieldScrollPane);
                    splitPane.setOneTouchExpandable(true);
                    splitPane.setDividerLocation(-1);
                    splitPane.setResizeWeight(1.0 / language.numPlayfields());
                    pfComponent = splitPane;
                }
            }
            languageComponent = pfComponent;
        }

        //---------- the program text -----------
        if (language.hasProgramText()) {
            progBox = new ProgramTextArea();
            progBox.setFocusable(false);
            progBox.setEditable(false);
            progBox.setFont(editFont);
            JScrollPane progScrollPane = new JScrollPane(progBox);
            progScrollPane.setPreferredSize(new Dimension(640, 100));

            if (languageComponent == null) {
                languageComponent = progScrollPane;
            } else {
                JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                    progScrollPane, languageComponent);
                splitPane.setOneTouchExpandable(true);
                splitPane.setDividerLocation(-1);
                splitPane.setResizeWeight(0.0); // the bottom is what gets resized
                languageComponent = splitPane;
            }
        }

        //---------- input and output -----------
        if (language.hasInput() || language.hasOutput()) {
            inputBox = new InputTextArea();
            JScrollPane inputScrollPane = new JScrollPane(inputBox);
            inputScrollPane.setPreferredSize(new Dimension(640, 100));

            outputBox = new JTextArea();
            outputBox.setEditable(false);
            JScrollPane outputScrollPane = new JScrollPane(outputBox);
            outputScrollPane.setPreferredSize(new Dimension(640, 100));

            JSplitPane ioSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                inputBox, outputBox);
            ioSplitPane.setOneTouchExpandable(true);
            ioSplitPane.setDividerLocation(-1);
            ioSplitPane.setResizeWeight(0.5);

            world.setInputTextArea(inputBox);
            world.setOutputTextArea(outputBox);
 
            if (languageComponent == null) {
                languageComponent = ioSplitPane;
            } else {
                JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                    languageComponent, ioSplitPane);
                splitPane.setOneTouchExpandable(true);
                splitPane.setDividerLocation(-1);
                splitPane.setResizeWeight(1.0); // the top is what gets resized
                languageComponent = splitPane;
            }
        }

        //---------- the tape(s) -----------
        if (language.numTapes() > 0) {
            JComponent tapesComponent = null;
            for (int tapeNum = 0; tapeNum < language.numTapes(); tapeNum++) {
                td[tapeNum] = new TapeDepiction();
                td[tapeNum].setBackground(Color.blue);
                td[tapeNum].setMinimumSize(new Dimension(100, 50));

                JScrollPane tapeScrollPane = new JScrollPane(td[tapeNum]);
                tapeScrollPane.setPreferredSize(new Dimension(200, 100));

                if (tapesComponent == null) {
                    tapesComponent = tapeScrollPane;
                } else {
                    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                        tapesComponent, tapeScrollPane);
                    splitPane.setOneTouchExpandable(true);
                    splitPane.setDividerLocation(-1);
                    splitPane.setResizeWeight(1.0 / language.numTapes());
                    tapesComponent = splitPane;
                }
            }
            if (languageComponent == null) {
                languageComponent = tapesComponent;
            } else {
                JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                    languageComponent, tapesComponent);
                splitPane.setOneTouchExpandable(true);
                splitPane.setDividerLocation(-1);
                splitPane.setResizeWeight(1.0); // the left is what gets resized
                languageComponent = splitPane;
            }
        }

        interiorPanel.add(languageComponent, BorderLayout.CENTER);

        List<String> exampleNames = language.exampleProgramNames();
        examplesMenu.setVisible(exampleNames.size() > 0);
        examplesMenu.removeAll();
        int index = 0;
        for (String name : exampleNames) {
            JMenuItem mi = new JMenuItem(name);
            examplesMenu.add(mi);
            mi.addActionListener(new ExampleMenuItemActionListener(this, index));
            index++;
        }

        List<String> optionNames = language.getAvailableOptionNames();
        optionsMenu.setVisible(optionNames.size() > 0);
        optionsMenu.removeAll();
        for (String name : optionNames) {
            JMenuItem mi = new JCheckBoxMenuItem(name);
            optionsMenu.add(mi);
            mi.addActionListener(new OptionMenuItemActionListener(this, name));
        }
        
        viewMenu.setVisible(true);

        // We just selected a language, but we don't have a program yet
        buttonReset.setEnabled(false);
        buttonStep.setEnabled(false);
        buttonWalk.setEnabled(false);
        buttonRun.setEnabled(false);
        buttonEdit.setEnabled(false);
        buttonAbout.setText("About " + language.getName() + "...");
        buttonAbout.setEnabled(true);

        refreshDepictions();
        this.setVisible(true);
    }

    protected void refreshDepictions() {
        if (currentState == null) return;
  
        if (language.hasProgramText()) {
            progBox.setText(currentState.getProgramText());
            int pos = currentState.getProgramPosition();
            progBox.highlightPosition(pos);
        }

        for (int pfNum = 0; pfNum <= language.numPlayfields(); pfNum++) {
            if (pd[pfNum] != null) {
                pd[pfNum].setPlayfield(currentState.getPlayfield(pfNum));
                pd[pfNum].setView(currentState.getPlayfieldView(pfNum));
                pd[pfNum].setZoom(zoom);
                pd[pfNum].setGridLines(gridLines);
                pd[pfNum].resize();
            }
        }

        for (int tapeNum = 0; tapeNum <= language.numTapes(); tapeNum++) {
            if (td[tapeNum] != null) {
                td[tapeNum].setTape(currentState.getTape(tapeNum));
                td[tapeNum].setView(currentState.getTapeView(tapeNum));
                td[tapeNum].setZoom(zoom);
                td[tapeNum].setGridLines(gridLines);
                td[tapeNum].resize();
            }
        }
    }

    protected void walk() {
        disableButtonsPerRun();
        runThread.setTurbo(false);
        runThread.proceed();
    }

    protected void run() {
        disableButtonsPerRun();
        runThread.setTurbo(true);
        runThread.proceed();
    }

    protected void disableButtonsPerRun() {
        languageMenu.setEnabled(false);
        examplesMenu.setEnabled(false);
        optionsMenu.setEnabled(false);
        viewMenu.setEnabled(false);
        buttonEdit.setEnabled(false);
        buttonReset.setEnabled(false);
        buttonStep.setEnabled(false);
        buttonWalk.setEnabled(false);
        buttonRun.setEnabled(false);
        buttonAbout.setEnabled(false);
        buttonStop.setEnabled(true);
    }

    protected void stop() {
        languageMenu.setEnabled(true);
        examplesMenu.setEnabled(true);
        optionsMenu.setEnabled(true);
        viewMenu.setEnabled(true);
        buttonEdit.setEnabled(true);
        buttonStop.setEnabled(false);
        buttonReset.setEnabled(true);
        buttonAbout.setEnabled(true);
        if (!currentState.hasHalted()) {
            buttonStep.setEnabled(true);
            buttonWalk.setEnabled(true);
            buttonRun.setEnabled(true);
        }
        runThread.halt();
    }

    /*
     * Returns true iff we are OK to call step again on this
     * state in the future.
     */
    protected boolean step() {
        if (currentState.hasHalted()) {
            return false;
        }
        currentState.step(world);
        refreshDepictions();
        if (currentState.hasHalted()) {
            buttonStep.setEnabled(false);
            buttonWalk.setEnabled(false);
            buttonRun.setEnabled(false);
        }
        if (currentState.needsInput()) {
            if (inputBox != null) {
                inputBox.setBackground(Color.red);
            }
        }
        return (!currentState.hasHalted());
    }

    protected void reset() {
        currentState = initialState.clone();
        buttonEdit.setEnabled(true);
        buttonWalk.setEnabled(true);
        buttonRun.setEnabled(true);
        buttonStep.setEnabled(true);
        buttonStop.setEnabled(false);
        buttonReset.setEnabled(true);
        if (inputBox != null) {
            inputBox.setText("");
            inputBox.setBackground(Color.white);
        }
        if (outputBox != null) {
            outputBox.setText("");
        }
        refreshDepictions();
    }

    static String oopsMessage =
        "Could not load example program.  This is likely due to your Java Runtime's policy preventing it from\n" +
        "fetching the example program from a remote URL.  To allow this, add the following section to\n" +
        "your java.policy file:\n" +
        "\n" +
        "    grant {\n" +
        "        permission java.lang.RuntimePermission \"getProtectionDomain\";\n" +
        "    };\n" +
        "\n" +
        "On Ubuntu, when using the OpenJDK, this file is located at /usr/lib/jvm/java-6-openjdk/jre/lib/security/java.policy.\n" +
        "\n" +
        "You will still be asked if you want to allow the applet to connect to the remote site, so this\n" +
        "change will have no significant impact on the security of your Java installation.  For more\n" +
        "information on Java Security Policies, see:\n" +
        "\n" +
        "    http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html";

    protected void loadExampleProgram(int index) {
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        try {
            initialState = language.loadExampleProgram(index);
            applyCurrentlySelectedOptions(initialState);
            reset();
        } catch (AccessControlException e) {
            System.out.println(e);
            if (e.toString().indexOf("getProtectionDomain") > 0) {
                displayModalMessage(oopsMessage, false);
            }
            // XXX otherwise display the exception itself?
        }
        setCursor(null);
    }

    protected void showAboutBox() {
        String properties[][] = language.getProperties();
        String x = "";
        for (int i = 0; i < properties.length; i++) {
            String[] pair = properties[i];
            x += pair[0] + "\n" + pair[1] + "\n\n";
        }
        displayModalMessage(x, true);
    }

    protected void showEditor() {
        this.setVisible(false);
        interiorPanel.remove(buttonPanel);
        if (languageComponent != null) {
            interiorPanel.remove(languageComponent);
        }
        interiorPanel.add(editPanel, BorderLayout.CENTER);
        editPanel.setVisible(true);
        editBox.setText(initialState.exportToText());
        languageMenu.setEnabled(false);
        examplesMenu.setEnabled(false);
        optionsMenu.setEnabled(false);
        viewMenu.setEnabled(false);
        this.setVisible(true);
    }

    protected void displayModalMessage(String message, boolean showEsowikiButton) {
        this.setVisible(false);
        interiorPanel.remove(buttonPanel);
        if (languageComponent != null) {
            interiorPanel.remove(languageComponent);
        }
        interiorPanel.add(aboutPanel, BorderLayout.CENTER);
        aboutPanel.setVisible(true);
        buttonEsowiki.setVisible(showEsowikiButton);
        aboutBox.setText(message);
        languageMenu.setEnabled(false);
        examplesMenu.setEnabled(false);
        optionsMenu.setEnabled(false);
        viewMenu.setEnabled(false);
        this.setVisible(true);
    }

    protected void setOption(String name, boolean value) {
        if (initialState != null)
            initialState.setOption(name, value);
        if (currentState != null)
            currentState.setOption(name, value);
    }

    private void applyCurrentlySelectedOptions(State s) {
        int numOptions = optionsMenu.getItemCount();
        for (int i = 0; i < numOptions; i++) {
            AbstractButton x = (AbstractButton)optionsMenu.getItem(i);
            s.setOption(x.getText(), x.getModel().isSelected());
        }
    }

    public String getTitle() {
        return title;
    }
}