001    package org.hackystat.projectbrowser.page.trajectory.validator;
002    
003    import org.apache.wicket.markup.html.form.DropDownChoice;
004    import org.apache.wicket.markup.html.form.Form;
005    import org.apache.wicket.markup.html.form.FormComponent;
006    import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;
007    
008    /**
009     * Provides a telemetry validator. Since we support the only metrics so far, it's the easiest way to
010     * report errors.
011     * 
012     * @author Pavel Senin.
013     */
014    public class TelemetrySelectionValidator extends AbstractFormValidator {
015    
016      /** For serialization. */
017      private static final long serialVersionUID = 1L;
018    
019      /**
020       * Form components to be checked. The first must be the projectMenu (a ListMultipleChoice), the
021       * second must be a date (a DateTextField), and the third (if present) is another DateTextField.
022       * This enables the validator to be used with either forms with a single date (such as DPDs) as
023       * well as forms with a start and end date (such as Telemetry).
024       */
025      private final FormComponent[] components;
026    
027      /**
028       * Takes a Project menu and a single Date field.
029       * 
030       * @param telemetrySelection foo.
031       */
032      public TelemetrySelectionValidator(FormComponent telemetrySelection) {
033        if (telemetrySelection == null) {
034          throw new IllegalArgumentException("projectMenu cannot be null");
035        }
036        if (!(telemetrySelection instanceof DropDownChoice)) {
037          throw new IllegalArgumentException("ProjectDateValidator not given a ListMultipleChoice");
038        }
039        components = new FormComponent[] { telemetrySelection };
040      }
041    
042      /**
043       * Returns the form components.
044       * 
045       * @return The form components.
046       */
047      public FormComponent[] getDependentFormComponents() {
048        return components.clone();
049      }
050    
051      /**
052       * Performs the validation. Note that this validation must handle a projectMenu plus a single
053       * date, or a projectMenu plus two dates (start and end date).
054       * 
055       * @param trajectorySelectionForm foo.
056       */
057      public void validate(Form trajectorySelectionForm) {
058        if (components[0] instanceof DropDownChoice) {
059          DropDownChoice telemetryMenu = (DropDownChoice) components[0];
060          String telemetrySelection = (String) telemetryMenu.getConvertedInput();
061          if (!telemetrySelection.equalsIgnoreCase("Build")) { // NOPMD
062            error(telemetryMenu, "UnsupportedTelemetry");
063          }
064        }
065      }
066    }