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

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