001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Michael Danilov
019: * @version $Revision$
020: */package java.awt.event;
021:
022: import java.awt.AWTEvent;
023: import java.awt.ActiveEvent;
024:
025: import org.apache.harmony.awt.internal.nls.Messages;
026:
027: public class InvocationEvent extends AWTEvent implements ActiveEvent {
028:
029: private static final long serialVersionUID = 436056344909459450L;
030:
031: public static final int INVOCATION_FIRST = 1200;
032:
033: public static final int INVOCATION_DEFAULT = 1200;
034:
035: public static final int INVOCATION_LAST = 1200;
036:
037: protected Runnable runnable;
038:
039: protected Object notifier;
040:
041: protected boolean catchExceptions;
042:
043: private long when;
044: private Throwable throwable;
045:
046: public InvocationEvent(Object source, Runnable runnable) {
047: this (source, runnable, null, false);
048: }
049:
050: public InvocationEvent(Object source, Runnable runnable,
051: Object notifier, boolean catchExceptions) {
052: this (source, INVOCATION_DEFAULT, runnable, notifier,
053: catchExceptions);
054: }
055:
056: protected InvocationEvent(Object source, int id, Runnable runnable,
057: Object notifier, boolean catchExceptions) {
058: super (source, id);
059:
060: // awt.18C=Cannot invoke null runnable
061: assert runnable != null : Messages.getString("awt.18C"); //$NON-NLS-1$
062:
063: if (source == null) {
064: // awt.18D=Source is null
065: throw new IllegalArgumentException(Messages
066: .getString("awt.18D")); //$NON-NLS-1$
067: }
068: this .runnable = runnable;
069: this .notifier = notifier;
070: this .catchExceptions = catchExceptions;
071:
072: throwable = null;
073: when = System.currentTimeMillis();
074: }
075:
076: public void dispatch() {
077: if (!catchExceptions) {
078: runAndNotify();
079: } else {
080: try {
081: runAndNotify();
082: } catch (Throwable t) {
083: throwable = t;
084: }
085: }
086: }
087:
088: private void runAndNotify() {
089: if (notifier != null) {
090: synchronized (notifier) {
091: try {
092: runnable.run();
093: } finally {
094: notifier.notifyAll();
095: }
096: }
097: } else {
098: runnable.run();
099: }
100: }
101:
102: public Exception getException() {
103: return (throwable != null && throwable instanceof Exception) ? (Exception) throwable
104: : null;
105: }
106:
107: public Throwable getThrowable() {
108: return throwable;
109: }
110:
111: public long getWhen() {
112: return when;
113: }
114:
115: @Override
116: public String paramString() {
117: /* The format is based on 1.5 release behavior
118: * which can be revealed by the following code:
119: *
120: * InvocationEvent e = new InvocationEvent(new Component(){},
121: * new Runnable() { public void run(){} });
122: * System.out.println(e);
123: */
124:
125: return ((id == INVOCATION_DEFAULT ? "INVOCATION_DEFAULT" : "unknown type") + //$NON-NLS-1$ //$NON-NLS-2$
126: ",runnable=" + runnable + //$NON-NLS-1$
127: ",notifier=" + notifier + //$NON-NLS-1$
128: ",catchExceptions=" + catchExceptions + //$NON-NLS-1$
129: ",when=" + when); //$NON-NLS-1$
130: }
131:
132: }
|