Android Open Source - baracus-framework Error Handling Factory






From Project

Back to project page baracus-framework.

License

The source code is released under:

Apache License

If you think the Android project baracus-framework 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 net.mantucon.baracus.errorhandling;
//  w  w w.  j a  v a2  s . c om
import android.view.View;
import android.widget.TextView;
import net.mantucon.baracus.annotations.Bean;
import net.mantucon.baracus.lifecycle.Destroyable;
import net.mantucon.baracus.lifecycle.Initializeable;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: marcus
 * Date: 25.09.13
 * Time: 17:41
 * To change this template use File | Settings | File Templates.
 */
@Bean
public class ErrorHandlingFactory implements Initializeable, Destroyable {

    // Error handlers
    private Map<View, Map<Integer, Object[]>> errorMap = new HashMap<View, Map<Integer, Object[]>>();
    private final Map<Integer, CustomErrorHandler> registeredHandlers = new HashMap<Integer, CustomErrorHandler>();
    private final Set<StandardErrorHandler> standardHandlers = new HashSet<StandardErrorHandler>();


    /**
     * adds an error with a specific error level to the passed view.
     * If an implementation of CustomErrorHandler is registered for the affectedResource, the error
     * will be automatically routed to that field
     * <p/>
     * Notice : only to be used when you disregard automatic form validation
     *
     * @param container        - the containing view of the resource
     * @param affectedResource - the resource
     * @param messageId        - a message ID
     * @param severity         - the severity. can be used by the CustomErrorHandler.
     * @param params           - the parameters varags used to replace $1..$n tags in the message text
     */
    public void addErrorToView(View container, int affectedResource, int messageId, ErrorSeverity severity, String... params) {
        if (!errorMap.containsKey(container)) {
            errorMap.put(container, new HashMap<Integer, Object[]>());
        }

        Map<Integer, Object[]> assignment = errorMap.get(container);
        Object[] values = new Object[params != null ? params.length + 2 : 2];

        int id = -1;
        if (registeredHandlers.containsKey(affectedResource)) {
            id = registeredHandlers.get(affectedResource).getId();
        } else {
            id = affectedResource;
        }

        values[0] = Integer.valueOf(messageId);
        values[1] = severity;

        if (params != null && params.length > 0) {
            int i = 2;
            for (String param : params) {
                values[i] = param;
                i++;
            }
        }

        assignment.put(id, values);
    }

    /**
     * let all errors impact on the passed view. All bound fields and error handlers
     * associated with the passed will highlight any error mappeable to the view.
     *
     * @param container - the containing view
     */
    public void applyErrorsOnView(View container) {
        if (!errorMap.containsKey(container)) {
            return;
        }

        Map<Integer, Object[]> assignments = errorMap.get(container);

        for (Map.Entry<Integer, Object[]> set : assignments.entrySet()) {
            View v = container.findViewById(set.getKey());
            // First, handle specific custom handler for using own error handling
            // technique
            if (v != null && CustomErrorHandler.class.isAssignableFrom(v.getClass())) {
                ErrorHandler customErrorHandler = (ErrorHandler) v;
                Object[] params = set.getValue();
                if (params.length > 2) {
                    String[] strings = new String[params.length - 2];
                    for (int i = 0; i < params.length - 2; ++i) {
                        strings[i] = (String) params[i];
                    }
                    Integer msgId = (Integer) params[0];
                    ErrorSeverity severity = (ErrorSeverity) params[1];
                    customErrorHandler.handleError(container, msgId, severity, strings);
                } else {
                    Integer msgId = (Integer) params[0];
                    ErrorSeverity severity = (ErrorSeverity) params[1];
                    customErrorHandler.handleError(container, msgId, severity);
                }
            } else {
                // Now try all standard handlers
                for (StandardErrorHandler handler : standardHandlers) {
                    if (v != null && handler.canHandleView(v)) {
                        Object[] params = set.getValue();
                        TextView t = (TextView) v;
                        Integer msgId = (Integer) params[0];
                        ErrorSeverity severity = (ErrorSeverity) params[1];
                        if (params.length > 2) {
                            String[] strings = new String[params.length - 2];
                            for (int i = 0; i < params.length - 2; ++i) {
                                strings[i] = String.valueOf(params[i]);
                            }
                            handler.handleError(v, msgId, severity, strings);
                        } else {
                            handler.handleError(v, msgId, severity);
                        }
                    }
                }
            }
        }
    }

    /**
     * Determines, whether a view is sticked with errors
     *
     * @param v - the view to check
     * @return true, if any bound errors to the view are found in the errorMap
     */
    public boolean viewHasErrors(View v) {
        if (errorMap.containsKey(v)) {
            return errorMap.get(v).size() > 0;
        }
        return false;
    }

    /**
     * remove all errors from the passed view
     *
     * @param container - the view to clear
     */
    public void resetErrors(View container) {
        if (errorMap.containsKey(container)) {
            Map<Integer, Object[]> assignments = errorMap.get(container);

            for (Integer key : assignments.keySet()) {
                View v = container.findViewById(key);

                if (CustomErrorHandler.class.isAssignableFrom(v.getClass())) {
                    CustomErrorHandler customErrorHandler = (CustomErrorHandler) v;
                    // Object[] params =assignments.get(key);
                    customErrorHandler.reset(container);
                }

                if (TextView.class.isAssignableFrom(v.getClass())) {
                    TextView t = (TextView) v;
                    t.setError(null);
                }
            }

            errorMap.put(container, new HashMap<Integer, Object[]>());
        }
    }

    /**
     * register an error handler. an error handler normally is bound to another field in
     * the view. The error is raised by attaching an error to the field (view) component
     * bound to the CustomErrorHandler's idToDisplayFor-property
     *
     * @param CustomErrorHandler
     */
    public void registerCustomErrorHandler(CustomErrorHandler CustomErrorHandler) {
        if (CustomErrorHandler.getIdToDisplayFor() != -1) {
            registeredHandlers.put(CustomErrorHandler.getIdToDisplayFor(), CustomErrorHandler);
        }
    }

    /**
     * unregister all error handlers for the passed field. If you use the
     * net.mantucon.baracus.context.ManagedFragment component and set the View
     *
     * @param container
     */
    public void unregisterCustomErrorHandlersForView(View container) {
        errorMap.remove(container);
    }

    /**
     * register an error handling for the use of the general android error
     * handling stuff
     *
     * @param handler - the handler
     * @see TextEditErrorHandler
     */
    public void registerStandardErrorHandler(StandardErrorHandler handler) {
        standardHandlers.add(handler);
    }


    @Override
    public void onDestroy() {
        registeredHandlers.clear();
        standardHandlers.clear();
        errorMap.clear();
    }

    @Override
    public void postConstruct() {
        registerStandardErrorHandler(new TextEditErrorHandler());
    }

    /**
     * @return the map with all custom error handlers
     */
    public Map<Integer, CustomErrorHandler> getRegisteredHandlers() {
        return registeredHandlers;
    }

    /**
     * @return all standard handlers
     */
    public Set<StandardErrorHandler> getStandardHandlers() {
        return standardHandlers;
    }
}




Java Source Code List

net.mantucon.baracus.annotations.Bean.java
net.mantucon.baracus.context.AnnotationScanner.java
net.mantucon.baracus.context.BaracusApplicationContext.java
net.mantucon.baracus.context.BeanContainer.java
net.mantucon.baracus.context.Exceptions.java
net.mantucon.baracus.context.ManagedActivity.java
net.mantucon.baracus.context.ManagedFragment.java
net.mantucon.baracus.dao.BaracusOpenHelper.java
net.mantucon.baracus.dao.BaseDao.java
net.mantucon.baracus.dao.ConfigurationDao.java
net.mantucon.baracus.errorhandling.CustomErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandler.java
net.mantucon.baracus.errorhandling.ErrorHandlingFactory.java
net.mantucon.baracus.errorhandling.ErrorSeverity.java
net.mantucon.baracus.errorhandling.StandardErrorHandler.java
net.mantucon.baracus.errorhandling.TextEditErrorHandler.java
net.mantucon.baracus.lifecycle.ApplicationContextInitializer.java
net.mantucon.baracus.lifecycle.Destroyable.java
net.mantucon.baracus.lifecycle.Initializeable.java
net.mantucon.baracus.migr8.MigrationStep.java
net.mantucon.baracus.migr8.ModelVersion000.java
net.mantucon.baracus.model.ConfigurationParameter.java
net.mantucon.baracus.orm.AbstractModelBase.java
net.mantucon.baracus.orm.FieldList.java
net.mantucon.baracus.orm.Field.java
net.mantucon.baracus.orm.Identifiable.java
net.mantucon.baracus.orm.LazyCollection.java
net.mantucon.baracus.orm.LazyMorphicCollection.java
net.mantucon.baracus.orm.LazyReference.java
net.mantucon.baracus.orm.LegacyModelBase.java
net.mantucon.baracus.orm.ModelBase.java
net.mantucon.baracus.orm.NullReference.java
net.mantucon.baracus.orm.ObjectReference.java
net.mantucon.baracus.orm.OptimisticLockingModelBase.java
net.mantucon.baracus.orm.OptmisticLocking.java
net.mantucon.baracus.orm.ReferenceLoader.java
net.mantucon.baracus.orm.Reference.java
net.mantucon.baracus.orm.Timestamped.java
net.mantucon.baracus.signalling.ConfigurationChangeListener.java
net.mantucon.baracus.signalling.DataChangeAwareComponent.java
net.mantucon.baracus.signalling.DataSetChangeAwareComponent.java
net.mantucon.baracus.signalling.DeleteAwareComponent.java
net.mantucon.baracus.signalling.GenericEventAwareComponent.java
net.mantucon.baracus.signalling.GenericEvent.java
net.mantucon.baracus.ui.ConstrainedEditText.java
net.mantucon.baracus.ui.ConstrainedSpinner.java
net.mantucon.baracus.ui.ErrorView.java
net.mantucon.baracus.ui.Popup.java
net.mantucon.baracus.util.DBBackup.java
net.mantucon.baracus.util.DataUtil.java
net.mantucon.baracus.util.DateUtil.java
net.mantucon.baracus.util.DayDate.java
net.mantucon.baracus.util.Logger.java
net.mantucon.baracus.util.StringUtil.java
net.mantucon.baracus.validation.AbstractValidator.java
net.mantucon.baracus.validation.ConstrainedView.java
net.mantucon.baracus.validation.ValidatableView.java
net.mantucon.baracus.validation.ValidationFactory.java
net.mantucon.baracus.validation.Validator.java
net.mantucon.baracus.validation.builtins.DateFromNow.java
net.mantucon.baracus.validation.builtins.NumberMustBeGreaterThanZero.java
net.mantucon.baracus.validation.builtins.StringIsNumericDouble.java
net.mantucon.baracus.validation.builtins.StringIsNumericInteger.java
net.mantucon.baracus.validation.builtins.StringNotEmpty.java