Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 232   Methods: 20
NCLOC: 198   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ArrowButton.java 0% 0% 0% 0%
coverage
 1   
 package example;
 2   
 
 3   
 import java.awt.*;
 4   
 import java.awt.event.*;
 5   
 
 6   
 import javax.swing.JPanel;
 7   
 import javax.swing.border.EmptyBorder;
 8   
 /**
 9   
  * Source code for Tutorial 1.
 10   
  * Simple arrow button that fires one event when first clicked, then sends a
 11   
  * stream of events while held down. 
 12   
  */
 13   
 
 14   
 public class ArrowButton extends JPanel {
 15   
     /** Directions. */
 16   
     public static final String 
 17   
         LEFT = "LEFT", 
 18   
         RIGHT = "RIGHT", 
 19   
         UP = "UP", 
 20   
         DOWN = "DOWN";
 21   
 
 22   
     private String direction;
 23   
     private String lastPaintedDirection;
 24   
     private static ArrowRoller roller;
 25   
     private boolean down;
 26   
     private boolean in;
 27   
     private ActionListener listeners;
 28   
     private static final int SIZE = 12;
 29   
     private static final int BORDER = 2;
 30   
 
 31   
     private static class ArrowRoller extends Thread {
 32   
         /** How long to wait before repeating. */
 33   
         private static final int DELAY_MS = 400;
 34   
         /** How long between repeated firings. */
 35   
         private static final int REPEAT_MS = 100;
 36   
         private boolean die = false;
 37   
         private ArrowButton target;
 38   
         private int state;
 39   
 
 40  0
         ArrowRoller () {
 41  0
             super ("ArrowRoller");
 42  0
             setDaemon(true);
 43   
         }
 44   
 
 45  0
         synchronized void abort () {
 46  0
             die = true;
 47  0
             notify ();
 48  0
             target = null;
 49  0
             ++ state;
 50   
         }
 51   
 
 52  0
         synchronized void addListener (ArrowButton t) {
 53  0
             target = t;
 54  0
             ++ state;
 55  0
             notify ();
 56   
         }
 57   
 
 58  0
         synchronized void removeListener (ArrowButton t) {
 59  0
             if (target == t) {
 60  0
                 target = null;
 61  0
                 ++ state;
 62   
             }
 63   
         }
 64   
 
 65  0
         public void run () {
 66  0
             while (!die) {
 67  0
                 try {
 68  0
                     ArrowButton target;
 69  0
                     int state;
 70  0
                     synchronized (this) {
 71  0
                         while ((target = this.target) == null)
 72  0
                             wait ();
 73  0
                         state = this.state;
 74   
                     }
 75  0
                     sleep (DELAY_MS);
 76  0
                     while (state == this.state) {
 77  0
                         target.fireActionEvent ();
 78  0
                         sleep (REPEAT_MS);
 79   
                     }
 80   
                 } catch (InterruptedException ex) {
 81   
                 }
 82   
             }
 83   
         }
 84   
     }
 85   
 
 86  0
     public ArrowButton () {
 87  0
         this(LEFT);
 88   
     }
 89   
 
 90  0
     public ArrowButton (String direction) {
 91  0
         enableEvents (AWTEvent.MOUSE_EVENT_MASK);
 92  0
         setBorder(new EmptyBorder(BORDER, BORDER, BORDER, BORDER));
 93  0
         lastPaintedDirection = direction;
 94  0
         setDirection(direction);
 95   
     }
 96   
 
 97  0
     public void setDirection (String d) {
 98  0
         if (d.equals(LEFT))
 99  0
             direction = LEFT;
 100  0
         else if (d.equals(RIGHT))
 101  0
             direction = RIGHT;
 102  0
         else if (d.equals(UP))
 103  0
             direction = UP;
 104   
         else
 105  0
             direction = DOWN;
 106  0
         repaint();
 107   
     }
 108   
 
 109  0
     public String getDirection () {
 110  0
         return direction;
 111   
     }
 112   
 
 113  0
     public Dimension getMinimumSize () {
 114  0
         return getPreferredSize ();
 115   
     }
 116   
 
 117  0
     public Dimension getPreferredSize () {
 118  0
         return new Dimension (SIZE, SIZE);
 119   
     }
 120   
 
 121  0
     public Dimension getMaximumSize () {
 122  0
         return getPreferredSize ();
 123   
     }
 124   
 
 125  0
     public void update (Graphics g) {
 126  0
         if (lastPaintedDirection == direction) {
 127  0
             paint (g);
 128   
         }
 129   
         else {
 130  0
             super.update (g);
 131  0
             lastPaintedDirection = direction;
 132   
         }
 133   
     }
 134   
 
 135  0
     public void paint (Graphics g) {
 136  0
         super.paint(g);
 137  0
         int w = getSize ().width, h = getSize ().height;
 138  0
         g.setColor ((in && down) ? getBackground ().brighter () :
 139   
                     getBackground ().darker ());
 140  0
         if (direction == LEFT) {
 141  0
             g.drawLine (w - 2, 2, w - 2, h - 2);
 142  0
             g.drawLine (w - 2, h - 2, 2, (h - 2) / 2);
 143  0
             g.setColor ((in && down) ? getBackground ().darker () :
 144   
                         getBackground().brighter());
 145  0
             g.drawLine (2, (h - 2) / 2, w - 2, 2);
 146   
         }
 147  0
         else if (direction == RIGHT) {
 148  0
             g.drawLine (2, 2, w - 2, (h - 2) / 2);
 149  0
             g.drawLine (2, h - 2, w - 2, (h - 2) / 2);
 150  0
             g.setColor ((in && down) ? getBackground ().darker () :
 151   
                         getBackground().brighter());
 152  0
             g.drawLine (2, 2, 2, h - 2);
 153   
         }
 154  0
         else if (direction == UP){
 155  0
             g.drawLine (2, h - 2, w - 2, h - 2);
 156  0
             g.drawLine (w - 2, h - 2, (w - 2) / 2, 2);
 157  0
             g.setColor ((in && down) ? getBackground ().darker () :
 158   
                         getBackground().brighter());
 159  0
             g.drawLine (2, h - 2, (w - 2) / 2, 2);
 160   
         }
 161   
         else {
 162  0
             g.drawLine ((w - 2) / 2, h - 2, w - 2, 2);
 163  0
             g.setColor ((in && down) ? getBackground ().darker () :
 164   
                         getBackground().brighter());
 165  0
             g.drawLine (2, 2, (w - 2) / 2, h - 2);
 166  0
             g.drawLine (2, 2, w - 2, 2);
 167   
         }
 168   
     }
 169   
 
 170  0
     protected synchronized ArrowRoller getRoller () {
 171  0
         if (roller == null) {
 172  0
             roller = new ArrowRoller ();
 173  0
             roller.start();
 174   
         }
 175  0
         return roller;
 176   
     }
 177   
 
 178  0
     public synchronized void destroyRoller () {
 179  0
         roller.abort ();
 180  0
         roller = null;
 181   
     }
 182   
 
 183  0
     protected void processMouseEvent(MouseEvent e) {
 184  0
         ArrowRoller roller = getRoller();
 185  0
         switch (e.getID ()) {
 186  0
         case MouseEvent.MOUSE_PRESSED:
 187  0
             fireActionEvent ();
 188  0
             roller.addListener (this);
 189  0
             down = in = true;
 190  0
             repaint ();
 191  0
             break;
 192  0
         case MouseEvent.MOUSE_RELEASED:
 193  0
             roller.removeListener (this);
 194  0
             down = in = false;
 195  0
             repaint ();
 196  0
             break;
 197  0
         case MouseEvent.MOUSE_ENTERED:
 198  0
             in = true;
 199  0
             if (down) {
 200  0
                 roller.addListener (this);
 201  0
                 repaint ();
 202   
             }
 203  0
             break;
 204  0
         case MouseEvent.MOUSE_EXITED:
 205  0
             in = false;
 206  0
             if (down) {
 207  0
                 roller.removeListener (this);
 208  0
                 repaint ();
 209   
             }
 210  0
             break;
 211   
         }
 212  0
         super.processMouseEvent (e);
 213   
     }
 214   
 
 215  0
     public void addActionListener (ActionListener l) {
 216  0
         listeners = AWTEventMulticaster.add (l, listeners);
 217   
     }
 218   
 
 219  0
     public void removeActionListener (ActionListener l) {
 220  0
         listeners = AWTEventMulticaster.remove (l, listeners);
 221   
     }
 222   
 
 223  0
     protected void fireActionEvent () {
 224  0
         if (listeners != null){
 225  0
             ActionEvent ev = new ActionEvent (this, 
 226   
                                               ActionEvent.ACTION_PERFORMED, 
 227   
                                               direction);
 228  0
             listeners.actionPerformed (ev);
 229   
         }
 230   
     }
 231   
 }
 232