1 | package genius.gui.deadline;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 |
|
---|
9 | import javax.swing.JComboBox;
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.JSpinner;
|
---|
13 | import javax.swing.SpinnerNumberModel;
|
---|
14 | import javax.swing.event.ChangeEvent;
|
---|
15 | import javax.swing.event.ChangeListener;
|
---|
16 |
|
---|
17 | import genius.core.DeadlineType;
|
---|
18 | import genius.core.listener.Listener;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * panel allowing user to set the deadline.
|
---|
22 | *
|
---|
23 | * @author W.Pasman
|
---|
24 | *
|
---|
25 | */
|
---|
26 | @SuppressWarnings("serial")
|
---|
27 | public class DeadlinePanel extends JPanel {
|
---|
28 | private final SpinnerNumberModel valuemodel = new SpinnerNumberModel(60, 1,
|
---|
29 | 10000, 10);
|
---|
30 | private JSpinner spinner = new JSpinner(valuemodel);
|
---|
31 | private JComboBox<DeadlineType> combobox = new JComboBox<DeadlineType>(
|
---|
32 | DeadlineType.values());
|
---|
33 | private JLabel label = new JLabel("Deadline");
|
---|
34 | private DeadlineModel model;
|
---|
35 |
|
---|
36 | public DeadlinePanel(final DeadlineModel model) {
|
---|
37 | if (model == null)
|
---|
38 | throw new NullPointerException("model");
|
---|
39 | this.model = model;
|
---|
40 | setLayout(new BorderLayout());
|
---|
41 | label.setPreferredSize(new Dimension(120, 10));
|
---|
42 | add(label, BorderLayout.WEST);
|
---|
43 | add(spinner, BorderLayout.CENTER);
|
---|
44 | add(combobox, BorderLayout.EAST);
|
---|
45 | setMaximumSize(new Dimension(Short.MAX_VALUE, 30));
|
---|
46 | setAlignmentX(Component.RIGHT_ALIGNMENT);
|
---|
47 |
|
---|
48 | syncWithModel();
|
---|
49 |
|
---|
50 | spinner.addChangeListener(new ChangeListener() {
|
---|
51 | @Override
|
---|
52 | public void stateChanged(ChangeEvent e) {
|
---|
53 | model.setValue((Integer) valuemodel.getValue());
|
---|
54 | }
|
---|
55 | });
|
---|
56 | combobox.addActionListener(new ActionListener() {
|
---|
57 | @Override
|
---|
58 | public void actionPerformed(ActionEvent e) {
|
---|
59 | model.setType((DeadlineType) combobox.getSelectedItem());
|
---|
60 | }
|
---|
61 | });
|
---|
62 |
|
---|
63 | model.addListener(new Listener<DeadlineModel>() {
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | public void notifyChange(DeadlineModel data) {
|
---|
67 | syncWithModel();
|
---|
68 | }
|
---|
69 |
|
---|
70 | });
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Sync panel with the model.
|
---|
75 | */
|
---|
76 | private void syncWithModel() {
|
---|
77 | spinner.setValue(model.getDeadline().getValue());
|
---|
78 | combobox.setSelectedItem(model.getDeadline().getType());
|
---|
79 | }
|
---|
80 |
|
---|
81 | }
|
---|