You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ITextField.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.Event;
  8. import com.google.gwt.user.client.ui.ChangeListener;
  9. import com.google.gwt.user.client.ui.FocusListener;
  10. import com.google.gwt.user.client.ui.TextBoxBase;
  11. import com.google.gwt.user.client.ui.Widget;
  12. import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection;
  13. import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener;
  14. import com.itmill.toolkit.terminal.gwt.client.Paintable;
  15. import com.itmill.toolkit.terminal.gwt.client.ITooltip;
  16. import com.itmill.toolkit.terminal.gwt.client.UIDL;
  17. import com.itmill.toolkit.terminal.gwt.client.Util;
  18. /**
  19. * This class represents a basic text input field with one row.
  20. *
  21. * @author IT Mill Ltd.
  22. *
  23. */
  24. public class ITextField extends TextBoxBase implements Paintable, Field,
  25. ChangeListener, FocusListener, ContainerResizedListener {
  26. /**
  27. * The input node CSS classname.
  28. */
  29. public static final String CLASSNAME = "i-textfield";
  30. /**
  31. * This CSS classname is added to the input node on hover.
  32. */
  33. public static final String CLASSNAME_FOCUS = "focus";
  34. protected String id;
  35. protected ApplicationConnection client;
  36. private boolean immediate = false;
  37. private float proportionalHeight = -1;
  38. private float proportionalWidth = -1;
  39. private int extraHorizontalPixels = -1;
  40. private int extraVerticalPixels = -1;
  41. public ITextField() {
  42. this(DOM.createInputText());
  43. }
  44. protected ITextField(Element node) {
  45. super(node);
  46. setStyleName(CLASSNAME);
  47. addChangeListener(this);
  48. addFocusListener(this);
  49. sinkEvents(ITooltip.TOOLTIP_EVENTS);
  50. }
  51. public void onBrowserEvent(Event event) {
  52. super.onBrowserEvent(event);
  53. if (client != null) {
  54. client.handleTooltipEvent(event, this);
  55. }
  56. }
  57. public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
  58. this.client = client;
  59. id = uidl.getId();
  60. if (client.updateComponent(this, uidl, true)) {
  61. return;
  62. }
  63. if (uidl.getBooleanAttribute("readonly")) {
  64. setReadOnly(true);
  65. } else {
  66. setReadOnly(false);
  67. }
  68. immediate = uidl.getBooleanAttribute("immediate");
  69. if (uidl.hasAttribute("cols")) {
  70. setColumns(new Integer(uidl.getStringAttribute("cols")).intValue());
  71. }
  72. setText(uidl.getStringVariable("text"));
  73. }
  74. public void onChange(Widget sender) {
  75. if (client != null && id != null) {
  76. client.updateVariable(id, "text", getText(), immediate);
  77. }
  78. }
  79. public void onFocus(Widget sender) {
  80. addStyleDependentName(CLASSNAME_FOCUS);
  81. }
  82. public void onLostFocus(Widget sender) {
  83. removeStyleDependentName(CLASSNAME_FOCUS);
  84. }
  85. public void setColumns(int columns) {
  86. setColumns(getElement(), columns);
  87. }
  88. private native void setColumns(Element e, int c)
  89. /*-{
  90. try {
  91. switch(e.tagName.toLowerCase()) {
  92. case "input":
  93. //e.size = c;
  94. e.style.width = c+"em";
  95. break;
  96. case "textarea":
  97. //e.cols = c;
  98. e.style.width = c+"em";
  99. break;
  100. default:;
  101. }
  102. } catch (e) {}
  103. }-*/;
  104. public void setHeight(String height) {
  105. if (height != null && height.indexOf("%") > 0) {
  106. // special handling for proportional height
  107. proportionalHeight = Float.parseFloat(height.substring(0, height
  108. .indexOf("%"))) / 100;
  109. iLayout();
  110. } else {
  111. super.setHeight(height);
  112. proportionalHeight = -1;
  113. }
  114. }
  115. public void setWidth(String width) {
  116. if (width != null && width.indexOf("%") > 0) {
  117. // special handling for proportional w
  118. proportionalWidth = Float.parseFloat(width.substring(0, width
  119. .indexOf("%"))) / 100;
  120. iLayout();
  121. } else {
  122. super.setWidth(width);
  123. proportionalWidth = -1;
  124. }
  125. }
  126. public void iLayout() {
  127. if (proportionalWidth >= 0) {
  128. int availPixels = (int) (DOM.getElementPropertyInt(DOM
  129. .getParent(getElement()), "clientWidth") * proportionalWidth);
  130. availPixels -= getExtraHorizontalPixels();
  131. if (availPixels >= 0) {
  132. super.setWidth(availPixels + "px");
  133. }
  134. }
  135. if (proportionalHeight >= 0) {
  136. int availPixels = (int) (DOM.getElementPropertyInt(DOM
  137. .getParent(getElement()), "clientHeight") * proportionalHeight);
  138. availPixels -= getExtraVerticalPixels();
  139. super.setHeight(availPixels + "px");
  140. }
  141. }
  142. /**
  143. * @return space used by components paddings and borders
  144. */
  145. private int getExtraHorizontalPixels() {
  146. if (extraHorizontalPixels < 0) {
  147. detectExtraSizes();
  148. }
  149. return extraHorizontalPixels;
  150. }
  151. /**
  152. * @return space used by components paddings and borders
  153. */
  154. private int getExtraVerticalPixels() {
  155. if (extraVerticalPixels < 0) {
  156. detectExtraSizes();
  157. }
  158. return extraVerticalPixels;
  159. }
  160. /**
  161. * Detects space used by components paddings and borders. Used when
  162. * relational size are used.
  163. */
  164. private void detectExtraSizes() {
  165. Element clone = Util.cloneNode(getElement(), false);
  166. DOM.setElementAttribute(clone, "id", "");
  167. DOM.setStyleAttribute(clone, "visibility", "hidden");
  168. DOM.setStyleAttribute(clone, "position", "absolute");
  169. // due FF3 bug set size to 10px and later subtract it from extra pixels
  170. DOM.setStyleAttribute(clone, "width", "10px");
  171. DOM.setStyleAttribute(clone, "height", "10px");
  172. DOM.appendChild(DOM.getParent(getElement()), clone);
  173. extraHorizontalPixels = DOM.getElementPropertyInt(clone, "offsetWidth") - 10;
  174. extraVerticalPixels = DOM.getElementPropertyInt(clone, "offsetHeight") - 10;
  175. DOM.removeChild(DOM.getParent(getElement()), clone);
  176. }
  177. }