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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.GWT.forms.client; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.GWT.client.*; import org.litesoft.GWT.client.widgets.*; import org.litesoft.GWT.forms.client.components.*; import org.litesoft.GWT.forms.client.components.nonpublic.*; import org.litesoft.core.simpletypes.*; import org.litesoft.commonfoundation.typeutils.Objects; import org.litesoft.core.util.*; import org.litesoft.uispecification.*; import com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; import static org.litesoft.uispecification.FormWidgetAspectCtrl.*; import static org.litesoft.uispecification.FormWidgetCtrl.*; public class FormEngine implements ClickHandler { public abstract static class CtrlProxy implements Enableable { private FormWidgetCtrl mFormWidgetCtrl; protected CtrlProxy( FormWidgetCtrl pFormWidgetCtrl ) { mFormWidgetCtrl = pFormWidgetCtrl; } abstract public boolean isHidden(); abstract public void setHidden( boolean pHide ); public boolean isVisible( FormState pFormState ) { return mFormWidgetCtrl.getVisible().satisfiedBy( pFormState ); } public boolean isEnabled( FormState pFormState ) { return mFormWidgetCtrl.getEnable().satisfiedBy( pFormState ); } public boolean isSearchFilter() { return mFormWidgetCtrl.isSearchFilter(); } public boolean isEditRequired() { return mFormWidgetCtrl.isEditRequired(); } } private static final String EDITING_SECTION_TITLE_TEXT = "Edit "; public enum Mode { Search( true, false, "Search for " ) { @Override public void update( FormState pFormState ) { pFormState.setModeSearch(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return ClearButton.factory().disabled().add( pClickHandler ).create(); } @Override public boolean isEditRequired( CtrlProxy pCtrlProxy ) { return false; } }, NewFromSearch( true, false, "Create " ) { @Override public void update( FormState pFormState ) { pFormState.setModeEditNew(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return SearchButton.factory().add( pClickHandler ).create(); } }, EditFromSearch( true, true, EDITING_SECTION_TITLE_TEXT ) { @Override public void update( FormState pFormState ) { pFormState.setModeEditExisting(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return SearchButton.factory().add( pClickHandler ).create(); } }, EditRegular( false, true, EDITING_SECTION_TITLE_TEXT ) { @Override public void update( FormState pFormState ) { pFormState.setModeEditExisting(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return null; } }, ViewFromSearch( false, true, "Confirm " ) { @Override public void update( FormState pFormState ) { pFormState.setModeView(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return SearchButton.factory().add( pClickHandler ).create(); } @Override public boolean isEditRequired( CtrlProxy pCtrlProxy ) { return false; } }, ViewRegular( false, true, "" ) { @Override public void update( FormState pFormState ) { pFormState.setModeView(); } @Override public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ) { return null; } @Override public boolean isEditRequired( CtrlProxy pCtrlProxy ) { return false; } }; private boolean mMonitorSearchFields, mClearFields; private String mSectionTitleText; Mode( boolean pMonitorSearchFields, boolean pClearFields, String pSectionTitleText ) { mMonitorSearchFields = pMonitorSearchFields; mClearFields = pClearFields; mSectionTitleText = pSectionTitleText; } abstract public void update( FormState pFormState ); public boolean isEditing() { return EDITING_SECTION_TITLE_TEXT.equals( mSectionTitleText ); } public boolean isVisible( CtrlProxy pCtrlProxy, FormState pFormState ) { return pCtrlProxy.isVisible( pFormState ); } public boolean isEnabled( CtrlProxy pCtrlProxy, FormState pFormState ) { return pCtrlProxy.isEnabled( pFormState ); } public boolean isSearchFilter( CtrlProxy pCtrlProxy ) { return mMonitorSearchFields && pCtrlProxy.isSearchFilter(); } public boolean isEditRequired( CtrlProxy pCtrlProxy ) { return pCtrlProxy.isEditRequired(); } public final boolean shouldMonitorSearchFields() { return mMonitorSearchFields; } public final boolean shouldClearFields() { return mClearFields; } public final String getSectionTitleText() { return mSectionTitleText; } abstract public ButtonBase createSectionTitleRightButton( ClickHandler pClickHandler ); } private FormState mFormState = new FormState(); private List<ButtonBase> mDefaultButtons = new ArrayList<ButtonBase>(); private LeftCenterRightHorizontalPanel mSectionTitlePanel = null; private OurLabel mSectionTitleLabel = null; private ButtonBase mSectionTitleButton = null; private String mSectionTitlePrefix, mSectionTitleSuffix; private Map<Mode, String> mSectionTitleCustomText = null; protected WidgetCtrlMap mWidgetCtrlMap; protected Mode mMode; private IFormComponent.Listener.Factory mListenerFactory; public FormEngine( WidgetCtrlMap pWidgetCtrlMap, Mode pInitialMode, IFormComponent.Listener.Factory pListenerFactory ) { Objects.assertNotNull( "WidgetCtrlMap", mWidgetCtrlMap = pWidgetCtrlMap ); mMode = (pInitialMode != null) ? pInitialMode : Mode.ViewRegular; mListenerFactory = (pListenerFactory != null) ? pListenerFactory : NoOpFormListenerFactory.INSTANCE; } public boolean isEditRequired( String pName ) { return mWidgetCtrlMap.get( pName ).isEditRequired(); } public boolean isSearchFilter( String pName ) { return mWidgetCtrlMap.get( pName ).isSearchFilter(); } public boolean isUnique( String pName ) { return mWidgetCtrlMap.get( pName ).isUnique(); } public void setSectionTitleCustomText( Mode pMode, String pText ) { Objects.assertNotNull( "Mode", pMode ); if ( pText == null ) { if ( mSectionTitleCustomText != null ) { mSectionTitleCustomText.remove( pMode ); } return; } if ( mSectionTitleCustomText == null ) { mSectionTitleCustomText = new HashMap<Mode, String>(); } mSectionTitleCustomText.put( pMode, pText ); } /** * May only create a SINGLE Section Title */ public Widget createSectionTitle( String pPrefix, String pSuffix ) { mSectionTitlePrefix = Strings.deNull( pPrefix ); mSectionTitleSuffix = Strings.deNull( pSuffix ); mSectionTitleLabel = new OurLabel( getSectionTitleText(), false ); mSectionTitlePanel = new LeftCenterRightHorizontalPanel().style( "SectionTitle" ); mSectionTitlePanel.addCenter( mSectionTitleLabel ); mSectionTitlePanel.addRight( createSectionTitleModeButton() ); return mSectionTitlePanel; } private void LLsectionTitleUpdate() { if ( mSectionTitlePanel != null ) { String zText = getSectionTitleText(); Widget zWidget = createSectionTitleModeButton(); mSectionTitleLabel.setText( zText ); mSectionTitlePanel.getRightContainer().clear(); mSectionTitlePanel.addRight( zWidget ); mSectionTitlePanel.relayoutFromHereDown(); } } private void LLsectionTitleOnChange( boolean pAnyChanges ) { if ( Mode.Search.equals( mMode ) && (mSectionTitleButton != null) ) { mSectionTitleButton.setEnabled( pAnyChanges ); } } @Override public void onClick( ClickEvent event ) { if ( Mode.Search.equals( mMode ) ) // Clear Button { resetForm(); LLsectionTitleOnChange( mFormState.hasChanges() ); } else // Search Button { setMode( Mode.Search ); } } private Widget createSectionTitleModeButton() { mSectionTitleButton = mMode.createSectionTitleRightButton( this ); return (mSectionTitleButton != null) ? mSectionTitleButton : new TransparentImage(); } private String getSectionTitleText() { return mSectionTitlePrefix + getSectionTitleModeText() + mSectionTitleSuffix; } private String getSectionTitleModeText() { if ( mSectionTitleCustomText != null ) { String rv = mSectionTitleCustomText.get( mMode ); if ( rv != null ) { return rv; } } return mMode.getSectionTitleText(); } /** * May only create a SINGLE Section Title */ public Widget createSectionTitle( String pSuffix ) { return createSectionTitle( "", pSuffix ); } public final void resetForm() { LLresetForm( true ); } protected void LLresetForm( boolean pResetAllValues ) { LLresetComponents( pResetAllValues ); setFocus(); } protected void LLresetComponents( boolean pResetAllValues ) { Collection<ProxyExtendedListener> zListeners = mListeners.values(); for ( ProxyExtendedListener zListener : zListeners ) { if ( pResetAllValues || !mMode.isSearchFilter( zListener ) ) { zListener.reset( Mode.Search == mMode ); // Search toOrig, everyone else toLastSetValue } else { zListener.makeNonOrigChanged(); } } mFormState.setErrorChangeState( scanForErrorChangeState( zListeners ) ); LLupdateControls(); } public void setFocus() { Scheduler.get().scheduleDeferred( mFocusCommand ); } private Command mFocusCommand = new Command() { @Override public void execute() { for ( IFormComponent zComponent : mComponents ) { if ( !UtilsGwt.isHidden( (Widget) zComponent ) ) // todo: this does NOT check the Hierarchy for Visibility!!! { if ( zComponent.isEnabled() ) { zComponent.setFocus(); return; } } } for ( ButtonBase zButton : mDefaultButtons ) { if ( zButton.isEnabled() ) { zButton.setFocus( true ); return; } } if ( mSectionTitleButton != null ) { mSectionTitleButton.setFocus( true ); } } }; protected final void updateHiders( String pComponentName ) { Hider zHider = mNamedHiders.get( pComponentName ); if ( zHider != null ) { zHider.update(); } } protected Map<String, Hider> mNamedHiders = new HashMap<String, Hider>(); private void register( String pComponentName, HiderBridgeBridge pBridge ) { Hider zExistingHider = mNamedHiders.get( pComponentName ); if ( zExistingHider == null ) { mNamedHiders.put( pComponentName, pBridge ); } else { mNamedHiders.put( pComponentName, new HiderPair( pBridge, zExistingHider ) ); } } protected List<CtrlProxy> mCtrlProxys = new ArrayList<CtrlProxy>(); protected Map<String, CtrlProxy> mNamedCtrlProxys = new HashMap<String, CtrlProxy>(); private void register( String pName, CtrlProxy pCtrlProxy ) { mCtrlProxys.add( pCtrlProxy ); if ( pName != null ) { mNamedCtrlProxys.put( pName, pCtrlProxy ); } } protected List<IFormComponent> mComponents = new ArrayList<IFormComponent>(); protected Map<String, IFormComponent> mNamedComponents = new HashMap<String, IFormComponent>(); protected Map<IFormComponent, String> mComponentNames = new HashMap<IFormComponent, String>(); protected Map<IFormComponent, ProxyExtendedListener> mListeners = new HashMap<IFormComponent, ProxyExtendedListener>(); private void register( String pName, FormWidgetCtrl pFormWidgetCtrl, IFormComponent pComponent, IFormComponent.Listener pListener ) { ProxyExtendedListener zExt = new ProxyExtendedListener( pName, pListener, pFormWidgetCtrl ); register( pName, zExt ); mComponents.add( pComponent ); mListeners.put( pComponent, zExt ); pComponent.addFormComponentListener( zExt ); if ( pName != null ) { addNamedComponent( pName, pComponent ); } } protected void addNamedComponent( String pName, IFormComponent pComponent ) { mNamedComponents.put( pName, pComponent ); mComponentNames.put( pComponent, pName ); } public Set<String> getNamedComponentNames() { return new HashSet<String>( mNamedComponents.keySet() ); } public String getComponentName( IFormComponent pComponent ) { return mComponentNames.get( pComponent ); } public IFormComponent getNamedComponent( String pName ) { return mNamedComponents.get( pName ); } public IFormComponent getRequiredNamedComponent( String pName ) { IFormComponent zComponent = getNamedComponent( pName ); if ( zComponent == null ) { throw new NoSuchElementException( "No Form Component w/ name: " + pName ); } return zComponent; } public Object getNamedComponentValue( String pName ) throws NoSuchElementException { return mListeners.get( getRequiredNamedComponent( pName ) ).getCurValue(); } public void setNamedComponentValue( String pName, Object pValue ) throws NoSuchElementException { mListeners.get( getRequiredNamedComponent( pName ) ).setCurValue( pValue ); updateControls(); } public void setNamedComponentValueAsUser( String pName, Object pValue ) throws NoSuchElementException { IFormComponent zComponent = getRequiredNamedComponent( pName ); ProxyExtendedListener zListener = mListeners.get( zComponent ); zListener.setCurValueAsUser( pValue ); updateControls(); } public void setNamedComponentError( String pName, String pError ) throws NoSuchElementException { getRequiredNamedComponent( pName ).setError( pError ); updateControls(); } public void setNamedComponentEnabled( String pName, boolean pEnabled ) throws NoSuchElementException { getRequiredNamedComponent( pName ).setEnabled( pEnabled ); updateControls(); } public void setNamedComponentChanged( String pName, boolean pChanged ) { IFormComponent zComponent = getRequiredNamedComponent( pName ); zComponent.setValueChanged( pChanged ); LLchangeOccurredOn( pName, zComponent, pChanged ); updateControls(); } private boolean updateControls() { ErrorChangeState zOrigErrorChangeState = mFormState.getErrorChangeState(); ErrorChangeState zNewErrorChangeState = mFormState.setErrorChangeState( scanForErrorChangeState( mListeners.values() ) ); if ( zOrigErrorChangeState != zNewErrorChangeState ) { LLupdateControls(); } return (zOrigErrorChangeState.hasChanges() != zNewErrorChangeState.hasChanges()); } private void LLupdateControls() { for ( CtrlProxy zCtrlProxy : mCtrlProxys ) { if ( mMode.isVisible( zCtrlProxy, mFormState ) ) { zCtrlProxy.setHidden( false ); zCtrlProxy.setEnabled( mMode.isEnabled( zCtrlProxy, mFormState ) ); } else { zCtrlProxy.setHidden( true ); zCtrlProxy.setEnabled( false ); } } } private Widget addHideable( String pName, FormWidgetCtrl pFormWidgetCtrl, Widget pWidget ) { register( pName, new WidgetCtrlProxy( pWidget, pFormWidgetCtrl ) ); return pWidget; } public Widget addHideable( String pName, Widget pWidget ) { return addHideable( pName, mWidgetCtrlMap.get( pName ), pWidget ); } public Widget addHideable( FormWidgetCtrl pFormWidgetCtrl, Widget pWidget ) { return addHideable( null, pFormWidgetCtrl, pWidget ); } public Widget addComponentDrivenHideable( Widget pWidget, String pComponentName, HiderBridge pHiderBridge ) { Objects.assertNotNull( "Widget", pWidget ); Objects.assertNotNull( "ComponentName", pComponentName ); Objects.assertNotNull( "HiderBridge", pHiderBridge ); register( pComponentName, new HiderBridgeBridge( pWidget, pHiderBridge, getRequiredNamedComponent( pComponentName ) ) ); return pWidget; } private FocusWidget addControlled( String pName, FormWidgetCtrl pFormWidgetCtrl, FocusWidget pWidget ) { register( pName, new FocusWidgetCtrlProxy( pWidget, pFormWidgetCtrl ) ); return pWidget; } public FocusWidget addControlled( FormWidgetCtrl pFormWidgetCtrl, FocusWidget pWidget ) { return addControlled( null, pFormWidgetCtrl, pWidget ); } public FocusWidget addControlled( String pName, FocusWidget pWidget ) { return addControlled( pName, mWidgetCtrlMap.get( pName ), pWidget ); } private ButtonBase addButton( String pName, FormWidgetCtrl pFormWidgetCtrl, ButtonBase pButton ) { addControlled( pName, pFormWidgetCtrl, pButton ); return pButton; } public ButtonBase addButton( FormWidgetCtrl pFormWidgetCtrl, ButtonBase pButton ) { return addButton( null, pFormWidgetCtrl, pButton ); } public ButtonBase addButton( boolean pDefault, FormWidgetCtrl pFormWidgetCtrl, ButtonBase pButton ) { if ( pDefault ) { mDefaultButtons.add( pButton ); } return addButton( pFormWidgetCtrl, pButton ); } public ButtonBase addButton( String pName, ButtonBase pButton ) { return addButton( pName, mWidgetCtrlMap.get( pName ), pButton ); } public ButtonBase addEditChangeButton( ButtonBase pButton ) { return addButton( Visible( Edit ).enable( Edit_Changes ), pButton ); } public ButtonBase addEditRevertButton( ButtonBase pButton ) { pButton.addClickHandler( new ClickHandler() { @Override public void onClick( ClickEvent event ) { resetForm(); } } ); return addButton( Visible( Edit_Existing ).enable( Edit_Existing_Changes ), pButton ); } public ButtonBase addEditSubmitButton( ButtonBase pButton ) { return addEditSubmitButton( false, pButton ); } public ButtonBase addEditSubmitButton( boolean pDefault, ButtonBase pButton ) { return addButton( pDefault, Visible( Edit ).enable( Edit_NoErrors_Changes ), pButton ); } public ButtonBase addEditSubmitNextButton( boolean pDefault, ViewDef pTarget, UriFragmentIdParams.ScreenParamsFactory pFactory ) { return addEditSubmitButton( pDefault, NextButton.factory().add( pTarget, pFactory ).create() ); } public ButtonBase addExistingUnchangedButton( boolean pDefault, ButtonBase pButton ) { return addButton( pDefault, Visible( Existing ).enable( Existing_NoChanges ), pButton ); } public ButtonBase addExistingUnchangedNextButton( boolean pDefault, ViewDef pTarget ) { return addExistingUnchangedButton( pDefault, NextButton.factory().add( pTarget ).create() ); } public ButtonBase addExistingUnchangedNextButton( boolean pDefault, ViewDef pTarget, UriFragmentIdParams.ScreenParamsFactory pFactory ) { return addExistingUnchangedButton( pDefault, NextButton.factory().add( pTarget, pFactory ).create() ); } private AbstractFormElement add( String pName, FormWidgetCtrl pFormWidgetCtrl, AbstractFormElement pField ) { register( pName, pFormWidgetCtrl, pField, mListenerFactory.createListenerFor( pName, pField ) ); return pField; } public AbstractFormElement add( FormWidgetCtrl pFormWidgetCtrl, AbstractFormElement pField ) { return add( null, pFormWidgetCtrl, pField ); } public AbstractFormElement add( String pName, boolean pRequired, AbstractFormElement pField ) { FormWidgetCtrl zFormWidgetCtrl = mWidgetCtrlMap.get( pName ); if ( pRequired ) { zFormWidgetCtrl = zFormWidgetCtrl.required(); } return add( pName, zFormWidgetCtrl, pField ); } public AbstractFormElement add( String pName, AbstractFormElement pField ) { return add( pName, false, pField ); } public AbstractFormElement add( AbstractFormElement pField ) { return add( null, false, pField ); } public void initialize() { setMode( mMode ); } public Mode getMode() { return mMode; } public void setMode( Mode pMode ) { (mMode = (pMode != null) ? pMode : Mode.ViewRegular).update( mFormState ); LLsectionTitleUpdate(); LLresetForm( mMode.shouldClearFields() ); LLsectionTitleOnChange( mFormState.hasChanges() ); } private void LLchangeOccurredOn( String pName, IFormComponent pComponent, boolean pChanged ) { changeOccurredOn( pName, pComponent, pChanged ); updateHiders( pName ); if ( updateControls() ) { LLsectionTitleOnChange( mFormState.hasChanges() ); } } protected void changeOccurredOn( String pName, IFormComponent pComponent, boolean pChanged ) { } protected void blurOccurredOn( String pName, IFormComponent pComponent ) { } protected void focusOccuredOn( String pName, IFormComponent pComponent ) { } protected void enterPressedOn( String pName, IFormComponent pComponent, KeyboardKeyModifier pModifiers ) { for ( ButtonBase zButton : mDefaultButtons ) { if ( zButton.isEnabled() ) { DomEvent.fireNativeEvent( Document.get().createClickEvent( 0, 0, 0, 0, 0, false, false, false, false ), zButton ); } } } public ErrorChangeState scanForErrorChangeState( Collection<ProxyExtendedListener> pListeners ) { ErrorChangeState zErrorChangeState = ErrorChangeState.NoErrors_NoChanges; for ( ProxyExtendedListener zListener : pListeners ) { if ( ErrorChangeState.Errors_Changes == (zErrorChangeState = zErrorChangeState.mergeToTrueFrom( zListener )) ) { break; } } return zErrorChangeState; } private static class WidgetCtrlProxy extends CtrlProxy { protected Widget mWidget; public WidgetCtrlProxy( Widget pWidget, FormWidgetCtrl pFormWidgetCtrl ) { super( pFormWidgetCtrl ); mWidget = pWidget; } @Override public boolean isHidden() { return (mWidget != null) && UtilsGwt.isHidden( mWidget ); } @Override public void setHidden( boolean pHide ) { if ( mWidget != null ) { UtilsGwt.setHidden( mWidget, pHide ); } } @Override public boolean isEnabled() { return false; } @Override public void setEnabled( boolean pEnabled ) { } @Override public String toString() { return "Widget: " + Objects.justClassNameOf( mWidget ); } } private static class FocusWidgetCtrlProxy extends CtrlProxy { private FocusWidget mWidget; public FocusWidgetCtrlProxy( FocusWidget pWidget, FormWidgetCtrl pFormWidgetCtrl ) { super( pFormWidgetCtrl ); mWidget = pWidget; } @Override public boolean isHidden() { return (mWidget != null) && UtilsGwt.isHidden( mWidget ); } @Override public void setHidden( boolean pHide ) { if ( mWidget != null ) { UtilsGwt.setHidden( mWidget, pHide ); } } @Override public boolean isEnabled() { return (mWidget != null) && mWidget.isEnabled(); } @Override public void setEnabled( boolean pEnabled ) { if ( mWidget != null ) { mWidget.setEnabled( pEnabled ); } } @Override public String toString() { return "FocusWidget: " + Objects.justClassNameOf( mWidget ); } } protected class ProxyExtendedListener extends CtrlProxy implements IFormComponent.Listener, ErrorChangeProvider { private String mName; private IFormComponent.Listener mProxied; private IFormComponent mComponent; private Object mOrigValue, mCurValue, mLastSetValue = null; boolean mLSVvalid = false; private boolean mChanged = false; public ProxyExtendedListener( String pName, IFormComponent.Listener pProxied, FormWidgetCtrl pFormWidgetCtrl ) { super( pFormWidgetCtrl ); mName = pName; mProxied = (pProxied != null) ? pProxied : NoOpFormListener.INSTANCE; } private void clearLSV() { mLSVvalid = false; mLastSetValue = null; } private void setLSV( Object pLastSetValue ) { mLSVvalid = true; mLastSetValue = pLastSetValue; } private Object getBaseValue() { return mLSVvalid ? mLastSetValue : mOrigValue; } public boolean isChanged() { return mChanged; } public boolean isErrored() { return (mComponent != null) && mComponent.hasError(); } public Object getCurValue() { return mCurValue; } public Object getOrigValue() { return mOrigValue; } @Override public void add( IFormComponent pComponent ) { mProxied.add( mComponent = pComponent ); mOrigValue = mCurValue = getComponentCurrentValue(); mChanged = false; } @Override public void remove( IFormComponent pComponent ) { mProxied.remove( pComponent ); if ( pComponent == mComponent ) { mComponent = null; } } public void reset( boolean pToOrig ) { if ( pToOrig ) { clearLSV(); LLreset( mOrigValue ); } else { LLreset( getBaseValue() ); } } private void LLreset( Object pResetToValue ) { if ( mComponent != null ) { mComponent.setCurrentValue( mCurValue = pResetToValue ); adjustComponentState(); updateHiders( mName ); } } public void makeNonOrigChanged() { clearLSV(); if ( mComponent != null ) { adjustComponentState(); } } @Override public void changeOccurred() { if ( mComponent != null ) { mCurValue = getComponentCurrentValue(); adjustComponentState(); mProxied.changeOccurred(); LLchangeOccurredOn( mName, mComponent, mChanged ); } } @Override public void blurOccurred() { if ( mComponent != null ) { mComponent.setFocused( false ); mProxied.blurOccurred(); blurOccurredOn( mName, mComponent ); } } @Override public void focusOccured() { if ( mComponent != null ) { mComponent.setFocused( true ); mProxied.focusOccured(); focusOccuredOn( mName, mComponent ); } } @Override public void enterPressed( KeyboardKeyModifier pModifiers ) { if ( mComponent != null ) { mProxied.enterPressed( pModifiers ); enterPressedOn( mName, mComponent, pModifiers ); } } public void setCurValue( Object pNewValue ) { if ( mComponent != null ) { mComponent.setCurrentValue( pNewValue ); setLSV( mCurValue = getComponentCurrentValue() ); adjustComponentState(); updateHiders( mName ); } } public void setCurValueAsUser( Object pNewValue ) { if ( mComponent != null ) { mComponent.setCurrentValue( pNewValue ); changeOccurred(); } } @Override public boolean isHidden() { return (mComponent != null) && UtilsGwt.isHidden( (Widget) mComponent ); } @Override public void setHidden( boolean pHide ) { if ( mComponent != null ) { UtilsGwt.setHidden( (Widget) mComponent, pHide ); } } @Override public boolean isEnabled() { return (mComponent != null) && mComponent.isEnabled(); } @Override public void setEnabled( boolean pEnabled ) { if ( mComponent != null ) { mComponent.setEnabled( pEnabled ); } } private Object getComponentCurrentValue() { Object zValue = mComponent.getCurrentValue(); if ( zValue instanceof SimpleKeyValuePair ) { zValue = ((SimpleKeyValuePair) zValue).getKey(); } return zValue; } private void adjustComponentState() { mComponent.setValueChanged( mChanged = !Objects.areNonArraysEqual( getBaseValue(), mCurValue ) ); mComponent.setError( errorIfRequired() ); } private String errorIfRequired() { return (mMode.isEditRequired( this ) && isEmpty()) ? "Required Field" : null; } public boolean isEmpty() { if ( mCurValue != null ) { if ( !(mCurValue instanceof String) || (mCurValue.toString().trim().length() != 0) ) { return false; } } return true; } @Override public String toString() { return "Field: " + mName; } } private interface Hider { public void update(); } private static class HiderPair implements Hider { private Hider mHider1, mHider2; private HiderPair( Hider pHider1, Hider pHider2 ) { mHider1 = pHider1; mHider2 = pHider2; } @Override public void update() { mHider1.update(); mHider2.update(); } } private static class HiderBridgeBridge implements Hider { private Widget mWidget; private HiderBridge mHiderBridge; private IFormComponent mComponent; private HiderBridgeBridge( Widget pWidget, HiderBridge pHiderBridge, IFormComponent pComponent ) { mWidget = pWidget; mHiderBridge = pHiderBridge; mComponent = pComponent; update(); } @Override public void update() { Object zCurrentValue = mComponent.getCurrentValue(); boolean zHide = mHiderBridge.shouldBeHidden( zCurrentValue ); UtilsGwt.setHidden( mWidget, zHide ); } } } |
Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/FormEngine.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
917 Diff | GeorgeS | Sun 08 Dec, 2013 20:49:56 +0000 | 1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model! |
811 Diff | GeorgeS | Sat 18 Aug, 2012 13:45:18 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
802 Diff | GeorgeS | Wed 15 Aug, 2012 04:04:47 +0000 | |
212 Diff | GeorgeS | Tue 17 May, 2011 04:55:36 +0000 | |
207 Diff | GeorgeS | Mon 16 May, 2011 05:45:30 +0000 | |
200 Diff | GeorgeS | Fri 13 May, 2011 21:58:18 +0000 | |
154 | GeorgeS | Tue 22 Mar, 2011 20:46:40 +0000 |