litesoft
@ 948
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / client / foundation / widget / table / cell / AbstractDisableableBooleanEditableCell.java
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 |
package com.temp.client.foundation.widget.table.cell; import com.google.gwt.cell.client.*; import com.google.gwt.dom.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.safehtml.shared.*; public abstract class AbstractDisableableBooleanEditableCell extends AbstractEditableCell<Boolean, Boolean> { public static class CellHtmlProvider { private final SafeHtml enabledSelected; private final SafeHtml enabledNotSelected; private final SafeHtml disabledSelected; private final SafeHtml disabledNotSelected; public CellHtmlProvider( String enabledSelected, String enabledNotSelected, String disabledSelected, String disabledNotSelected ) { this.enabledSelected = SafeHtmlUtils.fromTrustedString( enabledSelected ); this.enabledNotSelected = SafeHtmlUtils.fromTrustedString( enabledNotSelected ); this.disabledSelected = SafeHtmlUtils.fromTrustedString( disabledSelected ); this.disabledNotSelected = SafeHtmlUtils.fromTrustedString( disabledNotSelected ); } public SafeHtml htmlEnabledSelected() { return enabledSelected; } public SafeHtml htmlEnabledNotSelected() { return enabledNotSelected; } public SafeHtml htmlDisabledSelected() { return disabledSelected; } public SafeHtml htmlDisabledNotSelected() { return disabledNotSelected; } } private final boolean dependsOnSelection; private final boolean handlesSelection; private final CellHtmlProvider htmlProvider; /** * Construct a new AbstractDisableableBooleanEditableCell that optionally controls selection. * * @param dependsOnSelection true if the cell depends on the selection state * @param handlesSelection true if the cell modifies the selection state */ protected AbstractDisableableBooleanEditableCell( boolean dependsOnSelection, boolean handlesSelection, CellHtmlProvider htmlProvider ) { super( "change", "keydown" ); this.htmlProvider = htmlProvider; this.dependsOnSelection = dependsOnSelection; this.handlesSelection = handlesSelection; } /** * Construct a new AbstractDisableableBooleanEditableCell that optionally controls selection. * * @param isSelectBox true if the cell controls the selection state */ protected AbstractDisableableBooleanEditableCell( boolean isSelectBox, CellHtmlProvider htmlProvider ) { this( isSelectBox, isSelectBox, htmlProvider ); } /** * Construct a new AbstractDisableableBooleanEditableCell. */ protected AbstractDisableableBooleanEditableCell( CellHtmlProvider htmlProvider ) { this( false, htmlProvider ); } @Override public boolean dependsOnSelection() { return dependsOnSelection; } @Override public boolean handlesSelection() { return handlesSelection; } @Override public boolean isEditing( Context context, Element parent, Boolean value ) { // A 'Boolean' is never in "edit mode". There is no intermediate state // between Selected and Not Selected. return false; } @Override public void onBrowserEvent( Context context, Element parent, Boolean value, NativeEvent event, ValueUpdater<Boolean> valueUpdater ) { String type = event.getType(); boolean enterPressed = "keydown".equals( type ) && event.getKeyCode() == KeyCodes.KEY_ENTER; if ( "change".equals( type ) || enterPressed ) { InputElement input = parent.getFirstChild().cast(); Boolean isChecked = input.isChecked(); /* * Toggle the value if the enter key was pressed and the cell handles * selection or doesn't depend on selection. If the cell depends on * selection but doesn't handle selection, then ignore the enter key and * let the SelectionEventManager determine which keys will trigger a * change. */ if ( enterPressed && (handlesSelection() || !dependsOnSelection()) ) { isChecked = !isChecked; input.setChecked( isChecked ); } /* * Save the new value. However, if the cell depends on the selection, then * do not save the value because we can get into an inconsistent state. */ if ( value != isChecked && !dependsOnSelection() ) { setViewData( context.getKey(), isChecked ); } else { clearViewData( context.getKey() ); } if ( valueUpdater != null ) { valueUpdater.update( isChecked ); } } } @Override public void render( Context context, Boolean value, SafeHtmlBuilder sb ) { // Get the view data. Object key = context.getKey(); Boolean viewData = getViewData( key ); if ( viewData != null && viewData.equals( value ) ) { clearViewData( key ); viewData = null; } if ( value != null && ((viewData != null) ? viewData : value) ) { sb.append( isEnabled() ? htmlProvider.htmlEnabledSelected() : htmlProvider.htmlDisabledSelected() ); } else { sb.append( isEnabled() ? htmlProvider.htmlEnabledNotSelected() : htmlProvider.htmlDisabledNotSelected() ); } } public abstract boolean isEnabled(); } |