選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

FocusUtil.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2000-2016 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. public static native Element[] getFocusableChildren(Element parent)
  90. /*-{
  91. var focusableChildren = parent.querySelectorAll('[type][tabindex]:not([tabindex="-1"]), [role=button][tabindex]:not([tabindex="-1"])');
  92. return focusableChildren;
  93. }-*/;
  94. public static void focusOnFirstFocusableElement(Element parent) {
  95. Element[] focusableChildren = getFocusableChildren(parent);
  96. if (focusableChildren.length > 0) {
  97. focusableChildren[0].focus();
  98. }
  99. }
  100. public static void focusOnLastFocusableElement(Element parent) {
  101. Element[] focusableChildren = getFocusableChildren(parent);
  102. if (focusableChildren.length > 0) {
  103. focusableChildren[focusableChildren.length - 1].focus();
  104. }
  105. }
  106. }