1 | package genius.gui.panels;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Dimension;
|
---|
5 |
|
---|
6 | import javax.swing.JLabel;
|
---|
7 | import javax.swing.JPanel;
|
---|
8 | import javax.swing.JSlider;
|
---|
9 | import javax.swing.JSpinner;
|
---|
10 |
|
---|
11 | import genius.core.listener.Listener;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Shows slider plus spinner with optional ticks, optionally logarithmic, and
|
---|
15 | * optionally with percentages
|
---|
16 | *
|
---|
17 | */
|
---|
18 | @SuppressWarnings("serial")
|
---|
19 | public class SliderPanel extends JPanel {
|
---|
20 | private IntegerModel model;
|
---|
21 | private JSlider slider;
|
---|
22 | private JLabel label;
|
---|
23 | private JSpinner spinner;
|
---|
24 |
|
---|
25 | public SliderPanel(String name, IntegerModel m) {
|
---|
26 | model = m;
|
---|
27 | setLayout(new BorderLayout());
|
---|
28 | label = new JLabel(name);
|
---|
29 | add(label, BorderLayout.WEST);
|
---|
30 |
|
---|
31 | slider = new JSlider(new IntegerToBoundeRangeAdapter(model));
|
---|
32 | add(slider, BorderLayout.CENTER);
|
---|
33 | spinner = new JSpinner(model.getSpinnerModel());
|
---|
34 | spinner.setMaximumSize(new Dimension(300, 30));
|
---|
35 | add(spinner, BorderLayout.EAST);
|
---|
36 |
|
---|
37 | // connect model->enabled to GUI appearance.
|
---|
38 | model.addListener(new Listener<Integer>() {
|
---|
39 | @Override
|
---|
40 | public void notifyChange(Integer data) {
|
---|
41 | updateEnabledness();
|
---|
42 | }
|
---|
43 |
|
---|
44 | });
|
---|
45 | updateEnabledness();
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | * @return the slider part. Used for testing, you should not need this and
|
---|
51 | * communicate through the model you provided.
|
---|
52 | */
|
---|
53 | public JSlider getSlider() {
|
---|
54 | return slider;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void updateEnabledness() {
|
---|
58 | boolean enabled = !model.isLocked();
|
---|
59 | setEnabled(enabled);
|
---|
60 | label.setEnabled(enabled);
|
---|
61 | slider.setEnabled(enabled);
|
---|
62 | spinner.setEnabled(enabled);
|
---|
63 | }
|
---|
64 |
|
---|
65 | }
|
---|