001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings;
022:
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025:
026: /**
027: * A collection of utility methods for switching to event thread.
028: *
029: * <br>
030: * <b>This class is thread safe.</b>
031: **/
032: public class ThreadUtils implements ActionListener {
033: private static ThreadUtils the;
034:
035: private static ThreadUtils the() {
036: if (the == null) {
037: try {
038: if (System.getProperty("java.version", "0").compareTo(
039: "1.2") >= 0) {
040: the = (ThreadUtils) Class.forName(
041: "com.javujavu.javux.wings.ThreadUtils12")
042: .newInstance();
043: }
044: } catch (Exception e) {
045: }
046:
047: if (the == null)
048: the = new ThreadUtils();
049: }
050: return the;
051: }
052:
053: /**
054: * Causes <code>r.run()</code> to be executed asynchronously on the
055: * AWT event dispatching thread.
056: **/
057: public static void invokeLater(Runnable r) {
058: the().implLater(r);
059: }
060:
061: /**
062: * Causes <code>r.run()</code> to be executed synchronously on the
063: * AWT event dispatching thread. This call blocks until
064: * <code>r.run()</code> returns.
065: **/
066: public static void invokeAndWait(Runnable r) {
067: the().implAndWait(r);
068: }
069:
070: /**
071: * Returns true if the current thread is an AWT event dispatching thread.
072: **/
073: public static boolean isEventDispatchThread() {
074: return the().implIsEventThread();
075: }
076:
077: private WingComponent dispatcher;
078:
079: private void implLater(Runnable r) {
080: invokeLater(r, null);
081: }
082:
083: private void implAndWait(Runnable r) {
084: if (implIsEventThread()) {
085: r.run();
086: return;
087: }
088:
089: Object lock = new Object();
090: synchronized (lock) {
091: invokeLater(r, lock);
092: try {
093: lock.wait();
094: } catch (InterruptedException e) {
095: }
096: }
097: }
098:
099: private void invokeLater(Runnable run, Object lock) {
100: if (dispatcher == null) {
101: WingComponent d = new WingComponent();
102: d.addActionListener(this );
103: synchronized (this ) {
104: if (dispatcher == null)
105: dispatcher = d;
106: }
107: }
108: dispatcher.postOnEventThread(new InvokeEvent(dispatcher, run,
109: lock));
110: }
111:
112: private boolean implIsEventThread() {
113: Thread ct = Thread.currentThread();
114:
115: return (WingComponent.eventThread == ct || ct.getClass()
116: .getName().indexOf("EventDispatchThread") != -1);
117: }
118:
119: public void actionPerformed(ActionEvent e) {
120: if (e instanceof InvokeEvent) {
121: ((InvokeEvent) e).invoke();
122: }
123: }
124:
125: static class InvokeEvent extends ActionEvent {
126: private final Runnable run;
127: private final Object lock;
128:
129: public InvokeEvent(Object source, Runnable run, Object lock) {
130: super (source, ActionEvent.ACTION_PERFORMED, "invoke");
131: this .run = run;
132: this .lock = lock;
133: }
134:
135: public void invoke() {
136: if (lock == null)
137: invoke2();
138: else {
139: synchronized (lock) {
140: invoke2();
141: lock.notify();
142: }
143: }
144: }
145:
146: private void invoke2() {
147: if (run != null) {
148: try {
149: run.run();
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154: }
155: }
156: }
|