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

it.tidalwave.metadata.viewer.MetadataItemPaneSupport Maven / Gradle / Ivy

/*******************************************************************************
 *
 * blueMarine - open source photo workflow
 * =======================================
 *
 * Copyright (C) 2003-2009 by Fabrizio Giudici
 * Project home page: http://bluemarine.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. 
 *
 *******************************************************************************
 *
 * $Id$
 *
 ******************************************************************************/
package it.tidalwave.metadata.viewer;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JCheckBox;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.beans.AbstractEnhancer.PropertyFilter;
import it.tidalwave.metadata.MetadataItemHolder;

/*******************************************************************************
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 ******************************************************************************/
public abstract class MetadataItemPaneSupport extends PaneSupport
  {
    private static final String CLASS = MetadataItemPaneSupport.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);
    
    private final List availabilityCheckboxes = new ArrayList();

    private boolean availabilityOptionVisible;
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    private final ContainerListener availabilityCheckBoxTracker = new ContainerListener() 
      {
        public void componentAdded (@Nonnull final ContainerEvent event)
          {
            final Component child = (Component)event.getChild();
            
            if (isAvailabilityCheckbox(child))
              {
                availabilityCheckboxes.add((JCheckBox)child);
              }
            else if (child instanceof Container)
              {
                ((Container)child).addContainerListener(availabilityCheckBoxTracker);  
              }
          }

        public void componentRemoved (@Nonnull final ContainerEvent event)
          {
            final Component child = (Component)event.getChild();
            
            if (isAvailabilityCheckbox(child))
              {
                availabilityCheckboxes.remove((JCheckBox)child);
              }
            else if (child instanceof Container)
              {
                ((Container)child).removeContainerListener(availabilityCheckBoxTracker);  
              }
          }
        
        private boolean isAvailabilityCheckbox (@Nonnull final Component component)
          {
            // Relies upon Matisse 'set name' option being set
            return (component instanceof JCheckBox) && ((JCheckBox)component).getName().matches("^cb.*Available$");
          }
      };

    /***************************************************************************
     *
     *
     **************************************************************************/
    protected MetadataItemPaneSupport (@Nonnull final Class beanClass)
      {
        this(beanClass, PropertyFilter.DEFAULT_FILTER);
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    protected MetadataItemPaneSupport (@Nonnull final Class beanClass,
                                       @Nonnull final PropertyFilter propertyFilter)
      {
        super(beanClass, propertyFilter);
        setAvailabilityOptionVisible(false);
        addContainerListener(availabilityCheckBoxTracker);
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public void bind (@Nonnull final MetadataItemHolder holder)
      throws IllegalArgumentException
      {
        logger.info("bind(%s)", holder);
        bind(holder.get());
      }

    /***************************************************************************
     *
     *
     **************************************************************************/
    public void setAvailabilityOptionVisible (final boolean visible)
      {
        // TODO: handle when it's not called from the EDT Thread
        final boolean oldValue = this.availabilityOptionVisible;
        this.availabilityOptionVisible = visible;
        firePropertyChange("availabilityOptionVisible", oldValue, visible);
        
        for (final JCheckBox checkBox : availabilityCheckboxes)
          {
            checkBox.setVisible(visible);  
          }
      }
    
    /***************************************************************************
     *
     *
     **************************************************************************/
    public boolean isAvailabilityOptionVisible()
      {
        return availabilityOptionVisible;    
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy