Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 128   Methods: 9
NCLOC: 94   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JComponentRecorder.java 90% 95.8% 100% 95.5%
coverage coverage
 1   
 package abbot.editor.recorder;
 2   
 
 3   
 import java.awt.AWTEvent;
 4   
 import java.awt.event.KeyEvent;
 5   
 
 6   
 import javax.swing.*;
 7   
 
 8   
 import abbot.Log;
 9   
 import abbot.script.*;
 10   
 
 11   
 /**
 12   
  * Record basic semantic events you might find on an JComponent. <p>
 13   
  * 
 14   
  * Watches for events that trigger an action from the component's action map.  
 15   
  * As of 1.3.1, KEY_TYPED and KEY_RELEASED events can trigger an action.
 16   
  */
 17   
 public class JComponentRecorder extends ContainerRecorder {
 18   
 
 19   
     private JComponent target;
 20   
     private String actionKey;
 21   
 
 22   
     public static final int SE_ACTION_MAP = 20;
 23   
 
 24  372
     public JComponentRecorder(Resolver resolver) {
 25  372
         super(resolver);
 26   
     }
 27   
 
 28  337
     protected void init(int rtype) {
 29  337
         super.init(rtype);
 30  337
         target = null;
 31  337
         actionKey = null;
 32   
     }
 33   
 
 34   
     /** Add handling for JComponent input-mapped actions. */
 35  315
     public boolean accept(AWTEvent event) {
 36  315
         boolean accepted;
 37  315
         if ((event instanceof KeyEvent)
 38   
             && (event.getSource() instanceof JComponent)
 39   
             && isMappedEvent((KeyEvent)event)) {
 40  9
             init(SE_ACTION_MAP);
 41  9
             accepted = true;
 42   
         }
 43   
         else {
 44  306
             accepted = super.accept(event);
 45   
         }
 46  315
         return accepted;
 47   
     }
 48   
 
 49  166
     protected javax.swing.Action getAction(KeyEvent ke) {
 50  166
         KeyStroke ks = KeyStroke.getKeyStrokeForEvent(ke);
 51  166
         JComponent comp = (JComponent)ke.getComponent();
 52  166
         Object binding = comp.getInputMap().get(ks);
 53  166
         return binding != null ? comp.getActionMap().get(binding) : null;
 54   
     }
 55   
 
 56  139
     protected boolean isMappedEvent(KeyEvent ke) {
 57  139
         return getAction(ke) != null;
 58   
     }
 59   
 
 60   
     /** Add handling for JComponent input-mapped actions. */
 61  1022
     public boolean parse(AWTEvent event) {
 62  1022
         boolean consumed = true;
 63  1022
         switch(getRecordingType()) {
 64  26
         case SE_ACTION_MAP:
 65  26
             consumed = parseActionMapEvent(event);
 66  26
             break;
 67  996
         default:
 68  996
             consumed = super.parse(event);
 69  996
             break;
 70   
         }
 71  1022
         return consumed;
 72   
     }
 73   
 
 74   
     /** Add handling for JComponent input-mapped actions. */
 75  26
     protected boolean parseActionMapEvent(AWTEvent event) {
 76  26
         if (target == null) {
 77  9
             target = (JComponent)event.getSource();
 78  9
             KeyStroke ks = KeyStroke.getKeyStrokeForEvent((KeyEvent)event);
 79  9
             Object binding = target.getInputMap().get(ks);
 80  9
             Log.debug("Binding is " + binding + " ("
 81   
                       + binding.getClass() + ")");
 82  9
             if (binding instanceof String) {
 83  9
                 actionKey = (String)binding;
 84   
             }
 85   
             else {
 86   
                 // 1.3 and prior sometimes used the actions themselves
 87   
                 // as the key
 88  0
                 javax.swing.Action action = target.getActionMap().get(binding);
 89  0
                 actionKey = (String)action.getValue(javax.swing.Action.NAME);
 90   
             }
 91  9
             Log.debug("Keystroke '" + ks + "' mapped to " + actionKey);
 92   
         }
 93   
         // Make sure the entire KEY_PRESSED/KEY_TYPED/KEY_RELEASED
 94   
         // sequence is eaten
 95   
         // NOTE: This assumes that no component expects to respond to both the
 96   
         // key shortcut AND accept the key input.
 97  26
         if (event.getID() == KeyEvent.KEY_RELEASED) {
 98  9
             setFinished(true);
 99   
         }
 100  26
         return true;
 101   
     }
 102   
 
 103   
     /** Add handling for JComponent input-mapped actions. */
 104  89
     protected Step createStep() {
 105  89
         Step step;
 106  89
         switch(getRecordingType()) {
 107  9
         case SE_ACTION_MAP:
 108  9
             step = createActionMap(target, actionKey);
 109  9
             break;
 110  80
         default:
 111  80
             step = super.createStep();
 112  80
             break;
 113   
         }
 114  89
         return step;
 115   
     }
 116   
 
 117   
     /** Create a JComponent input-mapped action invocation. */
 118  9
     protected Step createActionMap(JComponent target, String actionKey) {
 119  9
         ComponentReference cr = getResolver().addComponent(target);
 120  9
         return new abbot.script.Action(getResolver(), null,
 121   
                                        "actionActionMap",
 122   
                                        new String[] { cr.getID(),
 123   
                                                       actionKey },
 124   
                                        JComponent.class);
 125   
     }
 126   
 }
 127   
 
 128