Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CustomLayout.java 9.3KB

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