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.

FocusElementPanel.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.DivElement;
  18. import com.google.gwt.dom.client.Document;
  19. import com.google.gwt.dom.client.Style;
  20. import com.google.gwt.dom.client.Style.Position;
  21. import com.google.gwt.dom.client.Style.Unit;
  22. import com.google.gwt.user.client.DOM;
  23. import com.google.gwt.user.client.Event;
  24. import com.google.gwt.user.client.ui.Widget;
  25. import com.google.gwt.user.client.ui.impl.FocusImpl;
  26. /**
  27. * A panel that contains an always visible 0x0 size element that holds the
  28. * focus.
  29. */
  30. public class FocusElementPanel extends SimpleFocusablePanel {
  31. private DivElement focusElement;
  32. public FocusElementPanel() {
  33. focusElement = Document.get().createDivElement();
  34. }
  35. @Override
  36. public void setWidget(Widget w) {
  37. super.setWidget(w);
  38. if (focusElement.getParentElement() == null) {
  39. Style style = focusElement.getStyle();
  40. style.setPosition(Position.FIXED);
  41. style.setTop(0, Unit.PX);
  42. style.setLeft(0, Unit.PX);
  43. getElement().appendChild(focusElement);
  44. /* Sink from focusElement too as focus and blur don't bubble */
  45. DOM.sinkEvents(focusElement, Event.FOCUSEVENTS);
  46. // revert to original, not focusable
  47. getElement().setPropertyObject("tabIndex", null);
  48. } else {
  49. moveFocusElementAfterWidget();
  50. }
  51. }
  52. /**
  53. * Helper to keep focus element always in domChild[1]. Aids testing.
  54. */
  55. private void moveFocusElementAfterWidget() {
  56. getElement().insertAfter(focusElement, getWidget().getElement());
  57. }
  58. @Override
  59. public void setFocus(boolean focus) {
  60. if (focus) {
  61. FocusImpl.getFocusImplForPanel().focus(focusElement);
  62. } else {
  63. FocusImpl.getFocusImplForPanel().blur(focusElement);
  64. }
  65. }
  66. @Override
  67. public void setTabIndex(int tabIndex) {
  68. getElement().setTabIndex(-1);
  69. if (focusElement != null) {
  70. focusElement.setTabIndex(tabIndex);
  71. }
  72. }
  73. /**
  74. * @return the focus element
  75. */
  76. public com.google.gwt.user.client.Element getFocusElement() {
  77. return focusElement.cast();
  78. }
  79. }