Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This source code is released into the public domain by its author, Tim Peierls.
- */
- package com.example;
- import com.google.inject.Key;
- import com.google.inject.Provider;
- import com.google.inject.Scope;
- import java.lang.reflect.InvocationTargetException;
- import java.util.concurrent.atomic.AtomicReference;
- import javax.swing.SwingUtilities;
- /**
- * Scope that arranges to perform its provision in the EDT.
- */
- public class SwingScope implements Scope {
- public <T> Provider<T> scope(Key<T> key, final Provider<T> unscoped) {
- return new Provider<T>() {
- public T get() {
- if (SwingUtilities.isEventDispatchThread()) {
- return unscoped.get();
- }
- final AtomicReference<T> result = new AtomicReference<T>();
- try {
- SwingUtilities.invokeAndWait(new Runnable() {
- public void run() {
- result.set(unscoped.get());
- }
- });
- } catch (InterruptedException ex) {
- Thread.currentThread().interrupt();
- } catch (InvocationTargetException ex) {
- Throwable t = ex.getTargetException();
- if (t instanceof RuntimeException) {
- throw (RuntimeException) t;
- } else if (t instanceof Error) {
- throw (Error) t;
- } else {
- throw new RuntimeException(t);
- }
- }
- return result.get();
- }
- };
- }
- public String toString() {
- return "SwingScope";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement