blob: dee4fbaf9d0172c1042c58959c85bd26598eb6a7 (
plain)
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
|
package com.jetbrains.python.edu.ui;
import com.intellij.ui.DocumentAdapter;
import com.intellij.ui.components.JBScrollPane;
import org.jetbrains.annotations.NotNull;
import com.jetbrains.python.edu.course.UserTest;
import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.text.BadLocationException;
import java.awt.*;
public class StudyTestContentPanel extends JPanel {
public static final Dimension PREFERRED_SIZE = new Dimension(300, 200);
private static final Font HEADER_FONT = new Font("Arial", Font.BOLD, 16);
private final JTextArea myInputArea = new JTextArea();
private final JTextArea myOutputArea = new JTextArea();
public StudyTestContentPanel(UserTest userTest) {
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
initContentLabel("input", myInputArea);
myInputArea.getDocument().addDocumentListener(new BufferUpdater(userTest.getInputBuffer()));
myOutputArea.getDocument().addDocumentListener(new BufferUpdater(userTest.getOutputBuffer()));
initContentLabel("output", myOutputArea);
setEditable(userTest.isEditable());
}
private void initContentLabel(final String headerText, @NotNull final JTextArea contentArea) {
JLabel headerLabel = new JLabel(headerText);
headerLabel.setFont(HEADER_FONT);
this.add(headerLabel);
this.add(new JSeparator(SwingConstants.HORIZONTAL));
JScrollPane scroll = new JBScrollPane(contentArea);
scroll.setPreferredSize(PREFERRED_SIZE);
this.add(scroll);
}
private void setEditable(boolean isEditable) {
myInputArea.setEditable(isEditable);
myOutputArea.setEditable(isEditable);
}
public void addInputContent(final String content) {
myInputArea.setText(content);
}
public void addOutputContent(final String content) {
myOutputArea.setText(content);
}
private class BufferUpdater extends DocumentAdapter {
private final StringBuilder myBuffer;
private BufferUpdater(StringBuilder buffer) {
myBuffer = buffer;
}
@Override
protected void textChanged(DocumentEvent e) {
myBuffer.delete(0, myBuffer.length());
try {
myBuffer.append(e.getDocument().getText(0, e.getDocument().getLength()));
}
catch (BadLocationException e1) {
e1.printStackTrace();
}
}
}
}
|