litesoft
@ 947
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.ui.def.nonpublic.support; import java.util.*; import org.litesoft.ui.def.*; public abstract class AbstractUiChildrenHelper implements UiChildrenHelper { public static final UiDef[] NO_CHILDREN = new UiDef[0]; private UiDef[] mChildren = NO_CHILDREN; protected final UiContainerMarker mOwner; private int mMinChildren; private int mMaxChildren; // 0 - unlimited protected AbstractUiChildrenHelper( UiContainerMarker pOwner, int pMinChildren, int pMaxChildren ) { mOwner = pOwner; mMinChildren = pMinChildren; mMaxChildren = pMaxChildren; } protected AbstractUiChildrenHelper( UiContainerMarker pOwner, int pMinChildren ) { this( pOwner, pMinChildren, 0 ); } protected boolean anyChildren() { return (mChildren.length != 0); } public UiDef getFirstChild() { return anyChildren() ? mChildren[0] : null; } public UiDef[] getChildren() { return mChildren; } public void addChild( UiDef pChild ) { if ( pChild != null ) { validateChild( pChild ); extendChildren( pChild, mChildren.length ); } } public void insertChildBefore( UiDef pChild, int pAt ) { if ( pChild != null ) { validateChild( pChild ); extendChildren( pChild, ((0 <= pAt) && (pAt < mChildren.length)) ? pAt : mChildren.length ); } } public boolean removeChild( UiDef pChild ) { for ( int i = 0; i < mChildren.length; i++ ) { if ( pChild == mChildren[i] ) { reduceChildren( i ); return true; } } return false; } public UiDef removeChildAt( int pAt ) { if ( (0 <= pAt) && (pAt < mChildren.length) ) { UiDef rv = mChildren[pAt]; reduceChildren( pAt ); return rv; } return null; } public UiDef convertToActionOrInput( String pActionOrInput ) { return null; } public int getMinChildren() { return mMinChildren; } public int getMaxChildren() { return mMaxChildren; } public void customValidateChildren() { customValidateMinimumChildren( mMinChildren ); } protected void customValidateMinimumChildren( int pMinChildren ) { if ( mChildren.length < pMinChildren ) { throw new IllegalArgumentException( mOwner + " MUST have at least " + nChildren( pMinChildren ) + " for a layout" ); } } private void validateChild( UiDef pChild ) { if ( !acceptableChild( pChild ) ) { throw new IllegalArgumentException( "Unacceptable Child (" + pChild + ") for " + mOwner ); } } protected void prepareToRender( UiDef pComponent ) { if ( pComponent != null ) { pComponent.prepareToRender(); } } protected UiDef[] deNullSingleChildAndReturnAsArray( UiDef pUiDef ) { return (pUiDef == null) ? NO_CHILDREN : new UiDef[]{pUiDef}; } private String nChildren( int pNth ) { return "" + pNth + ((pNth == 1) ? " Child" : " Children"); } protected UiDef[] getChildren( List pCurrentChildren ) { UiDef[] defs = new UiDef[pCurrentChildren.size()]; for ( int i = 0; i < pCurrentChildren.size(); i++ ) { defs[i] = (UiDef) pCurrentChildren.get( i ); } return defs; } protected void childrenChanged() { } // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv Array Management vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv private void extendChildren( UiDef pChild, int pAt ) { int newLength = mChildren.length + 1; if ( (mMaxChildren != 0) && (newLength > mMaxChildren) ) { throw new IllegalArgumentException( mOwner + " will only accept " + nChildren( mMaxChildren ) + ". Attempting to add:" + pChild ); } UiDef[] zChildren = new UiDef[newLength]; int zCurIndex = 0; for ( int zNewIndex = 0; zNewIndex < zChildren.length; zNewIndex++ ) { if ( zNewIndex == pAt ) { zChildren[zNewIndex] = pChild; } else { zChildren[zNewIndex] = mChildren[zCurIndex++]; } } mChildren = zChildren; childrenChanged(); } private void reduceChildren( int pAt ) { UiDef[] zChildren = new UiDef[mChildren.length]; int zNewIndex = 0; for ( int zCurIndex = 0; zCurIndex < mChildren.length; zCurIndex++ ) { if ( zCurIndex != pAt ) { zChildren[zNewIndex++] = mChildren[zCurIndex]; } } mChildren = zChildren; childrenChanged(); } } |