1 package com.timjohnstondev.timer;
2
3 import java.awt.Dimension;
4 import java.awt.Window;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import javax.swing.BorderFactory;
8 import javax.swing.Box;
9 import javax.swing.BoxLayout;
10 import javax.swing.JButton;
11 import javax.swing.JLabel;
12 import javax.swing.JPanel;
13 import javax.swing.JSlider;
14 import javax.swing.event.ChangeEvent;
15 import javax.swing.event.ChangeListener;
16 import com.sun.awt.AWTUtilities;
17
18 public class TopPanel extends JPanel implements ActionListener
19 {
20 private CountDownPanel timerPanel;
21 private CountDownTimer timer;
22 private JSlider slider;
23 private JLabel sliderValue;
24 private JButton startButton;
25 private JButton reset30Button;
26
27 private final int sliderDefault = 70;
28 private final String startAction = "Start";
29 private final String stopAction = "Pause";
30 private final String reset30 = "30";
31 private final String percent = "%";
32
33 TopPanel(final CountDownPanel newTimerPanel)
34 {
35 timerPanel = newTimerPanel;
36 timer = timerPanel.getTimer();
37 configureSlider();
38 configureSliderValue();
39 configureStartButton();
40 configureReset30Button();
41
42 setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
43 setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 0));
44 add(startButton);
45 add(reset30Button);
46 add(Box.createHorizontalGlue());
47 add(slider);
48 add(Box.createRigidArea(new Dimension(5, 0)));
49 add(sliderValue);
50 }
51
52 final void setSliderValue(final int newValue)
53 {
54 slider.setValue(newValue);
55 }
56
57 final int getSliderValue()
58 {
59 return slider.getValue();
60 }
61
62 private void configureReset30Button()
63 {
64 reset30Button = new JButton(reset30);
65 reset30Button.setActionCommand(reset30);
66 reset30Button.addActionListener(this);
67 reset30Button.setMinimumSize(new Dimension(50, 30));
68 reset30Button.setPreferredSize(new Dimension(50, 30));
69 }
70
71 private void configureStartButton()
72 {
73 startButton = new JButton(startAction);
74 startButton.setActionCommand(startAction);
75 startButton.addActionListener(this);
76 startButton.setMinimumSize(new Dimension(90, 30));
77 startButton.setPreferredSize(new Dimension(90, 30));
78 }
79
80 private void configureSlider()
81 {
82 slider = new JSlider(10, 100, 70);
83 slider.setMaximumSize(new Dimension(150, 30));
84 slider.setValue(sliderDefault);
85 slider.addChangeListener(new SliderAction());
86 }
87
88 private void configureSliderValue()
89 {
90 sliderValue = new JLabel(sliderDefault + percent);
91 sliderValue.setMinimumSize(new Dimension(40, 30));
92 sliderValue.setPreferredSize(new Dimension(40, 30));
93 }
94
95 public final void actionPerformed(final ActionEvent ae)
96 {
97 final TimerApplication timerApp = (TimerApplication) getParent().getParent().getParent().getParent();
98 if (startAction.equals(ae.getActionCommand()))
99 {
100 timerPanel.setEnabled(false);
101 timerPanel.setRemainingTime();
102 timer.start();
103 startButton.setActionCommand(stopAction);
104 startButton.setText(stopAction);
105 timerApp.startDispayTimer();
106 }
107
108 else if (stopAction.equals(ae.getActionCommand()))
109 {
110 timerPanel.setEnabled(true);
111 timer.stop();
112 startButton.setActionCommand(startAction);
113 startButton.setText(startAction);
114 timerApp.stopDispayTimer();
115 }
116
117 else if (reset30.equals(ae.getActionCommand()))
118 {
119 timer.stop();
120 timer.initialize(CountDownTimer.COUNTER_DEFAULT);
121 timerPanel.setEnabled(false);
122 timer.start();
123 startButton.setActionCommand(stopAction);
124 startButton.setText(stopAction);
125 timerApp.startDispayTimer();
126 }
127 }
128
129 class SliderAction implements ChangeListener
130 {
131 public void stateChanged(final ChangeEvent ce)
132 {
133 final int value = slider.getValue();
134 sliderValue.setText(Integer.toString(value) + percent);
135 AWTUtilities.setWindowOpacity((Window) getTopLevelAncestor(), value / 100f);
136 }
137 }
138 }