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

it.tidalwave.metadata.persistence.node.impl.MetadataPersistenceNodeManagerImpl 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 javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.awt.Image;
import java.awt.image.BufferedImage;
import org.openide.nodes.Node;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.metadata.MetadataItemFormat;
import it.tidalwave.metadata.persistence.MetadataProperty;
import it.tidalwave.metadata.persistence.MetadataPropertySet;
import it.tidalwave.metadata.persistence.PropertyAndValue;
import it.tidalwave.metadata.persistence.node.MetadataPersistenceNodeManager;

/*******************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public class MetadataPersistenceNodeManagerImpl implements MetadataPersistenceNodeManager
  {
    private static final String CLASS = MetadataPersistenceNodeManagerImpl.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    @Nonnull
    private final MetadataItemFormat itemFormat = MetadataItemFormat.Locator.findMetadataItemFormat();

    @CheckForNull
    private MetadataRootNode metadataRootNode;

    @CheckForNull
    private Image empty;
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public synchronized Node getRootNode() 
      {
        if (metadataRootNode == null)
          {
            metadataRootNode = new MetadataRootNode();
          }
        
        return metadataRootNode;
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public Image findIcon (final Node node) 
      {
        if (empty == null)
          {
            empty = new BufferedImage(32, 32, BufferedImage.TYPE_4BYTE_ABGR);
          }
        
        return empty;
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public String getDisplayName (@Nonnull final MetadataCompositeNode metadataNode)
      {
        logger.fine("getDisplayName(%s)", metadataNode);
        
        final MetadataProperty property = metadataNode.getLookup().lookup(MetadataProperty.class);
        String result = "???";
        
        if (property != null)
          {
            final Class itemClass = property.getItemClass();
            final String propertyName = property.getPropertyName();
            result = itemFormat.getPropertyDisplayName(itemClass, propertyName);
          }
        else
          {
            final MetadataPropertySet propertySet = metadataNode.getLookup().lookup(MetadataPropertySet.class);

            if (propertySet != null)
              {
                final Class itemClass = propertySet.getItemClass();
                result = itemFormat.getItemDisplayName(itemClass);
              }
          }
        
        logger.finer(">>>> returning %s", result);
        
        return result;
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    @Nonnull
    public String getDisplayName (@Nonnull final ValueNode valueNode)
      {
        logger.fine("getDisplayName(%s)", valueNode);
        
        final PropertyAndValue propertyAndValue = valueNode.getLookup().lookup(PropertyAndValue.class);
        final MetadataProperty property = propertyAndValue.getProperty();
        final String propertyName = property.getPropertyName();
        final Object value = propertyAndValue.getValue();
        final String result = itemFormat.format(property.getItemClass(), propertyName, value);
        logger.finer(">>>> returning %s",  result);
        
        return result;
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    protected void refreshNode (@Nonnull final Class propertySetClass,
                                @Nonnull final String propertyName)
      {
        logger.fine("refreshNode(%s.%s)", propertySetClass, propertyName);
        
        if (metadataRootNode != null) // it may happen that no nodes have been loaded so far
          {
            for (final Node propertySetNode : metadataRootNode.getChildren().getNodes()) 
              {
                final MetadataPropertySet propertySet = propertySetNode.getLookup().lookup(MetadataPropertySet.class);
                
                // isAssignableFrom() because propertySetClass might be a CGLIB synthetic class
                // FIXME: but I don't like it very much, since it could be a regular sub-class as well
                // FIXME: must find a way to check a perfect match
                if ((propertySet != null) && (propertySet.getItemClass().isAssignableFrom(propertySetClass)))
                  {
                    refreshPropertySetNode((MetadataCompositeNode)propertySetNode, propertyName);
                    return; 
                  }
              }
            
            // propertySetNode not found, this means that a new property set has been added
            logger.finest(">>>> refreshing root node...");
            metadataRootNode.refreshChildren();
          }
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    private void refreshPropertySetNode (@Nonnull final MetadataCompositeNode propertySetNode,
                                         @Nonnull final String propertyName)
      {
        logger.finest("refreshPropertySetNode(%s)", propertySetNode);
        
        propertySetNode.refreshAttributes(); // refresh DataObject counter
        
        for (final Node propertyNode : propertySetNode.getChildren().getNodes()) 
          {
            final MetadataProperty property = propertyNode.getLookup().lookup(MetadataProperty.class);

            if ((property != null) && propertyName.equals(property.getPropertyName()))
              {
                refreshPropertyNode((MetadataCompositeNode)propertyNode);
                return;
              }
          }

         // Node not found: can happen if the node has not been loaded yet, ignore that
      }
     
    /***************************************************************************
     *
     *
     **************************************************************************/
    private void refreshPropertyNode (@Nonnull final MetadataCompositeNode propertyNode)
      {
        logger.finest("refreshPropertyNode(%s)", propertyNode);
        
        propertyNode.refreshAttributes(); // counter
        propertyNode.refreshChildren();   // maybe a new value
        
        // FIXME: if you had the value, you could only invalidate a single node for recounting...
        for (final Node valueNode : propertyNode.getChildren().getNodes()) 
          {
            final PropertyAndValue propertyAndValue = valueNode.getLookup().lookup(PropertyAndValue.class);

            if (propertyAndValue != null) 
              {
                logger.finest(">>>> refreshing node: %s", valueNode);
                propertyAndValue.invalidateDataObjectCount();
              }
          }
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy