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.4KB

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