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 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 2000-2014 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.user.client.ui.Focusable;
  18. import com.google.gwt.user.client.ui.Widget;
  19. /**
  20. * A helper class used to make it easier for {@link Widget}s to implement
  21. * {@link Focusable}.
  22. *
  23. * @author Vaadin Ltd
  24. * @version @VERSION@
  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.getElement() != null) : "Can't setAccessKey for a widget without an element";
  39. focusable.getElement().setPropertyString("accessKey", "" + key);
  40. }
  41. /**
  42. * Explicitly focus/unfocus the given widget. Only one widget can have focus
  43. * at a time, and the widget that does will receive all keyboard events.
  44. *
  45. * @param focusable
  46. * the widget to focus/unfocus
  47. * @param focused
  48. * whether this widget should take focus or release it
  49. */
  50. public static void setFocus(Widget focusable, boolean focus) {
  51. assert (focusable != null && focusable.getElement() != null) : "Can't setFocus for a widget without an element";
  52. if (focus) {
  53. focusable.getElement().focus();
  54. } else {
  55. focusable.getElement().blur();
  56. }
  57. }
  58. /**
  59. * Sets the widget's position in the tab index. If more than one widget has
  60. * the same tab index, each such widget will receive focus in an arbitrary
  61. * order. Setting the tab index to <code>-1</code> will cause the widget to
  62. * be removed from the tab order.
  63. *
  64. * @param focusable
  65. * The widget
  66. * @param tabIndex
  67. * the widget's tab index
  68. */
  69. public static void setTabIndex(Widget focusable, int tabIndex) {
  70. assert (focusable != null && focusable.getElement() != null) : "Can't setTabIndex for a widget without an element";
  71. focusable.getElement().setTabIndex(tabIndex);
  72. }
  73. /**
  74. * Gets the widget's position in the tab index.
  75. *
  76. * @param focusable
  77. * The widget
  78. *
  79. * @return the widget's tab index
  80. */
  81. public static int getTabIndex(Widget focusable) {
  82. assert (focusable != null && focusable.getElement() != null) : "Can't getTabIndex for a widget without an element";
  83. return focusable.getElement().getTabIndex();
  84. }
  85. }