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.

OrderedLayout.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* *************************************************************************
  2. IT Mill Toolkit
  3. Development of Browser User Interfaces Made Easy
  4. Copyright (C) 2000-2006 IT Mill Ltd
  5. *************************************************************************
  6. This product is distributed under commercial license that can be found
  7. from the product package on license.pdf. Use of this product might
  8. require purchasing a commercial license from IT Mill Ltd. For guidelines
  9. on usage, see licensing-guidelines.html
  10. *************************************************************************
  11. For more information, contact:
  12. IT Mill Ltd phone: +358 2 4802 7180
  13. Ruukinkatu 2-4 fax: +358 2 4802 7181
  14. 20540, Turku email: info@itmill.com
  15. Finland company www: www.itmill.com
  16. Primary source for information and releases: www.itmill.com
  17. ********************************************************************** */
  18. package com.itmill.toolkit.ui;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.LinkedList;
  22. import java.util.Map;
  23. import com.itmill.toolkit.terminal.PaintException;
  24. import com.itmill.toolkit.terminal.PaintTarget;
  25. /**
  26. * Ordered layout.
  27. *
  28. * <code>OrderedLayout</code> is a component container, which shows the
  29. * subcomponents in the order of their addition in specified orientation.
  30. *
  31. * @author IT Mill Ltd.
  32. * @version
  33. * @VERSION@
  34. * @since 3.0
  35. */
  36. public class OrderedLayout extends AbstractLayout {
  37. /* Predefined orientations ***************************************** */
  38. /**
  39. * Components are to be laid out vertically.
  40. */
  41. public static int ORIENTATION_VERTICAL = 0;
  42. /**
  43. * Components are to be laid out horizontally.
  44. */
  45. public static int ORIENTATION_HORIZONTAL = 1;
  46. /**
  47. * Custom layout slots containing the components.
  48. */
  49. private LinkedList components = new LinkedList();
  50. /**
  51. * Mapping from components to alignments (horizontal + vertical).
  52. */
  53. private Map componentToAlignment = new HashMap();
  54. /**
  55. * Contained component should be aligned horizontally to the left.
  56. */
  57. private int ALIGNMENT_LEFT = 1;
  58. /**
  59. * Contained component should be aligned horizontally to the right.
  60. */
  61. private int ALIGNMENT_RIGHT = 2;
  62. /**
  63. * Contained component should be aligned vertically to the top.
  64. */
  65. private int ALIGNMENT_TOP = 4;
  66. /**
  67. * Contained component should be aligned vertically to the bottom.
  68. */
  69. private int ALIGNMENT_BOTTOM = 8;
  70. /**
  71. * Contained component should be horizontally aligned to center.
  72. */
  73. private int HORIZONTAL_ALIGNMENT_CENTER = 16;
  74. /**
  75. * Contained component should be vertically aligned to center.
  76. */
  77. private int VERTICAL_ALIGNMENT_CENTER = 32;
  78. /**
  79. * Orientation of the layout.
  80. */
  81. private int orientation;
  82. /**
  83. * Is spacing between contained components enabled. Defaults to false.
  84. */
  85. private boolean spacing = false;
  86. /**
  87. * Creates a new ordered layout. The order of the layout is
  88. * <code>ORIENTATION_VERTICAL</code>.
  89. */
  90. public OrderedLayout() {
  91. orientation = ORIENTATION_VERTICAL;
  92. }
  93. /**
  94. * Create a new ordered layout. The orientation of the layout is given as
  95. * parameters.
  96. *
  97. * @param orientation
  98. * the Orientation of the layout.
  99. */
  100. public OrderedLayout(int orientation) {
  101. this.orientation = orientation;
  102. }
  103. /**
  104. * Gets the component UIDL tag.
  105. *
  106. * @return the Component UIDL tag as string.
  107. */
  108. public String getTag() {
  109. return "orderedlayout";
  110. }
  111. /**
  112. * Add a component into this container. The component is added to the right
  113. * or under the previous component.
  114. *
  115. * @param c
  116. * the component to be added.
  117. */
  118. public void addComponent(Component c) {
  119. components.add(c);
  120. super.addComponent(c);
  121. requestRepaint();
  122. }
  123. /**
  124. * Adds a component into this container. The component is added to the left
  125. * or on top of the other components.
  126. *
  127. * @param c
  128. * the component to be added.
  129. */
  130. public void addComponentAsFirst(Component c) {
  131. components.addFirst(c);
  132. super.addComponent(c);
  133. requestRepaint();
  134. }
  135. /**
  136. * Adds a component into indexed position in this container.
  137. *
  138. * @param c
  139. * the component to be added.
  140. * @param index
  141. * the Index of the component position. The components currently
  142. * in and after the position are shifted forwards.
  143. */
  144. public void addComponent(Component c, int index) {
  145. components.add(index, c);
  146. super.addComponent(c);
  147. requestRepaint();
  148. }
  149. /**
  150. * Removes the component from this container.
  151. *
  152. * @param c
  153. * the component to be removed.
  154. */
  155. public void removeComponent(Component c) {
  156. super.removeComponent(c);
  157. components.remove(c);
  158. componentToAlignment.remove(c);
  159. requestRepaint();
  160. }
  161. /**
  162. * Gets the component container iterator for going trough all the components
  163. * in the container.
  164. *
  165. * @return the Iterator of the components inside the container.
  166. */
  167. public Iterator getComponentIterator() {
  168. return components.iterator();
  169. }
  170. /**
  171. * Paints the content of this component.
  172. *
  173. * @param target
  174. * the Paint Event.
  175. * @throws PaintException
  176. * if the paint operation failed.
  177. */
  178. public void paintContent(PaintTarget target) throws PaintException {
  179. super.paintContent(target);
  180. // Adds the attributes: orientation
  181. // note that the default values (b/vertival) are omitted
  182. if (orientation == ORIENTATION_HORIZONTAL)
  183. target.addAttribute("orientation", "horizontal");
  184. if (this.spacing)
  185. target.addAttribute("spacing", this.spacing);
  186. // Store alignment info in this String
  187. String alignments = "";
  188. // Adds all items in all the locations
  189. for (Iterator i = components.iterator(); i.hasNext();) {
  190. Component c = (Component) i.next();
  191. if (c != null) {
  192. // Paint child component UIDL
  193. c.paint(target);
  194. // Get alignment info for component
  195. if (componentToAlignment.containsKey(c))
  196. alignments += ((Integer) componentToAlignment.get(c))
  197. .intValue();
  198. else
  199. // Default alignment is top-left
  200. alignments += ALIGNMENT_TOP + ALIGNMENT_LEFT;
  201. if (i.hasNext())
  202. alignments += ",";
  203. }
  204. }
  205. // Add child component alignment info to layout tag
  206. target.addAttribute("alignments", alignments);
  207. }
  208. /**
  209. * Gets the orientation of the container.
  210. *
  211. * @return the Value of property orientation.
  212. */
  213. public int getOrientation() {
  214. return this.orientation;
  215. }
  216. /**
  217. * Set the orientation of the container.
  218. *
  219. * @param orientation
  220. * the New value of property orientation.
  221. */
  222. public void setOrientation(int orientation) {
  223. // Checks the validity of the argument
  224. if (orientation < ORIENTATION_VERTICAL
  225. || orientation > ORIENTATION_HORIZONTAL)
  226. throw new IllegalArgumentException();
  227. this.orientation = orientation;
  228. requestRepaint();
  229. }
  230. /* Documented in superclass */
  231. public void replaceComponent(Component oldComponent, Component newComponent) {
  232. // Gets the locations
  233. int oldLocation = -1;
  234. int newLocation = -1;
  235. int location = 0;
  236. for (Iterator i = components.iterator(); i.hasNext();) {
  237. Component component = (Component) i.next();
  238. if (component == oldComponent)
  239. oldLocation = location;
  240. if (component == newComponent)
  241. newLocation = location;
  242. location++;
  243. }
  244. if (oldLocation == -1)
  245. addComponent(newComponent);
  246. else if (newLocation == -1) {
  247. removeComponent(oldComponent);
  248. addComponent(newComponent, oldLocation);
  249. } else {
  250. if (oldLocation > newLocation) {
  251. components.remove(oldComponent);
  252. components.add(newLocation, oldComponent);
  253. components.remove(newComponent);
  254. componentToAlignment.remove(newComponent);
  255. components.add(oldLocation, newComponent);
  256. } else {
  257. components.remove(newComponent);
  258. components.add(oldLocation, newComponent);
  259. components.remove(oldComponent);
  260. componentToAlignment.remove(oldComponent);
  261. components.add(newLocation, oldComponent);
  262. }
  263. requestRepaint();
  264. }
  265. }
  266. /**
  267. * Set alignment for one contained component in this layout.
  268. *
  269. * @param childComponent
  270. * the component to align within it's layout cell.
  271. * @param horizontalAlignment
  272. * the horizontal alignment for the child component (left,
  273. * center, right).
  274. * @param verticalAlignment
  275. * the vertical alignment for the child component (top, center,
  276. * bottom).
  277. */
  278. public void setComponentAlignment(Component childComponent,
  279. int horizontalAlignment, int verticalAlignment) {
  280. componentToAlignment.put(childComponent, new Integer(
  281. horizontalAlignment + verticalAlignment));
  282. }
  283. /**
  284. * Enable spacing between child components within this layout.
  285. *
  286. * <p>
  287. * <strong>NOTE:</strong> This will only affect spaces between components,
  288. * not also all around spacing of the layout (i.e. do not mix this with HTML
  289. * Table elements cellspacing-attribute). Use {@link #setMargin(boolean)} to
  290. * add extra space around the layout.
  291. * </p>
  292. *
  293. * @param enabled
  294. */
  295. public void setSpacing(boolean enabled) {
  296. this.spacing = enabled;
  297. }
  298. }