Clover coverage report - Maven Clover report
Coverage timestamp: Thu Oct 11 2007 08:41:48 CEST
file stats: LOC: 131   Methods: 16
NCLOC: 89   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
KeyboardAnalyzer.java 50% 88% 81.2% 80.9%
coverage coverage
 1    /*
 2    * Copyright (C) 2007 Eric MIGNOT - mignots.eric@free.fr
 3    *
 4    * This library is free software; you can redistribute it and/or
 5    * modify it under the terms of the GNU Lesser General Public
 6    * License as published by the Free Software Foundation; either
 7    * version 2.1 of the License, or (at your option) any later version.
 8    *
 9    * This library is distributed in the hope that it will be useful,
 10    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 12    * Lesser General Public License for more details.
 13    *
 14    * You should have received a copy of the GNU Lesser General Public
 15    * License along with this library; if not, write to the Free Software
 16    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 17    */
 18    package org.ericmignot.modeler.util.misc;
 19   
 20    import java.awt.event.KeyEvent;
 21    import java.awt.event.KeyListener;
 22    import java.util.ArrayList;
 23    import java.util.List;
 24   
 25    public class KeyboardAnalyzer implements KeyListener
 26    {
 27   
 28    /**
 29    * Delay between 2 key released
 30    */
 31    private long delay;
 32   
 33    /**
 34    * The potential command build from kayboard entries
 35    */
 36    private String potentialCommand;
 37   
 38    /**
 39    * The time stamp of the last keyboard input
 40    *
 41    */
 42    private long lastInputTimeStamp;
 43   
 44    private List commands;
 45    private List commandListeners;
 46   
 47  122 public KeyboardAnalyzer()
 48    {
 49  122 this.setDelay(0);
 50  122 this.setLastInputTimeStamp(System.currentTimeMillis());
 51  122 this.commands = new ArrayList();
 52  122 this.commandListeners = new ArrayList();
 53  122 this.resetCommand();
 54    }
 55   
 56   
 57  119 public void addCommand (String command)
 58    {
 59  119 this.commands.add(command);
 60    }
 61  17 public boolean containsCommand(String command)
 62    {
 63  17 return this.commands.contains(command);
 64    }
 65  118 public void addCommandListener (CommandListener listener)
 66    {
 67  118 this.commandListeners.add(listener);
 68    }
 69  18 public long getDelay()
 70    {
 71  18 return delay;
 72    }
 73  243 public void setDelay(long delay)
 74    {
 75  243 this.delay = delay;
 76    }
 77  16 public long getLastInputTimeStamp()
 78    {
 79  16 return lastInputTimeStamp;
 80    }
 81  138 public void setLastInputTimeStamp(long lastInputDate)
 82    {
 83  138 this.lastInputTimeStamp = lastInputDate;
 84    }
 85  1 public void waitAtLeast(long ms)
 86    {
 87  1 this.lastInputTimeStamp = System.currentTimeMillis() - ms;
 88    }
 89   
 90  123 public void resetCommand()
 91    {
 92  123 this.potentialCommand = "";
 93    }
 94  16 public void addChar(char aChar)
 95    {
 96  16 this.potentialCommand += aChar;
 97    }
 98  18 public String getPotentialCommand()
 99    {
 100  18 return this.potentialCommand;
 101    }
 102   
 103  0 public void keyTyped(KeyEvent e) {}
 104   
 105  0 public void keyPressed(KeyEvent e) {}
 106   
 107  0 public void keyReleased(KeyEvent e)
 108    {
 109  0 treatChar(e.getKeyChar());
 110    }
 111   
 112  16 public synchronized void treatChar(char c)
 113    {
 114  16 long current = System.currentTimeMillis();
 115  16 if ((current - this.getLastInputTimeStamp() > this.getDelay()))
 116    {
 117  1 this.resetCommand();
 118    }
 119  16 this.addChar(c);
 120  16 if (this.containsCommand(this.getPotentialCommand()))
 121    {
 122  0 for (int i=0; i<this.commandListeners.size(); i++)
 123    {
 124  0 ((CommandListener) this.commandListeners.get(i))
 125    .commandFound(this.getPotentialCommand());
 126    }
 127    }
 128  16 this.setLastInputTimeStamp(current);
 129    }
 130   
 131    }