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.

VOverlay.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 java.util.logging.Logger;
  18. import com.google.gwt.aria.client.Roles;
  19. import com.google.gwt.core.client.GWT;
  20. import com.google.gwt.dom.client.Element;
  21. import com.google.gwt.user.client.DOM;
  22. import com.google.gwt.user.client.ui.RootPanel;
  23. import com.vaadin.client.ApplicationConnection;
  24. import com.vaadin.client.ComponentConnector;
  25. import com.vaadin.client.Util;
  26. import com.vaadin.client.widgets.Overlay;
  27. /**
  28. * In Vaadin UI this VOverlay should always be used for all elements that
  29. * temporary float over other components like context menus etc. This is to deal
  30. * stacking order correctly with VWindow objects.
  31. * <p>
  32. * To use this correctly, use {@link GWT#create(Class)} to create the
  33. * {@link Overlay} superclass and the default widgetset will replace it with
  34. * this. The widget will not be dependent on this Vaadin specific widget and can
  35. * be used in a pure GWT environment.
  36. *
  37. * @deprecated as this is specifically for Vaadin only, it should not be used
  38. * directly.
  39. */
  40. @Deprecated
  41. public class VOverlay extends Overlay {
  42. /*
  43. * ApplicationConnection that this overlay belongs to, which is needed to
  44. * create the overlay in the correct container so that the correct styles
  45. * are applied. If not given, owner will be used to figure out, and as a
  46. * last fallback, the overlay is created w/o container, potentially missing
  47. * styles.
  48. */
  49. protected ApplicationConnection ac;
  50. public VOverlay() {
  51. super();
  52. }
  53. public VOverlay(boolean autoHide) {
  54. super(autoHide);
  55. }
  56. public VOverlay(boolean autoHide, boolean modal) {
  57. super(autoHide, modal);
  58. }
  59. /**
  60. * Get the {@link ApplicationConnection} that this overlay belongs to. If
  61. * it's not set, {@link #getOwner()} is used to figure it out.
  62. *
  63. * @return
  64. */
  65. protected ApplicationConnection getApplicationConnection() {
  66. if (ac != null) {
  67. return ac;
  68. } else if (getOwner() != null) {
  69. ComponentConnector c = Util.findConnectorFor(getOwner());
  70. if (c != null) {
  71. ac = c.getConnection();
  72. }
  73. return ac;
  74. } else {
  75. return null;
  76. }
  77. }
  78. /**
  79. * Gets the 'overlay container' element. Tries to find the current
  80. * {@link ApplicationConnection} using {@link #getApplicationConnection()}.
  81. *
  82. * @return the overlay container element for the current
  83. * {@link ApplicationConnection} or another element if the current
  84. * {@link ApplicationConnection} cannot be determined.
  85. */
  86. @Override
  87. public com.google.gwt.user.client.Element getOverlayContainer() {
  88. ApplicationConnection ac = getApplicationConnection();
  89. if (ac == null) {
  90. // could not figure out which one we belong to, styling will
  91. // probably fail
  92. Logger.getLogger(getClass().getSimpleName()).warning(
  93. "Could not determine ApplicationConnection for Overlay. Overlay will be attached directly to the root panel");
  94. return super.getOverlayContainer();
  95. } else {
  96. return getOverlayContainer(ac);
  97. }
  98. }
  99. /**
  100. * Gets the 'overlay container' element pertaining to the given
  101. * {@link ApplicationConnection}. Each overlay should be created in a
  102. * overlay container element, so that the correct theme and styles can be
  103. * applied.
  104. *
  105. * @param ac
  106. * A reference to {@link ApplicationConnection}
  107. * @return The overlay container
  108. */
  109. public static com.google.gwt.user.client.Element getOverlayContainer(
  110. ApplicationConnection ac) {
  111. String id = ac.getConfiguration().getRootPanelId();
  112. id += "-overlays";
  113. Element container = DOM.getElementById(id);
  114. if (container == null) {
  115. container = DOM.createDiv();
  116. container.setId(id);
  117. String styles = ac.getUIConnector().getWidget().getParent()
  118. .getStyleName();
  119. if (styles != null && !styles.isEmpty()) {
  120. container.addClassName(styles);
  121. }
  122. container.addClassName(CLASSNAME_CONTAINER);
  123. RootPanel.get().getElement().appendChild(container);
  124. }
  125. return DOM.asOld(container);
  126. }
  127. /**
  128. * Set the label of the container element, where tooltip, notification and
  129. * dialogs are added to.
  130. *
  131. * @param applicationConnection
  132. * the application connection for which to change the label
  133. * @param overlayContainerLabel
  134. * label for the container
  135. */
  136. public static void setOverlayContainerLabel(
  137. ApplicationConnection applicationConnection,
  138. String overlayContainerLabel) {
  139. Roles.getAlertRole().setAriaLabelProperty(
  140. VOverlay.getOverlayContainer(applicationConnection),
  141. overlayContainerLabel);
  142. }
  143. /**
  144. * Sets the {@link ApplicationConnection} that this overlay belongs to.
  145. *
  146. * @see #getApplicationConnection()
  147. *
  148. * @param ac
  149. * the connection
  150. */
  151. public void setApplicationConnection(ApplicationConnection ac) {
  152. this.ac = ac;
  153. }
  154. }