001 /* 002 * Jpkg - Java library and tools for operating system package creation. 003 * 004 * Copyright (c) 2007-2008 Three Rings Design, Inc. 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 1. Redistributions of source code must retain the above copyright 011 * notice, this list of conditions and the following disclaimer. 012 * 2. Redistributions in binary form must reproduce the above copyright 013 * notice, this list of conditions and the following disclaimer in the 014 * documentation and/or other materials provided with the distribution. 015 * 3. Neither the name of the copyright owner nor the names of contributors 016 * may be used to endorse or promote products derived from this software 017 * without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 023 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 024 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 025 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 026 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 027 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 029 * POSSIBILITY OF SUCH DAMAGE. 030 */ 031 package com.threerings.antidote.field; 032 033 import java.util.ArrayList; 034 import java.util.List; 035 036 import com.threerings.antidote.RequiresValidationException; 037 import com.threerings.antidote.ValidStatus; 038 039 import static com.threerings.antidote.MutabilityHelper.requiresValidation; 040 041 /** 042 * An abstract {@link BaseComponent} designed to hold lists of {@link BaseComponent} objects and 043 * tools to validate those fields. 044 * Package private. Use one of the subclasses. 045 * @see ListField 046 * @see ListTask 047 */ 048 abstract class ListComponent<V extends ReferenceField> extends BaseComponent 049 { 050 /** 051 * Returns the name of the child fields of this ListField. 052 */ 053 public abstract String getChildFieldName (); 054 055 /** 056 * Append a field requiring validation to the list. 057 */ 058 protected void appendRequiresValidation (V needsValidation) 059 { 060 _requireValidation.add(new RequiredField<V>(needsValidation, this)); 061 } 062 063 /** 064 * Return the list of validated fields. Must be called after {@link #validateFieldList()} 065 */ 066 protected List<V> getValidatedFieldList () 067 { 068 requireChildFieldValidation(); 069 return _validatedFields; 070 } 071 072 /** 073 * Throws an {@link RequiresValidationException} if the child fields have not been validated. 074 */ 075 protected void requireChildFieldValidation () 076 { 077 requiresValidation(_validatedFields); 078 } 079 080 /** 081 * Adds a violation and returns true if no child fields were defined, false otherwise. 082 */ 083 protected boolean noChildFieldsDefined () 084 { 085 if (_requireValidation.size() == 0) { 086 appendViolation(new AtLeastOneFieldViolation(this)); 087 return true; 088 089 } else { 090 return false; 091 } 092 } 093 094 /** 095 * Validate the list of declared child fields. Must be called before {@link #getValidatedFieldList()}. 096 * Returns a {@link ValidStatus} enum describing the validated fields. 097 */ 098 protected ValidStatus validateFieldList () 099 { 100 final ValidStatus status = validateChildFields(_requireValidation); 101 switch (status) { 102 case ALL_INVALID: 103 case SOME_INVALID: 104 return status; 105 106 case ALL_VALID: 107 break; 108 } 109 110 _validatedFields = new ArrayList<V>(); 111 for (final RequiredField<V> field : _requireValidation) { 112 _validatedFields.add(field.getField()); 113 } 114 return status; 115 } 116 117 /** The list of fields defined in the section that need to be validated. */ 118 private final List<RequiredField<V>> _requireValidation = new ArrayList<RequiredField<V>>(); 119 120 /** The list of fields after they have been validated. */ 121 private List<V> _validatedFields; 122 } 123