001: /*
002: * @(#)EventQueueConfiguration.java 1.2 04/12/06
003: *
004: * Copyright (c) 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.awt;
010:
011: import java.lang.reflect.Constructor;
012: import java.lang.reflect.Method;
013: import java.lang.reflect.InvocationTargetException;
014: import java.awt.EventQueue;
015: import pnuts.lang.Runtime;
016: import pnuts.lang.Context;
017: import pnuts.lang.Configuration;
018: import pnuts.lang.PnutsException;
019: import pnuts.ext.ConfigurationAdapter;
020:
021: /**
022: * This class allows you to execute all Java API access from scrips on the event dispatch thread.
023: *
024: * <pre>e.g.
025: * context.setConfiguration(new EventQueueConfiguration(context.getConfiguration()));
026: * </pre>
027: */
028: public class EventQueueConfiguration extends ConfigurationAdapter {
029:
030: /**
031: * Constructor
032: */
033: public EventQueueConfiguration() {
034: }
035:
036: /**
037: * Constructor
038: *
039: * @param base the base configuration
040: */
041: public EventQueueConfiguration(Configuration base) {
042: super (base);
043: }
044:
045: /**
046: * Calls a method.
047: *
048: * @param method the method to call
049: * @param target the object
050: * @param args the arguments
051: *
052: * @return the methods return value
053: */
054: public Object callMethod(final Context context, final Class c,
055: final String name, final Object args[],
056: final Class types[], final Object target) {
057: Object result = null;
058:
059: if (EventQueue.isDispatchThread()) {
060: result = super .callMethod(context, c, name, args, types,
061: target);
062: } else {
063: try {
064: result = (new Invoker() {
065: public Object runSelf() throws Throwable {
066: return EventQueueConfiguration.super
067: .callMethod(context, c, name, args,
068: types, target);
069: }
070: }).invoke();
071: } catch (IllegalAccessException e0) {
072: throw new PnutsException(e0, context);
073: } catch (InvocationTargetException e1) {
074: throw new PnutsException(e1.getTargetException(),
075: context);
076: }
077: }
078: return result;
079: }
080:
081: /*
082: *
083: */
084: public Object callConstructor(final Context context,
085: final Class clazz, final Object args[], final Class types[]) {
086: Object result = null;
087:
088: if (EventQueue.isDispatchThread()) {
089: result = super .callConstructor(context, clazz, args, types);
090: } else {
091: try {
092: result = (new Invoker() {
093: public Object runSelf() throws Throwable {
094: return EventQueueConfiguration.super
095: .callConstructor(context, clazz, args,
096: types);
097: }
098: }).invoke();
099: } catch (IllegalAccessException e0) {
100: throw new PnutsException(e0, context);
101: } catch (InvocationTargetException e1) {
102: throw new PnutsException(e1.getTargetException(),
103: context);
104: }
105: }
106: return result;
107: }
108:
109: public Object getField(final Context context, final Object target,
110: final String name) {
111: Object result = null;
112:
113: if (EventQueue.isDispatchThread()) {
114: result = super .getField(context, target, name);
115: } else {
116: try {
117: result = (new Invoker() {
118: public Object runSelf() throws Throwable {
119: return EventQueueConfiguration.super .getField(
120: context, target, name);
121: }
122: }).invoke();
123: } catch (IllegalAccessException e0) {
124: throw new PnutsException(e0, context);
125: } catch (InvocationTargetException e1) {
126: throw new PnutsException(e1.getTargetException(),
127: context);
128: }
129: }
130: return result;
131: }
132:
133: public void putField(final Context context, final Object target,
134: final String name, final Object expr) {
135: if (EventQueue.isDispatchThread()) {
136: super .putField(context, target, name, expr);
137: } else {
138: try {
139: (new Invoker() {
140: public Object runSelf() throws Throwable {
141: EventQueueConfiguration.super .putField(context,
142: target, name, expr);
143: return null;
144: }
145: }).invoke();
146: } catch (IllegalAccessException e0) {
147: throw new PnutsException(e0, context);
148: } catch (InvocationTargetException e1) {
149: throw new PnutsException(e1.getTargetException(),
150: context);
151: }
152: }
153: }
154:
155: public Object getStaticField(final Context context,
156: final Class clazz, final String name) {
157: Object result = null;
158:
159: if (EventQueue.isDispatchThread()) {
160: result = super .getStaticField(context, clazz, name);
161: } else {
162: try {
163: result = (new Invoker() {
164: public Object runSelf() throws Throwable {
165: return EventQueueConfiguration.super
166: .getStaticField(context, clazz, name);
167: }
168: }).invoke();
169: } catch (IllegalAccessException e0) {
170: throw new PnutsException(e0, context);
171: } catch (InvocationTargetException e1) {
172: throw new PnutsException(e1, context);
173: }
174: }
175: return result;
176: }
177:
178: public void putStaticField(final Context context,
179: final Class clazz, final String name, final Object value) {
180: if (EventQueue.isDispatchThread()) {
181: super .putStaticField(context, clazz, name, value);
182: } else {
183: try {
184: (new Invoker() {
185: public Object runSelf() throws Throwable {
186: EventQueueConfiguration.super .putStaticField(
187: context, clazz, name, value);
188: return null;
189: }
190: }).invoke();
191: } catch (IllegalAccessException e0) {
192: throw new PnutsException(e0, context);
193: } catch (InvocationTargetException e1) {
194: throw new PnutsException(e1.getTargetException(),
195: context);
196: }
197: }
198: }
199:
200: private abstract class Invoker implements Runnable {
201: private Object result = null;
202: private Throwable throwable = null;
203:
204: public Object invoke() throws InvocationTargetException,
205: IllegalAccessException {
206: try {
207: EventQueue.invokeAndWait(this );
208: } catch (InterruptedException e) {
209: throwable = e;
210: }
211:
212: if (throwable != null) {
213: if (throwable instanceof IllegalAccessException) {
214: throw (IllegalAccessException) throwable;
215: } else if (throwable instanceof InvocationTargetException) {
216: throw (InvocationTargetException) throwable;
217: } else {
218: throw new InvocationTargetException(throwable);
219: }
220: } else {
221: return result;
222: }
223: }
224:
225: public void run() {
226: try {
227: result = runSelf();
228: } catch (Throwable t) {
229: throwable = t;
230: }
231: }
232:
233: protected abstract Object runSelf() throws Throwable;
234: }
235: }
|