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

it.tidalwave.geo.viewer.impl.feature.DefaultFeatureManager Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * forceTen - open source geography
 * Copyright (C) 2007-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://forceten.tidalwave.it
 * SCM: https://bitbucket.org/tidalwave/forceten-src
 *
 **********************************************************************************************************************/
package it.tidalwave.geo.viewer.impl.feature;

import java.util.Collection;
import javax.annotation.Nonnull;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.openide.util.NotImplementedException;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.jdesktop.observablecollections.ObservableCollections;
import org.jdesktop.observablecollections.ObservableList;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.netbeans.util.LookupDelegatingToProvider;
import it.tidalwave.netbeans.nodes.role.NodeDecorator;
import it.tidalwave.netbeans.nodes.role.NodeDelegateFactory;
import it.tidalwave.geo.Coordinate;
import it.tidalwave.geo.viewer.FeatureManager;
import it.tidalwave.geo.viewer.spi.feature.FeatureManagerSpi;
import it.tidalwave.netbeans.nodes.NodePresentationModel;
import java.util.Iterator;

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

    private final ObservableList features = ObservableCollections.observableList(new ArrayList());

    private final Map> coordinateProviderMapByObject =
          new HashMap>();
    
    private final Map nodeMapByObject = new HashMap();

    @Inject
    private NodeDecorator nodeDecorator;
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void addFeature (final @Nonnull Object feature)
      {
        throw new NotImplementedException();
        // TODO: create a proper Provider and then pass to addFeature()
//        features.add(feature);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    @edu.umd.cs.findbugs.annotations.SuppressWarnings("SIC_INNER_SHOULD_BE_STATIC_ANON")
    public void addFeature (final @Nonnull Object feature, final @Nonnull Coordinate coordinate)
      {
        logger.fine("addFeature(%s, %s)", feature, coordinate);

        addFeature(feature, new Provider()
          {
            @Override @Nonnull
            public Coordinate get()
              {
                return coordinate;
              }
          });
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void removeFeature (final @Nonnull Object feature)
      {
        logger.fine("removeFeature(%s)", feature);
        features.remove(feature);
        coordinateProviderMapByObject.remove(feature);
        nodeMapByObject.remove(feature);
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    public void removeAllFeatures()
      {
        logger.fine("removeAllFeatures()");
        features.clear();
        coordinateProviderMapByObject.clear();
        nodeMapByObject.clear();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override
    @Nonnull
    public ObservableList getFeatures()
      {
        return features;
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Coordinate findCoordinate (final @Nonnull Object feature)
      {
        return coordinateProviderMapByObject.get(feature).get();
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Node findNode (final @Nonnull Object feature)
      {
        return nodeMapByObject.get(feature);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void addFeature (final @Nonnull Object feature, final @Nonnull Provider coordinateProvider)
      {
        coordinateProviderMapByObject.put(feature, coordinateProvider);

        Node node = null;
        final LookupDelegatingToProvider coordinateLookup = new LookupDelegatingToProvider(Coordinate.class, coordinateProvider);

        if (feature instanceof Lookup.Provider)
          {
            logger.finer(">>>> feature is a Lookup.Provider");
            final Lookup lookup = ((Lookup.Provider)feature).getLookup();
            final NodeDelegateFactory nodeDelegateFactory = lookup.lookup(NodeDelegateFactory.class);

            if (nodeDelegateFactory != null)
              {
                node = nodeDelegateFactory.createNodeDelegate();
              }
            else
              {
                // FIXME: workaround for a bug in WrappedLookup
                final Collection all = new ArrayList(lookup.lookupAll(Object.class));

                for (final Iterator i = all.iterator(); i.hasNext(); )
                  {
                    if (i.next() == null)
                      {
                        i.remove();
                      }
                  }

                node = new NodePresentationModel(feature,
                                       Children.LEAF,
                                       new ProxyLookup(coordinateLookup, Lookups.fixed(all.toArray())));
//                node = new EnhancedNode(feature, Children.LEAF, new ProxyLookup(coordinateLookup, lookup));
              }
          }
        else
          {
            node = new NodePresentationModel(feature, Children.LEAF, coordinateLookup);
          }

        logger.finer(">>>> nodeDelegate: %s", node);

        if (nodeDecorator != null)
          {
            logger.finer(">>>> nodeDecorator: %s", nodeDecorator);
            node = nodeDecorator.decorate(node);
          }

        logger.finer(">>>> decorated nodeDelegate: %s", node);

        nodeMapByObject.put(feature, node);
        features.add(feature);
      }
  }