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.

SplitPanel.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.Iterator;
  20. import com.itmill.toolkit.terminal.PaintException;
  21. import com.itmill.toolkit.terminal.PaintTarget;
  22. import com.itmill.toolkit.terminal.Sizeable;
  23. /**
  24. * SplitPanel.
  25. *
  26. * <code>SplitPanel</code> is a component container, that can contain two
  27. * components (possibly containers) which are split by divider element.
  28. *
  29. * @author IT Mill Ltd.
  30. * @version
  31. * @VERSION@
  32. * @since 5.0
  33. */
  34. public class SplitPanel extends AbstractLayout {
  35. /* Predefined orientations ***************************************** */
  36. /**
  37. * Components are to be layed out vertically.
  38. */
  39. public static int ORIENTATION_VERTICAL = 0;
  40. /**
  41. * Components are to be layed out horizontally.
  42. */
  43. public static int ORIENTATION_HORIZONTAL = 1;
  44. private Component firstComponent;
  45. private Component secondComponent;
  46. /**
  47. * Orientation of the layout.
  48. */
  49. private int orientation;
  50. private int pos = 50;
  51. private int posUnit = UNITS_PERCENTAGE;
  52. /**
  53. * Creates a new split panel. The orientation of the panels is
  54. * <code>ORIENTATION_VERTICAL</code>.
  55. */
  56. public SplitPanel() {
  57. orientation = ORIENTATION_VERTICAL;
  58. setSizeFull();
  59. }
  60. /**
  61. * Create a new split panels. The orientation of the panel is given as
  62. * parameters.
  63. *
  64. * @param orientation
  65. * the Orientation of the layout.
  66. */
  67. public SplitPanel(int orientation) {
  68. this.orientation = orientation;
  69. setSizeFull();
  70. }
  71. /**
  72. * Gets the component UIDL tag.
  73. *
  74. * @return the Component UIDL tag as string.
  75. */
  76. public String getTag() {
  77. if (orientation == ORIENTATION_HORIZONTAL) {
  78. return "hsplitpanel";
  79. } else {
  80. return "vsplitpanel";
  81. }
  82. }
  83. /**
  84. * Add a component into this container. The component is added to the right
  85. * or under the previous component.
  86. *
  87. * @param c
  88. * the component to be added.
  89. */
  90. public void addComponent(Component c) {
  91. if (firstComponent == null) {
  92. firstComponent = c;
  93. } else if (secondComponent == null) {
  94. secondComponent = c;
  95. } else {
  96. throw new UnsupportedOperationException(
  97. "Split panel can contain only two components");
  98. }
  99. super.addComponent(c);
  100. requestRepaint();
  101. }
  102. public void setFirstComponent(Component c) {
  103. if (firstComponent != null) {
  104. // detach old
  105. removeComponent(firstComponent);
  106. }
  107. firstComponent = c;
  108. super.addComponent(c);
  109. }
  110. public void setSecondComponent(Component c) {
  111. if (secondComponent != null) {
  112. // detach old
  113. removeComponent(c);
  114. }
  115. secondComponent = c;
  116. super.addComponent(c);
  117. }
  118. /**
  119. * Removes the component from this container.
  120. *
  121. * @param c
  122. * the component to be removed.
  123. */
  124. public void removeComponent(Component c) {
  125. super.removeComponent(c);
  126. if (c == firstComponent)
  127. firstComponent = null;
  128. else
  129. secondComponent = null;
  130. requestRepaint();
  131. }
  132. /**
  133. * Gets the component container iterator for going trough all the components
  134. * in the container.
  135. *
  136. * @return the Iterator of the components inside the container.
  137. */
  138. public Iterator getComponentIterator() {
  139. return new Iterator() {
  140. int i = 0;
  141. public boolean hasNext() {
  142. if (i < (firstComponent == null ? 0 : 1)
  143. + (secondComponent == null ? 0 : 1))
  144. return true;
  145. return false;
  146. }
  147. public Object next() {
  148. if (!hasNext())
  149. return null;
  150. i++;
  151. if (i == 1)
  152. return firstComponent == null ? secondComponent
  153. : firstComponent;
  154. else if (i == 2)
  155. return secondComponent;
  156. return null;
  157. }
  158. public void remove() {
  159. if (i == 1) {
  160. if (firstComponent != null) {
  161. setFirstComponent(null);
  162. i = 0;
  163. } else
  164. setSecondComponent(null);
  165. } else if (i == 2)
  166. setSecondComponent(null);
  167. }
  168. };
  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. String position = pos + UNIT_SYMBOLS[posUnit];
  181. target.addAttribute("position", position);
  182. if (firstComponent != null)
  183. firstComponent.paint(target);
  184. else
  185. (new OrderedLayout()).paint(target);
  186. if (secondComponent != null)
  187. secondComponent.paint(target);
  188. else
  189. (new OrderedLayout()).paint(target);
  190. }
  191. /**
  192. * Gets the orientation of the container.
  193. *
  194. * @return the Value of property orientation.
  195. */
  196. public int getOrientation() {
  197. return this.orientation;
  198. }
  199. /**
  200. * Set the orientation of the container.
  201. *
  202. * @param orientation
  203. * the New value of property orientation.
  204. */
  205. public void setOrientation(int orientation) {
  206. // Checks the validity of the argument
  207. if (orientation < ORIENTATION_VERTICAL
  208. || orientation > ORIENTATION_HORIZONTAL)
  209. throw new IllegalArgumentException();
  210. this.orientation = orientation;
  211. requestRepaint();
  212. }
  213. /* Documented in superclass */
  214. public void replaceComponent(Component oldComponent, Component newComponent) {
  215. if (oldComponent == firstComponent) {
  216. setFirstComponent(newComponent);
  217. } else if (oldComponent == secondComponent) {
  218. setSecondComponent(secondComponent);
  219. }
  220. requestRepaint();
  221. }
  222. /**
  223. * Moves the position of the splitter.
  224. *
  225. * @param pos
  226. * the new size of the first region in persentage
  227. */
  228. public void setSplitPosition(int pos) {
  229. setSplitPosition(pos, UNITS_PERCENTAGE);
  230. }
  231. /**
  232. * Moves the position of the splitter with given position and unit.
  233. *
  234. * @param pos
  235. * size of the first region
  236. * @param unit the unit (from {@link Sizeable}) in which the size is given.
  237. */
  238. public void setSplitPosition(int pos, int unit) {
  239. this.pos = pos;
  240. this.posUnit = unit;
  241. }
  242. }