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.

FocusableScrollPanel.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 java.util.ArrayList;
  18. import com.google.gwt.core.client.Scheduler;
  19. import com.google.gwt.core.client.Scheduler.ScheduledCommand;
  20. import com.google.gwt.dom.client.DivElement;
  21. import com.google.gwt.dom.client.Document;
  22. import com.google.gwt.dom.client.Style;
  23. import com.google.gwt.dom.client.Style.Position;
  24. import com.google.gwt.dom.client.Style.Unit;
  25. import com.google.gwt.event.dom.client.HasScrollHandlers;
  26. import com.google.gwt.event.dom.client.ScrollEvent;
  27. import com.google.gwt.event.dom.client.ScrollHandler;
  28. import com.google.gwt.event.shared.HandlerRegistration;
  29. import com.google.gwt.user.client.DOM;
  30. import com.google.gwt.user.client.Event;
  31. import com.google.gwt.user.client.ui.ScrollPanel;
  32. import com.google.gwt.user.client.ui.Widget;
  33. import com.google.gwt.user.client.ui.impl.FocusImpl;
  34. import com.vaadin.client.BrowserInfo;
  35. /**
  36. * A scrollhandlers similar to {@link ScrollPanel}.
  37. *
  38. */
  39. public class FocusableScrollPanel extends SimpleFocusablePanel
  40. implements HasScrollHandlers, ScrollHandler {
  41. public FocusableScrollPanel() {
  42. // Prevent IE standard mode bug when a AbsolutePanel is contained.
  43. TouchScrollDelegate.enableTouchScrolling(this, getElement());
  44. Style style = getElement().getStyle();
  45. style.setProperty("zoom", "1");
  46. style.setPosition(Position.RELATIVE);
  47. browserInfo = BrowserInfo.get();
  48. }
  49. private DivElement focusElement;
  50. private BrowserInfo browserInfo;
  51. public FocusableScrollPanel(boolean useFakeFocusElement) {
  52. this();
  53. if (useFakeFocusElement) {
  54. focusElement = Document.get().createDivElement();
  55. }
  56. }
  57. private boolean useFakeFocusElement() {
  58. return focusElement != null;
  59. }
  60. @Override
  61. public void setWidget(Widget w) {
  62. super.setWidget(w);
  63. if (useFakeFocusElement()) {
  64. if (focusElement.getParentElement() == null) {
  65. Style style = focusElement.getStyle();
  66. style.setPosition(Position.FIXED);
  67. style.setTop(0, Unit.PX);
  68. style.setLeft(0, Unit.PX);
  69. if (browserInfo.isIE() || browserInfo.isEdge()) {
  70. // for #15294: artificially hide little bit more the
  71. // focusElement, otherwise IE will make the window to scroll
  72. // into it when focused
  73. style.setLeft(-999, Unit.PX);
  74. }
  75. getElement().appendChild(focusElement);
  76. /* Sink from focusElemet too as focusa and blur don't bubble */
  77. DOM.sinkEvents(focusElement, Event.FOCUSEVENTS);
  78. // revert to original, not focusable
  79. getElement().setPropertyObject("tabIndex", null);
  80. } else {
  81. moveFocusElementAfterWidget();
  82. }
  83. }
  84. }
  85. /**
  86. * Helper to keep focus element always in domChild[1]. Aids testing.
  87. */
  88. private void moveFocusElementAfterWidget() {
  89. getElement().insertAfter(focusElement, getWidget().getElement());
  90. }
  91. @Override
  92. public void setFocus(boolean focus) {
  93. if (useFakeFocusElement()) {
  94. if (focus) {
  95. FocusImpl.getFocusImplForPanel().focus(focusElement);
  96. } else {
  97. FocusImpl.getFocusImplForPanel().blur(focusElement);
  98. }
  99. } else {
  100. super.setFocus(focus);
  101. }
  102. }
  103. @Override
  104. public void setTabIndex(int tabIndex) {
  105. if (useFakeFocusElement()) {
  106. getElement().setTabIndex(-1);
  107. if (focusElement != null) {
  108. focusElement.setTabIndex(tabIndex);
  109. }
  110. } else {
  111. super.setTabIndex(tabIndex);
  112. }
  113. }
  114. @Override
  115. public HandlerRegistration addScrollHandler(ScrollHandler handler) {
  116. return addDomHandler(handler, ScrollEvent.getType());
  117. }
  118. /**
  119. * Gets the horizontal scroll position.
  120. *
  121. * @return the horizontal scroll position, in pixels
  122. */
  123. public int getHorizontalScrollPosition() {
  124. return getElement().getScrollLeft();
  125. }
  126. /**
  127. * Gets the vertical scroll position.
  128. *
  129. * @return the vertical scroll position, in pixels
  130. */
  131. public int getScrollPosition() {
  132. if (getElement().getPropertyJSO("_vScrollTop") != null) {
  133. return getElement().getPropertyInt("_vScrollTop");
  134. } else {
  135. return getElement().getScrollTop();
  136. }
  137. }
  138. /**
  139. * Sets the horizontal scroll position.
  140. *
  141. * @param position
  142. * the new horizontal scroll position, in pixels
  143. */
  144. public void setHorizontalScrollPosition(int position) {
  145. getElement().setScrollLeft(position);
  146. }
  147. /**
  148. * Sets the vertical scroll position.
  149. *
  150. * @param position
  151. * the new vertical scroll position, in pixels
  152. */
  153. public void setScrollPosition(int position) {
  154. if (BrowserInfo.get().isAndroidWithBrokenScrollTop()
  155. && BrowserInfo.get().requiresTouchScrollDelegate()) {
  156. ArrayList<com.google.gwt.dom.client.Element> elements = TouchScrollDelegate
  157. .getElements(getElement());
  158. for (com.google.gwt.dom.client.Element el : elements) {
  159. final Style style = el.getStyle();
  160. style.setProperty("webkitTransform",
  161. "translate3d(0px," + -position + "px,0px)");
  162. }
  163. getElement().setPropertyInt("_vScrollTop", position);
  164. } else {
  165. getElement().setScrollTop(position);
  166. }
  167. }
  168. @Override
  169. public void onScroll(ScrollEvent event) {
  170. Scheduler.get().scheduleDeferred(new ScheduledCommand() {
  171. @Override
  172. public void execute() {
  173. focusElement.getStyle().setTop(getScrollPosition(), Unit.PX);
  174. focusElement.getStyle().setLeft(getHorizontalScrollPosition(),
  175. Unit.PX);
  176. }
  177. });
  178. }
  179. public com.google.gwt.user.client.Element getFocusElement() {
  180. if (useFakeFocusElement()) {
  181. return focusElement.cast();
  182. } else {
  183. return getElement();
  184. }
  185. }
  186. }