001: /*
002: * Copyright (c) 2003-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.validation.extras;
032:
033: import java.beans.PropertyChangeEvent;
034: import java.beans.PropertyChangeListener;
035:
036: import com.jgoodies.validation.ValidationResult;
037: import com.jgoodies.validation.ValidationResultModel;
038: import com.jgoodies.validation.util.DefaultValidationResultModel;
039:
040: /**
041: * An implementation of {@link ValidationResultModel} that wraps another
042: * ValidationResultModel to limit the number of reported
043: * {@link com.jgoodies.validation.ValidationMessage}s.<p>
044: *
045: * <strong>Note:</strong> This class is not yet part of the binary Validation
046: * library; it comes with the Validation distributions as an extra.
047: * <strong>The API is work in progress and may change without notice;
048: * this class may even be completely removed from future distributions.</strong>
049: * If you want to use this class, you may consider copying it into
050: * your code base.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.14 $
054: *
055: * @see com.jgoodies.validation.ValidationResultModel
056: */
057: public final class LimitedValidationResultModel extends
058: DefaultValidationResultModel {
059:
060: /**
061: * Holds the upper bound for the number of messages in
062: * ValidationResults this model reports.
063: */
064: private final int limit;
065:
066: // Instance Creation ******************************************************
067:
068: /**
069: * Constructs an <code>LimitedValidationResultModel</code>
070: * on the given ValidationResultModel.
071: *
072: * @param model the underlying model that provides all validation messages
073: * @param limit the upper bound for the number of messages this model returns
074: */
075: public LimitedValidationResultModel(ValidationResultModel model,
076: int limit) {
077: if (model == null)
078: throw new NullPointerException(
079: "The ValidationResultModel must not be null.");
080: if (limit <= 0)
081: throw new IllegalArgumentException(
082: "The limit must be positive.");
083:
084: this .limit = limit;
085: model.addPropertyChangeListener(
086: ValidationResultModel.PROPERTYNAME_RESULT,
087: new ValidationResultChangeHandler());
088: }
089:
090: // Implementing the Limitation ********************************************
091:
092: private ValidationResult limitedResult(ValidationResult result) {
093: return result.isEmpty() ? ValidationResult.EMPTY : result
094: .subResult(0, Math.min(limit, result.size()));
095: }
096:
097: // Helper Class ***********************************************************
098:
099: /**
100: * Updates the limited validation result if the underlying model changes.
101: */
102: private final class ValidationResultChangeHandler implements
103: PropertyChangeListener {
104:
105: /**
106: * The underlying validation result model has changed.
107: * Updates this model's validation result to a limited version
108: * of the new full validation result.
109: *
110: * @param evt the property change event to handle
111: */
112: public void propertyChange(PropertyChangeEvent evt) {
113: ValidationResult result = (ValidationResult) evt
114: .getNewValue();
115: setResult(limitedResult(result));
116: }
117:
118: }
119:
120: }
|