| 1 | |
package org.jtheque.core.spring.factory; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import org.jtheque.core.utils.CoreUtils; |
| 20 | |
import org.springframework.beans.factory.config.AbstractFactoryBean; |
| 21 | |
|
| 22 | |
import javax.swing.SwingUtilities; |
| 23 | |
import java.lang.reflect.InvocationTargetException; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 0 | public final class SwingBeanCreator extends AbstractFactoryBean { |
| 31 | |
private final Class<?> beanClass; |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public SwingBeanCreator(Class<?> beanClass) { |
| 39 | 0 | super(); |
| 40 | |
|
| 41 | 0 | this.beanClass = beanClass; |
| 42 | 0 | } |
| 43 | |
|
| 44 | |
@Override |
| 45 | |
public Class<?> getObjectType() { |
| 46 | 0 | return beanClass; |
| 47 | |
} |
| 48 | |
|
| 49 | |
@Override |
| 50 | |
protected Object createInstance() { |
| 51 | 0 | final Object[] instance = new Object[1]; |
| 52 | |
|
| 53 | 0 | Runnable runnable = new Runnable() { |
| 54 | |
@Override |
| 55 | |
public void run() { |
| 56 | |
try { |
| 57 | 0 | instance[0] = beanClass.newInstance(); |
| 58 | 0 | } catch (InstantiationException e) { |
| 59 | 0 | CoreUtils.getLogger(SwingBeanCreator.this.getClass()).error(e); |
| 60 | 0 | } catch (IllegalAccessException e) { |
| 61 | 0 | CoreUtils.getLogger(SwingBeanCreator.this.getClass()).error(e); |
| 62 | 0 | } |
| 63 | 0 | } |
| 64 | |
}; |
| 65 | |
|
| 66 | 0 | if (SwingUtilities.isEventDispatchThread()) { |
| 67 | 0 | runnable.run(); |
| 68 | |
} else { |
| 69 | |
try { |
| 70 | 0 | SwingUtilities.invokeAndWait(runnable); |
| 71 | 0 | } catch (InterruptedException e) { |
| 72 | 0 | CoreUtils.getLogger(getClass()).error(e); |
| 73 | 0 | } catch (InvocationTargetException e) { |
| 74 | 0 | CoreUtils.getLogger(getClass()).error(e); |
| 75 | 0 | } |
| 76 | |
} |
| 77 | |
|
| 78 | 0 | return instance[0]; |
| 79 | |
} |
| 80 | |
} |