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

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