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.

AbstractLayout.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package com.itmill.toolkit.ui;
  2. import com.itmill.toolkit.terminal.PaintException;
  3. import com.itmill.toolkit.terminal.PaintTarget;
  4. /**
  5. * An abstract class that defines default implementation for the {@link Layout}
  6. * interface.
  7. *
  8. * @author IT Mill Ltd.
  9. * @version
  10. * @VERSION@
  11. * @since 5.0
  12. */
  13. public abstract class AbstractLayout extends AbstractComponentContainer
  14. implements Layout {
  15. /**
  16. * Layout edge margins, clockwise from top: top, right, bottom, left. Each
  17. * is set to true, if the client-side implementation should leave extra
  18. * space at that edge.
  19. */
  20. private boolean[] margins;
  21. /**
  22. * Height of the layout. Set to -1 for undefined height.
  23. */
  24. private int height = -1;
  25. /**
  26. * Height unit.
  27. *
  28. * @see com.itmill.toolkit.terminal.Sizeable.UNIT_SYMBOLS;
  29. */
  30. private int heightUnit = UNITS_PIXELS;
  31. /**
  32. * Width of the layout. Set to -1 for undefined width.
  33. */
  34. private int width = -1;
  35. /**
  36. * Width unit.
  37. *
  38. * @see com.itmill.toolkit.terminal.Sizeable.UNIT_SYMBOLS;
  39. */
  40. private int widthUnit = UNITS_PIXELS;
  41. /*
  42. * (non-Javadoc)
  43. *
  44. * @see com.itmill.toolkit.ui.AbstractComponent#getTag()
  45. */
  46. public abstract String getTag();
  47. /*
  48. * (non-Javadoc)
  49. *
  50. * @see com.itmill.toolkit.ui.Layout#setMargin(boolean)
  51. */
  52. public void setMargin(boolean enabled) {
  53. margins = new boolean[] { enabled, enabled, enabled, enabled };
  54. }
  55. /*
  56. * (non-Javadoc)
  57. *
  58. * @see com.itmill.toolkit.ui.Layout#setMargin(boolean, boolean, boolean,
  59. * boolean)
  60. */
  61. public void setMargin(boolean topEnabled, boolean rightEnabled,
  62. boolean bottomEnabled, boolean leftEnabled) {
  63. margins = new boolean[] { topEnabled, rightEnabled, bottomEnabled,
  64. leftEnabled };
  65. }
  66. /*
  67. * (non-Javadoc)
  68. *
  69. * @see com.itmill.toolkit.terminal.Sizeable#getHeight()
  70. */
  71. public int getHeight() {
  72. return height;
  73. }
  74. /*
  75. * (non-Javadoc)
  76. *
  77. * @see com.itmill.toolkit.terminal.Sizeable#getHeightUnits()
  78. */
  79. public int getHeightUnits() {
  80. return heightUnit;
  81. }
  82. /*
  83. * (non-Javadoc)
  84. *
  85. * @see com.itmill.toolkit.terminal.Sizeable#getWidth()
  86. */
  87. public int getWidth() {
  88. return width;
  89. }
  90. /*
  91. * (non-Javadoc)
  92. *
  93. * @see com.itmill.toolkit.terminal.Sizeable#getWidthUnits()
  94. */
  95. public int getWidthUnits() {
  96. return widthUnit;
  97. }
  98. /*
  99. * (non-Javadoc)
  100. *
  101. * @see com.itmill.toolkit.terminal.Sizeable#setHeight(int)
  102. */
  103. public void setHeight(int height) {
  104. this.height = height;
  105. requestRepaint();
  106. }
  107. /*
  108. * (non-Javadoc)
  109. *
  110. * @see com.itmill.toolkit.terminal.Sizeable#setHeightUnits(int)
  111. */
  112. public void setHeightUnits(int units) {
  113. this.heightUnit = units;
  114. requestRepaint();
  115. }
  116. /*
  117. * (non-Javadoc)
  118. *
  119. * @see com.itmill.toolkit.terminal.Sizeable#setSizeFull()
  120. */
  121. public void setSizeFull() {
  122. height = 100;
  123. width = 100;
  124. heightUnit = UNITS_PERCENTAGE;
  125. widthUnit = UNITS_PERCENTAGE;
  126. requestRepaint();
  127. }
  128. /*
  129. * (non-Javadoc)
  130. *
  131. * @see com.itmill.toolkit.terminal.Sizeable#setSizeUndefined()
  132. */
  133. public void setSizeUndefined() {
  134. height = -1;
  135. width = -1;
  136. heightUnit = UNITS_PIXELS;
  137. widthUnit = UNITS_PIXELS;
  138. requestRepaint();
  139. }
  140. /*
  141. * (non-Javadoc)
  142. *
  143. * @see com.itmill.toolkit.terminal.Sizeable#setWidth(int)
  144. */
  145. public void setWidth(int width) {
  146. this.width = width;
  147. requestRepaint();
  148. }
  149. /*
  150. * (non-Javadoc)
  151. *
  152. * @see com.itmill.toolkit.terminal.Sizeable#setWidthUnits(int)
  153. */
  154. public void setWidthUnits(int units) {
  155. this.widthUnit = units;
  156. requestRepaint();
  157. }
  158. /*
  159. * (non-Javadoc)
  160. *
  161. * @see com.itmill.toolkit.ui.AbstractComponent#paintContent(com.itmill.toolkit.terminal.PaintTarget)
  162. */
  163. public void paintContent(PaintTarget target) throws PaintException {
  164. super.paintContent(target);
  165. // Add margin info. Defaults to false.
  166. if (margins == null)
  167. setMargin(false);
  168. target.addAttribute("marginTop", margins[0]);
  169. target.addAttribute("marginRight", margins[1]);
  170. target.addAttribute("marginBottom", margins[2]);
  171. target.addAttribute("marginLeft", margins[3]);
  172. // Add size info
  173. if (getHeight() > -1)
  174. target.addAttribute("height", getHeight()
  175. + UNIT_SYMBOLS[getHeightUnits()]);
  176. if (getWidth() > -1)
  177. target.addAttribute("height", getWidth()
  178. + UNIT_SYMBOLS[getWidthUnits()]);
  179. }
  180. }