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

it.tidalwave.role.ui.impl.AsLookupPresentationModel Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Stats
 * Copyright (C) 2011-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://bluebill.tidalwave.it/mobile/
 * SCM: http://java.net/hg/bluebill-server~stats-src
 *
 **********************************************************************************************************************/
package it.tidalwave.role.ui.impl;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.beans.PropertyChangeSupport;
import it.tidalwave.util.As;
import it.tidalwave.role.Composite;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.netbeans.util.AsLookupSupport;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import lombok.Delegate;

/***********************************************************************************************************************
 *
 * TODO: should have an owner and include the owner Lookup.
 * As the NetBeans Node, it should allow children, have event listeners for children added/removed/changed.
 * This class so becomes the true M in MVC.
 * 
 * @stereotype Role
 * 
 * @author  Fabrizio Giudici
 * @version $Id: AsLookupPresentationModel.java,v d460c8cb3874 2011/07/14 14:24:33 fabrizio $
 *
 **********************************************************************************************************************/
public class AsLookupPresentationModel implements PresentationModel, Lookup.Provider
  {
    class MyAsLookupSupport extends AsLookupSupport
      {
        public MyAsLookupSupport() 
          {
            super();
          }

        public MyAsLookupSupport (final @Nonnull Object[] roles) 
          {
            super(roles);
          }
        
        public MyAsLookupSupport (final @Nonnull Object owner) 
          {
            super(owner);
          }

        public MyAsLookupSupport (final @Nonnull Object owner, final @Nonnull Object[] roles)
          {
            super(owner, roles);
          }

        @Override @Nonnull
        protected Lookup createLookup()
          {
            final Lookup superLookup = super.createLookup();
                    
            Composite delegateComposite = superLookup.lookup(Composite.class);
            
            if (delegateComposite == null)
              {
                delegateComposite = Composite.DEFAULT;
              }
            
            composite = new PresentationModelComposite(delegateComposite);
            
            return new ProxyLookup(Lookups.fixed(composite), superLookup);
          }        
      }
            
    @Delegate(types={ As.class, Lookup.Provider.class })
    protected final MyAsLookupSupport asDelegate;
    
    @Delegate(types=PropertyChangeSupport.class)
    protected final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
    
    @CheckForNull
    protected PresentationModelComposite composite;
    
    private boolean inToString = false;
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AsLookupPresentationModel()
      {
        asDelegate = new MyAsLookupSupport();
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AsLookupPresentationModel (final @Nonnull Object owner)
      {
        asDelegate = new MyAsLookupSupport(owner, new Object[]{ owner });
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AsLookupPresentationModel (final @Nonnull Object owner, final @Nonnull Object[] instanceRoles)
      {
        asDelegate = new MyAsLookupSupport(owner, instanceRoles);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AsLookupPresentationModel (final @Nonnull Object[] instanceRoles) 
      {
        asDelegate = new MyAsLookupSupport(instanceRoles);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public String toString() 
      {
        if (inToString || (composite == null) || !composite.isCountComputed())
          {
            return String.format("%s@%x[children count not computed]", getClass().getSimpleName(), System.identityHashCode(this));
          }
        
        try
          {
            inToString = true;
            return String.format("%s@%x[%d children]", getClass().getSimpleName(), 
                                                       System.identityHashCode(this),
                                                       composite.findChildren().count());
          }
        finally
          {
            inToString = false;
          }
      }     
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy