Coverage Report - org.jtheque.core.spring.factory.SwingBeanCreator
 
Classes in this File Line Coverage Branch Coverage Complexity
SwingBeanCreator
0 %
0/16
0 %
0/2
2.25
SwingBeanCreator$1
0 %
0/8
N/A
2.25
 
 1  
 package org.jtheque.core.spring.factory;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *            
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License. 
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 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  
  * A factory bean for keep a proxy of a bean.
 27  
  *
 28  
  * @author Baptiste Wicht
 29  
  */
 30  0
 public final class SwingBeanCreator extends AbstractFactoryBean {
 31  
     private final Class<?> beanClass;
 32  
 
 33  
     /**
 34  
      * Construct a new LazyFactoryBean.
 35  
      *
 36  
      * @param beanClass The class of the bean to keep the proxy for.
 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  
 }