1 /*
   2  * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.util.LinkedList;
  29 import sun.awt.AWTAccessor;
  30 import sun.awt.AppContext;
  31 import sun.awt.SunToolkit;
  32 
  33 /**
  34  * A mechanism for ensuring that a series of AWTEvents are executed in a
  35  * precise order, even across multiple AppContexts. The nested events will be
  36  * dispatched in the order in which their wrapping SequencedEvents were
  37  * constructed. The only exception to this rule is if the peer of the target of
  38  * the nested event was destroyed (with a call to Component.removeNotify)
  39  * before the wrapping SequencedEvent was able to be dispatched. In this case,
  40  * the nested event is never dispatched.
  41  *
  42  * @author David Mendenhall
  43  */
  44 class SequencedEvent extends AWTEvent implements ActiveEvent {
  45     /*
  46      * serialVersionUID
  47      */
  48     private static final long serialVersionUID = 547742659238625067L;
  49 
  50     private static final int ID =
  51         java.awt.event.FocusEvent.FOCUS_LAST + 1;
  52     private static final LinkedList<SequencedEvent> list = new LinkedList<>();
  53 
  54     private final AWTEvent nested;
  55     private AppContext appContext;
  56     private boolean disposed;
  57 
  58     static {
  59         AWTAccessor.setSequencedEventAccessor(new AWTAccessor.SequencedEventAccessor() {
  60             public AWTEvent getNested(AWTEvent sequencedEvent) {
  61                 return ((SequencedEvent)sequencedEvent).nested;
  62             }
  63             public boolean isSequencedEvent(AWTEvent event) {
  64                 return event instanceof SequencedEvent;
  65             }
  66 
  67             public AWTEvent create(AWTEvent event) {
  68                 return new SequencedEvent(event);
  69             }
  70         });
  71     }
  72 
  73     /**
  74      * Constructs a new SequencedEvent which will dispatch the specified
  75      * nested event.
  76      *
  77      * @param nested the AWTEvent which this SequencedEvent's dispatch()
  78      *        method will dispatch
  79      */
  80     public SequencedEvent(AWTEvent nested) {
  81         super(nested.getSource(), ID);
  82         this.nested = nested;
  83         // All AWTEvents that are wrapped in SequencedEvents are (at
  84         // least currently) implicitly generated by the system
  85         SunToolkit.setSystemGenerated(nested);
  86         synchronized (SequencedEvent.class) {
  87             list.add(this);
  88         }
  89     }
  90 
  91     /**
  92      * Dispatches the nested event after all previous nested events have been
  93      * dispatched or disposed. If this method is invoked before all previous nested events
  94      * have been dispatched, then this method blocks until such a point is
  95      * reached.
  96      * While waiting disposes nested events to disposed AppContext
  97      *
  98      * NOTE: Locking protocol.  Since dispose() can get EventQueue lock,
  99      * dispatch() shall never call dispose() while holding the lock on the list,
 100      * as EventQueue lock is held during dispatching.  The locks should be acquired
 101      * in the same order.
 102      */
 103     public final void dispatch() {
 104         try {
 105             appContext = AppContext.getAppContext();
 106 
 107             if (getFirst() != this) {
 108                 if (EventQueue.isDispatchThread()) {
 109                     EventDispatchThread edt = (EventDispatchThread)
 110                         Thread.currentThread();
 111                     edt.pumpEvents(SentEvent.ID, new Conditional() {
 112                         public boolean evaluate() {
 113                             return !SequencedEvent.this.isFirstOrDisposed();
 114                         }
 115                     });
 116                 } else {
 117                     while(!isFirstOrDisposed()) {
 118                         synchronized (SequencedEvent.class) {
 119                             try {
 120                                 SequencedEvent.class.wait(1000);
 121                             } catch (InterruptedException e) {
 122                                 break;
 123                             }
 124                         }
 125                     }
 126                 }
 127             }
 128 
 129             if (!disposed) {
 130                 KeyboardFocusManager.getCurrentKeyboardFocusManager().
 131                     setCurrentSequencedEvent(this);
 132                 Toolkit.getEventQueue().dispatchEvent(nested);
 133             }
 134         } finally {
 135             dispose();
 136         }
 137     }
 138 
 139     /**
 140      * true only if event exists and nested source appContext is disposed.
 141      */
 142     private static final boolean isOwnerAppContextDisposed(SequencedEvent se) {
 143         if (se != null) {
 144             Object target = se.nested.getSource();
 145             if (target instanceof Component) {
 146                 return ((Component)target).appContext.isDisposed();
 147             }
 148         }
 149         return false;
 150     }
 151 
 152     /**
 153      * Sequenced events are dispatched in order, so we cannot dispatch
 154      * until we are the first sequenced event in the queue (i.e. it's our
 155      * turn).  But while we wait for our turn to dispatch, the event
 156      * could have been disposed for a number of reasons.
 157      */
 158     public final boolean isFirstOrDisposed() {
 159         if (disposed) {
 160             return true;
 161         }
 162         // getFirstWithContext can dispose this
 163         return this == getFirstWithContext() || disposed;
 164     }
 165 
 166     private static final synchronized SequencedEvent getFirst() {
 167         return list.getFirst();
 168     }
 169 
 170     /* Disposes all events from disposed AppContext
 171      * return first valid event
 172      */
 173     private static final SequencedEvent getFirstWithContext() {
 174         SequencedEvent first = getFirst();
 175         while(isOwnerAppContextDisposed(first)) {
 176             first.dispose();
 177             first = getFirst();
 178         }
 179         return first;
 180     }
 181 
 182     /**
 183      * Disposes of this instance. This method is invoked once the nested event
 184      * has been dispatched and handled, or when the peer of the target of the
 185      * nested event has been disposed with a call to Component.removeNotify.
 186      *
 187      * NOTE: Locking protocol.  Since SunToolkit.postEvent can get EventQueue lock,
 188      * it shall never be called while holding the lock on the list,
 189      * as EventQueue lock is held during dispatching and dispatch() will get
 190      * lock on the list. The locks should be acquired in the same order.
 191      */
 192     final void dispose() {
 193       synchronized (SequencedEvent.class) {
 194             if (disposed) {
 195                 return;
 196             }
 197             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().
 198                     getCurrentSequencedEvent() == this) {
 199                 KeyboardFocusManager.getCurrentKeyboardFocusManager().
 200                     setCurrentSequencedEvent(null);
 201             }
 202             disposed = true;
 203         }
 204         // Wake myself up
 205         if (appContext != null) {
 206             SunToolkit.postEvent(appContext, new SentEvent());
 207         }
 208 
 209         SequencedEvent next = null;
 210 
 211         synchronized (SequencedEvent.class) {
 212           SequencedEvent.class.notifyAll();
 213 
 214           if (list.getFirst() == this) {
 215               list.removeFirst();
 216 
 217               if (!list.isEmpty()) {
 218                     next = list.getFirst();
 219               }
 220           } else {
 221               list.remove(this);
 222           }
 223       }
 224         // Wake up waiting threads
 225         if (next != null && next.appContext != null) {
 226             SunToolkit.postEvent(next.appContext, new SentEvent());
 227         }
 228     }
 229 }