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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.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. * A "thread local" of sorts, set temporarily so that VOverlayImpl knows
  61. * which VOverlay is using it, so that it can be attached to the correct
  62. * overlay container.
  63. *
  64. * TODO this is a strange pattern that we should get rid of when possible.
  65. */
  66. protected static VOverlay current;
  67. /**
  68. * Get the {@link ApplicationConnection} that this overlay belongs to. If
  69. * it's not set, {@link #getOwner()} is used to figure it out.
  70. *
  71. * @return
  72. */
  73. protected ApplicationConnection getApplicationConnection() {
  74. if (ac != null) {
  75. return ac;
  76. } else if (getOwner() != null) {
  77. ComponentConnector c = Util.findConnectorFor(getOwner());
  78. if (c != null) {
  79. ac = c.getConnection();
  80. }
  81. return ac;
  82. } else {
  83. return null;
  84. }
  85. }
  86. /**
  87. * Gets the 'overlay container' element. Tries to find the current
  88. * {@link ApplicationConnection} using {@link #getApplicationConnection()}.
  89. *
  90. * @return the overlay container element for the current
  91. * {@link ApplicationConnection} or another element if the current
  92. * {@link ApplicationConnection} cannot be determined.
  93. */
  94. @Override
  95. public com.google.gwt.user.client.Element getOverlayContainer() {
  96. ApplicationConnection ac = getApplicationConnection();
  97. if (ac == null) {
  98. // could not figure out which one we belong to, styling will
  99. // probably fail
  100. Logger.getLogger(getClass().getSimpleName()).warning(
  101. "Could not determine ApplicationConnection for Overlay. Overlay will be attached directly to the root panel");
  102. return super.getOverlayContainer();
  103. } else {
  104. return getOverlayContainer(ac);
  105. }
  106. }
  107. /**
  108. * Gets the 'overlay container' element pertaining to the given
  109. * {@link ApplicationConnection}. Each overlay should be created in a
  110. * overlay container element, so that the correct theme and styles can be
  111. * applied.
  112. *
  113. * @param ac
  114. * A reference to {@link ApplicationConnection}
  115. * @return The overlay container
  116. */
  117. public static com.google.gwt.user.client.Element getOverlayContainer(
  118. ApplicationConnection ac) {
  119. String id = ac.getConfiguration().getRootPanelId();
  120. id = id += "-overlays";
  121. Element container = DOM.getElementById(id);
  122. if (container == null) {
  123. container = DOM.createDiv();
  124. container.setId(id);
  125. String styles = ac.getUIConnector().getWidget().getParent()
  126. .getStyleName();
  127. if (styles != null && !styles.isEmpty()) {
  128. container.addClassName(styles);
  129. }
  130. container.addClassName(CLASSNAME_CONTAINER);
  131. RootPanel.get().getElement().appendChild(container);
  132. }
  133. return DOM.asOld(container);
  134. }
  135. /**
  136. * Set the label of the container element, where tooltip, notification and
  137. * dialogs are added to.
  138. *
  139. * @param applicationConnection
  140. * the application connection for which to change the label
  141. * @param overlayContainerLabel
  142. * label for the container
  143. */
  144. public static void setOverlayContainerLabel(
  145. ApplicationConnection applicationConnection,
  146. String overlayContainerLabel) {
  147. Roles.getAlertRole().setAriaLabelProperty(
  148. VOverlay.getOverlayContainer(applicationConnection),
  149. overlayContainerLabel);
  150. }
  151. }