001: package abbot.util;
002:
003: import java.awt.AWTException;
004: import java.awt.Toolkit;
005: import java.awt.Robot;
006: import java.awt.event.KeyEvent;
007: import java.io.PrintStream;
008: import java.util.Properties;
009:
010: import javax.swing.UIManager;
011:
012: import abbot.Log;
013:
014: /** Preserve and restore system state.
015: This includes the following:
016: <ul>
017: <li><code>System.out/err</code> streams
018: <li><code>System</code> properties
019: <li>Security manager
020: </ul>
021: */
022: public class SystemState {
023:
024: public static final int LOCKING_CODES[] = { KeyEvent.VK_CAPS_LOCK,
025: KeyEvent.VK_NUM_LOCK, KeyEvent.VK_SCROLL_LOCK,
026: KeyEvent.VK_KANA_LOCK };
027: private Properties oldProps;
028: private PrintStream oldOut;
029: private PrintStream oldErr;
030: private SecurityManager oldsm;
031: private String oldLookAndFeel;
032: private boolean lockingKeys[];
033: private static Robot robot = null;
034:
035: static {
036: try {
037: robot = new Robot();
038: } catch (AWTException e) {
039: }
040: }
041:
042: /** Take a snapshot of the current System state for later restoration. */
043: public SystemState() {
044: lockingKeys = new boolean[LOCKING_CODES.length];
045: Toolkit toolkit = Toolkit.getDefaultToolkit();
046: for (int i = 0; i < LOCKING_CODES.length; i++) {
047: try {
048: lockingKeys[i] = toolkit
049: .getLockingKeyState(LOCKING_CODES[i]);
050: } catch (UnsupportedOperationException e) {
051: // Nothing much we can do
052: }
053: }
054: oldLookAndFeel = UIManager.getLookAndFeel().getClass()
055: .getName();
056: oldOut = System.out;
057: oldErr = System.err;
058: System.setOut(new ProtectedStream(oldOut));
059: System.setErr(new ProtectedStream(oldErr));
060: oldProps = (Properties) System.getProperties().clone();
061: oldsm = System.getSecurityManager();
062: }
063:
064: /** Restore the state captured in the ctor. */
065: public void restore() {
066: System.setSecurityManager(oldsm);
067: System.setProperties(oldProps);
068: System.setOut(oldOut);
069: System.setErr(oldErr);
070: try {
071: UIManager.setLookAndFeel(oldLookAndFeel);
072: } catch (Exception e) {
073: Log.warn("Could not restore LAF: " + e);
074: }
075: Toolkit toolkit = Toolkit.getDefaultToolkit();
076: for (int i = 0; i < LOCKING_CODES.length; i++) {
077: try {
078: boolean state = toolkit
079: .getLockingKeyState(LOCKING_CODES[i]);
080: if (state != lockingKeys[i]) {
081: try {
082: toolkit.setLockingKeyState(LOCKING_CODES[i],
083: lockingKeys[i]);
084: } catch (UnsupportedOperationException e) {
085: if (robot != null) {
086: try {
087: robot.keyPress(LOCKING_CODES[i]);
088: robot.keyRelease(LOCKING_CODES[i]);
089: } catch (IllegalArgumentException ex) {
090: // ignore
091: }
092: }
093: }
094: }
095: } catch (UnsupportedOperationException e) {
096: // Oh, well
097: }
098: }
099: }
100:
101: /** Provide a wrapper that prevents the original stream from being
102: closed.
103: */
104: private class ProtectedStream extends PrintStream {
105: private boolean closed = false;
106:
107: public ProtectedStream(PrintStream original) {
108: super (original);
109: }
110:
111: public void flush() {
112: if (!closed)
113: super .flush();
114: }
115:
116: public void close() {
117: closed = true;
118: }
119:
120: public void write(int b) {
121: if (!closed)
122: super .write(b);
123: }
124:
125: public void write(byte[] buf, int off, int len) {
126: if (!closed)
127: super .write(buf, off, len);
128: }
129: }
130:
131: /** Clear all locking keys. */
132: public static void clearLockingKeys() {
133: Toolkit toolkit = Toolkit.getDefaultToolkit();
134: for (int i = 0; i < LOCKING_CODES.length; i++) {
135: try {
136: if (toolkit.getLockingKeyState(LOCKING_CODES[i])) {
137: try {
138: toolkit.setLockingKeyState(LOCKING_CODES[i],
139: false);
140: } catch (UnsupportedOperationException e) {
141: if (robot != null) {
142: try {
143: robot.keyPress(LOCKING_CODES[i]);
144: robot.keyRelease(LOCKING_CODES[i]);
145: } catch (IllegalArgumentException ex) {
146: // ignore
147: }
148: }
149: }
150: }
151: } catch (UnsupportedOperationException e) {
152: // can't read locking keys state, so do nothing
153: }
154: }
155: }
156: }
|