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 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 com.itmill.toolkit.terminal.PaintException;
  20. import com.itmill.toolkit.terminal.PaintTarget;
  21. import java.util.Iterator;
  22. import java.util.HashMap;
  23. /**
  24. * <p>
  25. * A container component with freely designed layout and style. The container
  26. * consists of items with textually represented locations. Each item contains
  27. * one sub-component. The adapter and theme are responsible for rendering the
  28. * layout with given style by placing the items on the screen in defined
  29. * locations.
  30. * </p>
  31. *
  32. * <p>
  33. * The definition of locations is not fixed - the each style can define its
  34. * locations in a way that is suitable for it. One typical example would be to
  35. * create visual design for a web site as a custom layout: the visual design
  36. * could define locations for "menu", "body" and "title" for example. The layout
  37. * would then be implemented as XLS-template with for given style.
  38. * </p>
  39. *
  40. * <p>
  41. * The default theme handles the styles that are not defined by just drawing the
  42. * subcomponents as in OrderedLayout.
  43. * </p>
  44. *
  45. * @author IT Mill Ltd.
  46. * @version
  47. * @VERSION@
  48. * @since 3.0
  49. */
  50. public class CustomLayout extends AbstractLayout {
  51. /**
  52. * Custom layout slots containing the components.
  53. */
  54. private HashMap slots = new HashMap();
  55. private String templateName;
  56. /**
  57. * Constructor for custom layout with given template name.
  58. */
  59. public CustomLayout(String template) {
  60. templateName = template;
  61. }
  62. /**
  63. * Gets the component UIDL tag.
  64. *
  65. * @return the Component UIDL tag as string.
  66. */
  67. public String getTag() {
  68. return "customlayout";
  69. }
  70. /**
  71. * Adds the component into this container to given location.
  72. *
  73. * @param c
  74. * the component to be added.
  75. * @param location
  76. * the location of the component.
  77. */
  78. public void addComponent(Component c, String location) {
  79. Component old = (Component) slots.get(location);
  80. if (old != null) {
  81. removeComponent(old);
  82. }
  83. slots.put(location, c);
  84. c.setParent(this);
  85. fireComponentAttachEvent(c);
  86. requestRepaint();
  87. }
  88. /**
  89. * Adds the component into this container. The component is added without
  90. * specifying the location (empty string is then used as location). Only one
  91. * component can be added to the default "" location and adding more
  92. * components into that location overwrites the old components.
  93. *
  94. * @param c
  95. * the component to be added.
  96. */
  97. public void addComponent(Component c) {
  98. this.addComponent(c, "");
  99. }
  100. /**
  101. * Removes the component from this container.
  102. *
  103. * @param c
  104. * the component to be removed.
  105. */
  106. public void removeComponent(Component c) {
  107. if (c == null)
  108. return;
  109. slots.values().remove(c);
  110. c.setParent(null);
  111. fireComponentDetachEvent(c);
  112. requestRepaint();
  113. }
  114. /**
  115. * Removes the component from this container from given location.
  116. *
  117. * @param location
  118. * the Location identifier of the component.
  119. */
  120. public void removeComponent(String location) {
  121. this.removeComponent((Component) slots.get(location));
  122. }
  123. /**
  124. * Gets the component container iterator for going trough all the components
  125. * in the container.
  126. *
  127. * @return the Iterator of the components inside the container.
  128. */
  129. public Iterator getComponentIterator() {
  130. return slots.values().iterator();
  131. }
  132. /**
  133. * Gets the child-component by its location.
  134. *
  135. * @param location
  136. * the name of the location where the requested component
  137. * resides.
  138. * @return the Component in the given location or null if not found.
  139. */
  140. public Component getComponent(String location) {
  141. return (Component) slots.get(location);
  142. }
  143. /**
  144. * Paints the content of this component.
  145. *
  146. * @param target
  147. * @throws PaintException
  148. * if the paint operation failed.
  149. */
  150. public void paintContent(PaintTarget target) throws PaintException {
  151. super.paintContent(target);
  152. target.addAttribute("template", templateName);
  153. // Adds all items in all the locations
  154. for (Iterator i = slots.keySet().iterator(); i.hasNext();) {
  155. // Gets the (location,component)
  156. String location = (String) i.next();
  157. Component c = (Component) slots.get(location);
  158. if (c != null) {
  159. // Writes the item
  160. target.startTag("location");
  161. target.addAttribute("name", location);
  162. c.paint(target);
  163. target.endTag("location");
  164. }
  165. }
  166. }
  167. /* Documented in superclass */
  168. public void replaceComponent(Component oldComponent, Component newComponent) {
  169. // Gets the locations
  170. String oldLocation = null;
  171. String newLocation = null;
  172. for (Iterator i = slots.keySet().iterator(); i.hasNext();) {
  173. String location = (String) i.next();
  174. Component component = (Component) slots.get(location);
  175. if (component == oldComponent)
  176. oldLocation = location;
  177. if (component == newComponent)
  178. newLocation = location;
  179. }
  180. if (oldLocation == null)
  181. addComponent(newComponent);
  182. else if (newLocation == null) {
  183. removeComponent(oldLocation);
  184. addComponent(newComponent, oldLocation);
  185. } else {
  186. slots.put(newLocation, oldComponent);
  187. slots.put(oldLocation, newComponent);
  188. requestRepaint();
  189. }
  190. }
  191. /**
  192. * CustomLayout's template selecting was previously implemented with
  193. * setStyle. Overriding to improve backwards compatibility.
  194. *
  195. * @param name
  196. * template name
  197. */
  198. public void setStyle(String name) {
  199. setTemplateName(name);
  200. }
  201. /** Get the name of the template */
  202. public String getTemplateName() {
  203. return templateName;
  204. }
  205. /**
  206. * Set the name of the template used to draw custom layout.
  207. *
  208. * With GWT-adapter, the template with name 'templatename' is loaded from
  209. * ITMILL/themes/themename/layouts/templatename.html. If the theme has not
  210. * been set (with Application.setTheme()), themename is 'default'.
  211. *
  212. * @param templateName
  213. */
  214. public void setTemplateName(String templateName) {
  215. this.templateName = templateName;
  216. requestRepaint();
  217. }
  218. }