選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CssLayout.java 7.3KB

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