diff --git a/api.progress/apichanges.xml b/api.progress/apichanges.xml --- a/api.progress/apichanges.xml +++ b/api.progress/apichanges.xml @@ -105,6 +105,17 @@ + + + RunOffAWT added. + + + + + RunOffAWT was added to allow movement of operations out of AWT thread while blocking UI. + + + Add ProgressHandle.suspend(String) method for visual suspend of a running task. diff --git a/api.progress/manifest.mf b/api.progress/manifest.mf --- a/api.progress/manifest.mf +++ b/api.progress/manifest.mf @@ -3,6 +3,6 @@ OpenIDE-Module-Localizing-Bundle: org/netbeans/progress/module/resources/Bundle.properties OpenIDE-Module-Layer: org/netbeans/progress/module/resources/layer.xml OpenIDE-Module-Implementation-Version: 1 -OpenIDE-Module-Recommends: org.netbeans.progress.spi.ProgressUIWorkerProvider +OpenIDE-Module-Recommends: org.netbeans.progress.spi.ProgressUIWorkerProvider, org.netbeans.progress.spi.RunOffAWTProvider AutoUpdate-Essential-Module: true diff --git a/api.progress/nbproject/project.properties b/api.progress/nbproject/project.properties --- a/api.progress/nbproject/project.properties +++ b/api.progress/nbproject/project.properties @@ -40,7 +40,7 @@ is.autoload=true javac.compilerargs=-Xlint:unchecked javac.source=1.5 -spec.version.base=1.15.0 +spec.version.base=1.16.0 javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml diff --git a/api.progress/nbproject/project.xml b/api.progress/nbproject/project.xml --- a/api.progress/nbproject/project.xml +++ b/api.progress/nbproject/project.xml @@ -63,9 +63,9 @@ - - - unit + + + unit org.netbeans.libs.junit4 @@ -75,13 +75,13 @@ - - org.netbeans.modules.progress.ui - - - - - + + org.netbeans.modules.progress.ui + + + + + org.netbeans.api.progress org.netbeans.api.progress.aggregate diff --git a/api.progress/src/org/netbeans/api/progress/RunOffAWT.java b/api.progress/src/org/netbeans/api/progress/RunOffAWT.java new file mode 100644 --- /dev/null +++ b/api.progress/src/org/netbeans/api/progress/RunOffAWT.java @@ -0,0 +1,97 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.api.progress; + +import java.util.concurrent.atomic.AtomicBoolean; +import javax.swing.SwingUtilities; +import org.netbeans.progress.spi.RunOffAWTProvider; +import org.openide.util.Lookup; +import org.openide.util.RequestProcessor; + +/** + * + * @author Tomas Holy + * @since 1.16 + */ +public final class RunOffAWT { + private static final RunOffAWTProvider PROVIDER = getProvider(); + private static final int DISPLAY_DIALOG_MS = 9450; + private static final int DISPLAY_WAIT_CURSOR_MS = 50; + + private static RunOffAWTProvider getProvider() { + RunOffAWTProvider p = Lookup.getDefault().lookup(RunOffAWTProvider.class); + return p != null ? p : new Trivial(); + } + + /** + * Runs operation out of AWT thread, blocks UI while operation is in progress. First it shows + * wait cursor after ~50ms elapses, if operation takes longer than ~10s a dialog with Cancel button is shown. + * @param operation operation to perform + * @param operationDescr text shown in dialog + * @param cancelOperation set to true if user cancelled the operation + */ + public static void runOffAWT(Runnable operation, String operationDescr, AtomicBoolean cancelOperation) { + PROVIDER.runOffAWT(operation, operationDescr, cancelOperation, DISPLAY_WAIT_CURSOR_MS, DISPLAY_DIALOG_MS); + } + + /** + * Runs operation out of AWT thread, blocks UI while operation is in progress. First it shows + * wait cursor after waitCursorAfter elapses, if operation takes longer than dialogAfter a dialog with Cancel button is shown. + * @param operation operation to perform + * @param operationDescr text shown in dialog + * @param cancelOperation set to true if user cancelled the operation + * @param waitCursorAfter time in ms after which wait cursor is shown + * @param dialogAfter time in ms after which dialog with "Cancel" button is shown + */ + public static void runOffAWT(Runnable operation, String operationDescr, AtomicBoolean cancelOperation, int waitCursorAfter, int dialogAfter) { + PROVIDER.runOffAWT(operation, operationDescr, cancelOperation, waitCursorAfter, dialogAfter); + } + + private static class Trivial implements RunOffAWTProvider { + + public void runOffAWT(Runnable operation, String operationDescr, AtomicBoolean cancelOperation, int waitCursorAfter, int dialogAfter) { + if (SwingUtilities.isEventDispatchThread()) { + RequestProcessor.getDefault().post(operation); + } else { + operation.run(); + } + } + } +} diff --git a/api.progress/src/org/netbeans/progress/spi/RunOffAWTProvider.java b/api.progress/src/org/netbeans/progress/spi/RunOffAWTProvider.java new file mode 100644 --- /dev/null +++ b/api.progress/src/org/netbeans/progress/spi/RunOffAWTProvider.java @@ -0,0 +1,49 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ + +package org.netbeans.progress.spi; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * @author Tomas Holy + */ +public interface RunOffAWTProvider { + void runOffAWT(Runnable operation, String operationDescr, AtomicBoolean cancelOperation, int waitCursorAfter, int dialogAfter); +} diff --git a/api.progress/test/unit/src/org/netbeans/api/progress/RunOffAWTTest.java b/api.progress/test/unit/src/org/netbeans/api/progress/RunOffAWTTest.java new file mode 100644 --- /dev/null +++ b/api.progress/test/unit/src/org/netbeans/api/progress/RunOffAWTTest.java @@ -0,0 +1,143 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ +package org.netbeans.api.progress; + +import java.awt.Dialog; +import java.awt.KeyboardFocusManager; +import java.awt.Window; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import javax.swing.SwingUtilities; +import org.netbeans.junit.NbTestCase; +import org.openide.util.Exceptions; + +/** + * + * @author Tomas Holy + */ +public class RunOffAWTTest extends NbTestCase { + + public RunOffAWTTest(String name) { + super(name); + } + + @Override + protected boolean runInEQ() { + return false; + } + + @Override + protected int timeOut() { + return 20000; + } + + private static class R implements Runnable { + + int runCount; + Thread runT; + CountDownLatch l; + + public void run() { + runCount++; + runT = Thread.currentThread(); + if (l != null) { + try { + l.await(); + } catch (InterruptedException ex) { + Exceptions.printStackTrace(ex); + } + } + } + } + + public void testOutOfAWTRunsImmediately() { + R r = new R(); + RunOffAWT.runOffAWT(r, "Simple", new AtomicBoolean()); + assertSame("Should be invoked by calling thread", Thread.currentThread(), r.runT); + assertEquals("Should run once", 1, r.runCount); + } + + public void testAWTBlockedUntilFinished() throws Exception { + final R r = new R(); + final Thread[] t = new Thread[1]; + r.l = new CountDownLatch(1); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + t[0] = Thread.currentThread(); + RunOffAWT.runOffAWT(r, "Test", new AtomicBoolean(false)); + } + }); + final AtomicBoolean invoked = new AtomicBoolean(false); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + invoked.set(true); + } + }); + Thread.sleep(500); + assertFalse("Should not run yet", invoked.get()); + r.l.countDown(); + + assertEquals("Should run once", 1, r.runCount); + assertNotSame("Different than AWT", t[0], r.runT); + } + + public void testDlgIsShown() throws Exception { + final R r = new R(); + final Thread[] t = new Thread[1]; + r.l = new CountDownLatch(1); + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + t[0] = Thread.currentThread(); + RunOffAWT.runOffAWT(r, "Test", new AtomicBoolean(false), 10, 100); + } + }); + for (int i = 0; i < 100; i++) { + Thread.sleep(100); + Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow(); + if (w != null) { + r.l.countDown(); + return; + } + } + fail("Dialog was not shown"); + } +} diff --git a/progress.ui/manifest.mf b/progress.ui/manifest.mf --- a/progress.ui/manifest.mf +++ b/progress.ui/manifest.mf @@ -2,6 +2,6 @@ OpenIDE-Module: org.netbeans.modules.progress.ui OpenIDE-Module-Layer: org/netbeans/modules/progress/ui/layer.xml OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/progress/ui/Bundle.properties -OpenIDE-Module-Provides: org.netbeans.progress.spi.ProgressUIWorkerProvider +OpenIDE-Module-Provides: org.netbeans.progress.spi.ProgressUIWorkerProvider, org.netbeans.progress.spi.RunOffAWTProvider AutoUpdate-Essential-Module: true diff --git a/progress.ui/src/org/netbeans/modules/progress/ui/Bundle.properties b/progress.ui/src/org/netbeans/modules/progress/ui/Bundle.properties --- a/progress.ui/src/org/netbeans/modules/progress/ui/Bundle.properties +++ b/progress.ui/src/org/netbeans/modules/progress/ui/Bundle.properties @@ -53,3 +53,7 @@ StatusLineComponent.Cancel=Cancel Process StatusLineComponent.View=Show Output ListComponent.Watch=Watch Process + +RunOffAWT.TITLE_Operation=Lengthy operation in progress +RunOffAWT.BTN_Cancel=Cancel + diff --git a/progress.ui/src/org/netbeans/modules/progress/ui/RunOffAWTImpl.java b/progress.ui/src/org/netbeans/modules/progress/ui/RunOffAWTImpl.java new file mode 100644 --- /dev/null +++ b/progress.ui/src/org/netbeans/modules/progress/ui/RunOffAWTImpl.java @@ -0,0 +1,155 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2009 Sun Microsystems, Inc. All rights reserved. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this + * particular file as subject to the "Classpath" exception as provided + * by Sun in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2009 Sun Microsystems, Inc. + */ +package org.netbeans.modules.progress.ui; + +import java.awt.Component; +import java.awt.Cursor; +import java.awt.Dialog; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; +import org.netbeans.api.progress.RunOffAWT; +import org.netbeans.progress.spi.RunOffAWTProvider; +import org.openide.DialogDescriptor; +import org.openide.DialogDisplayer; +import org.openide.NotifyDescriptor; +import org.openide.util.NbBundle; +import org.openide.util.Parameters; +import org.openide.util.RequestProcessor; +import org.openide.windows.WindowManager; + +/** + * + * @author Jan Lahoda, Tomas Holy + */ +@org.openide.util.lookup.ServiceProvider(service = org.netbeans.progress.spi.RunOffAWTProvider.class, position = 100) +public class RunOffAWTImpl implements RunOffAWTProvider { + + + private static final RequestProcessor WORKER = new RequestProcessor(RunOffAWT.class.getName()); + + public void runOffAWT(final Runnable operation, final String operationDescr, final AtomicBoolean cancelOperation, int waitCursorTime, int dlgTime) { + Parameters.notNull("operation", operation); + Parameters.notNull("cancelOperation", cancelOperation); + if (!SwingUtilities.isEventDispatchThread()) { + operation.run(); + return; + } + + final CountDownLatch latch = new CountDownLatch(1); + final AtomicReference d = new AtomicReference(); + + WORKER.post(new Runnable() { + + public void run() { + if (cancelOperation.get()) { + return; + } + operation.run(); + latch.countDown(); + + SwingUtilities.invokeLater(new Runnable() { + + public void run() { + Dialog dd = d.get(); + if (dd != null) { + dd.setVisible(false); + } + } + }); + } + }); + + Component glassPane = ((JFrame) WindowManager.getDefault().getMainWindow()).getGlassPane(); + + if (waitMomentarily(glassPane, null, waitCursorTime, latch)) { + return; + } + + Cursor wait = org.openide.util.Utilities.createProgressCursor(glassPane); + + if (waitMomentarily(glassPane, wait, dlgTime, latch)) { + return; + } + + String title = NbBundle.getMessage(RunOffAWTImpl.class, "RunOffAWT.TITLE_Operation"); + String cancelButton = NbBundle.getMessage(RunOffAWTImpl.class, "RunOffAWT.BTN_Cancel"); + + DialogDescriptor nd = new DialogDescriptor(operationDescr, title, true, new Object[]{cancelButton}, cancelButton, DialogDescriptor.DEFAULT_ALIGN, null, new ActionListener() { + + public void actionPerformed(ActionEvent e) { + cancelOperation.set(true); + d.get().setVisible(false); + } + }); + + nd.setMessageType(NotifyDescriptor.INFORMATION_MESSAGE); + + d.set(DialogDisplayer.getDefault().createDialog(nd)); + d.get().setVisible(true); + } + + private static boolean waitMomentarily(Component glassPane, Cursor wait, int timeout, final CountDownLatch l) { + Cursor original = glassPane.getCursor(); + + try { + if (wait != null) { + glassPane.setCursor(wait); + } + + glassPane.setVisible(true); + try { + return l.await(timeout, TimeUnit.MILLISECONDS); + } catch (InterruptedException ex) { + Logger.getLogger(RunOffAWT.class.getName()).log(Level.FINE, null, ex); + return true; + } + } finally { + glassPane.setVisible(false); + glassPane.setCursor(original); + } + } +}