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.

VCustomComponent.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.terminal.gwt.client.ui;
  5. import java.util.Set;
  6. import com.google.gwt.user.client.Command;
  7. import com.google.gwt.user.client.DeferredCommand;
  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.Paintable;
  13. import com.vaadin.terminal.gwt.client.RenderSpace;
  14. import com.vaadin.terminal.gwt.client.UIDL;
  15. import com.vaadin.terminal.gwt.client.Util;
  16. public class VCustomComponent extends SimplePanel implements Container {
  17. private static final String CLASSNAME = "i-customcomponent";
  18. private String height;
  19. private ApplicationConnection client;
  20. private boolean rendering;
  21. private String width;
  22. private RenderSpace renderSpace = new RenderSpace();
  23. public VCustomComponent() {
  24. super();
  25. setStyleName(CLASSNAME);
  26. }
  27. public void updateFromUIDL(UIDL uidl, final ApplicationConnection client) {
  28. rendering = true;
  29. if (client.updateComponent(this, uidl, true)) {
  30. rendering = false;
  31. return;
  32. }
  33. this.client = client;
  34. final UIDL child = uidl.getChildUIDL(0);
  35. if (child != null) {
  36. final Paintable p = client.getPaintable(child);
  37. if (p != getWidget()) {
  38. if (getWidget() != null) {
  39. client.unregisterPaintable((Paintable) getWidget());
  40. clear();
  41. }
  42. setWidget((Widget) p);
  43. }
  44. p.updateFromUIDL(child, client);
  45. }
  46. boolean updateDynamicSize = updateDynamicSize();
  47. if (updateDynamicSize) {
  48. DeferredCommand.addCommand(new Command() {
  49. public void execute() {
  50. // FIXME deferred relative size update needed to fix some
  51. // scrollbar issues in sampler. This must be the wrong way
  52. // to do it. Might be that some other component is broken.
  53. client.handleComponentRelativeSize(VCustomComponent.this);
  54. }
  55. });
  56. }
  57. renderSpace.setWidth(getElement().getOffsetWidth());
  58. renderSpace.setHeight(getElement().getOffsetHeight());
  59. rendering = false;
  60. }
  61. private boolean updateDynamicSize() {
  62. boolean updated = false;
  63. if (isDynamicWidth()) {
  64. int childWidth = Util.getRequiredWidth(getWidget());
  65. getElement().getStyle().setPropertyPx("width", childWidth);
  66. updated = true;
  67. }
  68. if (isDynamicHeight()) {
  69. int childHeight = Util.getRequiredHeight(getWidget());
  70. getElement().getStyle().setPropertyPx("height", childHeight);
  71. updated = true;
  72. }
  73. return updated;
  74. }
  75. private boolean isDynamicWidth() {
  76. return width == null || width.equals("");
  77. }
  78. private boolean isDynamicHeight() {
  79. return height == null || height.equals("");
  80. }
  81. public boolean hasChildComponent(Widget component) {
  82. if (getWidget() == component) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87. }
  88. public void replaceChildComponent(Widget oldComponent, Widget newComponent) {
  89. if (hasChildComponent(oldComponent)) {
  90. clear();
  91. setWidget(newComponent);
  92. } else {
  93. throw new IllegalStateException();
  94. }
  95. }
  96. public void updateCaption(Paintable component, UIDL uidl) {
  97. // NOP, custom component dont render composition roots caption
  98. }
  99. public boolean requestLayout(Set<Paintable> child) {
  100. return !updateDynamicSize();
  101. }
  102. public RenderSpace getAllocatedSpace(Widget child) {
  103. return renderSpace;
  104. }
  105. @Override
  106. public void setHeight(String height) {
  107. super.setHeight(height);
  108. renderSpace.setHeight(getElement().getOffsetHeight());
  109. if (!height.equals(this.height)) {
  110. this.height = height;
  111. if (!rendering) {
  112. client.runDescendentsLayout(this);
  113. }
  114. }
  115. }
  116. @Override
  117. public void setWidth(String width) {
  118. super.setWidth(width);
  119. renderSpace.setWidth(getElement().getOffsetWidth());
  120. if (!width.equals(this.width)) {
  121. this.width = width;
  122. if (!rendering) {
  123. client.runDescendentsLayout(this);
  124. }
  125. }
  126. }
  127. }