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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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(
  155. AbstractComponent::getPrimaryStyleName);
  156. }
  157. @Override
  158. public void setPrimaryStyleName(String style) {
  159. getRootOrThrow().setPrimaryStyleName(style);
  160. }
  161. private Component getRootOrThrow() {
  162. Component root = getCompositionRoot();
  163. if (root == null) {
  164. throw new IllegalStateException(
  165. "Composition root has not been set");
  166. }
  167. return root;
  168. }
  169. private AbstractComponent getRootAbstractComponentOrThrow() {
  170. Component root = getRootOrThrow();
  171. if (!(root instanceof AbstractComponent)) {
  172. throw new IllegalStateException(
  173. "Composition root is not AbstractComponent");
  174. }
  175. return (AbstractComponent) root;
  176. }
  177. private <T> T getRootPropertyOrNull(
  178. SerializableFunction<Component, T> getter) {
  179. Component root = getCompositionRoot();
  180. return root == null ? null : getter.apply(root);
  181. }
  182. private <T> T getRootAbstractComponentPropertyOrNull(
  183. SerializableFunction<AbstractComponent, T> getter) {
  184. Component root = getCompositionRoot();
  185. if (root instanceof AbstractComponent) {
  186. return getter.apply((AbstractComponent) root);
  187. }
  188. return null;
  189. }
  190. @Override
  191. public float getWidth() {
  192. return getRootOrThrow().getWidth();
  193. }
  194. @Override
  195. public float getHeight() {
  196. return getRootOrThrow().getHeight();
  197. }
  198. @Override
  199. public Unit getWidthUnits() {
  200. return getRootOrThrow().getWidthUnits();
  201. }
  202. @Override
  203. public Unit getHeightUnits() {
  204. return getRootOrThrow().getHeightUnits();
  205. }
  206. @Override
  207. public void setHeight(String height) {
  208. getRootOrThrow().setHeight(height);
  209. }
  210. @Override
  211. public void setWidth(float width, Unit unit) {
  212. getRootOrThrow().setWidth(width, unit);
  213. }
  214. @Override
  215. public void setHeight(float height, Unit unit) {
  216. getRootOrThrow().setHeight(height, unit);
  217. }
  218. @Override
  219. public void setWidth(String width) {
  220. getRootOrThrow().setWidth(width);
  221. }
  222. @Override
  223. public void setSizeFull() {
  224. getRootOrThrow().setSizeFull();
  225. }
  226. @Override
  227. public void setSizeUndefined() {
  228. getRootOrThrow().setSizeUndefined();
  229. }
  230. @Override
  231. public void setWidthUndefined() {
  232. getRootOrThrow().setWidthUndefined();
  233. }
  234. @Override
  235. public void setHeightUndefined() {
  236. getRootOrThrow().setHeightUndefined();
  237. }
  238. @Override
  239. public void setId(String id) {
  240. getRootOrThrow().setId(id);
  241. }
  242. @Override
  243. public String getId() {
  244. return getRootPropertyOrNull(Component::getId);
  245. }
  246. @Override
  247. public void setDebugId(String id) {
  248. getRootAbstractComponentOrThrow().setDebugId(id);
  249. }
  250. @Override
  251. public String getDebugId() {
  252. return getRootAbstractComponentPropertyOrNull(
  253. AbstractComponent::getDebugId);
  254. }
  255. @Override
  256. public String getCaption() {
  257. return getRootPropertyOrNull(Component::getCaption);
  258. }
  259. @Override
  260. public void setCaption(String caption) {
  261. getRootOrThrow().setCaption(caption);
  262. }
  263. @Override
  264. public void setCaptionAsHtml(boolean captionAsHtml) {
  265. getRootAbstractComponentOrThrow().setCaptionAsHtml(captionAsHtml);
  266. }
  267. @Override
  268. public boolean isCaptionAsHtml() {
  269. return getRootAbstractComponentPropertyOrNull(
  270. AbstractComponent::isCaptionAsHtml);
  271. }
  272. @Override
  273. public Resource getIcon() {
  274. return getRootPropertyOrNull(Component::getIcon);
  275. }
  276. @Override
  277. public void setIcon(Resource icon) {
  278. getRootOrThrow().setIcon(icon);
  279. }
  280. @Override
  281. public String getDescription() {
  282. return getRootOrThrow().getDescription();
  283. }
  284. @Override
  285. public void setDescription(String description) {
  286. getRootAbstractComponentOrThrow().setDescription(description);
  287. }
  288. @Override
  289. public void setDescription(String description, ContentMode mode) {
  290. getRootAbstractComponentOrThrow().setDescription(description, mode);
  291. }
  292. @Override
  293. public ErrorMessage getErrorMessage() {
  294. return getRootAbstractComponentPropertyOrNull(
  295. AbstractComponent::getErrorMessage);
  296. }
  297. @Override
  298. public ErrorMessage getComponentError() {
  299. return getRootAbstractComponentPropertyOrNull(
  300. AbstractComponent::getComponentError);
  301. }
  302. @Override
  303. public void setComponentError(ErrorMessage componentError) {
  304. getRootAbstractComponentOrThrow().setComponentError(componentError);
  305. }
  306. }