All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.netbeans.role.util.BeanFactory Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * OpenBlueSky - NetBeans Platform Enhancements
 * Copyright (C) 2006-2012 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://openbluesky.java.net
 * SCM: https://bitbucket.org/tidalwave/openbluesky-src
 *
 **********************************************************************************************************************/
package it.tidalwave.netbeans.role.util;

import javax.annotation.Nonnull;
import java.awt.Component;
import java.awt.EventQueue;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Property;
import org.openide.filesystems.FileObject;
import org.openide.util.Lookup;
import org.openide.util.Parameters;
import it.tidalwave.util.logging.Logger;

/***********************************************************************************************************************
 *
 * An utility class with a static factory method capable of instantiating a JavaBean, and populating it with properties
 * by means of introspection, out of a declaration in layer.xml. This factory guarantees that Swing components are
 * always created in the EDT thread.
 *
 * For instance, supposing one
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public final class BeanFactory
  {
    private static final String CLASS = BeanFactory.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private BeanFactory()
      {
      }

    /*******************************************************************************************************************
     *
     * Creates a new bean instance out of a {@link FileObject}. This is an example of a valid layer.xml section:
     *
     * 
     * 
     *     
     *     
     *     
     *     
     *     
     * 
     * 
* * @param fileObject the file with the declarative part * @return the instantiated bean * @throws IllegalArgumentException when the 'class' attribute is missing * @throws ClassNotFoundException if the bean couldn't be instantiated * @throws InstantiationException if the bean couldn't be instantiated * @throws IllegalAccessException if the bean couldn't be instantiated * @throws UnsupportedOperationException if trying to set a property that doesn't exist or is read-only * ******************************************************************************************************************/ @Nonnull public static Object createInstance (@Nonnull final FileObject fileObject) throws Exception { Parameters.notNull("fileObject", fileObject); logger.fine("createInstance(%s)", fileObject); final String className = (String)fileObject.getAttribute("class"); if (className == null) { throw new IllegalArgumentException("No required fileobject attribute: class"); } logger.finer(">>>> className: %s", className); final ClassLoader globalClassLoader = Lookup.getDefault().lookup(ClassLoader.class); final Class clazz = globalClassLoader.loadClass(className); final Object instance = instantiate(clazz); if (instance instanceof Exception) { throw (Exception)instance; } final List propertyNames = Collections.list(fileObject.getAttributes()); propertyNames.removeAll(Arrays.asList("class", "instanceCreate")); for (final String propertyName : propertyNames) { final Object propertyValue = fileObject.getAttribute(propertyName); final Property property = BeanProperty.create(propertyName); property.setValue(instance, propertyValue); logger.finer(">>>> setting %s=%s", propertyName, propertyValue); } logger.finer(">>>> created: %s", instance); return instance; } /******************************************************************************************************************* * * Instantiate the class, making sure that instance of Swing components are created in the EDT thread. * * @param clazz the class * @return the instantiated object * ******************************************************************************************************************/ protected static Object instantiate (final Class clazz) { if (EventQueue.isDispatchThread() || !Component.class.isAssignableFrom(clazz)) { try { return clazz.newInstance(); } catch (InstantiationException e) { return e; } catch (IllegalAccessException e) { return e; } } else { try { final Object[] result = new Object[1]; EventQueue.invokeAndWait(new Runnable() { @Override public void run() { try { result[0] = clazz.newInstance(); } catch (InstantiationException e) { result[0] = e; } catch (IllegalAccessException e) { result[0] = e; } } }); return result[0]; } catch (Exception e) { return e; } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy