Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 133   Methods: 7
NCLOC: 106   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
SystemState.java 37.5% 70.5% 71.4% 62.7%
coverage coverage
 1   
 package abbot.util;
 2   
 
 3   
 import java.awt.AWTException;
 4   
 import java.awt.Toolkit;
 5   
 import java.awt.Robot;
 6   
 import java.awt.event.KeyEvent;
 7   
 import java.io.PrintStream;
 8   
 import java.util.Properties;
 9   
 
 10   
 import javax.swing.UIManager;
 11   
 
 12   
 import abbot.Log;
 13   
 
 14   
 /** Preserve and restore system state.
 15   
     This includes the following:
 16   
     <ul>
 17   
     <li><code>System.out/err</code> streams
 18   
     <li><code>System</code> properties
 19   
     <li>Security manager
 20   
     </ul>
 21   
  */
 22   
 public class SystemState {
 23   
 
 24   
     private static final int CODES[] = {
 25   
         KeyEvent.VK_CAPS_LOCK,
 26   
         KeyEvent.VK_NUM_LOCK,
 27   
         KeyEvent.VK_SCROLL_LOCK,
 28   
         KeyEvent.VK_KANA_LOCK
 29   
     };
 30   
     private Properties oldProps;
 31   
     private PrintStream oldOut;
 32   
     private PrintStream oldErr;
 33   
     private SecurityManager oldsm;
 34   
     private String oldLookAndFeel;
 35   
     private boolean lockingKeys[];
 36   
     private static Robot robot = null;
 37   
 
 38   
     static {
 39  94
         try {
 40  94
             robot = new Robot();
 41   
         }
 42   
         catch(AWTException e) {
 43   
         }
 44   
     }
 45   
 
 46   
     /** Take a snapshot of the current System state for later restoration. */
 47  518
     public SystemState() {
 48  518
         lockingKeys = new boolean[CODES.length];
 49  518
         Toolkit toolkit = Toolkit.getDefaultToolkit();
 50  518
         for (int i=0;i < CODES.length;i++) {
 51  2072
             try {
 52  2072
                 lockingKeys[i] = toolkit.getLockingKeyState(CODES[i]);
 53  1554
                 try {
 54  1554
                     toolkit.setLockingKeyState(CODES[i], false);
 55   
                 }
 56   
                 catch(UnsupportedOperationException e) {
 57   
                     // Manually toggle the key
 58  0
                     if (lockingKeys[i] && robot != null) {
 59  0
                         robot.keyPress(CODES[i]);
 60  0
                         robot.keyRelease(CODES[i]);
 61   
                     }
 62   
                 }
 63   
             }
 64   
             catch(UnsupportedOperationException e) {
 65   
                 // Nothing much we can do
 66   
             }
 67   
         }
 68  518
         oldLookAndFeel = UIManager.getLookAndFeel().getClass().getName();
 69  518
         oldOut = System.out;
 70  518
         oldErr = System.err;
 71  518
         System.setOut(new ProtectedStream(oldOut));
 72  518
         System.setErr(new ProtectedStream(oldErr));
 73  518
         oldProps = (Properties)System.getProperties().clone();
 74  518
         oldsm = System.getSecurityManager();
 75   
     }
 76   
 
 77   
     /** Restore the state captured in the ctor. */
 78  514
     public void restore() {
 79  514
         System.setSecurityManager(oldsm); 
 80  514
         System.setProperties(oldProps); 
 81  514
         System.setOut(oldOut); 
 82  514
         System.setErr(oldErr); 
 83  514
         try { UIManager.setLookAndFeel(oldLookAndFeel); }
 84  0
         catch(Exception e) { Log.warn("Could not restore LAF: " + e); }
 85  514
         Toolkit toolkit = Toolkit.getDefaultToolkit();
 86  514
         for (int i=0;i < CODES.length;i++) {
 87  2056
             try {
 88  2056
                 boolean state = toolkit.getLockingKeyState(CODES[i]);
 89  1542
                 if (state != lockingKeys[i]) {
 90  0
                     try {
 91  0
                         toolkit.setLockingKeyState(CODES[i], lockingKeys[i]);
 92   
                     }
 93   
                     catch(UnsupportedOperationException e) {
 94  0
                         if (robot != null) {
 95  0
                             robot.keyPress(CODES[i]);
 96  0
                             robot.keyRelease(CODES[i]);
 97   
                         }
 98   
                     }
 99   
                 }
 100   
             }
 101   
             catch(UnsupportedOperationException e) {
 102   
                 // Oh, well
 103   
             }
 104   
         }
 105   
     }
 106   
 
 107   
     /** Provide a wrapper that prevents the original stream from being
 108   
         closed.
 109   
     */
 110   
     private class ProtectedStream extends PrintStream {
 111   
         private boolean closed = false;
 112  1036
         public ProtectedStream(PrintStream original) {
 113  1036
             super(original);
 114   
         }
 115  0
         public void flush() {
 116  0
             if (!closed)
 117  0
                 super.flush();
 118   
         }
 119  2
         public void close() {
 120  2
             closed = true;
 121   
         }
 122  0
         public void write(int b) {
 123  0
             if (!closed)
 124  0
                 super.write(b);
 125   
         }
 126  54
         public void write(byte[] buf, int off, int len) {
 127  54
             if (!closed)
 128  54
                 super.write(buf, off, len);
 129   
         }
 130   
     }
 131   
 }
 132   
 
 133