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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 java.util.Optional;
  21. import com.vaadin.server.ErrorMessage;
  22. import com.vaadin.server.Resource;
  23. import com.vaadin.shared.composite.CompositeState;
  24. import com.vaadin.shared.ui.ContentMode;
  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
  44. */
  45. public class Composite extends AbstractComponent implements HasComponents {
  46. private static final String COMPOSITE_HAS_NO_DOM_OR_WIDGET = "A composite has no DOM or widget";
  47. /**
  48. * The contained component.
  49. */
  50. private Component root = null;
  51. /**
  52. * Constructs a new empty composite.
  53. * <p>
  54. * Use {@link #setCompositionRoot(Component)} to define the contents of the
  55. * composite.
  56. */
  57. public Composite() {
  58. }
  59. /**
  60. * Constructs a new composite containing the given component.
  61. *
  62. * @param compositionRoot
  63. * the root of the composition component tree. It must not be
  64. * null.
  65. */
  66. public Composite(AbstractComponent compositionRoot) {
  67. this();
  68. Objects.requireNonNull(compositionRoot);
  69. setCompositionRoot(compositionRoot);
  70. }
  71. /**
  72. * Returns the composition root.
  73. *
  74. * @return the Component Composition root.
  75. */
  76. protected Component getCompositionRoot() {
  77. return root;
  78. }
  79. /**
  80. * Sets the component contained in the composite.
  81. * <p>
  82. * You must set the composition root to a non-null value before the
  83. * component can be used. It cannot be changed.
  84. * </p>
  85. *
  86. * @param compositionRoot
  87. * the root of the composition component tree.
  88. */
  89. protected void setCompositionRoot(Component compositionRoot) {
  90. if (root != null) {
  91. throw new IllegalStateException(
  92. "Composition root cannot be changed");
  93. }
  94. if (compositionRoot == null) {
  95. throw new IllegalArgumentException(
  96. "Composition root cannot be null");
  97. }
  98. // set new component
  99. if (compositionRoot.getParent() != null) {
  100. // If the component already has a parent, try to remove it
  101. AbstractSingleComponentContainer.removeFromParent(compositionRoot);
  102. }
  103. compositionRoot.setParent(this);
  104. root = compositionRoot;
  105. markAsDirty();
  106. }
  107. /* Basic component features ------------------------------------------ */
  108. @Override
  109. public Iterator<Component> iterator() {
  110. if (getCompositionRoot() != null) {
  111. return Collections.singletonList(getCompositionRoot()).iterator();
  112. } else {
  113. return Collections.<Component> emptyList().iterator();
  114. }
  115. }
  116. /**
  117. * Gets the number of contained components.
  118. *
  119. * @return the number of contained components (zero or one)
  120. */
  121. public int getComponentCount() {
  122. return (getCompositionRoot() != null ? 1 : 0);
  123. }
  124. @Override
  125. protected CompositeState getState() {
  126. return (CompositeState) super.getState();
  127. }
  128. @Override
  129. protected CompositeState getState(boolean markAsDirty) {
  130. return (CompositeState) super.getState(markAsDirty);
  131. }
  132. @Override
  133. public void beforeClientResponse(boolean initial) {
  134. super.beforeClientResponse(initial);
  135. if (getComponentCount() != 1) {
  136. throw new IllegalStateException(
  137. "A composite must always have a composition root");
  138. }
  139. }
  140. @Override
  141. public String getStyleName() {
  142. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  143. }
  144. @Override
  145. public void setStyleName(String style) {
  146. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  147. }
  148. @Override
  149. public void setStyleName(String style, boolean add) {
  150. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  151. }
  152. @Override
  153. public void addStyleName(String style) {
  154. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  155. }
  156. @Override
  157. public void removeStyleName(String style) {
  158. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  159. }
  160. @Override
  161. public String getPrimaryStyleName() {
  162. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  163. }
  164. @Override
  165. public void setPrimaryStyleName(String style) {
  166. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  167. }
  168. private Component getRootOrThrow() {
  169. return Optional.ofNullable(getCompositionRoot())
  170. .orElseThrow(() -> new IllegalStateException(
  171. "Composition root has not been set"));
  172. }
  173. @Override
  174. public float getWidth() {
  175. return getRootOrThrow().getWidth();
  176. }
  177. @Override
  178. public float getHeight() {
  179. return getRootOrThrow().getHeight();
  180. }
  181. @Override
  182. public Unit getWidthUnits() {
  183. return getRootOrThrow().getWidthUnits();
  184. }
  185. @Override
  186. public Unit getHeightUnits() {
  187. return getRootOrThrow().getHeightUnits();
  188. }
  189. @Override
  190. public void setHeight(String height) {
  191. getRootOrThrow().setHeight(height);
  192. }
  193. @Override
  194. public void setWidth(float width, Unit unit) {
  195. getRootOrThrow().setWidth(width, unit);
  196. }
  197. @Override
  198. public void setHeight(float height, Unit unit) {
  199. getRootOrThrow().setHeight(height, unit);
  200. }
  201. @Override
  202. public void setWidth(String width) {
  203. getRootOrThrow().setWidth(width);
  204. }
  205. @Override
  206. public void setSizeFull() {
  207. getRootOrThrow().setSizeFull();
  208. }
  209. @Override
  210. public void setSizeUndefined() {
  211. getRootOrThrow().setSizeUndefined();
  212. }
  213. @Override
  214. public void setWidthUndefined() {
  215. getRootOrThrow().setWidthUndefined();
  216. }
  217. @Override
  218. public void setHeightUndefined() {
  219. getRootOrThrow().setHeightUndefined();
  220. }
  221. @Override
  222. public void setId(String id) {
  223. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  224. }
  225. @Override
  226. public String getId() {
  227. // Design.read relies on being able to call this
  228. return null;
  229. }
  230. @Override
  231. public void setDebugId(String id) {
  232. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  233. }
  234. @Override
  235. public String getDebugId() {
  236. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  237. }
  238. @Override
  239. public String getCaption() {
  240. // Design.read relies on being able to call this
  241. return null;
  242. }
  243. @Override
  244. public void setCaption(String caption) {
  245. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  246. }
  247. @Override
  248. public void setCaptionAsHtml(boolean captionAsHtml) {
  249. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  250. }
  251. @Override
  252. public boolean isCaptionAsHtml() {
  253. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  254. }
  255. @Override
  256. public Resource getIcon() {
  257. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  258. }
  259. @Override
  260. public void setIcon(Resource icon) {
  261. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  262. }
  263. @Override
  264. public String getDescription() {
  265. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  266. }
  267. @Override
  268. public void setDescription(String description) {
  269. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  270. }
  271. @Override
  272. public void setDescription(String description, ContentMode mode) {
  273. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  274. }
  275. @Override
  276. public ErrorMessage getErrorMessage() {
  277. return null;
  278. }
  279. @Override
  280. public ErrorMessage getComponentError() {
  281. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  282. }
  283. @Override
  284. public void setComponentError(ErrorMessage componentError) {
  285. throw new UnsupportedOperationException(COMPOSITE_HAS_NO_DOM_OR_WIDGET);
  286. }
  287. }