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.

AbstractOrderedLayout.java 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.terminal.PaintException;
  10. import com.vaadin.terminal.PaintTarget;
  11. import com.vaadin.terminal.Sizeable;
  12. @SuppressWarnings("serial")
  13. public abstract class AbstractOrderedLayout extends AbstractLayout implements
  14. Layout.AlignmentHandler, Layout.SpacingHandler {
  15. private static final Alignment ALIGNMENT_DEFAULT = Alignment.TOP_LEFT;
  16. /**
  17. * Custom layout slots containing the components.
  18. */
  19. protected LinkedList<Component> components = new LinkedList<Component>();
  20. /* Child component alignments */
  21. /**
  22. * Mapping from components to alignments (horizontal + vertical).
  23. */
  24. private final Map<Component, Alignment> componentToAlignment = new HashMap<Component, Alignment>();
  25. private final Map<Component, Float> componentToExpandRatio = new HashMap<Component, Float>();
  26. /**
  27. * Is spacing between contained components enabled. Defaults to false.
  28. */
  29. private boolean spacing = false;
  30. /**
  31. * Gets the component UIDL tag.
  32. *
  33. * @return the Component UIDL tag as string.
  34. */
  35. @Override
  36. public String getTag() {
  37. return "orderedlayout";
  38. }
  39. /**
  40. * Add a component into this container. The component is added to the right
  41. * or under the previous component.
  42. *
  43. * @param c
  44. * the component to be added.
  45. */
  46. @Override
  47. public void addComponent(Component c) {
  48. super.addComponent(c);
  49. components.add(c);
  50. requestRepaint();
  51. }
  52. /**
  53. * Adds a component into this container. The component is added to the left
  54. * or on top of the other components.
  55. *
  56. * @param c
  57. * the component to be added.
  58. */
  59. public void addComponentAsFirst(Component c) {
  60. super.addComponent(c);
  61. components.addFirst(c);
  62. requestRepaint();
  63. }
  64. /**
  65. * Adds a component into indexed position in this container.
  66. *
  67. * @param c
  68. * the component to be added.
  69. * @param index
  70. * the Index of the component position. The components currently
  71. * in and after the position are shifted forwards.
  72. */
  73. public void addComponent(Component c, int index) {
  74. super.addComponent(c);
  75. components.add(index, c);
  76. requestRepaint();
  77. }
  78. /**
  79. * Removes the component from this container.
  80. *
  81. * @param c
  82. * the component to be removed.
  83. */
  84. @Override
  85. public void removeComponent(Component c) {
  86. super.removeComponent(c);
  87. components.remove(c);
  88. componentToAlignment.remove(c);
  89. componentToExpandRatio.remove(c);
  90. requestRepaint();
  91. }
  92. /**
  93. * Gets the component container iterator for going trough all the components
  94. * in the container.
  95. *
  96. * @return the Iterator of the components inside the container.
  97. */
  98. public Iterator getComponentIterator() {
  99. return components.iterator();
  100. }
  101. /**
  102. * Paints the content of this component.
  103. *
  104. * @param target
  105. * the Paint Event.
  106. * @throws PaintException
  107. * if the paint operation failed.
  108. */
  109. @Override
  110. public void paintContent(PaintTarget target) throws PaintException {
  111. super.paintContent(target);
  112. // Add spacing attribute (omitted if false)
  113. if (spacing) {
  114. target.addAttribute("spacing", spacing);
  115. }
  116. // Adds all items in all the locations
  117. for (Component c : components) {
  118. // Paint child component UIDL
  119. c.paint(target);
  120. }
  121. // Add child component alignment info to layout tag
  122. target.addAttribute("alignments", componentToAlignment);
  123. target.addAttribute("expandRatios", componentToExpandRatio);
  124. }
  125. /* Documented in superclass */
  126. public void replaceComponent(Component oldComponent, Component newComponent) {
  127. // Gets the locations
  128. int oldLocation = -1;
  129. int newLocation = -1;
  130. int location = 0;
  131. for (final Iterator i = components.iterator(); i.hasNext();) {
  132. final Component component = (Component) i.next();
  133. if (component == oldComponent) {
  134. oldLocation = location;
  135. }
  136. if (component == newComponent) {
  137. newLocation = location;
  138. }
  139. location++;
  140. }
  141. if (oldLocation == -1) {
  142. addComponent(newComponent);
  143. } else if (newLocation == -1) {
  144. removeComponent(oldComponent);
  145. addComponent(newComponent, oldLocation);
  146. } else {
  147. if (oldLocation > newLocation) {
  148. components.remove(oldComponent);
  149. components.add(newLocation, oldComponent);
  150. components.remove(newComponent);
  151. componentToAlignment.remove(newComponent);
  152. components.add(oldLocation, newComponent);
  153. } else {
  154. components.remove(newComponent);
  155. components.add(oldLocation, newComponent);
  156. components.remove(oldComponent);
  157. componentToAlignment.remove(oldComponent);
  158. components.add(newLocation, oldComponent);
  159. }
  160. requestRepaint();
  161. }
  162. }
  163. /*
  164. * (non-Javadoc)
  165. *
  166. * @see com.vaadin.ui.Layout.AlignmentHandler#setComponentAlignment(com
  167. * .vaadin.ui.Component, int, int)
  168. */
  169. public void setComponentAlignment(Component childComponent,
  170. int horizontalAlignment, int verticalAlignment) {
  171. if (components.contains(childComponent)) {
  172. // Alignments are bit masks
  173. componentToAlignment.put(childComponent, new Alignment(
  174. horizontalAlignment + verticalAlignment));
  175. requestRepaint();
  176. } else {
  177. throw new IllegalArgumentException(
  178. "Component must be added to layout before using setComponentAlignment()");
  179. }
  180. }
  181. public void setComponentAlignment(Component childComponent,
  182. Alignment alignment) {
  183. if (components.contains(childComponent)) {
  184. componentToAlignment.put(childComponent, alignment);
  185. requestRepaint();
  186. } else {
  187. throw new IllegalArgumentException(
  188. "Component must be added to layout before using setComponentAlignment()");
  189. }
  190. }
  191. /*
  192. * (non-Javadoc)
  193. *
  194. * @see com.vaadin.ui.Layout.AlignmentHandler#getComponentAlignment(com
  195. * .vaadin.ui.Component)
  196. */
  197. public Alignment getComponentAlignment(Component childComponent) {
  198. Alignment alignment = componentToAlignment.get(childComponent);
  199. if (alignment == null) {
  200. return ALIGNMENT_DEFAULT;
  201. } else {
  202. return alignment;
  203. }
  204. }
  205. /*
  206. * (non-Javadoc)
  207. *
  208. * @see com.vaadin.ui.Layout.SpacingHandler#setSpacing(boolean)
  209. */
  210. public void setSpacing(boolean enabled) {
  211. spacing = enabled;
  212. requestRepaint();
  213. }
  214. /*
  215. * (non-Javadoc)
  216. *
  217. * @see com.vaadin.ui.Layout.SpacingHandler#isSpacing()
  218. */
  219. @Deprecated
  220. public boolean isSpacingEnabled() {
  221. return spacing;
  222. }
  223. /*
  224. * (non-Javadoc)
  225. *
  226. * @see com.vaadin.ui.Layout.SpacingHandler#isSpacing()
  227. */
  228. public boolean isSpacing() {
  229. return spacing;
  230. }
  231. /**
  232. * <p>
  233. * This method is used to control how excess space in layout is distributed
  234. * among components. Excess space may exist if layout is sized and contained
  235. * non relatively sized components don't consume all available space.
  236. *
  237. * <p>
  238. * Example how to distribute 1:3 (33%) for component1 and 2:3 (67%) for
  239. * component2 :
  240. *
  241. * <code>
  242. * layout.setExpandRatio(component1, 1);<br>
  243. * layout.setExpandRatio(component2, 2);
  244. * </code>
  245. *
  246. * <p>
  247. * If no ratios have been set, the excess space is distributed evenly among
  248. * all components.
  249. *
  250. * <p>
  251. * Note, that width or height (depending on orientation) needs to be defined
  252. * for this method to have any effect.
  253. *
  254. * @see Sizeable
  255. *
  256. * @param component
  257. * the component in this layout which expand ratio is to be set
  258. * @param ratio
  259. */
  260. public void setExpandRatio(Component component, float ratio) {
  261. if (components.contains(component)) {
  262. componentToExpandRatio.put(component, ratio);
  263. requestRepaint();
  264. } else {
  265. throw new IllegalArgumentException(
  266. "Component must be added to layout before using setExpandRatio()");
  267. }
  268. };
  269. /**
  270. * Returns the expand ratio of given component.
  271. *
  272. * @param component
  273. * which expand ratios is requested
  274. * @return expand ratio of given component, 0.0f by default
  275. */
  276. public float getExpandRatio(Component component) {
  277. Float ratio = componentToExpandRatio.get(component);
  278. return (ratio == null) ? 0 : ratio.floatValue();
  279. }
  280. public void setComponentAlignment(Component component, String alignment) {
  281. AlignmentUtils.setComponentAlignment(this, component, alignment);
  282. }
  283. }