1 | package tudelft.mentalhealth.motivatepersisting;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.FlowLayout;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.ActionListener;
|
---|
7 | import java.util.Locale;
|
---|
8 |
|
---|
9 | import javax.swing.JButton;
|
---|
10 | import javax.swing.JComboBox;
|
---|
11 | import javax.swing.JFrame;
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JTextArea;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * An interactive example showing the {@link MotivationalAnswer}
|
---|
18 | */
|
---|
19 | public class Example extends JFrame {
|
---|
20 | private final ComboWithLabel<PclTrend> trend = new ComboWithLabel<PclTrend>(
|
---|
21 | PclTrend.values(), "PCL Trend");
|
---|
22 | private final ComboWithLabel<Trust> trust = new ComboWithLabel<Trust>(
|
---|
23 | Trust.values(), "trust");
|
---|
24 | private final JButton textButton = new JButton("make text");
|
---|
25 | private final JTextArea text = new JTextArea(4, 50);
|
---|
26 | private final Locale NL = new Locale("NL", "nl");
|
---|
27 | private final Locale EN = new Locale("en", "");
|
---|
28 | private final ComboWithLabel<Locale> locales = new ComboWithLabel<Locale>(
|
---|
29 | new Locale[] { NL, EN }, "language");
|
---|
30 |
|
---|
31 | public Example() {
|
---|
32 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
---|
33 | add(panel());
|
---|
34 |
|
---|
35 | textButton.addActionListener(new ActionListener() {
|
---|
36 | @Override
|
---|
37 | public void actionPerformed(ActionEvent e) {
|
---|
38 | Situation situation = new Situation(trend.getSelectedItem(),
|
---|
39 | trust.getSelectedItem());
|
---|
40 | Locale locale = locales.getSelectedItem();
|
---|
41 | text.setText(
|
---|
42 | new MotivationalAnswer(situation, locale).getText());
|
---|
43 | }
|
---|
44 | });
|
---|
45 |
|
---|
46 | pack();
|
---|
47 | setVisible(true);
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | private JPanel panel() {
|
---|
52 | JPanel panel = new JPanel(new BorderLayout());
|
---|
53 | text.setWrapStyleWord(true);
|
---|
54 | text.setLineWrap(true);
|
---|
55 | panel.add(text, BorderLayout.CENTER);
|
---|
56 | JPanel buttons = new JPanel(new FlowLayout());
|
---|
57 | buttons.add(trend);
|
---|
58 | buttons.add(trust);
|
---|
59 | buttons.add(locales);
|
---|
60 | buttons.add(textButton);
|
---|
61 | panel.add(buttons, BorderLayout.SOUTH);
|
---|
62 |
|
---|
63 | return panel;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static void main(String[] args) {
|
---|
67 | new Example();
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Combobox with a label
|
---|
73 | *
|
---|
74 | * @param <T> the element type of the combobox.
|
---|
75 | */
|
---|
76 | @SuppressWarnings("serial")
|
---|
77 | class ComboWithLabel<T> extends JPanel {
|
---|
78 | private JComboBox combo;
|
---|
79 |
|
---|
80 | public ComboWithLabel(T[] items, String label) {
|
---|
81 | this.combo = new JComboBox<>(items);
|
---|
82 | setLayout(new BorderLayout());
|
---|
83 | add(new JLabel(" " + label), BorderLayout.NORTH);
|
---|
84 | add(combo, BorderLayout.SOUTH);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @SuppressWarnings("unchecked")
|
---|
88 | public T getSelectedItem() {
|
---|
89 | return (T) combo.getSelectedItem();
|
---|
90 | }
|
---|
91 | } |
---|