001: /*=============================================================================
002: * Copyright Texas Instruments 2002. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package ti.chimera.plugin;
020:
021: import ti.chimera.service.SwingWorker;
022: import ti.chimera.service.Prompt;
023: import ti.chimera.Main;
024: import ti.chimera.Plugin;
025: import ti.chimera.Service;
026: import ti.swing.SwingQueue;
027:
028: import java.awt.*;
029:
030: /**
031: * An implementation of the "swing worker" {@link SwingWorker} service.
032: *
033: * @author Rob Clark
034: * @version 0.1
035: */
036: public class SwingWorkerPlugin extends Plugin {
037:
038: /**
039: * Class Constructor.
040: *
041: * @param main the main application
042: */
043: public SwingWorkerPlugin(Main main) {
044: super (main, "Swing Worker");
045:
046: registerServiceFactory(new ServiceFactory() {
047:
048: public Service createService() {
049: return new SwingWorker() {
050:
051: public void run(final Runnable r, String str)
052: throws Throwable {
053: if (!(javax.swing.SwingUtilities
054: .isEventDispatchThread() || (Thread
055: .currentThread() instanceof ti.pub.WorkerThread))) {
056: r.run();
057: } else {
058: final SwingQueue sq = new SwingQueue();
059: final Throwable[] ts = new Throwable[1];
060:
061: Thread swt = new Thread(new Runnable() {
062:
063: public void run() {
064: Thread.currentThread().setPriority(
065: Thread.MIN_PRIORITY);
066: try {
067: r.run();
068: } catch (Throwable t) {
069: ts[0] = t;
070: } finally {
071: sq.enqueue("done");
072: }
073: }
074:
075: }, "Swing Worker for: "
076: + Thread.currentThread().getName());
077:
078: Prompt.Progress p = null;
079:
080: if (str != null)
081: p = ((Prompt) (getMain().getRegistry()
082: .getService("prompt")))
083: .showProgress(str);
084:
085: swt.start();
086: sq.dequeue();
087:
088: if (p != null)
089: p.dispose();
090:
091: if (ts[0] != null)
092: throw ts[0];
093:
094: }
095: }
096:
097: };
098: }
099:
100: });
101: }
102:
103: private Main getMain() {
104: return main;
105: }
106: }
107:
108: /*
109: * Local Variables:
110: * tab-width: 2
111: * indent-tabs-mode: nil
112: * mode: java
113: * c-indentation-style: java
114: * c-basic-offset: 2
115: * eval: (c-set-offset 'substatement-open '0)
116: * eval: (c-set-offset 'case-label '+)
117: * eval: (c-set-offset 'inclass '+)
118: * eval: (c-set-offset 'inline-open '0)
119: * End:
120: */
|