1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Michael Danilov
19 * @version $Revision$
20 */
21package java.awt.event;
22
23import java.awt.AWTEvent;
24import java.awt.ActiveEvent;
25
26import org.apache.harmony.awt.internal.nls.Messages;
27
28/**
29 * This class is not supported in Android 1.0. It is merely provided to maintain
30 * interface compatibility with desktop Java implementations.
31 *
32 * @since Android 1.0
33 */
34public class InvocationEvent extends AWTEvent implements ActiveEvent {
35
36    private static final long serialVersionUID = 436056344909459450L;
37
38    public static final int INVOCATION_FIRST = 1200;
39
40    public static final int INVOCATION_DEFAULT = 1200;
41
42    public static final int INVOCATION_LAST = 1200;
43
44    protected Runnable runnable;
45
46    protected Object notifier;
47
48    protected boolean catchExceptions;
49
50    private long when;
51    private Throwable throwable;
52
53    public InvocationEvent(Object source, Runnable runnable) {
54        this(source, runnable, null, false);
55    }
56
57    public InvocationEvent(Object source, Runnable runnable,
58                           Object notifier, boolean catchExceptions) {
59        this(source, INVOCATION_DEFAULT, runnable, notifier, catchExceptions);
60    }
61
62    protected InvocationEvent(Object source, int id, Runnable runnable,
63            Object notifier, boolean catchExceptions)
64    {
65        super(source, id);
66
67        // awt.18C=Cannot invoke null runnable
68        assert runnable != null : Messages.getString("awt.18C"); //$NON-NLS-1$
69
70        if (source == null) {
71            // awt.18D=Source is null
72            throw new IllegalArgumentException(Messages.getString("awt.18D")); //$NON-NLS-1$
73        }
74        this.runnable = runnable;
75        this.notifier = notifier;
76        this.catchExceptions = catchExceptions;
77
78        throwable = null;
79        when = System.currentTimeMillis();
80    }
81
82    public void dispatch() {
83        if (!catchExceptions) {
84            runAndNotify();
85        } else {
86            try {
87                runAndNotify();
88            } catch (Throwable t) {
89                throwable = t;
90            }
91        }
92    }
93
94    private void runAndNotify() {
95        if (notifier != null) {
96            synchronized(notifier) {
97                try {
98                    runnable.run();
99                } finally {
100                    notifier.notifyAll();
101                }
102            }
103        } else {
104            runnable.run();
105        }
106    }
107
108    public Exception getException() {
109        return (throwable != null && throwable instanceof Exception) ?
110                (Exception)throwable : null;
111    }
112
113    public Throwable getThrowable() {
114        return throwable;
115    }
116
117    public long getWhen() {
118        return when;
119    }
120
121    @Override
122    public String paramString() {
123        /* The format is based on 1.5 release behavior
124         * which can be revealed by the following code:
125         *
126         * InvocationEvent e = new InvocationEvent(new Component(){},
127         *       new Runnable() { public void run(){} });
128         * System.out.println(e);
129         */
130
131        return ((id == INVOCATION_DEFAULT ? "INVOCATION_DEFAULT" : "unknown type") + //$NON-NLS-1$ //$NON-NLS-2$
132                ",runnable=" + runnable + //$NON-NLS-1$
133                ",notifier=" + notifier + //$NON-NLS-1$
134                ",catchExceptions=" + catchExceptions + //$NON-NLS-1$
135                ",when=" + when); //$NON-NLS-1$
136    }
137
138}
139