View Javadoc

1   package com.melloware.jukes.gui.view.validation;
2   
3   import com.jgoodies.validation.ValidationResult;
4   import com.jgoodies.validation.Validator;
5   import com.jgoodies.validation.util.PropertyValidationSupport;
6   import com.jgoodies.validation.util.ValidationUtils;
7   import com.melloware.jukes.db.orm.Track;
8   import com.melloware.jukes.gui.tool.Resources;
9   
10  /**
11   * Validates Tracks.
12   * <p>
13   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
14   * @author Emil A. Lefkof III <info@melloware.com>
15   * @version 4.0
16   */
17  public final class TrackValidator implements Validator {
18  
19     private static final String TITLE = "Title";
20     private static final String COMMENT = "Comment";
21     private static final String TRACK = "Track Number";
22     private static final String MESSAGE_MANDATORY = Resources.getString("messages.isMandatory");
23  
24     /**
25      * Holds the track to be validated.
26      */
27     private final Track track;
28  
29     /**
30      * Constructs a TrackValidator on the given Track.
31      * @param track the track to be validated
32      */
33     public TrackValidator(Track track) {
34        this.track = track;
35     }
36  
37     /**
38      * Validates this Validator's Order and returns the result as an instance of
39      * {@link ValidationResult}.
40      * @return the ValidationResult of the track validation
41      */
42     @Override
43     public ValidationResult validate(Object validationTarget) {
44        final PropertyValidationSupport support = new PropertyValidationSupport(track, "Track");
45  
46        if (ValidationUtils.isBlank(track.getName())) {
47           support.addError(TITLE, MESSAGE_MANDATORY);
48        }
49  
50        if (!ValidationUtils.hasMaximumLength(track.getName(), 100)) {
51           support.addError(TITLE, Resources.getString("messages.Length100"));
52        }
53  
54        if (!ValidationUtils.hasMaximumLength(track.getComment(), 254)) {
55           support.addError(COMMENT, Resources.getString("messages.Length254"));
56        }
57  
58        if (ValidationUtils.isBlank(track.getTrackNumber())) {
59           support.addError(TRACK, MESSAGE_MANDATORY);
60        } else if (!ValidationUtils.hasMinimumLength(track.getTrackNumber(), 2)) { // NOPMD
61           support.addError(TRACK, Resources.getString("messages.Length2"));
62        } else if (!ValidationUtils.isNumeric(track.getTrackNumber())) { // NOPMD
63           support.addError(TRACK, Resources.getString("messages.mustbeanumber"));
64        }
65  
66        return support.getResult();
67     }
68  
69  }