1 7 8 package java.awt; 9 10 import java.awt.AWTEvent ; 11 import java.awt.ActiveEvent ; 12 import java.util.LinkedList ; 13 import sun.awt.AppContext; 14 import sun.awt.SunToolkit; 15 16 28 class SequencedEvent extends AWTEvent implements ActiveEvent { 29 private static final int ID = 30 java.awt.event.FocusEvent.FOCUS_LAST + 1; 31 private static final LinkedList list = new LinkedList (); 32 33 private final AWTEvent nested; 34 private AppContext appContext; 35 private boolean disposed; 36 37 44 public SequencedEvent(AWTEvent nested) { 45 super(nested.getSource(), ID); 46 this.nested = nested; 47 synchronized (SequencedEvent .class) { 48 list.add(this); 49 } 50 } 51 52 64 public final void dispatch() { 65 try { 66 appContext = AppContext.getAppContext(); 67 68 if (getFirst() != this) { 69 if (EventQueue.isDispatchThread()) { 70 EventDispatchThread edt = (EventDispatchThread ) 71 Thread.currentThread(); 72 edt.pumpEvents(SentEvent.ID, new Conditional () { 73 public boolean evaluate() { 74 return !SequencedEvent.this.isFirstOrDisposed(); 75 } 76 }); 77 } else { 78 while(!isFirstOrDisposed()) { 79 synchronized (SequencedEvent .class) { 80 try { 81 SequencedEvent .class.wait(1000); 82 } catch (InterruptedException e) { 83 break; 84 } 85 } 86 } 87 } 88 } 89 90 if (!disposed) { 91 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 92 setCurrentSequencedEvent(this); 93 Toolkit.getEventQueue().dispatchEvent(nested); 94 } 95 } finally { 96 dispose(); 97 } 98 } 99 100 103 private final static boolean isOwnerAppContextDisposed(SequencedEvent se) { 104 if (se != null) { 105 Object target = se.nested.getSource(); 106 if (target instanceof Component ) { 107 return ((Component )target).appContext.isDisposed(); 108 } 109 } 110 return false; 111 } 112 113 119 public final boolean isFirstOrDisposed() { 120 if (disposed) { 121 return true; 122 } 123 return this == getFirstWithContext() || disposed; 125 } 126 127 private final synchronized static SequencedEvent getFirst() { 128 return (SequencedEvent )list.getFirst(); 129 } 130 131 134 private final static SequencedEvent getFirstWithContext() { 135 SequencedEvent first = getFirst(); 136 while(isOwnerAppContextDisposed(first)) { 137 first.dispose(); 138 first = getFirst(); 139 } 140 return first; 141 } 142 143 153 final void dispose() { 154 synchronized (SequencedEvent .class) { 155 if (disposed) { 156 return; 157 } 158 if (KeyboardFocusManager.getCurrentKeyboardFocusManager(). 159 getCurrentSequencedEvent() == this) { 160 KeyboardFocusManager.getCurrentKeyboardFocusManager(). 161 setCurrentSequencedEvent(null); 162 } 163 disposed = true; 164 } 165 if (appContext != null) { 167 SunToolkit.postEvent(appContext, new SentEvent ()); 168 } 169 170 SequencedEvent next = null; 171 172 synchronized (SequencedEvent .class) { 173 SequencedEvent .class.notifyAll(); 174 175 if (list.getFirst() == this) { 176 list.removeFirst(); 177 178 if (!list.isEmpty()) { 179 next = (SequencedEvent )list.getFirst(); 180 } 181 } else { 182 list.remove(this); 183 } 184 } 185 if (next != null && next.appContext != null) { 187 SunToolkit.postEvent(next.appContext, new SentEvent ()); 188 } 189 } 190 } 191 | Popular Tags |