litesoft
@ 809
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.GWT.forms.client.nonpublic; import java.util.*; import org.litesoft.GWT.eventbus.client.*; import org.litesoft.core.simpletypes.*; import org.litesoft.core.typeutils.*; import org.litesoft.ui.def.nonpublic.support.*; public class AttributeAdapterManager { private FormAttributeAdapter[] mAttributeAdapters = FormAttributeAdapter.EMPTY_ARRAY; private Map mAttributeAdaptersByUniqueID = new HashMap(); private FormAttributeAdapter[] getAll() { return mAttributeAdapters; } public void dispose() { mAttributeAdapters = FormAttributeAdapter.EMPTY_ARRAY; mAttributeAdaptersByUniqueID.clear(); } public FormAttributeAdapter get( Integer pUniqueID ) { return (FormAttributeAdapter) mAttributeAdaptersByUniqueID.get( pUniqueID ); } public void add( FormAttributeAdapter pAttributeAdapter ) { mAttributeAdaptersByUniqueID.put( pAttributeAdapter.getUniqueID(), pAttributeAdapter ); mAttributeAdapters = FormAttributeAdapter.Helper.append( mAttributeAdapters, pAttributeAdapter ); } public void apply( FormDataRowKey pFormDataRowKey, AttributeUpdateFormData[] pAttributeUpdatesNonNullOrEmpty ) { for ( int i = 0; i < pAttributeUpdatesNonNullOrEmpty.length; i++ ) { AttributeUpdateFormData update = pAttributeUpdatesNonNullOrEmpty[i]; if ( Objects.areNonArraysEqual( pFormDataRowKey, update.getFormDataRowKey() ) ) { apply( update ); } } } private void apply( AttributeUpdateFormData pUpdate ) { // todo: need to add a FormAttributeAdapter for the Tab Label FormAttributeAdapter adapter = get( pUpdate.getUniqueID() ); if ( adapter == null ) { System.out.println( "update '" + pUpdate + "', but no AttributeAdapter" ); return; } if ( pUpdate.hasValue() ) { adapter.setBaseValue( pUpdate.getValue() ); } if ( pUpdate.getDisable() != null ) { adapter.setDisabled( pUpdate.getDisable().booleanValue() ); } adapter.setErrorText( Strings.noEmpty( pUpdate.getErrorText() ) ); if ( pUpdate instanceof ComboBoxAttributeUpdateFormData ) { SimpleKeyValuePair[] newValidOptions = ((ComboBoxAttributeUpdateFormData) pUpdate).getValidOptions(); if ( Objects.isNullOrEmpty( newValidOptions ) ) { System.out.println( "update '" + pUpdate + "', but no ValidOptions" ); } else { adapter.updateValidOptions( newValidOptions ); } } } public boolean recalcAnyAttributes( FormAttributeAdapter.BooleanValueAccessor pValueAccessor ) { FormAttributeAdapter[] adapters = getAll(); for ( int i = 0; i < adapters.length; i++ ) { if ( pValueAccessor.isBooleanValue( adapters[i] ) ) { return true; } } return false; } public void publishAnyPendingAttributeChanges() { FormAttributeAdapter[] adapters = getAll(); for ( int i = 0; i < adapters.length; i++ ) { adapters[i].publishAnyPendingChanges(); } } public boolean setInputDisabled( String pAttributeName, boolean pDisabled ) { boolean rv = false; if ( pAttributeName != null ) { FormAttributeAdapter[] all = getAll(); for ( int i = 0; i < all.length; i++ ) { FormAttributeAdapter adapter = all[i]; if ( pAttributeName.equals( adapter.getAttributeReference() ) ) { rv = true; adapter.setDisabled( pDisabled ); } } } return rv; } public boolean setFocus() { FormAttributeAdapter[] all = getAll(); for ( int i = 0; i < all.length; i++ ) { FormAttributeAdapter adapter = all[i]; if ( adapter.isVisible() && adapter.isEnabled() ) { if ( adapter.setFocus() ) { return true; } } } return false; } public static final String USER_STATE_PREFIX = "FORM-"; public static final String ENTRIES = "Entries"; public void captureUserState( String pRootType, ParamMap pParamMap ) { if ( pParamMap != null ) { pParamMap.add( USER_STATE_PREFIX + pRootType, captureUserState() ); } } public void restoreUserState( String pRootType, ParamMap pParamMap ) { if ( pParamMap != null ) { ParamMap map = pParamMap.getParamMap( USER_STATE_PREFIX + pRootType ); if ( map != null ) { restoreUserState( map ); } } } private String createEntrys() { StringBuilder sb = new StringBuilder(); for ( int i = 0; i < mAttributeAdapters.length; i++ ) { FormAttributeAdapter adapter = mAttributeAdapters[i]; sb.append( adapter.getAttributeReference() ).append( '|' ); } return sb.toString(); } private ParamMap captureUserState() { ParamMap map = new ParamMap(); map.add( ENTRIES, createEntrys() ); for ( int i = 0; i < mAttributeAdapters.length; i++ ) { FormAttributeAdapter adapter = mAttributeAdapters[i]; if ( adapter.isChangedState() ) { String value = adapter.getUserValue(); map.add( "" + i, value ); } } return map; } private void restoreUserState( ParamMap pParamMap ) { String expectedEntrys = createEntrys(); String actualEntrys = pParamMap.getString( ENTRIES ); if ( !expectedEntrys.equals( actualEntrys ) ) { System.err.println( "AttributeAdapterManager.restoreUserState entries\n" + // " Expected: " + expectedEntrys + "\n" + // " Actual: " + actualEntrys ); return; } for ( int i = 0; i < mAttributeAdapters.length; i++ ) { FormAttributeAdapter adapter = mAttributeAdapters[i]; String value = pParamMap.getString( "" + i ); if ( value != null ) { adapter.setUserValue( value ); } } } } |