litesoft
@ 965
litesoft / trunk / GWT_Sandbox / FormEngine / src / com / temp / foundation / client / widget / table / cell / RadioButtonCell.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.foundation.client.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.*; /** * A {@link Cell} used to render a radio button. The value of the radio button * may be toggled using the ENTER key as well as via mouse click. */ public class RadioButtonCell extends AbstractEditableCell<Boolean, Boolean> { private static final String RADIO_INPUT_TEMPLATE_START = "<input type=\"radio\" tabindex=\"-1\" name=\""; private static final String RADIO_INPUT_TEMPLATE_END = "/>"; private static final String RADIO_INPUT_SELECTED_TOKEN = "checked=\"checked\" "; private static final String RADIO_INPUT_DISABLED_TOKEN = "disabled"; private boolean dependsOnSelection, handlesSelection, enabled; private String name; /** * Constructs a new {@link RadioButtonCell} that does not depend on * selection and does not handle selection. */ public RadioButtonCell( boolean enabled, String name ) { super( "change", "keydown" ); this.enabled = enabled; this.name = name; } /** * Constructs a new {@link RadioButtonCell} that can be configured to depend * and/or handle selection * * @param groupName HTML name attribute of the radiobutton * @param dependsOnSelection true if the cell depends on the selection state * @param handlesSelection true if the cell modifies the selection state */ public RadioButtonCell( boolean dependsOnSelection, boolean handlesSelection, boolean enabled, String name ) { this( enabled, name ); this.dependsOnSelection = dependsOnSelection; this.handlesSelection = handlesSelection; } @Override public boolean dependsOnSelection() { return dependsOnSelection; } @Override public boolean handlesSelection() { return handlesSelection; } @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(); /** * Check the radio button 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 = true; 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. */ Object key = context.getKey(); if ( value != isChecked && !dependsOnSelection() ) { setViewData( key, isChecked ); } else { clearViewData( key ); } if ( valueUpdater != null ) { valueUpdater.update( isChecked ); } } } @Override public boolean isEditing( com.google.gwt.cell.client.Cell.Context context, Element parent, Boolean value ) { // A radio button is never in "edit mode". There is no intermediate state // between checked and unchecked. return false; } @Override public void render( com.google.gwt.cell.client.Cell.Context context, Boolean value, SafeHtmlBuilder sb ) { 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( this.getRadioButtonInput( true ) ); } else { sb.append( this.getRadioButtonInput( false ) ); } } /** * Provides an HTML string representation of a radio button * * @param selected */ private SafeHtml getRadioButtonInput( boolean selected ) { StringBuffer sb = new StringBuffer( RADIO_INPUT_TEMPLATE_START ); String safeName = SafeHtmlUtils.htmlEscape( this.name ); sb.append( (null == this.name) ? "" : safeName ); sb.append( "\" " ); if ( selected ) { sb.append( RADIO_INPUT_SELECTED_TOKEN ); } if ( !this.enabled ) { sb.append( RADIO_INPUT_DISABLED_TOKEN ); } sb.append( RADIO_INPUT_TEMPLATE_END ); return SafeHtmlUtils.fromSafeConstant( sb.toString() ); } } |
Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/table/cell/RadioButtonCell.java
Revision | Author | Commited | Message |
---|---|---|---|
965 | GeorgeS | Fri 01 Aug, 2014 03:20:35 +0000 | ! |