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