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.

IToolkitOverlay.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal.gwt.client.ui;
  5. import com.google.gwt.user.client.DOM;
  6. import com.google.gwt.user.client.Element;
  7. import com.google.gwt.user.client.ui.HTML;
  8. import com.google.gwt.user.client.ui.PopupListener;
  9. import com.google.gwt.user.client.ui.PopupPanel;
  10. import com.google.gwt.user.client.ui.RootPanel;
  11. import com.itmill.toolkit.terminal.gwt.client.BrowserInfo;
  12. /**
  13. * In Toolkit UI this Overlay should always be used for all elements that
  14. * temporary float over other components like context menus etc. This is to deal
  15. * stacking order correctly with IWindow objects.
  16. */
  17. public class IToolkitOverlay extends PopupPanel {
  18. public static final int Z_INDEX = 20000;
  19. private Shadow shadow;
  20. public IToolkitOverlay() {
  21. super();
  22. adjustZIndex();
  23. }
  24. public IToolkitOverlay(boolean autoHide) {
  25. super(autoHide);
  26. adjustZIndex();
  27. }
  28. public IToolkitOverlay(boolean autoHide, boolean modal) {
  29. super(autoHide, modal);
  30. adjustZIndex();
  31. }
  32. public IToolkitOverlay(boolean autoHide, boolean modal, boolean showShadow) {
  33. super(autoHide, modal);
  34. if (showShadow) {
  35. shadow = new Shadow();
  36. }
  37. adjustZIndex();
  38. }
  39. private void adjustZIndex() {
  40. DOM.setStyleAttribute(getElement(), "zIndex", "" + Z_INDEX);
  41. }
  42. public void setPopupPosition(int left, int top) {
  43. super.setPopupPosition(left, top);
  44. if (shadow != null) {
  45. shadow.updateSizeAndPosition();
  46. }
  47. }
  48. public void show() {
  49. super.show();
  50. if (shadow != null) {
  51. DOM.appendChild(RootPanel.get().getElement(), shadow.getElement());
  52. shadow.updateSizeAndPosition();
  53. }
  54. if (BrowserInfo.get().isIE6()) {
  55. adjustIE6Frame(getElement(), Z_INDEX - 1);
  56. }
  57. }
  58. private native void adjustIE6Frame(Element popup, int zindex)
  59. /*-{
  60. // relies on PopupImplIE6
  61. popup.__frame.style.zIndex = zindex;
  62. }-*/;
  63. public void setShadowOffset(int top, int right, int bottom, int left) {
  64. if (shadow != null) {
  65. shadow.setOffset(top, right, bottom, left);
  66. }
  67. }
  68. private class Shadow extends HTML {
  69. private static final String CLASSNAME = "i-shadow";
  70. private static final String HTML = "<div class=\"top-left\"></div><div class=\"top\"></div><div class=\"top-right\"></div><div class=\"left\"></div><div class=\"center\"></div><div class=\"right\"></div><div class=\"bottom-left\"></div><div class=\"bottom\"></div><div class=\"bottom-right\"></div>";
  71. // Amount of shadow on each side.
  72. private int top = 2;
  73. private int right = 5;
  74. private int bottom = 6;
  75. private int left = 5;
  76. public Shadow() {
  77. super(HTML);
  78. setStyleName(CLASSNAME);
  79. DOM.setStyleAttribute(getElement(), "position", "absolute");
  80. addPopupListener(new PopupListener() {
  81. public void onPopupClosed(PopupPanel sender, boolean autoClosed) {
  82. DOM.removeChild(RootPanel.get().getElement(), shadow
  83. .getElement());
  84. }
  85. });
  86. }
  87. public void updateSizeAndPosition() {
  88. // Calculate proper z-index
  89. String zIndex = DOM.getStyleAttribute(IToolkitOverlay.this
  90. .getElement(), "zIndex");
  91. if (zIndex == null) {
  92. zIndex = "" + Z_INDEX;
  93. }
  94. // Calculate position and size
  95. if (BrowserInfo.get().isIE()) {
  96. // Shake IE
  97. IToolkitOverlay.this.getOffsetHeight();
  98. IToolkitOverlay.this.getOffsetWidth();
  99. }
  100. int x = IToolkitOverlay.this.getAbsoluteLeft() - left;
  101. int y = IToolkitOverlay.this.getAbsoluteTop() - top;
  102. int width = IToolkitOverlay.this.getOffsetWidth() + left + right;
  103. int height = IToolkitOverlay.this.getOffsetHeight() + top + bottom;
  104. if (width < 0) {
  105. width = 0;
  106. }
  107. if (height < 0) {
  108. height = 0;
  109. }
  110. // Update correct values
  111. DOM.setStyleAttribute(shadow.getElement(), "zIndex", ""
  112. + (Integer.parseInt(zIndex) - 1));
  113. DOM.setStyleAttribute(getElement(), "width", width + "px");
  114. DOM.setStyleAttribute(getElement(), "height", height + "px");
  115. DOM.setStyleAttribute(getElement(), "top", y + "px");
  116. DOM.setStyleAttribute(getElement(), "left", x + "px");
  117. }
  118. public void setOffset(int top, int right, int bottom, int left) {
  119. this.top = top;
  120. this.right = right;
  121. this.bottom = bottom;
  122. this.left = left;
  123. if (IToolkitOverlay.this.isAttached()) {
  124. updateSizeAndPosition();
  125. }
  126. }
  127. }
  128. }