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.

CustomComponent.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.Serializable;
  6. import java.util.Iterator;
  7. import com.vaadin.terminal.PaintException;
  8. import com.vaadin.terminal.PaintTarget;
  9. import com.vaadin.terminal.gwt.client.ui.VCustomComponent;
  10. /**
  11. * Custom component provides simple implementation of Component interface for
  12. * creation of new UI components by composition of existing components.
  13. * <p>
  14. * The component is used by inheriting the CustomComponent class and setting
  15. * composite root inside the Custom component. The composite root itself can
  16. * contain more components, but their interfaces are hidden from the users.
  17. * </p>
  18. *
  19. * @author IT Mill Ltd.
  20. * @version
  21. * @VERSION@
  22. * @since 3.0
  23. */
  24. @SuppressWarnings("serial")
  25. @ClientWidget(value = VCustomComponent.class, lazyLoad = false)
  26. public class CustomComponent extends AbstractComponentContainer {
  27. /**
  28. * The root component implementing the custom component.
  29. */
  30. private Component root = null;
  31. /**
  32. * Type of the component.
  33. */
  34. private String componentType = null;
  35. /**
  36. * Constructs a new custom component.
  37. *
  38. * <p>
  39. * The component is implemented by wrapping the methods of the composition
  40. * root component given as parameter. The composition root must be set
  41. * before the component can be used.
  42. * </p>
  43. */
  44. public CustomComponent() {
  45. // expand horizontally by default
  46. setWidth(100, UNITS_PERCENTAGE);
  47. }
  48. /**
  49. * Constructs a new custom component.
  50. *
  51. * <p>
  52. * The component is implemented by wrapping the methods of the composition
  53. * root component given as parameter. The composition root must not be null
  54. * and can not be changed after the composition.
  55. * </p>
  56. *
  57. * @param compositionRoot
  58. * the root of the composition component tree.
  59. */
  60. public CustomComponent(Component compositionRoot) {
  61. this();
  62. setCompositionRoot(compositionRoot);
  63. }
  64. /**
  65. * Returns the composition root.
  66. *
  67. * @return the Component Composition root.
  68. */
  69. protected final Component getCompositionRoot() {
  70. return root;
  71. }
  72. /**
  73. * Sets the compositions root.
  74. * <p>
  75. * The composition root must be set to non-null value before the component
  76. * can be used. The composition root can only be set once.
  77. * </p>
  78. *
  79. * @param compositionRoot
  80. * the root of the composition component tree.
  81. */
  82. protected final void setCompositionRoot(Component compositionRoot) {
  83. if (compositionRoot != root) {
  84. if (root != null) {
  85. // remove old component
  86. super.removeComponent(root);
  87. }
  88. if (compositionRoot != null) {
  89. // set new component
  90. super.addComponent(compositionRoot);
  91. }
  92. root = compositionRoot;
  93. requestRepaint();
  94. }
  95. }
  96. /* Basic component features ------------------------------------------ */
  97. @Override
  98. public void paintContent(PaintTarget target) throws PaintException {
  99. if (root == null) {
  100. throw new IllegalStateException("Composition root must be set to"
  101. + " non-null value before the " + getClass().getName()
  102. + " can be painted");
  103. }
  104. if (getComponentType() != null) {
  105. target.addAttribute("type", getComponentType());
  106. }
  107. root.paint(target);
  108. }
  109. /**
  110. * Gets the component type.
  111. *
  112. * The component type is textual type of the component. This is included in
  113. * the UIDL as component tag attribute.
  114. *
  115. * @deprecated not more useful as the whole tag system has been removed
  116. *
  117. * @return the component type.
  118. */
  119. @Deprecated
  120. public String getComponentType() {
  121. return componentType;
  122. }
  123. /**
  124. * Sets the component type.
  125. *
  126. * The component type is textual type of the component. This is included in
  127. * the UIDL as component tag attribute.
  128. *
  129. * @deprecated not more useful as the whole tag system has been removed
  130. *
  131. * @param componentType
  132. * the componentType to set.
  133. */
  134. @Deprecated
  135. public void setComponentType(String componentType) {
  136. this.componentType = componentType;
  137. }
  138. private class ComponentIterator implements Iterator<Component>,
  139. Serializable {
  140. boolean first = getCompositionRoot() != null;
  141. public boolean hasNext() {
  142. return first;
  143. }
  144. public Component next() {
  145. first = false;
  146. return root;
  147. }
  148. public void remove() {
  149. throw new UnsupportedOperationException();
  150. }
  151. }
  152. public Iterator<Component> getComponentIterator() {
  153. return new ComponentIterator();
  154. }
  155. /**
  156. * This method is not supported by CustomComponent.
  157. *
  158. * @see com.vaadin.ui.ComponentContainer#replaceComponent(com.vaadin.ui.Component,
  159. * com.vaadin.ui.Component)
  160. */
  161. public void replaceComponent(Component oldComponent, Component newComponent) {
  162. throw new UnsupportedOperationException();
  163. }
  164. /**
  165. * This method is not supported by CustomComponent. Use
  166. * {@link CustomComponent#setCompositionRoot(Component)} to set
  167. * CustomComponents "child".
  168. *
  169. * @see com.vaadin.ui.AbstractComponentContainer#addComponent(com.vaadin.ui.Component)
  170. */
  171. @Override
  172. public void addComponent(Component c) {
  173. throw new UnsupportedOperationException();
  174. }
  175. /**
  176. * This method is not supported by CustomComponent.
  177. *
  178. * @see com.vaadin.ui.AbstractComponentContainer#moveComponentsFrom(com.vaadin.ui.ComponentContainer)
  179. */
  180. @Override
  181. public void moveComponentsFrom(ComponentContainer source) {
  182. throw new UnsupportedOperationException();
  183. }
  184. /**
  185. * This method is not supported by CustomComponent.
  186. *
  187. * @see com.vaadin.ui.AbstractComponentContainer#removeAllComponents()
  188. */
  189. @Override
  190. public void removeAllComponents() {
  191. throw new UnsupportedOperationException();
  192. }
  193. /**
  194. * This method is not supported by CustomComponent.
  195. *
  196. * @see com.vaadin.ui.AbstractComponentContainer#removeComponent(com.vaadin.ui.Component)
  197. */
  198. @Override
  199. public void removeComponent(Component c) {
  200. throw new UnsupportedOperationException();
  201. }
  202. }