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.

CssLayout.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.ui;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.LinkedList;
  8. import java.util.Map;
  9. import com.vaadin.event.LayoutEvents.LayoutClickEvent;
  10. import com.vaadin.event.LayoutEvents.LayoutClickListener;
  11. import com.vaadin.terminal.PaintException;
  12. import com.vaadin.terminal.PaintTarget;
  13. import com.vaadin.terminal.Paintable;
  14. import com.vaadin.terminal.gwt.client.MouseEventDetails;
  15. import com.vaadin.terminal.gwt.client.ui.VCssLayout;
  16. /**
  17. * CssLayout is a layout component that can be used in browser environment only.
  18. * It simply renders components and their captions into a same div element.
  19. * Component layout can then be adjusted with css.
  20. * <p>
  21. * In comparison to {@link HorizontalLayout} and {@link VerticalLayout}
  22. * <ul>
  23. * <li>rather similar server side api
  24. * <li>no spacing, alignment or expand ratios
  25. * <li>much simpler DOM that can be styled by skilled web developer
  26. * <li>no abstraction of browser differences (developer must ensure that the
  27. * result works properly on each browser)
  28. * <li>different kind of handling for relative sizes (that are set from server
  29. * side) (*)
  30. * <li>noticeably faster rendering time in some situations as we rely more on
  31. * the browser's rendering engine.
  32. * </ul>
  33. * <p>
  34. * With {@link CustomLayout} one can often achieve similar results (good looking
  35. * layouts with web technologies), but with CustomLayout developer needs to work
  36. * with fixed templates.
  37. * <p>
  38. * By extending CssLayout one can also inject some css rules straight to child
  39. * components using {@link #getCss(Component)}.
  40. *
  41. * <p>
  42. * (*) Relative sizes (set from server side) are treated bit differently than in
  43. * other layouts in Vaadin. In cssLayout the size is calculated relatively to
  44. * CSS layouts content area which is pretty much as in html and css. In other
  45. * layouts the size of component is calculated relatively to the "slot" given by
  46. * layout.
  47. * <p>
  48. * Also note that client side framework in Vaadin modifies inline style
  49. * properties width and height. This happens on each update to component. If one
  50. * wants to set component sizes with CSS, component must have undefined size on
  51. * server side (which is not the default for all components) and the size must
  52. * be defined with class styles - not by directly injecting width and height.
  53. *
  54. * @since 6.1 brought in from "FastLayouts" incubator project
  55. *
  56. */
  57. @ClientWidget(VCssLayout.class)
  58. public class CssLayout extends AbstractLayout {
  59. private static final String CLICK_EVENT = VCssLayout.CLICK_EVENT_IDENTIFIER;
  60. /**
  61. * Custom layout slots containing the components.
  62. */
  63. protected LinkedList<Component> components = new LinkedList<Component>();
  64. /**
  65. * Add a component into this container. The component is added to the right
  66. * or under the previous component.
  67. *
  68. * @param c
  69. * the component to be added.
  70. */
  71. @Override
  72. public void addComponent(Component c) {
  73. super.addComponent(c);
  74. components.add(c);
  75. requestRepaint();
  76. }
  77. /**
  78. * Adds a component into this container. The component is added to the left
  79. * or on top of the other components.
  80. *
  81. * @param c
  82. * the component to be added.
  83. */
  84. public void addComponentAsFirst(Component c) {
  85. super.addComponent(c);
  86. components.addFirst(c);
  87. requestRepaint();
  88. }
  89. /**
  90. * Adds a component into indexed position in this container.
  91. *
  92. * @param c
  93. * the component to be added.
  94. * @param index
  95. * the Index of the component position. The components currently
  96. * in and after the position are shifted forwards.
  97. */
  98. public void addComponent(Component c, int index) {
  99. super.addComponent(c);
  100. components.add(index, c);
  101. requestRepaint();
  102. }
  103. /**
  104. * Removes the component from this container.
  105. *
  106. * @param c
  107. * the component to be removed.
  108. */
  109. @Override
  110. public void removeComponent(Component c) {
  111. super.removeComponent(c);
  112. components.remove(c);
  113. requestRepaint();
  114. }
  115. /**
  116. * Gets the component container iterator for going trough all the components
  117. * in the container.
  118. *
  119. * @return the Iterator of the components inside the container.
  120. */
  121. public Iterator<Component> getComponentIterator() {
  122. return components.iterator();
  123. }
  124. /**
  125. * Paints the content of this component.
  126. *
  127. * @param target
  128. * the Paint Event.
  129. * @throws PaintException
  130. * if the paint operation failed.
  131. */
  132. @Override
  133. public void paintContent(PaintTarget target) throws PaintException {
  134. super.paintContent(target);
  135. HashMap<Paintable, String> componentCss = null;
  136. // Adds all items in all the locations
  137. for (Component c : components) {
  138. // Paint child component UIDL
  139. c.paint(target);
  140. String componentCssString = getCss(c);
  141. if (componentCssString != null) {
  142. if (componentCss == null) {
  143. componentCss = new HashMap<Paintable, String>();
  144. }
  145. componentCss.put(c, componentCssString);
  146. }
  147. }
  148. if (componentCss != null) {
  149. target.addAttribute("css", componentCss);
  150. }
  151. }
  152. /**
  153. * Returns styles to be applied to given component. Override this method to
  154. * inject custom style rules to components.
  155. *
  156. * <p>
  157. * Note that styles are injected over previous styles before actual child
  158. * rendering. Previous styles are not cleared, but overridden.
  159. *
  160. * <p>
  161. * Note that one most often achieves better code style, by separating
  162. * styling to theme (with custom theme and {@link #addStyleName(String)}.
  163. * With own custom styles it is also very easy to break browser
  164. * compatibility.
  165. *
  166. * @param c
  167. * the component
  168. * @return css rules to be applied to component
  169. */
  170. protected String getCss(Component c) {
  171. return null;
  172. }
  173. /* Documented in superclass */
  174. public void replaceComponent(Component oldComponent, Component newComponent) {
  175. // Gets the locations
  176. int oldLocation = -1;
  177. int newLocation = -1;
  178. int location = 0;
  179. for (final Iterator i = components.iterator(); i.hasNext();) {
  180. final Component component = (Component) i.next();
  181. if (component == oldComponent) {
  182. oldLocation = location;
  183. }
  184. if (component == newComponent) {
  185. newLocation = location;
  186. }
  187. location++;
  188. }
  189. if (oldLocation == -1) {
  190. addComponent(newComponent);
  191. } else if (newLocation == -1) {
  192. removeComponent(oldComponent);
  193. addComponent(newComponent, oldLocation);
  194. } else {
  195. if (oldLocation > newLocation) {
  196. components.remove(oldComponent);
  197. components.add(newLocation, oldComponent);
  198. components.remove(newComponent);
  199. components.add(oldLocation, newComponent);
  200. } else {
  201. components.remove(newComponent);
  202. components.add(oldLocation, newComponent);
  203. components.remove(oldComponent);
  204. components.add(newLocation, oldComponent);
  205. }
  206. requestRepaint();
  207. }
  208. }
  209. @Override
  210. public void changeVariables(Object source, Map variables) {
  211. super.changeVariables(source, variables);
  212. if (variables.containsKey(CLICK_EVENT)) {
  213. fireClick((Map<String, Object>) variables.get(CLICK_EVENT));
  214. }
  215. }
  216. private void fireClick(Map<String, Object> parameters) {
  217. MouseEventDetails mouseDetails = MouseEventDetails
  218. .deSerialize((String) parameters.get("mouseDetails"));
  219. Component childComponent = (Component) parameters.get("component");
  220. fireEvent(new LayoutClickEvent(this, mouseDetails, childComponent));
  221. }
  222. /**
  223. * Add a click listener to the layout. The listener is called whenever the
  224. * user clicks inside the layout. Also when the click targets a component
  225. * inside the Panel, provided the targeted component does not prevent the
  226. * click event from propagating. A caption is not considered part of a
  227. * component.
  228. *
  229. * The child component that was clicked is included in the
  230. * {@link LayoutClickEvent}.
  231. *
  232. * Use {@link #removeListener(LayoutClickListener)} to remove the listener.
  233. *
  234. * @param listener
  235. * The listener to add
  236. */
  237. public void addListener(LayoutClickListener listener) {
  238. addListener(CLICK_EVENT, LayoutClickEvent.class, listener,
  239. LayoutClickListener.clickMethod);
  240. }
  241. /**
  242. * Remove a click listener from the layout. The listener should earlier have
  243. * been added using {@link #addListener(LayoutClickListener)}.
  244. *
  245. * @param listener
  246. * The listener to remove
  247. */
  248. public void removeListener(LayoutClickListener listener) {
  249. removeListener(CLICK_EVENT, LayoutClickEvent.class, listener);
  250. }
  251. }