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

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