View Javadoc
1   package fr.ifremer.dali.ui.swing.util.validator;
2   
3   /*
4    * #%L
5    * Dali :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2016 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
28  
29  import java.util.List;
30  
31  /**
32   * Created by Ludovic on 31/03/2016.
33   */
34  public abstract class AbstractControlExpressionValidator extends FieldValidatorSupport {
35  
36      private static final String NONE_LEVEL = "none";
37      private static final String ALL_LEVEL = "all";
38      private static final String ERROR_ONLY = "error";
39      private static final String WARNING_ONLY = "warning";
40      protected String level = ALL_LEVEL;
41      protected String controlLevel = ALL_LEVEL;
42  
43      /**
44       * <p>Setter for the field <code>level</code>.</p>
45       *
46       * @param level a {@link java.lang.String} object.
47       */
48      public void setLevel(String level) {
49  
50          if (!NONE_LEVEL.equalsIgnoreCase(level)
51                  && !ALL_LEVEL.equalsIgnoreCase(level)
52                  && !ERROR_ONLY.equalsIgnoreCase(level)
53                  && !WARNING_ONLY.equalsIgnoreCase(level)) {
54              throw new IllegalArgumentException("level must be one of " + NONE_LEVEL + "|" + ALL_LEVEL + "|" + ERROR_ONLY + "|" + WARNING_ONLY);
55          }
56  
57          this.level = level;
58      }
59  
60      /**
61       * <p>Setter for the field <code>controlLevel</code>.</p>
62       *
63       * @param controlLevel a {@link java.lang.String} object.
64       */
65      public void setControlLevel(String controlLevel) {
66  
67          if (!NONE_LEVEL.equalsIgnoreCase(controlLevel)
68                  && !ALL_LEVEL.equalsIgnoreCase(controlLevel)
69                  && !ERROR_ONLY.equalsIgnoreCase(controlLevel)
70                  && !WARNING_ONLY.equalsIgnoreCase(controlLevel)) {
71              throw new IllegalArgumentException("control level must be one of " + NONE_LEVEL + "|" + ALL_LEVEL + "|" + ERROR_ONLY + "|" + WARNING_ONLY);
72          }
73  
74          this.controlLevel = controlLevel;
75      }
76  
77      boolean isErrorActive() {
78          return ALL_LEVEL.equalsIgnoreCase(this.level) || ERROR_ONLY.equalsIgnoreCase(this.level);
79      }
80  
81      boolean isWarningActive() {
82          return ALL_LEVEL.equalsIgnoreCase(this.level) || WARNING_ONLY.equalsIgnoreCase(this.level);
83      }
84  
85      boolean isControlErrorActive() {
86          return ALL_LEVEL.equalsIgnoreCase(this.controlLevel) || ERROR_ONLY.equalsIgnoreCase(this.controlLevel);
87      }
88  
89      boolean isControlWarningActive() {
90          return ALL_LEVEL.equalsIgnoreCase(this.controlLevel) || WARNING_ONLY.equalsIgnoreCase(this.controlLevel);
91      }
92  
93      /**
94       * add a message on field if not already
95       * @param message message
96       */
97      void addFieldErrorMessage(String message) {
98          List<String> errors = getValidatorContext().getFieldErrors().get(getFieldName());
99          if (errors == null || !errors.contains(message)) {
100             getValidatorContext().addFieldError(getFieldName(), message);
101         }
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public String getMessage(Object object) {
107         return "should not see me !";
108     }
109 
110 }