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.

CustomLayout.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.util.HashMap;
  9. import java.util.Iterator;
  10. import com.vaadin.terminal.PaintException;
  11. import com.vaadin.terminal.PaintTarget;
  12. import com.vaadin.terminal.gwt.client.ui.VCustomLayout;
  13. /**
  14. * <p>
  15. * A container component with freely designed layout and style. The layout
  16. * consists of items with textually represented locations. Each item contains
  17. * one sub-component, which can be any Vaadin component, such as a layout. The
  18. * adapter and theme are responsible for rendering the layout with a given style
  19. * by placing the items in the defined locations.
  20. * </p>
  21. *
  22. * <p>
  23. * The placement of the locations is not fixed - different themes can define the
  24. * locations in a way that is suitable for them. One typical example would be to
  25. * create visual design for a web site as a custom layout: the visual design
  26. * would define locations for "menu", "body", and "title", for example. The
  27. * layout would then be implemented as an XHTML template for each theme.
  28. * </p>
  29. *
  30. * <p>
  31. * The default theme handles the styles that are not defined by drawing the
  32. * subcomponents just as in OrderedLayout.
  33. * </p>
  34. *
  35. * @author IT Mill Ltd.
  36. * @version
  37. * @VERSION@
  38. * @since 3.0
  39. */
  40. @SuppressWarnings("serial")
  41. @ClientWidget(VCustomLayout.class)
  42. public class CustomLayout extends AbstractLayout {
  43. private static final int BUFFER_SIZE = 10000;
  44. /**
  45. * Custom layout slots containing the components.
  46. */
  47. private final HashMap slots = new HashMap();
  48. private String templateContents = null;
  49. private String templateName = null;
  50. /**
  51. * Constructs a custom layout with the template given in the stream.
  52. *
  53. * @param templateStream
  54. * Stream containing template data. Must be using UTF-8 encoding.
  55. * To use a String as a template use for instance new
  56. * ByteArrayInputStream("<template>".getBytes()).
  57. * @param streamLength
  58. * Length of the templateStream
  59. * @throws IOException
  60. */
  61. public CustomLayout(InputStream templateStream) throws IOException {
  62. InputStreamReader reader = new InputStreamReader(templateStream,
  63. "UTF-8");
  64. StringBuffer b = new StringBuffer(BUFFER_SIZE);
  65. char[] cbuf = new char[BUFFER_SIZE];
  66. int offset = 0;
  67. while (true) {
  68. int nrRead = reader.read(cbuf, offset, BUFFER_SIZE);
  69. b.append(cbuf, 0, nrRead);
  70. if (nrRead < BUFFER_SIZE) {
  71. break;
  72. }
  73. }
  74. templateContents = b.toString();
  75. setWidth(100, UNITS_PERCENTAGE);
  76. }
  77. /**
  78. * Constructor for custom layout with given template name. Template file is
  79. * fetched from "<theme>/layout/<templateName>".
  80. */
  81. public CustomLayout(String template) {
  82. templateName = template;
  83. setWidth(100, UNITS_PERCENTAGE);
  84. }
  85. /**
  86. * Gets the component UIDL tag.
  87. *
  88. * @return the Component UIDL tag as string.
  89. */
  90. @Override
  91. public String getTag() {
  92. return "customlayout";
  93. }
  94. /**
  95. * Adds the component into this container to given location. If the location
  96. * is already populated, the old component is removed.
  97. *
  98. * @param c
  99. * the component to be added.
  100. * @param location
  101. * the location of the component.
  102. */
  103. public void addComponent(Component c, String location) {
  104. final Component old = (Component) slots.get(location);
  105. if (old != null) {
  106. removeComponent(old);
  107. }
  108. slots.put(location, c);
  109. c.setParent(this);
  110. fireComponentAttachEvent(c);
  111. requestRepaint();
  112. }
  113. /**
  114. * Adds the component into this container. The component is added without
  115. * specifying the location (empty string is then used as location). Only one
  116. * component can be added to the default "" location and adding more
  117. * components into that location overwrites the old components.
  118. *
  119. * @param c
  120. * the component to be added.
  121. */
  122. @Override
  123. public void addComponent(Component c) {
  124. this.addComponent(c, "");
  125. }
  126. /**
  127. * Removes the component from this container.
  128. *
  129. * @param c
  130. * the component to be removed.
  131. */
  132. @Override
  133. public void removeComponent(Component c) {
  134. if (c == null) {
  135. return;
  136. }
  137. slots.values().remove(c);
  138. c.setParent(null);
  139. fireComponentDetachEvent(c);
  140. requestRepaint();
  141. }
  142. /**
  143. * Removes the component from this container from given location.
  144. *
  145. * @param location
  146. * the Location identifier of the component.
  147. */
  148. public void removeComponent(String location) {
  149. this.removeComponent((Component) slots.get(location));
  150. }
  151. /**
  152. * Gets the component container iterator for going trough all the components
  153. * in the container.
  154. *
  155. * @return the Iterator of the components inside the container.
  156. */
  157. public Iterator getComponentIterator() {
  158. return slots.values().iterator();
  159. }
  160. /**
  161. * Gets the child-component by its location.
  162. *
  163. * @param location
  164. * the name of the location where the requested component
  165. * resides.
  166. * @return the Component in the given location or null if not found.
  167. */
  168. public Component getComponent(String location) {
  169. return (Component) slots.get(location);
  170. }
  171. /**
  172. * Paints the content of this component.
  173. *
  174. * @param target
  175. * @throws PaintException
  176. * if the paint operation failed.
  177. */
  178. @Override
  179. public void paintContent(PaintTarget target) throws PaintException {
  180. super.paintContent(target);
  181. if (templateName != null) {
  182. target.addAttribute("template", templateName);
  183. } else {
  184. target.addAttribute("templateContents", templateContents);
  185. }
  186. // Adds all items in all the locations
  187. for (final Iterator i = slots.keySet().iterator(); i.hasNext();) {
  188. // Gets the (location,component)
  189. final String location = (String) i.next();
  190. final Component c = (Component) slots.get(location);
  191. if (c != null) {
  192. // Writes the item
  193. target.startTag("location");
  194. target.addAttribute("name", location);
  195. c.paint(target);
  196. target.endTag("location");
  197. }
  198. }
  199. }
  200. /* Documented in superclass */
  201. public void replaceComponent(Component oldComponent, Component newComponent) {
  202. // Gets the locations
  203. String oldLocation = null;
  204. String newLocation = null;
  205. for (final Iterator i = slots.keySet().iterator(); i.hasNext();) {
  206. final String location = (String) i.next();
  207. final Component component = (Component) slots.get(location);
  208. if (component == oldComponent) {
  209. oldLocation = location;
  210. }
  211. if (component == newComponent) {
  212. newLocation = location;
  213. }
  214. }
  215. if (oldLocation == null) {
  216. addComponent(newComponent);
  217. } else if (newLocation == null) {
  218. removeComponent(oldLocation);
  219. addComponent(newComponent, oldLocation);
  220. } else {
  221. slots.put(newLocation, oldComponent);
  222. slots.put(oldLocation, newComponent);
  223. requestRepaint();
  224. }
  225. }
  226. /**
  227. * CustomLayout's template selecting was previously implemented with
  228. * setStyle. Overriding to improve backwards compatibility.
  229. *
  230. * @param name
  231. * template name
  232. */
  233. @Override
  234. public void setStyle(String name) {
  235. setTemplateName(name);
  236. }
  237. /** Get the name of the template */
  238. public String getTemplateName() {
  239. return templateName;
  240. }
  241. /**
  242. * Set the name of the template used to draw custom layout.
  243. *
  244. * With GWT-adapter, the template with name 'templatename' is loaded from
  245. * VAADIN/themes/themename/layouts/templatename.html. If the theme has not
  246. * been set (with Application.setTheme()), themename is 'default'.
  247. *
  248. * @param templateName
  249. */
  250. public void setTemplateName(String templateName) {
  251. this.templateName = templateName;
  252. templateContents = null;
  253. requestRepaint();
  254. }
  255. /**
  256. * Although most layouts support margins, CustomLayout does not. The
  257. * behaviour of this layout is determined almost completely by the actual
  258. * template.
  259. *
  260. * @throws UnsupportedOperationException
  261. */
  262. @Override
  263. public void setMargin(boolean enabled) {
  264. throw new UnsupportedOperationException(
  265. "CustomLayout does not support margins.");
  266. }
  267. /**
  268. * Although most layouts support margins, CustomLayout does not. The
  269. * behaviour of this layout is determined almost completely by the actual
  270. * template.
  271. *
  272. * @throws UnsupportedOperationException
  273. */
  274. @Override
  275. public void setMargin(boolean topEnabled, boolean rightEnabled,
  276. boolean bottomEnabled, boolean leftEnabled) {
  277. throw new UnsupportedOperationException(
  278. "CustomLayout does not support margins.");
  279. }
  280. }