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