Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VAbsoluteLayout.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import com.google.gwt.dom.client.DivElement;
  8. import com.google.gwt.dom.client.Document;
  9. import com.google.gwt.dom.client.Style;
  10. import com.google.gwt.user.client.DOM;
  11. import com.google.gwt.user.client.Element;
  12. import com.google.gwt.user.client.ui.ComplexPanel;
  13. import com.google.gwt.user.client.ui.SimplePanel;
  14. import com.google.gwt.user.client.ui.Widget;
  15. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  16. import com.vaadin.terminal.gwt.client.UIDL;
  17. import com.vaadin.terminal.gwt.client.Util;
  18. import com.vaadin.terminal.gwt.client.VCaption;
  19. import com.vaadin.terminal.gwt.client.ConnectorMap;
  20. import com.vaadin.terminal.gwt.client.ComponentConnector;
  21. public class VAbsoluteLayout extends ComplexPanel {
  22. /** Tag name for widget creation */
  23. public static final String TAGNAME = "absolutelayout";
  24. /** Class name, prefix in styling */
  25. public static final String CLASSNAME = "v-absolutelayout";
  26. private DivElement marginElement;
  27. protected final Element canvas = DOM.createDiv();
  28. private Object previousStyleName;
  29. Map<String, AbsoluteWrapper> pidToComponentWrappper = new HashMap<String, AbsoluteWrapper>();
  30. protected ApplicationConnection client;
  31. public VAbsoluteLayout() {
  32. setElement(Document.get().createDivElement());
  33. setStyleName(CLASSNAME);
  34. marginElement = Document.get().createDivElement();
  35. canvas.getStyle().setProperty("position", "relative");
  36. canvas.getStyle().setProperty("overflow", "hidden");
  37. marginElement.appendChild(canvas);
  38. getElement().appendChild(marginElement);
  39. }
  40. AbsoluteWrapper getWrapper(ApplicationConnection client, UIDL componentUIDL) {
  41. AbsoluteWrapper wrapper = pidToComponentWrappper.get(componentUIDL
  42. .getId());
  43. if (wrapper == null) {
  44. wrapper = new AbsoluteWrapper(client.getPaintable(componentUIDL));
  45. pidToComponentWrappper.put(componentUIDL.getId(), wrapper);
  46. add(wrapper);
  47. }
  48. return wrapper;
  49. }
  50. @Override
  51. public void add(Widget child) {
  52. super.add(child, canvas);
  53. }
  54. @Override
  55. public void setWidth(String width) {
  56. super.setWidth(width);
  57. // TODO do this so that canvas gets the sized properly (the area
  58. // inside marginals)
  59. canvas.getStyle().setProperty("width", width);
  60. }
  61. @Override
  62. public void setHeight(String height) {
  63. super.setHeight(height);
  64. // TODO do this so that canvas gets the sized properly (the area
  65. // inside marginals)
  66. canvas.getStyle().setProperty("height", height);
  67. }
  68. public class AbsoluteWrapper extends SimplePanel {
  69. private String css;
  70. String left;
  71. String top;
  72. String right;
  73. String bottom;
  74. private String zIndex;
  75. private ComponentConnector paintable;
  76. private VCaption caption;
  77. public AbsoluteWrapper(ComponentConnector paintable) {
  78. this.paintable = paintable;
  79. setStyleName(CLASSNAME + "-wrapper");
  80. }
  81. public void updateCaption(UIDL uidl) {
  82. boolean captionIsNeeded = VCaption.isNeeded(uidl,
  83. paintable.getState());
  84. if (captionIsNeeded) {
  85. if (caption == null) {
  86. caption = new VCaption(paintable, client);
  87. VAbsoluteLayout.this.add(caption);
  88. }
  89. caption.updateCaption(uidl);
  90. updateCaptionPosition();
  91. } else {
  92. if (caption != null) {
  93. caption.removeFromParent();
  94. caption = null;
  95. }
  96. }
  97. }
  98. @Override
  99. public void setWidget(Widget w) {
  100. // this fixes #5457 (Widget implementation can change on-the-fly)
  101. paintable = ConnectorMap.get(client).getConnector(w);
  102. super.setWidget(w);
  103. }
  104. public void destroy() {
  105. if (caption != null) {
  106. caption.removeFromParent();
  107. }
  108. client.unregisterPaintable(paintable);
  109. removeFromParent();
  110. }
  111. public void updateFromUIDL(UIDL componentUIDL) {
  112. setPosition(componentUIDL.getStringAttribute("css"));
  113. if (getWidget() != paintable.getWidget()) {
  114. setWidget(paintable.getWidget());
  115. }
  116. UIDL childUIDL = componentUIDL.getChildUIDL(0);
  117. paintable.updateFromUIDL(childUIDL, client);
  118. if (childUIDL.hasAttribute("cached")) {
  119. // child may need relative size adjustment if wrapper details
  120. // have changed this could be optimized (check if wrapper size
  121. // has changed)
  122. client.handleComponentRelativeSize(paintable
  123. .getWidget());
  124. }
  125. }
  126. public void setPosition(String stringAttribute) {
  127. if (css == null || !css.equals(stringAttribute)) {
  128. css = stringAttribute;
  129. top = right = bottom = left = zIndex = null;
  130. if (!css.equals("")) {
  131. String[] properties = css.split(";");
  132. for (int i = 0; i < properties.length; i++) {
  133. String[] keyValue = properties[i].split(":");
  134. if (keyValue[0].equals("left")) {
  135. left = keyValue[1];
  136. } else if (keyValue[0].equals("top")) {
  137. top = keyValue[1];
  138. } else if (keyValue[0].equals("right")) {
  139. right = keyValue[1];
  140. } else if (keyValue[0].equals("bottom")) {
  141. bottom = keyValue[1];
  142. } else if (keyValue[0].equals("z-index")) {
  143. zIndex = keyValue[1];
  144. }
  145. }
  146. }
  147. // ensure ne values
  148. Style style = getElement().getStyle();
  149. /*
  150. * IE8 dies when nulling zIndex, even in IE7 mode. All other css
  151. * properties (and even in older IE's) accept null values just
  152. * fine. Assign empty string instead of null.
  153. */
  154. if (zIndex != null) {
  155. style.setProperty("zIndex", zIndex);
  156. } else {
  157. style.setProperty("zIndex", "");
  158. }
  159. style.setProperty("top", top);
  160. style.setProperty("left", left);
  161. style.setProperty("right", right);
  162. style.setProperty("bottom", bottom);
  163. }
  164. updateCaptionPosition();
  165. }
  166. void updateCaptionPosition() {
  167. if (caption != null) {
  168. Style style = caption.getElement().getStyle();
  169. style.setProperty("position", "absolute");
  170. style.setPropertyPx("left", getElement().getOffsetLeft());
  171. style.setPropertyPx("top", getElement().getOffsetTop()
  172. - caption.getHeight());
  173. }
  174. }
  175. }
  176. /**
  177. * Returns the deepest nested child component which contains "element". The
  178. * child component is also returned if "element" is part of its caption.
  179. *
  180. * @param element
  181. * An element that is a nested sub element of the root element in
  182. * this layout
  183. * @return The Paintable which the element is a part of. Null if the element
  184. * belongs to the layout and not to a child.
  185. */
  186. ComponentConnector getComponent(Element element) {
  187. return Util.getPaintableForElement(client, this, element);
  188. }
  189. }