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

it.tidalwave.bluebill.mobile.observation.share.TextReportGenerator Maven / Gradle / Ivy

There is a newer version: 1.0.15
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: TextReportGenerator.java,v 0fa9706c3125 2010/06/27 20:48:54 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.observation.share;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import it.tidalwave.util.AsException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.geo.Range;
import it.tidalwave.bluebill.observation.Location;
import it.tidalwave.bluebill.observation.Observation;
import it.tidalwave.bluebill.observation.ObservationItem;
import it.tidalwave.bluebill.observation.ObservationSet;
import it.tidalwave.mobile.location.LocationPreferences;
import it.tidalwave.bluebill.mobile.GeneralPreferences;
import it.tidalwave.bluebill.mobile.observation.ObservationPreferences;
import org.openide.util.NbBundle;
import static it.tidalwave.bluebill.mobile.observation.ObservationPreferences.Extra.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class TextReportGenerator extends ReportGenerator
  {
    private static final Class Range = Range.class; // FIXME: move to Range

    private ObservationPreferences observationPreferences;

    private int previousDay = 0;

    private final Calendar calendar = Calendar.getInstance();

    private final List strings = new ArrayList();

    @Nonnull
    private final GeneralPreferences generalPreferences = Locator.find(GeneralPreferences.class);

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void begin (final @Nonnull ObservationSet observationSet)
      {
        observationPreferences = Locator.find(ObservationPreferences.class);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void end (final @Nonnull ObservationSet observationSet)
      {
        buffer.append(NbBundle.getMessage(TextReportGenerator.class, "signature"));
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void beginVisit (final @Nonnull Observation observation)
      {
        final Date date = observation.getDate();
        final Location location = observation.getLocation();
        
        calendar.setTime(date);
        final int thisDay = calendar.get(Calendar.DAY_OF_YEAR);

        if (previousDay != thisDay)
          {
            buffer.append(generalPreferences.formatDateAndTime(date));
          }
        else
          {
            buffer.append(generalPreferences.formatTime(date));
          }

        previousDay = thisDay;

        buffer.append(" ").append(location.getDisplayName());

        try
          {
            final LocationPreferences locationPreferences = Locator.find(LocationPreferences.class);
            buffer.append(" - ").append(locationPreferences.format(location.as(Range)));
          }
        catch (AsException e)
          {
            // Range not available, never mind
          }

        buffer.append("\n");
        strings.clear();
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void visit (final @Nonnull ObservationItem observationItem)
      {
        strings.add("* " + observationPreferences.format(observationItem, CARDINALITY, GENDER, MODE, NOTE_FULL));
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void endVisit (final @Nonnull Observation observation)
      {
        Collections.sort(strings);

        for (final String string : strings)
          {
            buffer.append(string).append("\n");
          }

        buffer.append("\n");
        strings.clear();
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy