| 1 | package br.org.agilcoop.cursos.testes.unidade.timer; |
| 2 | |
| 3 | import java.awt.FlowLayout; |
| 4 | import java.awt.event.ActionEvent; |
| 5 | import java.awt.event.ActionListener; |
| 6 | import javax.swing.JButton; |
| 7 | import javax.swing.JFrame; |
| 8 | import javax.swing.JLabel; |
| 9 | import javax.swing.JSpinner; |
| 10 | import javax.swing.event.ChangeEvent; |
| 11 | import javax.swing.event.ChangeListener; |
| 12 | import br.org.agilcoop.cursos.testes.unidade.timer.eventos.UbuntuNotifyEvent; |
| 13 | |
| 14 | @SuppressWarnings("serial") |
| 15 | public class TimerFrame extends JFrame { |
| 16 | private final JSpinner hours = new JSpinner(); |
| 17 | private final JSpinner minutes = new JSpinner(); |
| 18 | private final JSpinner seconds = new JSpinner(); |
| 19 | private final JButton start = new JButton("\u22b3"); |
| 20 | private final JButton pause = new JButton("\u2225"); |
| 21 | private final JButton stop = new JButton("\u220e"); |
| 22 | private final JLabel elapsed = new JLabel(); |
| 23 | |
| 24 | private Timer timer = new Timer(); |
| 25 | private Thread thread; |
| 26 | |
| 27 | public TimerFrame() { |
| 28 | timer.addEvent(new UbuntuNotifyEvent()); |
| 29 | updateElapsed(); |
| 30 | setTitle("Timer"); |
| 31 | setLayout(new FlowLayout()); |
| 32 | add(hours); |
| 33 | add(minutes); |
| 34 | add(seconds); |
| 35 | add(start); |
| 36 | add(pause); |
| 37 | add(stop); |
| 38 | add(elapsed); |
| 39 | |
| 40 | ChangeListener changeListener = new ChangeListener() { |
| 41 | public void stateChanged(ChangeEvent e) { |
| 42 | adjustTimer(); |
| 43 | updateElapsed(); |
| 44 | } |
| 45 | }; |
| 46 | hours.addChangeListener(changeListener); |
| 47 | minutes.addChangeListener(changeListener); |
| 48 | seconds.addChangeListener(changeListener); |
| 49 | |
| 50 | start.addActionListener(new ActionListener() { |
| 51 | public void actionPerformed(ActionEvent e) { |
| 52 | if(!timer.running() && !timer.finished()) { |
| 53 | thread = new Thread(new Runnable() { |
| 54 | public void run() { |
| 55 | while(!timer.finished()) { |
| 56 | try { |
| 57 | updateElapsed(); |
| 58 | Thread.sleep(1000); |
| 59 | } catch (InterruptedException e) { |
| 60 | } |
| 61 | } |
| 62 | updateElapsed(); |
| 63 | } |
| 64 | }); |
| 65 | thread.start(); |
| 66 | } |
| 67 | |
| 68 | timer.start(); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | pause.addActionListener(new ActionListener() { |
| 73 | public void actionPerformed(ActionEvent e) { |
| 74 | timer.pause(); |
| 75 | if(thread != null && !thread.isInterrupted()) thread.interrupt(); |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | stop.addActionListener(new ActionListener() { |
| 80 | public void actionPerformed(ActionEvent e) { |
| 81 | timer.stop(); |
| 82 | if(thread != null && !thread.isInterrupted()) thread.interrupt(); |
| 83 | updateElapsed(); |
| 84 | } |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | private void updateElapsed() { |
| 89 | elapsed.setText(timer.elapsedFormatted() + " / " + timer.totalFormatted()); |
| 90 | } |
| 91 | |
| 92 | private void adjustTimer() { |
| 93 | timer.adjust( |
| 94 | Integer.parseInt(hours.getValue().toString()), |
| 95 | Integer.parseInt(minutes.getValue().toString()), |
| 96 | Integer.parseInt(seconds.getValue().toString())); |
| 97 | } |
| 98 | |
| 99 | public static void main(String[] args) { |
| 100 | TimerFrame timerFrame = new TimerFrame(); |
| 101 | timerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 102 | timerFrame.pack(); |
| 103 | timerFrame.setSize(500, 100); |
| 104 | timerFrame.setVisible(true); |
| 105 | } |
| 106 | } |