package edu.princeton.toy; import java.awt.*; import javax.swing.*; import edu.princeton.swing.*; /** * TReferencePane is a convenient subclass of JPanel which provides reference information regarding * the TOY language. * * @author btsang * @version 7.1 */ public class TReferencePane extends JPanel { /** * The text that is displayed in the reference text area. */ public static final String REFERENCE_TEXT = "INSTRUCTION FORMATS\n" + "\n" + " | .... | .... | .... | .... |\n" + "Format RR: | op | d | s | t |\n" + "Format A: | op | d | addr |\n" + "\n" + "\n" + "ARITHMETIC and LOGICAL operations\n" + " 1: add R[d] <- R[s] + R[t]\n" + " 2: subtract R[d] <- R[s] - R[t]\n" + " 3: and R[d] <- R[s] & R[t]\n" + " 4: xor R[d] <- R[s] ^ R[t]\n" + " 5: shift left R[d] <- R[s] << R[t]\n" + " 6: shift right R[d] <- R[s] >> R[t]\n" + "\n" + "TRANSFER between registers and memory\n" + " 7: load address R[d] <- addr\n" + " 8: load R[d] <- M[addr]\n" + " 9: store M[addr] <- R[d]\n" + " A: load indirect R[d] <- M[R[t]]\n" + " B: store indirect M[R[t]] <- R[d]\n" + "\n" + "CONTROL\n" + " 0: halt halt\n" + " C: branch zero if (R[d] == 0) PC <- addr\n" + " D: branch positive if (R[d] > 0) PC <- addr\n" + " E: jump register PC <- R[d]\n" + " F: jump and link R[d] <- PC; PC <- addr\n" + "\n" + "\n" + "Register 0 always reads 0.\n" + "Loads from M[FF] come from stdin.\n" + "Stores to M[FF] go to stdout.\n" + "\n" + "16-bit registers (two's complement)\n" + "16-bit memory locations\n" + " 8-bit program counter\n"; private JToolBar baseConverterToolbar; private Object lastToolbarLocation; /** * Constructs a new TReferencePane. */ public TReferencePane() { super(new BorderLayout()); { // Prepare the base converter toolbar baseConverterToolbar = new JToolBar(); baseConverterToolbar.setFont(null); { // Prepare the base converter pane TBaseConverterPane baseConverterPane = new TBaseConverterPane(); baseConverterPane.setFont(null); baseConverterToolbar.add(baseConverterPane); } lastToolbarLocation = BorderLayout.NORTH; add(baseConverterToolbar, BorderLayout.NORTH); } { // Prepare the language reference panel JPanel languageReferencePanel = new JPanel(new GridBagLayout()); languageReferencePanel.setFont(null); languageReferencePanel.add( new JLabel("Language Reference"), new GridBagConstraints( 0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(2, 2, 2, 2), 0, 0 ) ); PTextArea referenceTextArea = new PTextArea(REFERENCE_TEXT); referenceTextArea.setEditable(false); referenceTextArea.setFont(null); JScrollPane scrollPane = new JScrollPane( referenceTextArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED ); scrollPane.setFont(null); scrollPane.getViewport().setFont(null); languageReferencePanel.add( scrollPane, new GridBagConstraints( 0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(2, 2, 2, 2), 0, 0 ) ); add(languageReferencePanel, BorderLayout.CENTER); } } /** * Override setFont to pass the font onto the toolbar. */ public void setFont(Font font) { if (baseConverterToolbar != null) baseConverterToolbar.setFont(font); super.setFont(font); } /** * Override addImpl to prevent the JToolbar from being placed in either the BorderLayout.EAST * or BorderLayout.WEST. */ protected void addImpl(Component comp, Object constraints, int index) { if (comp == baseConverterToolbar) { if (constraints != BorderLayout.NORTH && constraints != BorderLayout.SOUTH) { constraints = lastToolbarLocation; baseConverterToolbar.setOrientation(JToolBar.HORIZONTAL); } else { lastToolbarLocation = constraints; } } super.addImpl(comp, constraints, index); } }