Coverage Report - org.jtheque.core.utils.ui.ValidationUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidationUtils
0 %
0/25
0 %
0/18
2.25
 
 1  
 package org.jtheque.core.utils.ui;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.Managers;
 20  
 import org.jtheque.core.managers.error.InternationalizedError;
 21  
 import org.jtheque.core.managers.error.JThequeError;
 22  
 import org.jtheque.core.managers.language.ILanguageManager;
 23  
 import org.jtheque.utils.StringUtils;
 24  
 
 25  
 import javax.swing.ComboBoxModel;
 26  
 import javax.swing.JList;
 27  
 import java.util.Collection;
 28  
 
 29  
 /**
 30  
  * Validation utils.
 31  
  *
 32  
  * @author Baptiste Wicht
 33  
  */
 34  
 public final class ValidationUtils {
 35  
     /**
 36  
      * Create a new ValidationUtils.
 37  
      */
 38  
     private ValidationUtils() {
 39  0
         super();
 40  0
     }
 41  
 
 42  
     /**
 43  
      * Reject the field if empty.
 44  
      *
 45  
      * @param field  The field to test.
 46  
      * @param name   The name of the field.
 47  
      * @param errors The errors list.
 48  
      */
 49  
     public static void rejectIfEmpty(CharSequence field, String name, Collection<JThequeError> errors) {
 50  0
         if (StringUtils.isEmpty(field)) {
 51  0
             errors.add(new InternationalizedError("error.validation.field.empty", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 52  
         }
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Reject the field if longer than a max.
 57  
      *
 58  
      * @param field  The field to test.
 59  
      * @param name   The name of the field.
 60  
      * @param max    The max length of the field.
 61  
      * @param errors The errors list.
 62  
      */
 63  
     public static void rejectIfLongerThan(CharSequence field, String name, int max, Collection<JThequeError> errors) {
 64  0
         if (!StringUtils.isEmpty(field) && field.length() > max) {
 65  0
             errors.add(new InternationalizedError("error.validation.field.lenght", Managers.getManager(ILanguageManager.class).getMessage(name), max));
 66  
         }
 67  0
     }
 68  
 
 69  
     /**
 70  
      * Reject the list if nothing is selected.
 71  
      *
 72  
      * @param list   The list to test.
 73  
      * @param name   The name of the field.
 74  
      * @param errors The errors list.
 75  
      */
 76  
     public static void rejectIfNothingSelected(JList list, String name, Collection<JThequeError> errors) {
 77  0
         if (list.getSelectedIndex() <= -1) {
 78  0
             errors.add(new InternationalizedError("error.validation.nothing.selected", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 79  
         }
 80  0
     }
 81  
 
 82  
     /**
 83  
      * Reject the model if nothing is selected.
 84  
      *
 85  
      * @param model  The model to test.
 86  
      * @param name   The name of the field.
 87  
      * @param errors The errors list.
 88  
      */
 89  
     public static void rejectIfNothingSelected(ComboBoxModel model, String name, Collection<JThequeError> errors) {
 90  0
         if (model.getSelectedItem() == null) {
 91  0
             errors.add(new InternationalizedError("error.validation.nothing.selected", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 92  
         }
 93  0
     }
 94  
 
 95  
     /**
 96  
      * Reject the field if not numerical.
 97  
      *
 98  
      * @param field  The field to test.
 99  
      * @param name   The name of the field.
 100  
      * @param errors The errors list.
 101  
      */
 102  
     public static void rejectIfNotNumerical(String field, String name, Collection<JThequeError> errors) {
 103  
         try {
 104  0
             Integer.parseInt(field);
 105  0
         } catch (NumberFormatException e) {
 106  0
             errors.add(new InternationalizedError("error.validation.field.numerical", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 107  0
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Reject the list if empty.
 112  
      *
 113  
      * @param list   The list to test.
 114  
      * @param name   The name of the field.
 115  
      * @param errors The errors list.
 116  
      */
 117  
     public static void rejectIfEmpty(Collection<?> list, String name, Collection<JThequeError> errors) {
 118  0
         if (list != null && list.isEmpty()) {
 119  0
             errors.add(new InternationalizedError("error.validation.field.empty", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 120  
         }
 121  0
     }
 122  
 
 123  
     /**
 124  
      * Reject the list if there is empty.
 125  
      *
 126  
      * @param list   The list to test.
 127  
      * @param name   The name of the list.
 128  
      * @param errors The errors to fill.
 129  
      */
 130  
     public static void rejectIfEmpty(JList list, String name, Collection<JThequeError> errors) {
 131  0
         if (list != null && list.getModel().getSize() < 1) {
 132  0
             errors.add(new InternationalizedError("error.validation.field.empty", new Object[]{Managers.getManager(ILanguageManager.class).getMessage(name)}));
 133  
         }
 134  0
     }
 135  
 }