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

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.TaxonFactSheetActivitySupport 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://kenai.com/hg/bluebill~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import it.tidalwave.ui.android.widget.PresentationModelAdapter;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.mobile.ui.QuestionWithFeedback;
import it.tidalwave.bluebill.taxonomy.mobile.Taxon;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.TaxonFactSheetViewController;
import it.tidalwave.bluebill.mobile.taxonomy.ui.TaxonRenderingOptions;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import it.tidalwave.ui.android.app.AndroidActivityHelper;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonIntentHelper;
import it.tidalwave.bluebill.mobile.android.util.CommonOptionsMenuController;
import lombok.RequiredArgsConstructor;
import static org.openide.util.NbBundle.*;
import static it.tidalwave.role.ui.android.TextViewRenderable.TextViewRenderable;

/***********************************************************************************************************************
 *
 * @stereotype View
 * @stereotype Activity
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@RequiredArgsConstructor
public abstract class TaxonFactSheetActivitySupport extends Activity
  {
    private static final Logger log = LoggerFactory.getLogger(TaxonFactSheetActivitySupport.class);
    
    private static final Class _ = TaxonFactSheetContainerActivity.class;
    
    @CheckForNull
    protected Taxon taxon;

    private final AndroidActivityHelper activityHelper = new AndroidActivityHelper(this);

    private final CommonOptionsMenuController commonOptionsMenuController = new CommonOptionsMenuController(this);
    
    protected ProgressDialog progressDialog;

    protected ListView list;

    protected Controller controller;

    private final int contentViewId;
    
    @CheckForNull
    protected PresentationModelAdapter adapter;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
//    @Override
    public void notifyStatusChanged()
      {
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                if (adapter != null)
                  {
                    adapter.notifyDataSetChanged();
                  }
              }
          });
      }
    
    /*******************************************************************************************************************
     *
     * Notifies that the data to render are ready.
     *
     ******************************************************************************************************************/
    public void populate (final @Nonnull List presentationModels)
      {
        log.info("populate({} items)", presentationModels.size());
        runOnUiThread(new Runnable()
          {
            public void run()
              {
                adapter = new PresentationModelAdapter(TaxonFactSheetActivitySupport.this, presentationModels);  
                list.setAdapter(adapter);
                list.setOnItemClickListener(adapter.getOnItemClickListener());
                progressDialog.dismiss();
              }
          });
      }

    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     * 
     ******************************************************************************************************************/
    public void confirmToDownloadMedia (final @Nonnull QuestionWithFeedback questionWithFeedback) 
      {
        log.info("confirmToDownloadMedia({})", questionWithFeedback);
        activityHelper.showConfirmationDialog(questionWithFeedback);
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     * 
     ******************************************************************************************************************/
    public void confirmToDeleteMedia (final @Nonnull QuestionWithFeedback questionWithFeedback) 
      {
        log.info("confirmToDeleteMedia({})", questionWithFeedback);
        activityHelper.showConfirmationDialog(questionWithFeedback);
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     * 
     ******************************************************************************************************************/
    public void notifySdCardNotReady (final @Nonnull QuestionWithFeedback questionWithFeedback) 
      {
        log.info("notifySdCardNotReady({})", questionWithFeedback);
        activityHelper.showErrorDialog(questionWithFeedback);
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void notifyCompletion (final @Nonnull String message)
      {
        log.info("notifyCompletion({})", message);
        activityHelper.showLightNotification(message);
      }
    
    /*******************************************************************************************************************
     *
     * {@inheritDoc}
     *
     ******************************************************************************************************************/
    public void notifyError (final @Nonnull String message)
      {
        log.info("notifyError({})", message);
        activityHelper.showLightNotification(message);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void openWebPage (final @Nonnull String url) 
      {
        log.info("openWebPage({})", url);
        activityHelper.openWebPage(url);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onCreate (final @Nonnull Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(contentViewId);
        list = (ListView)findViewById(R.id.list);
        
        if (list != null) // some subclasses might not use it   
          {
            registerForContextMenu(list);
          }

        try
          {
            taxon = TaxonIntentHelper.getTaxon(getIntent());
            controller = createController(); // after taxon is set
            final TextView textView = (TextView)findViewById(R.id.tvTaxon);
            taxon.as(TextViewRenderable).render(textView, TaxonRenderingOptions.PRIMARY_AND_SCIENTIFIC_NAME);
            
//            factSheetProvider = new FactSheetProvider()
//              {
//                @Nonnull
//                public FactSheet findFactSheetFor (final @Nonnull As as)
//                  throws IOException
//                  {
////                    try
////                      {
//                        throw new IOException("BBC Fact sheet disabled");
////                        return new BbcFactSheetProvider().createFactSheet(taxon);
////                      }
////                    catch (NotFoundException e)
////                      {
////                        throw new IOException(e.toString()); // FIXME: return empty fact sheet
////                      }
//                  }
//              };
          }
        catch (NotFoundException e)
          {
            throw new RuntimeException("Can't find taxon", e); 
            // TODO: logging
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public void onResume()
      {
        super.onResume();
        final String preparingData = getMessage(_, "preparingData");
        progressDialog = ProgressDialog.show(this, "", preparingData, true);

        try
          {
            controller.initialize();
          }
        catch (Exception e)
          {
            log.warn("While setting up {}", e.toString());
            log.warn("", e);
            // FIXME: diags
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onCreateOptionsMenu (final @Nonnull Menu menu)
      {
        getMenuInflater().inflate(R.menu.common_options_menu, menu);
        return true;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    public boolean onOptionsItemSelected (final @Nonnull MenuItem item)
      {
        return commonOptionsMenuController.onOptionsItemSelected(item);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected abstract Controller createController();
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy