1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Checkbox;
|
---|
5 | import java.awt.event.ItemEvent;
|
---|
6 | import java.awt.event.ItemListener;
|
---|
7 |
|
---|
8 | import javax.swing.JLabel;
|
---|
9 | import javax.swing.JPanel;
|
---|
10 |
|
---|
11 | import genius.core.listener.Listener;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Panel showing a checkbox with a title and value. This panel supports a
|
---|
15 | * BooleanModel (you can't with the default {@link Checkbox}).
|
---|
16 | */
|
---|
17 | @SuppressWarnings("serial")
|
---|
18 | public class CheckboxPanel extends JPanel {
|
---|
19 |
|
---|
20 | private JLabel label;
|
---|
21 | private Checkbox box;
|
---|
22 | private BooleanModel model;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | *
|
---|
26 | * @param text
|
---|
27 | * the text for the label
|
---|
28 | * @param boolModel
|
---|
29 | * the boolean model
|
---|
30 | */
|
---|
31 | public CheckboxPanel(final String text, final BooleanModel boolModel) {
|
---|
32 | this.model = boolModel;
|
---|
33 |
|
---|
34 | setLayout(new BorderLayout());
|
---|
35 | label = new JLabel(text);
|
---|
36 | box = new Checkbox("", boolModel.getValue());
|
---|
37 | enable1();
|
---|
38 |
|
---|
39 | // connect the box to the model,
|
---|
40 | box.addItemListener(new ItemListener() {
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void itemStateChanged(ItemEvent e) {
|
---|
44 | if (box.getState() != model.getValue()) {
|
---|
45 | model.setValue(box.getState());
|
---|
46 | enable1();
|
---|
47 | }
|
---|
48 | }
|
---|
49 | });
|
---|
50 |
|
---|
51 | // and the model to the box
|
---|
52 | model.addListener(new Listener<Boolean>() {
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void notifyChange(Boolean data) {
|
---|
56 | // FIXME invokeLater
|
---|
57 | box.setState(data);
|
---|
58 | enable1();
|
---|
59 | }
|
---|
60 | });
|
---|
61 |
|
---|
62 | add(box, BorderLayout.WEST);
|
---|
63 | add(label, BorderLayout.CENTER);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Update enabled-ness of panel. Just calling enable() doesn nothing?
|
---|
68 | */
|
---|
69 | private void enable1() {
|
---|
70 | boolean enabled = !model.isLocked();
|
---|
71 | box.setEnabled(enabled);
|
---|
72 | label.setEnabled(enabled);
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|