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.

Composite.java 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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.ui;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.Objects;
  20. import com.vaadin.server.ErrorMessage;
  21. import com.vaadin.server.Resource;
  22. import com.vaadin.server.SerializableFunction;
  23. import com.vaadin.shared.ui.ContentMode;
  24. import com.vaadin.shared.ui.composite.CompositeState;
  25. /**
  26. * Composite allows creating new UI components by composition of existing
  27. * server-side components.
  28. * <p>
  29. * A composite is created by extending the Composite class and setting the
  30. * composition root component using {@link #setCompositionRoot(Component)}.
  31. * </p>
  32. * <p>
  33. * The composition root itself can contain more components. The advantage of
  34. * wrapping it in a composite is that the details of the composition root, such
  35. * as its public API, are hidden from the users of the composite.
  36. * </p>
  37. * <p>
  38. * A composite itself does not contribute to the DOM in any way (contrary to
  39. * {@link CustomComponent} which adds a {@code <div>} to the DOM.
  40. * </p>
  41. *
  42. * @author Vaadin Ltd.
  43. * @since 8.1
  44. */
  45. public class Composite extends AbstractComponent implements HasComponents {
  46. /**
  47. * The contained component.
  48. */
  49. private Component root = null;
  50. /**
  51. * Constructs a new empty composite.
  52. * <p>
  53. * Use {@link #setCompositionRoot(Component)} to define the contents of the
  54. * composite.
  55. */
  56. public Composite() {
  57. }
  58. /**
  59. * Constructs a new composite containing the given component.
  60. *
  61. * @param compositionRoot
  62. * the root of the composition component tree. It must not be
  63. * null.
  64. */
  65. public Composite(AbstractComponent compositionRoot) {
  66. this();
  67. Objects.requireNonNull(compositionRoot);
  68. setCompositionRoot(compositionRoot);
  69. }
  70. /**
  71. * Returns the composition root.
  72. *
  73. * @return the Component Composition root.
  74. */
  75. protected Component getCompositionRoot() {
  76. return root;
  77. }
  78. /**
  79. * Sets the component contained in the composite.
  80. * <p>
  81. * You must set the composition root to a non-null value before the
  82. * component can be used. It cannot be changed.
  83. * </p>
  84. *
  85. * @param compositionRoot
  86. * the root of the composition component tree.
  87. */
  88. protected void setCompositionRoot(Component compositionRoot) {
  89. if (root != null) {
  90. throw new IllegalStateException(
  91. "Composition root cannot be changed");
  92. }
  93. if (compositionRoot == null) {
  94. throw new IllegalArgumentException(
  95. "Composition root cannot be null");
  96. }
  97. // set new component
  98. if (compositionRoot.getParent() != null) {
  99. // If the component already has a parent, try to remove it
  100. AbstractSingleComponentContainer.removeFromParent(compositionRoot);
  101. }
  102. compositionRoot.setParent(this);
  103. root = compositionRoot;
  104. markAsDirty();
  105. }
  106. /* Basic component features ------------------------------------------ */
  107. @Override
  108. public Iterator<Component> iterator() {
  109. if (getCompositionRoot() != null) {
  110. return Collections.singletonList(getCompositionRoot()).iterator();
  111. } else {
  112. return Collections.<Component>emptyList().iterator();
  113. }
  114. }
  115. @Override
  116. protected CompositeState getState() {
  117. return (CompositeState) super.getState();
  118. }
  119. @Override
  120. protected CompositeState getState(boolean markAsDirty) {
  121. return (CompositeState) super.getState(markAsDirty);
  122. }
  123. @Override
  124. public void beforeClientResponse(boolean initial) {
  125. super.beforeClientResponse(initial);
  126. if (getCompositionRoot() == null) {
  127. throw new IllegalStateException(
  128. "A composite must always have a composition root");
  129. }
  130. }
  131. @Override
  132. public String getStyleName() {
  133. Component root = getCompositionRoot();
  134. return root == null ? "" : root.getStyleName();
  135. }
  136. @Override
  137. public void setStyleName(String style) {
  138. getRootOrThrow().setStyleName(style);
  139. }
  140. @Override
  141. public void setStyleName(String style, boolean add) {
  142. getRootAbstractComponentOrThrow().setStyleName(style, add);
  143. }
  144. @Override
  145. public void addStyleName(String style) {
  146. getRootOrThrow().addStyleName(style);
  147. }
  148. @Override
  149. public void removeStyleName(String style) {
  150. getRootOrThrow().removeStyleName(style);
  151. }
  152. @Override
  153. public String getPrimaryStyleName() {
  154. return getRootAbstractComponentPropertyOrNull(AbstractComponent::getPrimaryStyleName);
  155. }
  156. @Override
  157. public void setPrimaryStyleName(String style) {
  158. getRootOrThrow().setPrimaryStyleName(style);
  159. }
  160. private Component getRootOrThrow() {
  161. Component root = getCompositionRoot();
  162. if(root == null) throw new IllegalStateException("Composition root has not been set");
  163. return root;
  164. }
  165. private AbstractComponent getRootAbstractComponentOrThrow() {
  166. Component root = getRootOrThrow();
  167. if (!(root instanceof AbstractComponent)) {
  168. throw new IllegalStateException("Composition root is not AbstractComponent");
  169. }
  170. return (AbstractComponent) root;
  171. }
  172. private <T> T getRootPropertyOrNull(SerializableFunction<Component, T> getter) {
  173. Component root = getCompositionRoot();
  174. return root == null ? null : getter.apply(root);
  175. }
  176. private <T> T getRootAbstractComponentPropertyOrNull(SerializableFunction<AbstractComponent, T> getter) {
  177. Component root = getCompositionRoot();
  178. if(root instanceof AbstractComponent) {
  179. return getter.apply((AbstractComponent) root);
  180. }
  181. return null;
  182. }
  183. @Override
  184. public float getWidth() {
  185. return getRootOrThrow().getWidth();
  186. }
  187. @Override
  188. public float getHeight() {
  189. return getRootOrThrow().getHeight();
  190. }
  191. @Override
  192. public Unit getWidthUnits() {
  193. return getRootOrThrow().getWidthUnits();
  194. }
  195. @Override
  196. public Unit getHeightUnits() {
  197. return getRootOrThrow().getHeightUnits();
  198. }
  199. @Override
  200. public void setHeight(String height) {
  201. getRootOrThrow().setHeight(height);
  202. }
  203. @Override
  204. public void setWidth(float width, Unit unit) {
  205. getRootOrThrow().setWidth(width, unit);
  206. }
  207. @Override
  208. public void setHeight(float height, Unit unit) {
  209. getRootOrThrow().setHeight(height, unit);
  210. }
  211. @Override
  212. public void setWidth(String width) {
  213. getRootOrThrow().setWidth(width);
  214. }
  215. @Override
  216. public void setSizeFull() {
  217. getRootOrThrow().setSizeFull();
  218. }
  219. @Override
  220. public void setSizeUndefined() {
  221. getRootOrThrow().setSizeUndefined();
  222. }
  223. @Override
  224. public void setWidthUndefined() {
  225. getRootOrThrow().setWidthUndefined();
  226. }
  227. @Override
  228. public void setHeightUndefined() {
  229. getRootOrThrow().setHeightUndefined();
  230. }
  231. @Override
  232. public void setId(String id) {
  233. getRootOrThrow().setId(id);
  234. }
  235. @Override
  236. public String getId() {
  237. return getRootPropertyOrNull(Component::getId);
  238. }
  239. @Override
  240. public void setDebugId(String id) {
  241. getRootAbstractComponentOrThrow().setDebugId(id);
  242. }
  243. @Override
  244. public String getDebugId() {
  245. return getRootAbstractComponentPropertyOrNull(AbstractComponent::getDebugId);
  246. }
  247. @Override
  248. public String getCaption() {
  249. return getRootPropertyOrNull(Component::getCaption);
  250. }
  251. @Override
  252. public void setCaption(String caption) {
  253. getRootOrThrow().setCaption(caption);
  254. }
  255. @Override
  256. public void setCaptionAsHtml(boolean captionAsHtml) {
  257. getRootAbstractComponentOrThrow().setCaptionAsHtml(captionAsHtml);
  258. }
  259. @Override
  260. public boolean isCaptionAsHtml() {
  261. return getRootAbstractComponentPropertyOrNull(AbstractComponent::isCaptionAsHtml);
  262. }
  263. @Override
  264. public Resource getIcon() {
  265. return getRootPropertyOrNull(Component::getIcon);
  266. }
  267. @Override
  268. public void setIcon(Resource icon) {
  269. getRootOrThrow().setIcon(icon);
  270. }
  271. @Override
  272. public String getDescription() {
  273. return getRootOrThrow().getDescription();
  274. }
  275. @Override
  276. public void setDescription(String description) {
  277. getRootAbstractComponentOrThrow().setDescription(description);
  278. }
  279. @Override
  280. public void setDescription(String description, ContentMode mode) {
  281. getRootAbstractComponentOrThrow().setDescription(description, mode);
  282. }
  283. @Override
  284. public ErrorMessage getErrorMessage() {
  285. return getRootAbstractComponentPropertyOrNull(AbstractComponent::getErrorMessage);
  286. }
  287. @Override
  288. public ErrorMessage getComponentError() {
  289. return getRootAbstractComponentPropertyOrNull(AbstractComponent::getComponentError);
  290. }
  291. @Override
  292. public void setComponentError(ErrorMessage componentError) {
  293. getRootAbstractComponentOrThrow().setComponentError(componentError);
  294. }
  295. }