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

it.tidalwave.bluebill.mobile.taxonomy.ui.DefaultTaxonPickerViewController Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-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: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.taxonomy.ui;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import it.tidalwave.util.Task;
import it.tidalwave.role.ui.spi.ActionProviderSupport;
import it.tidalwave.role.ui.spi.DefaultPresentationModel;
import it.tidalwave.util.ui.FlowController;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.netbeans.capabilitiesprovider.ThreadLookupBinder;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.role.util.RoleRegister;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonHistory;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon.Rank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.RequiredArgsConstructor;
import static org.openide.util.NbBundle.*;
import static it.tidalwave.util.ui.UserNotification.notification;

/***********************************************************************************************************************
 *
 * @stereotype Controller
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor
public class DefaultTaxonPickerViewController implements TaxonPickerViewController
  {
    private static final Logger log = LoggerFactory.getLogger(DefaultTaxonPickerViewController.class);
    
    private static final Class _ = DefaultTaxonPickerViewController.class;

    @Nonnull
    private final TaxonPickerView view;
    
    @Nonnull
    private final FlowController controlFlow;
    
    private final DefaultPresentationModel taxaPM = new DefaultPresentationModel();

    private final TaxonomyPreferences preferences = Locator.find(TaxonomyPreferences.class);

    private final TaxonHistory taxonHistory = Locator.find(TaxonHistory.class);

    private final TaxonomyFooterController taxonomyFooterController = new TaxonomyFooterController();
    
    protected final RoleRegister roleRegister = new RoleRegister();
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @RequiredArgsConstructor
    class SelectTaxonAction extends AbstractAction
      {
        private final Taxon taxon;
        
          { 
            putValue(NAME, "select");
          }

        public void actionPerformed (final @Nonnull ActionEvent event) 
          {
            log.info("SelectTaxonAction.actionPerformed(): {}", taxon);
            controlFlow.toNextStep(taxon);
          }
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    class TaxonActionProvider extends ActionProviderSupport
      {
        private final Action selectTaxonAction;
        
        public TaxonActionProvider (final @Nonnull Taxon taxon)
          {
            selectTaxonAction = new SelectTaxonAction(taxon);
          }

        @Override @Nonnull
        public Action getDefaultAction() 
          {
            return selectTaxonAction;
          }
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    @Override @Nonnull
    public Action getBrowseByRankAction (final @Nonnull Rank rank) 
      {
        return new AbstractAction(getMessage(_, "browseBy" + rank))
          {
            public void actionPerformed (final ActionEvent event) 
              {
                log.info("BrowseByRankAction.actionPerformed(): {}", rank);
                controlFlow.toNextStep(rank);
              }
          };
      }
        
    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public void initialize() 
      {
        log.info("initialize()");
        view.lock(notification().withText(_, "preparingData")); 
        view.showTaxonomyName(taxonomyFooterController.getTaxonomyDisplayName());
        view.showTaxonomyLanguages(taxonomyFooterController.getTaxonLanguages());
        
        new Thread()
          {
            @Override
            public void run()
              {        
                populateView();
              }
          }.start();
      }
    
    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    public void clearTaxonHistory()
      {
        log.info("clearTaxonHistory()");
        taxonHistory.clearTaxonHistory();
        taxaPM.clear();
        view.populate(taxaPM);
      }

    /*******************************************************************************************************************
     *
     *
     *
     ******************************************************************************************************************/
    protected void populateView()
      {
        log.info("populateView()");
        final List taxa = taxonHistory.getRecentTaxa();
        final long time = System.currentTimeMillis();
        Collections.sort(taxa, preferences.getTaxonComparator());
        log.debug(">>>> {} taxa sorted in {} msec", taxa.size(), System.currentTimeMillis() - time);
        
        taxaPM.clear();
        
        ThreadLookupBinder.with(roleRegister).run(new Task() 
          {
            @Override
            public Void run()
              {
                for (final Taxon taxon : taxa)
                  {
                    taxaPM.addChild(new DefaultPresentationModel(taxon, new Object[] { new TaxonActionProvider(taxon) } ));                
                  }

                return null;
              }
          });
        
        view.unlock();
        view.populate(taxaPM);
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy