001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.ui;
042:
043: import java.lang.reflect.InvocationTargetException;
044: import java.util.concurrent.ExecutorService;
045: import java.util.concurrent.Executors;
046: import javax.swing.SwingUtilities;
047:
048: /**
049: * Mimics the functionality of the SwingWorker from JDK6+
050: * @author Jaroslav Bachorik
051: */
052: public abstract class SwingWorker {
053: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
054:
055: private static ExecutorService warmupService;
056: private static ExecutorService taskService;
057:
058: //~ Instance fields ----------------------------------------------------------------------------------------------------------
059:
060: private final Object warmupLock = new Object();
061: private boolean useEQ;
062:
063: //@GuardedBy warmupLock
064: private boolean workerRunning;
065: private Runnable warmupTimer = new Runnable() {
066: public void run() {
067: synchronized (warmupLock) {
068: try {
069: if (workerRunning) {
070: warmupLock.wait(getWarmup());
071: }
072:
073: if (workerRunning) {
074: nonResponding();
075: }
076: } catch (InterruptedException ex) {
077: ex.printStackTrace();
078: }
079: }
080: }
081: };
082:
083: //~ Constructors -------------------------------------------------------------------------------------------------------------
084:
085: /** Creates a new instance of SwingWorker */
086: public SwingWorker(boolean forceEQ) {
087: sinit();
088: this .useEQ = forceEQ;
089: }
090:
091: public SwingWorker() {
092: this (true);
093: }
094:
095: //~ Methods ------------------------------------------------------------------------------------------------------------------
096:
097: /**
098: * Executes the UI task. Starts the background task and handles it's execution cycle
099: * If the background task blocks for more than getWarmup() milis the nonResponding() method is invoked
100: */
101: public void execute() {
102: postRunnable(new Runnable() {
103: public void run() {
104: synchronized (warmupLock) {
105: workerRunning = true;
106: }
107:
108: warmupService.submit(warmupTimer);
109:
110: try {
111: doInBackground();
112: } finally {
113: synchronized (warmupLock) {
114: workerRunning = false;
115: }
116:
117: if (useEQ) {
118: runInEventDispatchThread(new Runnable() {
119: public void run() {
120: done();
121: }
122: });
123: } else {
124: done();
125: }
126: }
127: }
128: });
129: }
130:
131: /**
132: * @return Returns a warmup time - time in ms before a "non responding" message is shown; default is 500ms
133: */
134: protected int getWarmup() {
135: return 500;
136: }
137:
138: /**
139: * Implementors will implement this method to provide the background task logic
140: */
141: protected abstract void doInBackground();
142:
143: /**
144: * Executed after the background task had finished
145: * It's run in EQ
146: */
147: protected void done() {
148: // override to provide a functionality
149: }
150:
151: /**
152: * Called when the background thread lasts longer than the warmup time
153: * The implementor must take care of rescheduling on AWT thread if appropriate
154: */
155: protected void nonResponding() {
156: // override to provide functionality
157: }
158:
159: protected void postRunnable(Runnable runnable) {
160: taskService.submit(runnable);
161: }
162:
163: static synchronized void sinit() {
164: if (warmupService == null) {
165: UIUtils.runInEventDispatchThreadAndWait(new Runnable() {
166: public void run() {
167: warmupService = Executors.newCachedThreadPool();
168: taskService = Executors.newCachedThreadPool();
169: }
170: });
171: }
172: }
173:
174: private static void runInEventDispatchThread(final Runnable r) {
175: if (SwingUtilities.isEventDispatchThread()) {
176: r.run();
177: } else {
178: SwingUtilities.invokeLater(r);
179: }
180: }
181:
182: private static void runInEventDispatchThreadAndWait(final Runnable r) {
183: if (SwingUtilities.isEventDispatchThread()) {
184: r.run();
185: } else {
186: try {
187: SwingUtilities.invokeAndWait(r);
188: } catch (InterruptedException e) {
189: Thread.currentThread().interrupt(); // don't swallow the interrupted exception!
190: } catch (InvocationTargetException e) {
191: }
192: }
193: }
194: }
|