Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

VCustomComponent.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Set;
  6. import com.google.gwt.core.client.Scheduler;
  7. import com.google.gwt.user.client.Command;
  8. import com.google.gwt.user.client.ui.SimplePanel;
  9. import com.google.gwt.user.client.ui.Widget;
  10. import com.vaadin.terminal.gwt.client.ApplicationConnection;
  11. import com.vaadin.terminal.gwt.client.Container;
  12. import com.vaadin.terminal.gwt.client.RenderSpace;
  13. import com.vaadin.terminal.gwt.client.UIDL;
  14. import com.vaadin.terminal.gwt.client.Util;
  15. import com.vaadin.terminal.gwt.client.VPaintableMap;
  16. import com.vaadin.terminal.gwt.client.VPaintableWidget;
  17. public class VCustomComponent extends SimplePanel implements Container {
  18. private static final String CLASSNAME = "v-customcomponent";
  19. private String height;
  20. private ApplicationConnection client;
  21. private boolean rendering;
  22. private String width;
  23. private RenderSpace renderSpace = new RenderSpace();
  24. public VCustomComponent() {
  25. super();
  26. setStyleName(CLASSNAME);
  27. }
  28. public void updateFromUIDL(UIDL uidl, final ApplicationConnection client) {
  29. rendering = true;
  30. if (client.updateComponent(this, uidl, true)) {
  31. rendering = false;
  32. return;
  33. }
  34. this.client = client;
  35. final UIDL child = uidl.getChildUIDL(0);
  36. if (child != null) {
  37. final VPaintableWidget paintable = client.getPaintable(child);
  38. Widget widget = paintable.getWidgetForPaintable();
  39. if (widget != getWidget()) {
  40. if (getWidget() != null) {
  41. client.unregisterPaintable(VPaintableMap.get(client)
  42. .getPaintable(getWidget()));
  43. clear();
  44. }
  45. setWidget(widget);
  46. }
  47. paintable.updateFromUIDL(child, client);
  48. }
  49. boolean updateDynamicSize = updateDynamicSize();
  50. if (updateDynamicSize) {
  51. Scheduler.get().scheduleDeferred(new Command() {
  52. public void execute() {
  53. // FIXME deferred relative size update needed to fix some
  54. // scrollbar issues in sampler. This must be the wrong way
  55. // to do it. Might be that some other component is broken.
  56. client.handleComponentRelativeSize(VCustomComponent.this);
  57. }
  58. });
  59. }
  60. renderSpace.setWidth(getElement().getOffsetWidth());
  61. renderSpace.setHeight(getElement().getOffsetHeight());
  62. /*
  63. * Needed to update client size if the size of this component has
  64. * changed and the child uses relative size(s).
  65. */
  66. client.runDescendentsLayout(this);
  67. rendering = false;
  68. }
  69. private boolean updateDynamicSize() {
  70. boolean updated = false;
  71. if (isDynamicWidth()) {
  72. int childWidth = Util.getRequiredWidth(getWidget());
  73. getElement().getStyle().setPropertyPx("width", childWidth);
  74. updated = true;
  75. }
  76. if (isDynamicHeight()) {
  77. int childHeight = Util.getRequiredHeight(getWidget());
  78. getElement().getStyle().setPropertyPx("height", childHeight);
  79. updated = true;
  80. }
  81. return updated;
  82. }
  83. protected boolean isDynamicWidth() {
  84. return width == null || width.equals("");
  85. }
  86. protected boolean isDynamicHeight() {
  87. return height == null || height.equals("");
  88. }
  89. public boolean hasChildComponent(Widget component) {
  90. if (getWidget() == component) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  97. if (hasChildComponent(oldComponent)) {
  98. clear();
  99. setWidget(newComponent);
  100. } else {
  101. throw new IllegalStateException();
  102. }
  103. }
  104. public void updateCaption(VPaintableWidget component, UIDL uidl) {
  105. // NOP, custom component dont render composition roots caption
  106. }
  107. public boolean requestLayout(Set<Widget> children) {
  108. // If a child grows in size, it will not necessarily be calculated
  109. // correctly unless we remove previous size definitions
  110. if (isDynamicWidth()) {
  111. getElement().getStyle().setProperty("width", "");
  112. }
  113. if (isDynamicHeight()) {
  114. getElement().getStyle().setProperty("height", "");
  115. }
  116. return !updateDynamicSize();
  117. }
  118. public RenderSpace getAllocatedSpace(Widget child) {
  119. return renderSpace;
  120. }
  121. @Override
  122. public void setHeight(String height) {
  123. super.setHeight(height);
  124. renderSpace.setHeight(getElement().getOffsetHeight());
  125. if (!height.equals(this.height)) {
  126. this.height = height;
  127. if (!rendering) {
  128. client.handleComponentRelativeSize(getWidget());
  129. }
  130. }
  131. }
  132. @Override
  133. public void setWidth(String width) {
  134. super.setWidth(width);
  135. renderSpace.setWidth(getElement().getOffsetWidth());
  136. if (!width.equals(this.width)) {
  137. this.width = width;
  138. if (!rendering) {
  139. client.handleComponentRelativeSize(getWidget());
  140. }
  141. }
  142. }
  143. public Widget getWidgetForPaintable() {
  144. return this;
  145. }
  146. }