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

it.tidalwave.metadata.persistence.node.impl.MetadataCompositeNode Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * blueMarine Metadata - open source media workflow
 * Copyright (C) 2007-2011 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://bluemarine.tidalwave.it
 * SCM: https://kenai.com/hg/bluemarine~metadata-src
 *
 **********************************************************************************************************************/
package it.tidalwave.metadata.persistence.node.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import it.tidalwave.util.logging.Logger;
import java.text.Collator;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import it.tidalwave.metadata.persistence.MetadataComposite;
import it.tidalwave.metadata.persistence.MetadataProperty;
import it.tidalwave.metadata.persistence.MetadataPropertySet;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class MetadataCompositeNode extends ManagedNode
  {
    private static final String CLASS = MetadataCompositeNode.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);
    
    protected final MetadataCompositeChildFactory childFactory;

    @CheckForNull
    private final MetadataComposite composite;
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    protected static class MetadataCompositeChildFactory extends ChildFactory
      {
        private final MetadataComposite composite;

        /***********************************************************************
         *
         *
         **********************************************************************/
        public MetadataCompositeChildFactory (@CheckForNull final MetadataComposite composite)
          {
            this.composite = composite;
          }
        
        /***********************************************************************
         *
         *
         **********************************************************************/
        @Override
        @Nonnull
        protected final Node createNodeForKey (@Nonnull final Object object)
          {
            logger.finest("createNodeForKey(%s) - I'm %s", object, composite);
            
            if ((object instanceof MetadataProperty) || (object instanceof MetadataPropertySet))
              {
                return new MetadataCompositeNode((MetadataComposite)object);
              }
            
            else
              {
                return new ValueNode((MetadataProperty)composite, object);
              }
          }
        
        /***********************************************************************
         *
         *
         **********************************************************************/
        protected final boolean createKeys (@Nonnull final List keys)
          {
            try
              {
                if ((composite == null) /*root*/ || (composite instanceof MetadataPropertySet))
                  {
                    keys.addAll(findProperties());
                  }
                else
                  {
                    keys.addAll(findValues());
                  }
              }
            catch (Exception e)
              {
                logger.severe(String.format("Failed to create keys for node %s", this));
                logger.throwing(CLASS, "createKeys()", e);
              }
            
            return true;
          }
        
        /***********************************************************************
         *
         *
         **********************************************************************/
        @Nonnull
        @edu.umd.cs.findbugs.annotations.SuppressWarnings(value={"SIC_INNER_SHOULD_BE_STATIC_ANON"})
        protected List findProperties()
          {
            final List properties = composite.getChildProperties();
            final Collator collator = Collator.getInstance();
            Collections.sort(properties, new Comparator() 
              {
                public int compare (@Nonnull final MetadataProperty p1,
                                    @Nonnull final MetadataProperty p2)
                  {
                    return collator.compare(p1.getPropertyName(), p2.getPropertyName());
                  }
              });
              
            return properties;
          }
        
        /***********************************************************************
         *
         *
         **********************************************************************/
        @Nonnull
        protected List findValues()
          {
            final List values = ((MetadataProperty)composite).getValues();
//            Collections.sort(values);
            return values;
          }
        
        /***********************************************************************
         *
         *
         **********************************************************************/
        public final void refresh()
          {
            refresh(false);    
          }
      }
    
    /***************************************************************************
     *
     * Creates a new instance bound to a property.
     *
     * @param  metadataProperty  the property
     *
     **************************************************************************/
    protected MetadataCompositeNode (@Nonnull final MetadataComposite composite)
      {
        this(composite, new MetadataCompositeChildFactory(composite));
      }

    /***************************************************************************
     *
     * Creates a new instance bound to a {@link MetadataProperty}.
     *
     **************************************************************************/
    private MetadataCompositeNode (@Nonnull final MetadataComposite composite,
                                   @Nonnull final MetadataCompositeChildFactory childFactory)
      {
        super(Children.create(childFactory, true), composite);
        this.composite = composite;
        this.childFactory = childFactory;
        refreshAttributes();
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    protected MetadataCompositeNode (@CheckForNull final MetadataCompositeChildFactory childFactory)
      {
        super(Children.create(childFactory, true));
        this.composite = null;
        this.childFactory = childFactory;
        refreshAttributes();
      }

//    /***************************************************************************
//     *
//     *
//     **************************************************************************/
//    @Override
//    public void destroy() 
//      throws IOException
//      {
//        super.destroy();
//      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public final void refreshChildren()
      {
        childFactory.refresh();
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    @Override
    @Nonnull
    public String toString()
      {
        return String.format("MetadataCompositeNode[%s]", composite);
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    protected void refreshAttributes() 
      {
        final String string = nodeManager.getDisplayName(this);
        setName(string);
        setDisplayName(string);
      }
  }