-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeViewPanel.java
96 lines (88 loc) · 3.02 KB
/
CodeViewPanel.java
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
package pippin;
import java.util.Observer;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.Observable;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class CodeViewPanel implements Observer {
private MachineView machineView;
private Code code;
private JScrollPane scroller;
private JTextField[] codeText = new JTextField[Code.CODE_MAX];
private int previousColor = -1;
public CodeViewPanel(MachineView machineView) {
this.machineView = machineView;
machineView.addObserver(this);
}
public JComponent createCodeDisplay() {
JPanel returnPanel = new JPanel();
JPanel panel = new JPanel();
JPanel numPanel = new JPanel();
JPanel sourcePanel = new JPanel();
returnPanel.setPreferredSize(new Dimension(300,150));;
returnPanel.setLayout(new BorderLayout());
panel.setLayout(new BorderLayout());
numPanel.setLayout(new GridLayout(0,1));
sourcePanel.setLayout(new GridLayout(0,1));
for(int i = 0; i < Code.CODE_MAX; i++) {
numPanel.add(new JLabel(i+": ", JLabel.RIGHT));
codeText[i] = new JTextField(10);
sourcePanel.add(codeText[i]);
}
Border border = BorderFactory.createTitledBorder(
BorderFactory.createLineBorder(Color.BLACK), "Code Memory View",
TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION);
returnPanel.setBorder(border);
panel.add(numPanel, BorderLayout.LINE_START);
panel.add(sourcePanel, BorderLayout.CENTER);
scroller = new JScrollPane(panel);
returnPanel.add(scroller);
return returnPanel;
}
@Override
public void update(Observable arg0, Object arg1) {
if(arg1 != null && arg1.equals("Load Code")) {
code = machineView.getCode();
for(int i = 0; i < Code.CODE_MAX; i++) {
codeText[i].setText(code.getText(i));
}
previousColor = machineView.getProgramCounter();
codeText[previousColor].setBackground(Color.YELLOW);
}
if(arg1 != null && arg1.equals("Clear")) {
for(int i = 0; i < Code.CODE_MAX; i++) {
codeText[i].setText("");
}
if(previousColor >= 0 && previousColor < Code.CODE_MAX) {
codeText[previousColor].setBackground(Color.WHITE);
}
previousColor = -1;
}
if(this.previousColor >= 0 && previousColor < Code.CODE_MAX) {
codeText[previousColor].setBackground(Color.WHITE);
previousColor = machineView.getProgramCounter();
if(this.previousColor >= 0 && previousColor < Code.CODE_MAX) {
codeText[previousColor].setBackground(Color.YELLOW);
}
}
if(scroller != null && code != null && machineView!= null) {
JScrollBar bar= scroller.getVerticalScrollBar();
int pc = machineView.getProgramCounter();
if(pc < Code.CODE_MAX && codeText[pc] != null) {
Rectangle bounds = codeText[pc].getBounds();
bar.setValue(Math.max(0, bounds.y - 15*bounds.height));
}
}
}
}