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