1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 |
|
---|
5 | import javax.swing.JPanel;
|
---|
6 | import javax.swing.JTextField;
|
---|
7 | import javax.swing.SwingUtilities;
|
---|
8 | import javax.swing.event.DocumentEvent;
|
---|
9 | import javax.swing.event.DocumentListener;
|
---|
10 |
|
---|
11 | import genius.core.listener.Listener;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Shows a single text line input area.
|
---|
15 | */
|
---|
16 | @SuppressWarnings("serial")
|
---|
17 | public class TextPanel extends JPanel {
|
---|
18 | private final JTextField textfield;
|
---|
19 |
|
---|
20 | public TextPanel(final TextModel model) {
|
---|
21 | setLayout(new BorderLayout());
|
---|
22 | this.textfield = new JTextField(model.getText());
|
---|
23 | add(textfield, BorderLayout.CENTER);
|
---|
24 | // not working?
|
---|
25 | // setMaximumSize(new Dimension(99999999, 30));
|
---|
26 | textfield.getDocument().addDocumentListener(new DocumentListener() {
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public void removeUpdate(DocumentEvent e) {
|
---|
30 | if (!textfield.getText().isEmpty()) {
|
---|
31 | model.setText(textfield.getText());
|
---|
32 | }
|
---|
33 | // we ignore empty text to avoid reacting on replace procedure
|
---|
34 | // too early
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void insertUpdate(DocumentEvent e) {
|
---|
39 | model.setText(textfield.getText());
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void changedUpdate(DocumentEvent e) {
|
---|
44 | model.setText(textfield.getText());
|
---|
45 | }
|
---|
46 | });
|
---|
47 |
|
---|
48 | model.addListener(new Listener<String>() {
|
---|
49 | @Override
|
---|
50 | public void notifyChange(final String data) {
|
---|
51 | SwingUtilities.invokeLater(new Runnable() {
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public void run() {
|
---|
55 | textfield.setText((String) data);
|
---|
56 | }
|
---|
57 | });
|
---|
58 | }
|
---|
59 | });
|
---|
60 |
|
---|
61 | }
|
---|
62 | public JTextField getTextField() {
|
---|
63 | return textfield;
|
---|
64 | }
|
---|
65 | }
|
---|