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.

FocusUtil.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui;
  17. import com.google.gwt.dom.client.Element;
  18. import com.google.gwt.user.client.ui.Focusable;
  19. import com.google.gwt.user.client.ui.Widget;
  20. /**
  21. * A helper class used to make it easier for {@link Widget}s to implement
  22. * {@link Focusable}.
  23. *
  24. * @author Vaadin Ltd
  25. * @since 7.0.3
  26. *
  27. */
  28. public class FocusUtil {
  29. /**
  30. * Sets the access key property.
  31. *
  32. * @param focusable
  33. * The widget for which we want to set the access key.
  34. * @param key
  35. * The access key to set
  36. */
  37. public static void setAccessKey(Widget focusable, char key) {
  38. assert (focusable != null && focusable
  39. .getElement() != null) : "Can't setAccessKey for a widget without an element";
  40. focusable.getElement().setPropertyString("accessKey", "" + key);
  41. }
  42. /**
  43. * Explicitly focus/unfocus the given widget. Only one widget can have focus
  44. * at a time, and the widget that does will receive all keyboard events.
  45. *
  46. * @param focusable
  47. * the widget to focus/unfocus
  48. * @param focus
  49. * whether this widget should take focus or release it
  50. */
  51. public static void setFocus(Widget focusable, boolean focus) {
  52. assert (focusable != null && focusable
  53. .getElement() != null) : "Can't setFocus for a widget without an element";
  54. if (focus) {
  55. focusable.getElement().focus();
  56. } else {
  57. focusable.getElement().blur();
  58. }
  59. }
  60. /**
  61. * Sets the widget's position in the tab index. If more than one widget has
  62. * the same tab index, each such widget will receive focus in an arbitrary
  63. * order. Setting the tab index to <code>-1</code> will cause the widget to
  64. * be removed from the tab order.
  65. *
  66. * @param focusable
  67. * The widget
  68. * @param tabIndex
  69. * the widget's tab index
  70. */
  71. public static void setTabIndex(Widget focusable, int tabIndex) {
  72. assert (focusable != null && focusable
  73. .getElement() != null) : "Can't setTabIndex for a widget without an element";
  74. focusable.getElement().setTabIndex(tabIndex);
  75. }
  76. /**
  77. * Gets the widget's position in the tab index.
  78. *
  79. * @param focusable
  80. * The widget
  81. *
  82. * @return the widget's tab index
  83. */
  84. public static int getTabIndex(Widget focusable) {
  85. assert (focusable != null && focusable
  86. .getElement() != null) : "Can't getTabIndex for a widget without an element";
  87. return focusable.getElement().getTabIndex();
  88. }
  89. /**
  90. * Finds all the focusable children of given parent element.
  91. *
  92. * @param parent
  93. * the parent element
  94. * @return array of focusable children
  95. * @since 8.1.7
  96. */
  97. public static native Element[] getFocusableChildren(Element parent)
  98. /*-{
  99. var focusableChildren = parent.querySelectorAll('[type][tabindex]:not([tabindex="-1"]), [role=button][tabindex]:not([tabindex="-1"])');
  100. return focusableChildren;
  101. }-*/;
  102. /**
  103. * Moves the focus to the first focusable child of given parent element.
  104. *
  105. * @param parent
  106. * the parent element
  107. * @since 8.1.7
  108. */
  109. public static void focusOnFirstFocusableElement(Element parent) {
  110. Element[] focusableChildren = getFocusableChildren(parent);
  111. if (focusableChildren.length == 0) {
  112. return;
  113. }
  114. // find the first element that doesn't have "disabled" in the class name
  115. for (int i = 0; i < focusableChildren.length; i++) {
  116. Element element = focusableChildren[i];
  117. String classes = element.getAttribute("class");
  118. if (classes == null
  119. || !classes.toLowerCase().contains("disabled")) {
  120. element.focus();
  121. return;
  122. }
  123. }
  124. }
  125. /**
  126. * Moves the focus to the last focusable child of given parent element.
  127. *
  128. * @param parent
  129. * the parent element
  130. * @since 8.1.7
  131. */
  132. public static void focusOnLastFocusableElement(Element parent) {
  133. Element[] focusableChildren = getFocusableChildren(parent);
  134. if (focusableChildren.length > 0) {
  135. focusableChildren[focusableChildren.length - 1].focus();
  136. }
  137. }
  138. }