1 /* 2 * LabeledTextBox.java 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 package org.rstudio.core.client.widget; 16 17 import com.google.gwt.aria.client.Roles; 18 import com.google.gwt.dom.client.Style.Display; 19 import com.google.gwt.event.dom.client.HasKeyUpHandlers; 20 import com.google.gwt.event.dom.client.KeyUpHandler; 21 import com.google.gwt.event.shared.HandlerRegistration; 22 import com.google.gwt.uibinder.client.UiConstructor; 23 import com.google.gwt.user.client.ui.Composite; 24 import com.google.gwt.user.client.ui.FlowPanel; 25 import com.google.gwt.user.client.ui.HasText; 26 import com.google.gwt.user.client.ui.TextBox; 27 import org.rstudio.core.client.dom.DomUtils; 28 29 /** 30 * A TextBox with an associated label. 31 */ 32 public class LabeledTextBox extends Composite 33 implements HasText, HasKeyUpHandlers 34 { LabeledTextBox(String textBoxId)35 public @UiConstructor LabeledTextBox(String textBoxId) 36 { 37 FlowPanel flowPanel = new FlowPanel(); 38 label_ = new FormLabel(); 39 textBox_ = new TextBox(); 40 textBox_.getElement().setId(textBoxId); 41 label_.setFor(textBox_); 42 setLabelInline(false); 43 flowPanel.add(label_); 44 flowPanel.add(textBox_); 45 46 initWidget(flowPanel); 47 } 48 setLabelText(String label)49 public void setLabelText(String label) 50 { 51 label_.setText(label); 52 } 53 54 @Override setText(String text)55 public void setText(String text) 56 { 57 textBox_.setText(text); 58 } 59 60 @Override getText()61 public String getText() 62 { 63 return textBox_.getText(); 64 } 65 setFocus(boolean focused)66 public void setFocus(boolean focused) 67 { 68 textBox_.setFocus(focused); 69 } 70 selectAll()71 public void selectAll() 72 { 73 textBox_.selectAll(); 74 } 75 setTextRequired(boolean required)76 public void setTextRequired(boolean required) 77 { 78 Roles.getTextboxRole().setAriaRequiredProperty(textBox_.getElement(), required); 79 } 80 disableAutoBehavior()81 public void disableAutoBehavior() 82 { 83 DomUtils.disableAutoBehavior(textBox_); 84 } 85 setEnableSpellcheck(boolean enable)86 public void setEnableSpellcheck(boolean enable) 87 { 88 textBox_.getElement().setAttribute("spellcheck", enable ? "true" : "false"); 89 } 90 91 /** 92 * @return underlying TextBox control 93 */ getTextBox()94 public TextBox getTextBox() 95 { 96 return textBox_; 97 } 98 99 /** 100 * @return underlying FormLabel control 101 */ getLabel()102 public FormLabel getLabel() 103 { 104 return label_; 105 } 106 107 /** 108 * @param inline true to have label inline with TextBox 109 */ setLabelInline(boolean inline)110 public void setLabelInline(boolean inline) 111 { 112 // by default a label element is inline 113 if (!inline) 114 label_.getElement().getStyle().setDisplay(Display.BLOCK); 115 else 116 label_.getElement().getStyle().clearDisplay(); 117 } 118 119 @Override addKeyUpHandler(KeyUpHandler handler)120 public HandlerRegistration addKeyUpHandler(KeyUpHandler handler) 121 { 122 return textBox_.addKeyUpHandler(handler); 123 } 124 setEnabled(boolean enabled)125 public void setEnabled(boolean enabled) 126 { 127 textBox_.setEnabled(enabled); 128 } 129 setLabelStyleName(String style)130 public void setLabelStyleName(String style) 131 { 132 label_.setStyleName(style); 133 } 134 setTextBoxStyleName(String style)135 public void setTextBoxStyleName(String style) 136 { 137 textBox_.setStyleName(style); 138 } 139 140 FormLabel label_; 141 TextBox textBox_; 142 } 143