| 
01 /*02  * Copyright 2008-2015 the original author or authors.
 03  *
 04  * Licensed under the Apache License, Version 2.0 (the "License");
 05  * you may not use this file except in compliance with the License.
 06  * You may obtain a copy of the License at
 07  *
 08  *     http://www.apache.org/licenses/LICENSE-2.0
 09  *
 10  * Unless required by applicable law or agreed to in writing, software
 11  * distributed under the License is distributed on an "AS IS" BASIS,
 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  * See the License for the specific language governing permissions and
 14  * limitations under the License.
 15  */
 16 package org.codehaus.griffon.runtime.swing;
 17
 18 import org.codehaus.griffon.runtime.core.threading.AbstractUIThreadManager;
 19
 20 import javax.annotation.Nonnull;
 21 import javax.swing.SwingUtilities;
 22 import java.lang.reflect.InvocationTargetException;
 23
 24 import static java.util.Objects.requireNonNull;
 25
 26 /**
 27  * Executes code using SwingUtilities.
 28  *
 29  * @author Andres Almiray
 30  */
 31 public class SwingUIThreadManager extends AbstractUIThreadManager {
 32     @Override
 33     public boolean isUIThread() {
 34         return SwingUtilities.isEventDispatchThread();
 35     }
 36
 37     @Override
 38     public void runInsideUIAsync(@Nonnull Runnable runnable) {
 39         requireNonNull(runnable, ERROR_RUNNABLE_NULL);
 40         SwingUtilities.invokeLater(runnable);
 41     }
 42
 43     @Override
 44     public void runInsideUISync(@Nonnull Runnable runnable) {
 45         requireNonNull(runnable, ERROR_RUNNABLE_NULL);
 46         if (isUIThread()) {
 47             runnable.run();
 48         } else {
 49             try {
 50                 SwingUtilities.invokeAndWait(runnable);
 51             } catch (InterruptedException ie) {
 52                 // ignore
 53             } catch (InvocationTargetException ite) {
 54                 Throwable t = ite.getCause();
 55                 if (t instanceof RuntimeException) {
 56                     throw (RuntimeException) t;
 57                 }
 58                 throw new RuntimeException(t);
 59             }
 60         }
 61     }
 62 }
 |