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

it.tidalwave.geo.viewer.impl.GeoRendererPanel 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;

import javax.annotation.PostConstruct;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.List;
import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.JPanel;
import org.jdesktop.observablecollections.ObservableList;
import org.jdesktop.observablecollections.ObservableListListener;
import it.tidalwave.util.logging.Logger;
import it.tidalwave.geo.viewer.FeatureManager;
import it.tidalwave.geo.viewer.spi.GeoViewProvider;
import javax.inject.Provider;

/***********************************************************************************************************************
 *
 * The component which renders a geographic view. It is implemented as a {@link JPanel} with a {@link CardLayout} that
 * contains all the rendering components for {@link GeoViewProvider}s and select the one related to the current
 * {@link GeoViewProvider}. Rendering components are added to the Swing hieararchy in lazy fashion, the first time they are
 * used.
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public final class GeoRendererPanel extends JPanel
  {
    public static final String PROP_SELECTED_GEO_VIEW_PROVIDER = "selectedGeoViewProvider";
    
    private static final String CLASS = GeoRendererPanel.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final CardLayout cardLayout = new CardLayout();

    @CheckForNull
    private GeoViewProvider selectedGeoViewProvider;

    @Inject
    private Provider featureManager;

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public GeoRendererPanel()
      {
        setLayout(cardLayout);
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public void setSelectedGeoViewProvider (final @Nullable GeoViewProvider selectedGeoViewProvider)
      {
        if (selectedGeoViewProvider != null)
          {
            final Component rendererComponent = selectedGeoViewProvider.getRendererComponent();
            assert rendererComponent != null : String.format("%s returned null component!", selectedGeoViewProvider);
            final String id = rendererComponent.getClass().getName();

            if (rendererComponent.getParent() != this)
              {
                logger.fine(">>>> adding new renderer component: %s", rendererComponent);
                add(rendererComponent, id);
              }

            cardLayout.show(this, id);
          }
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @CheckForNull
    public GeoViewProvider getSelectedGeoViewProvider()
      {
        return selectedGeoViewProvider;
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    @PostConstruct
    @SuppressWarnings("PMD.UnusedPrivateMethod") @edu.umd.cs.findbugs.annotations.SuppressWarnings("UPM_UNCALLED_PRIVATE_METHOD")
    private void initialize()
      {
        featureManager.get().getFeatures().addObservableListListener(new ObservableListListener()
          {
            @Override
            public void listElementsAdded(ObservableList ol, int i, int i1)
              {
                repaint();
              }

            @Override
            public void listElementsRemoved(ObservableList ol, int i, List list)
              {
                repaint();
              }

            @Override
            public void listElementReplaced(ObservableList ol, int i, Object o)
              {
                repaint();
              }

            @Override
            public void listElementPropertyChanged(ObservableList ol, int i)
              {
                repaint();
              }
          });
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy