Checkpoint of my Swing UI refactoring. Usable for output-only programs, but only barely.

This commit is contained in:
Seth Morabito committed 2012-05-28 22:18:35 -07:00
1 parent 5ce81ef82c
commit ed36690e9e
13 files changed
+776 -659

No files matched your search

@@ -7,6 +7,10 @@ import java.awt.event.MouseListener;
import com.grahamedgecombe.jterminal.JTerminal;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
/**
* The Console is a simulated 80 column x 24 row VT-100 terminal attached to
* the ACIA of the system. It provides basic keyboard I/O to Symon.
@@ -14,11 +18,14 @@ import com.grahamedgecombe.jterminal.JTerminal;
public class Console extends JTerminal implements KeyListener, MouseListener {
// TODO: Pass in io threads, read and write to ACIA.
public Console() {
super();
addKeyListener(this);
addMouseListener(this);
Border emptyBorder = BorderFactory.createEmptyBorder(5, 5, 5, 0);
Border bevelBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
Border compoundBorder = BorderFactory.createCompoundBorder(emptyBorder, bevelBorder);
this.setBorder(compoundBorder);
}
public void reset() {
@@ -0,0 +1,124 @@
package com.loomcom.symon.ui;
import com.loomcom.symon.Preferences;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
public class PreferencesDialog extends Observable implements Preferences {
private final JDialog dialog;
private JTextField aciaAddressField;
private JTextField programLoadAddressField;
private int programLoadAddress = DEFAULT_PROGRAM_LOAD_ADDRESS;
private int aciaAddress = DEFAULT_ACIA_ADDRESS;
public PreferencesDialog(Frame parent, boolean modal) {
this.dialog = new JDialog(parent, modal);
initComponents();
updateUi();
}
public JDialog getDialog() {
return dialog;
}
private void initComponents() {
dialog.setTitle("Preferences");
Container contents = dialog.getContentPane();
JPanel settingsContainer = new JPanel();
JPanel buttonsContainer = new JPanel();
GridBagLayout layout = new GridBagLayout();
settingsContainer.setLayout(layout);
final JLabel aciaAddressLabel = new JLabel("ACIA Address");
final JLabel programLoadAddressLabel = new JLabel("Program Load Address");
aciaAddressField = new JTextField(8);
programLoadAddressField = new JTextField(8);
aciaAddressLabel.setLabelFor(aciaAddressField);
programLoadAddressLabel.setLabelFor(programLoadAddressField);
GridBagConstraints constraints = new GridBagConstraints();
constraints.insets = new Insets(5, 5, 5, 5);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.gridx = 0;
constraints.gridy = 0;
settingsContainer.add(aciaAddressLabel, constraints);
constraints.gridx = 1;
settingsContainer.add(aciaAddressField, constraints);
constraints.gridy = 1;
constraints.gridx = 0;
settingsContainer.add(programLoadAddressLabel, constraints);
constraints.gridx = 1;
settingsContainer.add(programLoadAddressField, constraints);
JButton applyButton = new JButton("Apply");
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
updateUi();
dialog.setVisible(false);
}
});
applyButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
programLoadAddress = hexToInt(programLoadAddressField.getText());
aciaAddress = hexToInt(aciaAddressField.getText());
updateUi();
notifyObservers();
dialog.setVisible(false);
}
});
buttonsContainer.add(applyButton);
buttonsContainer.add(cancelButton);
contents.add(settingsContainer, BorderLayout.PAGE_START);
contents.add(buttonsContainer, BorderLayout.PAGE_END);
dialog.pack();
}
private String intToHex(int i) {
return String.format("%04x", i);
}
private int hexToInt(String s) {
try {
return Integer.parseInt(s, 16);
} catch (NumberFormatException ex) {
// TODO: Handle Displaying error.
return 0;
}
}
public int getProgramStartAddress() {
return programLoadAddress;
}
public int getAciaAddress() {
return aciaAddress;
}
public void updateUi() {
aciaAddressField.setText(intToHex(aciaAddress));
programLoadAddressField.setText(intToHex(programLoadAddress));
}
}
@@ -6,12 +6,10 @@ package com.loomcom.symon.ui;
import com.loomcom.symon.Cpu;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
public class StatusPane extends JPanel {
// The CPU to ask for state information.
private final Cpu cpu;
public class StatusPanel extends JPanel {
private final ImageIcon carryOn;
private final ImageIcon carryOff;
@@ -36,27 +34,36 @@ public class StatusPane extends JPanel {
private final JLabel overflowFlagLabel;
private final JLabel negativeFlagLabel;
private JTextField opcodeField;
private JTextField pcField;
private JTextField aField;
private JTextField xField;
private JTextField yField;
// private JTextField stepCountField;
private final JLabel opcodeLabel;
private final JLabel pcLabel;
private final JLabel aLabel;
private final JLabel xLabel;
private final JLabel yLabel;
private final JLabel stepCountLabel;
// private final JLabel stepCountLabel;
private static final int WIDTH = 134;
private static final int HEIGHT = 27;
private static final int WIDTH = 140;
private static final int HEIGHT = 200;
public StatusPane(Cpu cpu) {
public StatusPanel() {
super();
this.cpu = cpu;
Dimension dimensions = new Dimension(WIDTH, HEIGHT);
setMinimumSize(dimensions);
setPreferredSize(dimensions);
setMaximumSize(dimensions);
setLayout(new GridLayout(13,1));
JPanel statusFlagsPanel = new JPanel();
carryOn = new ImageIcon(this.getClass().getResource("images/C_on.png"));
carryOff = new ImageIcon(this.getClass().getResource("images/C_off.png"));
zeroOn = new ImageIcon(this.getClass().getResource("images/Z_on.png"));
@@ -81,52 +88,83 @@ public class StatusPane extends JPanel {
overflowFlagLabel = new JLabel(overflowOff, JLabel.CENTER);
negativeFlagLabel = new JLabel(negativeOff, JLabel.CENTER);
Border emptyBorder = BorderFactory.createEmptyBorder(8, 6, 0, 6);
// Create and add register and address labels
opcodeLabel = new JLabel("Instruction");
opcodeLabel.setBorder(emptyBorder);
pcLabel = new JLabel("Program Counter");
pcLabel.setBorder(emptyBorder);
aLabel = new JLabel("Accumulator");
aLabel.setBorder(emptyBorder);
xLabel = new JLabel("X Register");
xLabel.setBorder(emptyBorder);
yLabel = new JLabel("Y Register");
yLabel.setBorder(emptyBorder);
// stepCountLabel = new JLabel("Steps");
// stepCountLabel.setBorder(emptyBorder);
this.opcodeLabel = new JLabel();
this.pcLabel = new JLabel();
this.aLabel = new JLabel();
this.xLabel = new JLabel();
this.yLabel = new JLabel();
this.stepCountLabel = new JLabel();
opcodeField = new JTextField("");
pcField = new JTextField("");
aField = new JTextField("");
xField = new JTextField("");
yField = new JTextField("");
// stepCountField = new JTextField("");
this.opcodeLabel.setMinimumSize(new Dimension(100, 20));
this.pcLabel.setMinimumSize(new Dimension(80, 20));
this.aLabel.setMinimumSize(new Dimension(60, 20));
this.xLabel.setMinimumSize(new Dimension(60, 20));
this.yLabel.setMinimumSize(new Dimension(60, 20));
this.stepCountLabel.setMinimumSize(new Dimension(120, 20));
opcodeField.setEditable(false);
pcField.setEditable(false);
aField.setEditable(false);
xField.setEditable(false);
yField.setEditable(false);
// stepCountField.setEditable(false);
this.opcodeLabel.setPreferredSize(new Dimension(100, 20));
this.pcLabel.setPreferredSize(new Dimension(80, 20));
this.aLabel.setPreferredSize(new Dimension(60, 20));
this.xLabel.setPreferredSize(new Dimension(60, 20));
this.yLabel.setPreferredSize(new Dimension(60, 20));
this.stepCountLabel.setPreferredSize(new Dimension(120, 20));
opcodeLabel.setMinimumSize(new Dimension(100, 20));
pcLabel.setMinimumSize(new Dimension(80, 20));
aLabel.setMinimumSize(new Dimension(60, 20));
xLabel.setMinimumSize(new Dimension(60, 20));
yLabel.setMinimumSize(new Dimension(60, 20));
// stepCountLabel.setMinimumSize(new Dimension(120, 20));
this.setLayout(new FlowLayout());
opcodeLabel.setPreferredSize(new Dimension(100, 20));
pcLabel.setPreferredSize(new Dimension(80, 20));
aLabel.setPreferredSize(new Dimension(60, 20));
xLabel.setPreferredSize(new Dimension(60, 20));
yLabel.setPreferredSize(new Dimension(60, 20));
// stepCountLabel.setPreferredSize(new Dimension(120, 20));
this.add(negativeFlagLabel);
this.add(overflowFlagLabel);
this.add(breakFlagLabel);
this.add(decimalModeFlagLabel);
this.add(irqDisableFlagLabel);
this.add(zeroFlagLabel);
this.add(carryFlagLabel);
statusFlagsPanel.add(negativeFlagLabel);
statusFlagsPanel.add(overflowFlagLabel);
statusFlagsPanel.add(breakFlagLabel);
statusFlagsPanel.add(decimalModeFlagLabel);
statusFlagsPanel.add(irqDisableFlagLabel);
statusFlagsPanel.add(zeroFlagLabel);
statusFlagsPanel.add(carryFlagLabel);
this.add(opcodeLabel);
this.add(pcLabel);
this.add(aLabel);
this.add(xLabel);
this.add(yLabel);
this.add(stepCountLabel);
add(statusFlagsPanel);
add(opcodeLabel);
add(opcodeField);
add(pcLabel);
add(pcField);
add(aLabel);
add(aField);
add(xLabel);
add(xField);
add(yLabel);
add(yField);
// add(stepCountLabel);
// add(stepCountField);
updateState();
setBorder(BorderFactory.createBevelBorder(3));
}
public void updateState() {
/**
* Update the display based on the current state of the CPU.
*
* @param cpu The simulated 6502 CPU.
*/
public void updateState(Cpu cpu) {
// Update the Processor Status Flag display
int state = this.cpu.getProcessorStatus();
int state = cpu.getProcessorStatus();
carryFlagLabel.setIcon(iconForFlag(state, 0));
zeroFlagLabel.setIcon(iconForFlag(state, 1));
@@ -137,12 +175,12 @@ public class StatusPane extends JPanel {
negativeFlagLabel.setIcon(iconForFlag(state, 7));
// Update the register and address displays
opcodeLabel.setText(cpu.getOpcodeStatus());
pcLabel.setText(cpu.getProgramCounterStatus());
aLabel.setText(cpu.getARegisterStatus());
xLabel.setText(cpu.getXRegisterStatus());
yLabel.setText(cpu.getYRegisterStatus());
stepCountLabel.setText(Long.toString(cpu.getStepCounter()));
opcodeField.setText(cpu.getOpcodeStatus());
pcField.setText(cpu.getProgramCounterStatus());
aField.setText(cpu.getARegisterStatus());
xField.setText(cpu.getXRegisterStatus());
yField.setText(cpu.getYRegisterStatus());
// stepCountField.setText(Long.toString(cpu.getStepCounter()));
repaint();
}
@@ -1,63 +0,0 @@
package com.loomcom.symon.ui;
import com.loomcom.symon.MainWindow;
/**
* Update the console and status display. All direct manipulation of the
* main UI should go through this class. When not called from the Swing
* event dispatch thread, these methods should be called using
* SwingUtilities.invokeLater.
*/
public class UiUpdater {
private final Console console;
private final MainWindow mainWindow;
private final StatusPane statusPane;
private final StringBuffer data;
public UiUpdater(MainWindow mainWindow) {
this.mainWindow = mainWindow;
this.console = mainWindow.getConsole();
this.statusPane = mainWindow.getStatusPane();
this.data = new StringBuffer();
}
public void consoleWrite(int i) {
data.append((char) i);
}
/**
* Callback called by the simulator before exiting its run method.
*/
public void simulatorDidStop() {
mainWindow.getStepButton().setEnabled(true);
mainWindow.getLoadButton().setEnabled(true);
mainWindow.getRunButton().setText("Run");
updateUi();
}
/**
* Callback called by the simulator when entering its run method.
*/
public void simulatorDidStart() {
// Don't allow step while the simulator is running
mainWindow.getStepButton().setEnabled(false);
mainWindow.getLoadButton().setEnabled(false);
// Toggle the state of the run button
mainWindow.getRunButton().setText("Stop");
updateUi();
}
public void updateUi() {
// Update the console with any text
if (data.length() > 0) {
console.getModel().print(data.toString());
console.repaint();
// Clear the buffer
data.delete(0, data.length());
}
// Update the status UI.
statusPane.updateState();
}
}