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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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(VCustomComponent.class)
  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. * @return the component type.
  116. */
  117. public String getComponentType() {
  118. return componentType;
  119. }
  120. /**
  121. * Sets the component type.
  122. *
  123. * The component type is textual type of the component. This is included in
  124. * the UIDL as component tag attribute.
  125. *
  126. * @param componentType
  127. * the componentType to set.
  128. */
  129. public void setComponentType(String componentType) {
  130. this.componentType = componentType;
  131. }
  132. @Override
  133. public String getTag() {
  134. return "customcomponent";
  135. }
  136. private class ComponentIterator implements Iterator, Serializable {
  137. boolean first = getCompositionRoot() != null;
  138. public boolean hasNext() {
  139. return first;
  140. }
  141. public Object next() {
  142. first = false;
  143. return root;
  144. }
  145. public void remove() {
  146. throw new UnsupportedOperationException();
  147. }
  148. }
  149. @SuppressWarnings("unchecked")
  150. public Iterator getComponentIterator() {
  151. return new ComponentIterator();
  152. }
  153. /**
  154. * This method is not supported by CustomComponent.
  155. *
  156. * @see com.vaadin.ui.ComponentContainer#replaceComponent(com.vaadin.ui.Component,
  157. * com.vaadin.ui.Component)
  158. */
  159. public void replaceComponent(Component oldComponent, Component newComponent) {
  160. throw new UnsupportedOperationException();
  161. }
  162. /**
  163. * This method is not supported by CustomComponent. Use
  164. * {@link CustomComponent#setCompositionRoot(Component)} to set
  165. * CustomComponents "child".
  166. *
  167. * @see com.vaadin.ui.AbstractComponentContainer#addComponent(com.vaadin.ui.Component)
  168. */
  169. @Override
  170. public void addComponent(Component c) {
  171. throw new UnsupportedOperationException();
  172. }
  173. /**
  174. * This method is not supported by CustomComponent.
  175. *
  176. * @see com.vaadin.ui.AbstractComponentContainer#moveComponentsFrom(com.vaadin.ui.ComponentContainer)
  177. */
  178. @Override
  179. public void moveComponentsFrom(ComponentContainer source) {
  180. throw new UnsupportedOperationException();
  181. }
  182. /**
  183. * This method is not supported by CustomComponent.
  184. *
  185. * @see com.vaadin.ui.AbstractComponentContainer#removeAllComponents()
  186. */
  187. @Override
  188. public void removeAllComponents() {
  189. throw new UnsupportedOperationException();
  190. }
  191. /**
  192. * This method is not supported by CustomComponent.
  193. *
  194. * @see com.vaadin.ui.AbstractComponentContainer#removeComponent(com.vaadin.ui.Component)
  195. */
  196. @Override
  197. public void removeComponent(Component c) {
  198. throw new UnsupportedOperationException();
  199. }
  200. }