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

it.tidalwave.bluebill.mobile.observation.ui.CountAndGenderFormModel Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * 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.observation.ui;

import javax.annotation.Nonnull;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import it.tidalwave.util.ui.FormModel;
import it.tidalwave.util.ui.FormProperty;
import it.tidalwave.observation.bluebill.BirdGender;
import lombok.Getter;
import lombok.ToString;

/***********************************************************************************************************************
 *
 * @stereotype Model
 * 
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
@ToString
public class CountAndGenderFormModel extends FormModel 
  {
    public enum CountType
      {
        NOT_COUNTED,
        EXACT,
        APPROXIMATED,
        RANGE
      }
    
    @Getter
    private final FormProperty countType = new FormProperty(this, CountType.class, "Type", CountType.NOT_COUNTED); 
            
    @Getter
    private final FormProperty count = new FormProperty(this, String.class, "count", "1"); 
            
    @Getter
    private final FormProperty maxCount = new FormProperty(this, String.class, "maxCount", "1"); 
            
    @Getter
    private final FormProperty watched = new FormProperty(this, Boolean.class, "watched", true);
    
    @Getter
    private final FormProperty listened = new FormProperty(this, Boolean.class, "listened", false);
    
    @Getter
    private final FormProperty note = new FormProperty(this, String.class, "note", "");
    
    @Getter
    private final FormProperty gender = new FormProperty(this, BirdGender.class, "gender", BirdGender.NOT_RECORDED);
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public CountAndGenderFormModel()
      {
        computeEnablements();
        
        countType.addPropertyChangeListener(new PropertyChangeListener() 
          {
            public void propertyChange (final @Nonnull PropertyChangeEvent event) 
              {
                computeEnablements();
              }
          });            
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void computeEnablements()
      {
        count.setEnabled(countType.getValue() != CountType.NOT_COUNTED);
        maxCount.setEnabled(countType.getValue() == CountType.RANGE);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Override
    protected void revalidate()
      {
        int lower = 0;
        int upper = 0;
        boolean valid = true;
        
        if (countType.getValue() != CountType.NOT_COUNTED)
          {
            try
              {
                lower = Integer.parseInt(count.getValue());
                count.setValid(true);                
              }
            catch (NumberFormatException e)
              {
                valid = false;
                count.setValid(false);                
              }
          }
        
        if (valid && countType.getValue() == CountType.RANGE)
          {
            try
              {
                upper = Integer.parseInt(maxCount.getValue());
                maxCount.setValid(true);                
              }
            catch (NumberFormatException e)
              {
                valid = false;
                maxCount.setValid(false);                
              }

            if (valid && (lower >= upper))
              {
                valid = false;
                maxCount.setValid(false);                
              }
          }
        
        setValid(valid);
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy