Android Open Source - Joetz-Android-V2 Validator Binding






From Project

Back to project page Joetz-Android-V2.

License

The source code is released under:

GNU General Public License

If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.jens.myapplication.domain.binding;
/*from w  w  w  .  jav  a  2s. com*/
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;

import com.example.jens.myapplication.R;
import com.example.jens.myapplication.domain.converter.ErrorConverter;
import com.example.jens.myapplication.domain.validator.Validator;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * A binding to an EditText object that also validates it
 */
public abstract class ValidatorBinding<T extends Validator>{
    protected T validator;
    protected EditText view;
    protected int field;
    private ValidationTask validationTask;
    private String itemName;
    private boolean required;

    private LinkedList<View.OnFocusChangeListener> onFocusChangeListeners;

    /**
     *
     * @param validator The validator for this binding
     * @param view The view this bindign is bound to
     * @param field The field this validator will be validating (implemented in concrete
     *              implementation of this class as constant usually
     * @param itemName The name of the item this binding is bound to (for erorr validation messages)
     */
    public ValidatorBinding(T validator, EditText view, int field, String itemName){
        this(validator, view, field, itemName, true);
    }

    /**
     *
     * @param validator The validator for this binding
     * @param view The view this bindign is bound to
     * @param field The field this validator will be validating (implemented in concrete
     *              implementation of this class as constant usually
     * @param itemName The name of the item this binding is bound to (for erorr validation messages)
     * @param required True if the field is required to be filled in
     */
    public ValidatorBinding(T validator, EditText view, int field, String itemName, boolean required){
        this.validator = validator;
        this.view = view;
        this.field = field;
        this.required = required;
        this.validationTask = createTask(field);
        if(this.validationTask == null){
            throw new IllegalArgumentException("Invalid field ID for binding");
        }
        this.itemName = itemName;

        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    setValue(((EditText) v).getText().toString());
                    validate();
                }
                if(onFocusChangeListeners != null){
                    for(View.OnFocusChangeListener listener : onFocusChangeListeners){
                        listener.onFocusChange(v, hasFocus);
                    }
                }
            }
        });

        view.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                setValue(s.toString());
                //validate();
            }
        });


        final EditText fView = view;
        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_DEL){
                    setValue(fView.getText().toString());
                    validate();
                }
                return false;
            }
        });

        fillValue();
        setValue(view.getText().toString());
    }

    /**
     * Validate this field
     * @return The list of errors if there were validation errors,
     * null if there were no validation errors
     */
    public List<Integer> validate(){
        if(!required && view.getText().toString().isEmpty()){
            view.setError(null);
            return null;
        }
        List<Integer> errorIds = new ArrayList<Integer>();
        if(required && view.getText().toString().isEmpty()){
            errorIds.add(R.string.$_need_filled_in);
        }
        else{
            validationTask.validate(errorIds);
        }

        view.setError(ErrorConverter.createErrorsString(errorIds, itemName));

        if(errorIds.size() < 1){
            return null;
        }else{
            return errorIds;
        }
    }

    /**
     * Add a listener that will be called upon when the focus of this binding changes
     * @param focusChangeListener The listener object that will be notified of the change
     */
    public void addOnFocusChangeListener(View.OnFocusChangeListener focusChangeListener){
        if(onFocusChangeListeners == null){
            onFocusChangeListeners = new LinkedList<View.OnFocusChangeListener>();
        }
        onFocusChangeListeners.add(focusChangeListener);
    }

    public void setRequired(boolean required){
        this.required = required;
    }

    public boolean isRequired() {
        return required;
    }

    /**
     *
     * @return The view this binding is bound to
     */
    public EditText getView(){
        return view;
    }

    /**
     * Create the task that validates a field
     * @param field The field that should be validated
     * @return
     */
    protected abstract ValidationTask createTask(int field);

    /**
     * Fill the value of the bound object with the value of the view.
     * @param s The value to be inserted
     */
    protected abstract void setValue(String s);

    /**
     * Fill the value of the bound View with the value of the object.
     */
    public abstract void fillValue();

    protected interface ValidationTask{
        public List<Integer> validate(List<Integer> errors);
    }
}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java