Coverage Report - com.liquidatom.derbyscore.ui.TimeTextFieldKeyListener
 
Classes in this File Line Coverage Branch Coverage Complexity
TimeTextFieldKeyListener
0%
0/29
0%
0/14
3.2
 
 1  
 package com.liquidatom.derbyscore.ui;
 2  
 
 3  
 import com.liquidatom.derbyscore.domain.Clock;
 4  
 import com.liquidatom.derbyscore.domain.Duration;
 5  
 import java.awt.EventQueue;
 6  
 import java.awt.event.KeyEvent;
 7  
 import java.awt.event.KeyListener;
 8  
 import java.util.concurrent.TimeUnit;
 9  
 import java.util.regex.Pattern;
 10  
 import javax.annotation.concurrent.Immutable;
 11  
 import javax.annotation.concurrent.ThreadSafe;
 12  
 import javax.swing.JTextField;
 13  
 import org.slf4j.Logger;
 14  
 import org.slf4j.LoggerFactory;
 15  
 
 16  
 /**
 17  
  *
 18  
  * @author Russell Francis (russ@metro-six.com)
 19  
  */
 20  
 @Immutable
 21  
 @ThreadSafe
 22  
 public class TimeTextFieldKeyListener implements KeyListener {
 23  
     
 24  0
     static private final Logger log = LoggerFactory.getLogger(TimeTextFieldKeyListener.class);
 25  0
     static private final Pattern colonPattern = Pattern.compile(":");
 26  
 
 27  
     final private JTextField timeTextField;
 28  
     final private Clock clock;
 29  
 
 30  
     public TimeTextFieldKeyListener(final JTextField timeTextField, final Clock clock) {
 31  0
         super();
 32  0
         if (timeTextField == null) {
 33  0
             throw new NullPointerException("The parameter timeTextField must be non-null.");
 34  
         }
 35  0
         if (clock == null) {
 36  0
             throw new NullPointerException("The parameter clock must be non-null.");
 37  
         }
 38  0
         this.timeTextField = timeTextField;
 39  0
         this.clock = clock;
 40  0
     }
 41  
 
 42  
     public void keyReleased(final KeyEvent e) {
 43  0
     }
 44  
 
 45  
     public void keyTyped(final KeyEvent e) {
 46  0
     }
 47  
 
 48  
     public void keyPressed(final KeyEvent e) {
 49  0
         assertDispatchThread();
 50  0
         if (KeyEvent.VK_ENTER == e.getKeyCode()) {
 51  0
             final String text = timeTextField.getText();
 52  0
             final String[] parts = colonPattern.split(text);
 53  0
             if (parts != null && parts.length == 2) {
 54  
                 try {
 55  0
                     int minutes = Integer.parseInt(parts[0]);
 56  0
                     int seconds = Integer.parseInt(parts[1]);
 57  
 //                    clock.setTimeRemaining((((minutes * 60) + seconds) * 1000) + 999L);
 58  0
                     clock.reset(new Duration(((minutes * 60) + seconds) * 1000, TimeUnit.MILLISECONDS));
 59  
                 }
 60  0
                 catch (NumberFormatException ex) {
 61  0
                     if (log.isWarnEnabled()) {
 62  0
                         log.warn("Unable to convert '" + parts[0] + "' or '" + parts[1] + "' into integers.");
 63  
                     }
 64  0
                 }
 65  
             }
 66  
 
 67  0
             timeTextField.setText(clock.getDisplayTime());
 68  
         }
 69  0
     }
 70  
 
 71  
     protected void assertDispatchThread() {
 72  0
         if (!EventQueue.isDispatchThread()) {
 73  0
             throw new IllegalStateException("This method must be invoked from within the AWT Event Dispatch Thread!");
 74  
         }
 75  0
     }
 76  
 }