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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * Copyright 2000-2018 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 static java.nio.charset.StandardCharsets.UTF_8;
  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.Collections;
  23. import java.util.HashMap;
  24. import java.util.Iterator;
  25. import java.util.Map;
  26. import java.util.Map.Entry;
  27. import java.util.Set;
  28. import org.jsoup.nodes.Element;
  29. import com.vaadin.server.JsonPaintTarget;
  30. import com.vaadin.server.PaintException;
  31. import com.vaadin.server.PaintTarget;
  32. import com.vaadin.shared.ui.customlayout.CustomLayoutState;
  33. import com.vaadin.ui.declarative.DesignContext;
  34. /**
  35. * <p>
  36. * A container component with freely designed layout and style. The layout
  37. * consists of items with textually represented locations. Each item contains
  38. * one sub-component, which can be any Vaadin component, such as a layout. The
  39. * adapter and theme are responsible for rendering the layout with a given style
  40. * by placing the items in the defined locations.
  41. * </p>
  42. *
  43. * <p>
  44. * The placement of the locations is not fixed - different themes can define the
  45. * locations in a way that is suitable for them. One typical example would be to
  46. * create visual design for a web site as a custom layout: the visual design
  47. * would define locations for "menu", "body", and "title", for example. The
  48. * layout would then be implemented as an HTML template for each theme.
  49. * </p>
  50. *
  51. * <p>
  52. * A location is identified with the attribute "data-location" or "location"
  53. * which has the location name as its value.
  54. * </p>
  55. *
  56. * <p>
  57. * The default theme handles the styles that are not defined by drawing the
  58. * subcomponents just as in OrderedLayout.
  59. * </p>
  60. *
  61. * @author Vaadin Ltd.
  62. * @since 3.0
  63. */
  64. @SuppressWarnings("serial")
  65. public class CustomLayout extends AbstractLayout implements LegacyComponent {
  66. private static final int BUFFER_SIZE = 10000;
  67. /**
  68. * Custom layout slots containing the components.
  69. */
  70. private final Map<String, Component> slots = new HashMap<>();
  71. /**
  72. * Default constructor only used by subclasses. Subclasses are responsible
  73. * for setting the appropriate fields. Either
  74. * {@link #setTemplateName(String)}, that makes layout fetch the template
  75. * from theme, or {@link #setTemplateContents(String)}.
  76. *
  77. * @since 7.5.0
  78. */
  79. public CustomLayout() {
  80. setWidth(100, Unit.PERCENTAGE);
  81. }
  82. /**
  83. * Constructs a custom layout with the template given in the stream.
  84. *
  85. * @param templateStream
  86. * Stream containing template data. Must be using UTF-8 encoding.
  87. * To use a String as a template use for instance new
  88. * ByteArrayInputStream("&lt;template&gt;".getBytes()).
  89. * @throws IOException
  90. */
  91. public CustomLayout(InputStream templateStream) throws IOException {
  92. this();
  93. initTemplateContentsFromInputStream(templateStream);
  94. }
  95. /**
  96. * Constructor for custom layout with given template name. Template file is
  97. * fetched from "&lt;theme&gt;/layouts/&lt;templateName&gt;".
  98. */
  99. public CustomLayout(String template) {
  100. this();
  101. setTemplateName(template);
  102. }
  103. protected void initTemplateContentsFromInputStream(
  104. InputStream templateStream) throws IOException {
  105. BufferedReader reader = new BufferedReader(
  106. new InputStreamReader(templateStream, UTF_8));
  107. StringBuilder builder = new StringBuilder(BUFFER_SIZE);
  108. try {
  109. char[] cbuf = new char[BUFFER_SIZE];
  110. int nRead;
  111. while ((nRead = reader.read(cbuf, 0, BUFFER_SIZE)) > 0) {
  112. builder.append(cbuf, 0, nRead);
  113. }
  114. } finally {
  115. reader.close();
  116. }
  117. setTemplateContents(builder.toString());
  118. }
  119. @Override
  120. protected CustomLayoutState getState() {
  121. return (CustomLayoutState) super.getState();
  122. }
  123. @Override
  124. protected CustomLayoutState getState(boolean markAsDirty) {
  125. return (CustomLayoutState) super.getState(markAsDirty);
  126. }
  127. /**
  128. * Adds the component into this container to given location. If the location
  129. * is already populated, the old component is removed.
  130. *
  131. * @param c
  132. * the component to be added.
  133. * @param location
  134. * the location of the component.
  135. */
  136. public void addComponent(Component c, String location) {
  137. final Component old = slots.get(location);
  138. if (old != null) {
  139. removeComponent(old);
  140. }
  141. slots.put(location, c);
  142. getState().childLocations.put(c, location);
  143. super.addComponent(c);
  144. }
  145. /**
  146. * Adds the component into this container. The component is added without
  147. * specifying the location (empty string is then used as location). Only one
  148. * component can be added to the default "" location and adding more
  149. * components into that location overwrites the old components.
  150. *
  151. * @param c
  152. * the component to be added.
  153. */
  154. @Override
  155. public void addComponent(Component c) {
  156. this.addComponent(c, "");
  157. }
  158. /**
  159. * Removes the component from this container.
  160. *
  161. * @param c
  162. * the component to be removed.
  163. */
  164. @Override
  165. public void removeComponent(Component c) {
  166. if (c == null) {
  167. return;
  168. }
  169. slots.values().remove(c);
  170. getState().childLocations.remove(c);
  171. super.removeComponent(c);
  172. }
  173. /**
  174. * Removes the component from this container from given location.
  175. *
  176. * @param location
  177. * the Location identifier of the component.
  178. */
  179. public void removeComponent(String location) {
  180. this.removeComponent(slots.get(location));
  181. }
  182. /**
  183. * Gets the component container iterator for going trough all the components
  184. * in the container.
  185. *
  186. * @return the Iterator of the components inside the container.
  187. */
  188. @Override
  189. public Iterator<Component> iterator() {
  190. return Collections.unmodifiableCollection(slots.values()).iterator();
  191. }
  192. /**
  193. * Gets the number of contained components. Consistent with the iterator
  194. * returned by {@link #getComponentIterator()}.
  195. *
  196. * @return the number of contained components
  197. */
  198. @Override
  199. public int getComponentCount() {
  200. return slots.values().size();
  201. }
  202. /**
  203. * Gets the child-component by its location.
  204. *
  205. * @param location
  206. * the name of the location where the requested component
  207. * resides.
  208. * @return the Component in the given location or null if not found.
  209. */
  210. public Component getComponent(String location) {
  211. return slots.get(location);
  212. }
  213. /* Documented in superclass */
  214. @Override
  215. public void replaceComponent(Component oldComponent,
  216. Component newComponent) {
  217. // Gets the locations
  218. String oldLocation = null;
  219. String newLocation = null;
  220. for (final String location : slots.keySet()) {
  221. final Component component = slots.get(location);
  222. if (component == oldComponent) {
  223. oldLocation = location;
  224. }
  225. if (component == newComponent) {
  226. newLocation = location;
  227. }
  228. }
  229. if (oldLocation == null) {
  230. addComponent(newComponent);
  231. } else if (newLocation == null) {
  232. removeComponent(oldLocation);
  233. addComponent(newComponent, oldLocation);
  234. } else {
  235. slots.put(newLocation, oldComponent);
  236. slots.put(oldLocation, newComponent);
  237. getState().childLocations.put(newComponent, oldLocation);
  238. getState().childLocations.put(oldComponent, newLocation);
  239. }
  240. }
  241. /** Get the name of the template. */
  242. public String getTemplateName() {
  243. return getState(false).templateName;
  244. }
  245. /** Get the contents of the template. */
  246. public String getTemplateContents() {
  247. return getState(false).templateContents;
  248. }
  249. /**
  250. * Set the name of the template used to draw custom layout.
  251. *
  252. * With GWT-adapter, the template with name 'templatename' is loaded from
  253. * VAADIN/themes/themename/layouts/templatename.html. If the theme has not
  254. * been set (with Application.setTheme()), themename is 'default'.
  255. *
  256. * @param templateName
  257. */
  258. public void setTemplateName(String templateName) {
  259. getState().templateName = templateName;
  260. getState().templateContents = null;
  261. }
  262. /**
  263. * Set the contents of the template used to draw the custom layout.
  264. *
  265. * Note: setTemplateContents can be applied only before CustomLayout
  266. * instance has been attached.
  267. *
  268. * @param templateContents
  269. */
  270. public void setTemplateContents(String templateContents) {
  271. getState().templateContents = templateContents;
  272. getState().templateName = null;
  273. }
  274. @Override
  275. public void changeVariables(Object source, Map<String, Object> variables) {
  276. // Nothing to see here
  277. }
  278. @Override
  279. public void paintContent(PaintTarget target) throws PaintException {
  280. // Workaround to make the CommunicationManager read the template file
  281. // and send it to the client
  282. String templateName = getState(false).templateName;
  283. if (templateName != null && !templateName.isEmpty()) {
  284. Set<Object> usedResources = ((JsonPaintTarget) target)
  285. .getUsedResources();
  286. String resourceName = "layouts/" + templateName + ".html";
  287. usedResources.add(resourceName);
  288. }
  289. }
  290. @Override
  291. public void readDesign(Element design, DesignContext designContext) {
  292. super.readDesign(design, designContext);
  293. for (Element child : design.children()) {
  294. Component childComponent = designContext.readDesign(child);
  295. if (child.hasAttr(":location")) {
  296. addComponent(childComponent, child.attr(":location"));
  297. } else {
  298. addComponent(childComponent);
  299. }
  300. }
  301. }
  302. @Override
  303. public void writeDesign(Element design, DesignContext designContext) {
  304. super.writeDesign(design, designContext);
  305. for (Entry<String, Component> slot : slots.entrySet()) {
  306. Element child = designContext.createElement(slot.getValue());
  307. if (slots.size() > 1 || !"".equals(slot.getKey())) {
  308. child.attr(":location", slot.getKey());
  309. }
  310. design.appendChild(child);
  311. }
  312. }
  313. }