/Shared/src/org/aswing/border/DecorateBorder.as

https://github.com/hayesmaker/AS3-Dices
ActionScript | 144 lines | 71 code | 14 blank | 59 comment | 12 complexity | 2c454b20433ae0653b5751abe6e544e4 MD5 | raw file
  1. /*
  2. Copyright aswing.org, see the LICENCE.txt.
  3. */
  4. package org.aswing.border{
  5. import org.aswing.graphics.Graphics2D;
  6. import org.aswing.Border;
  7. import org.aswing.geom.IntRectangle;
  8. import org.aswing.Component;
  9. import org.aswing.Insets;
  10. import flash.display.DisplayObject;
  11. import flash.display.Sprite;
  12. import org.aswing.error.ImpMissError;
  13. /**
  14. * DecorateBorder make your border can represented as many border arounded.
  15. * DecorateBorder is a abstract class, you need to inherit it to implement your
  16. * real decrator border.
  17. * <p>
  18. * <b>Note:You should only need to override:</b>
  19. * <ul>
  20. * <li><code>getDisplayImp</code></li>
  21. * <li><code>updateBorderImp</code></li>
  22. * <li><code>getBorderInsetsImp</code></li>
  23. * </ul>
  24. * methods in sub-class.
  25. * </p>
  26. * @author iiley
  27. */
  28. public class DecorateBorder implements Border{
  29. private var interior:Border;
  30. private var disContainer:Sprite;
  31. public function DecorateBorder(interior:Border){
  32. this.interior = interior;
  33. }
  34. /**
  35. * Sets new interior border.
  36. * @param interior the new interior border
  37. */
  38. public function setInterior(interior:Border):void {
  39. this.interior = interior;
  40. }
  41. /**
  42. * Returns current interior border.
  43. * @return current interior border
  44. */
  45. public function getInterior():Border {
  46. return interior;
  47. }
  48. /**
  49. * Override this method in sub-class to return the display object if needed.
  50. * @return a display object, or null, do not need a display object.
  51. */
  52. public function getDisplayImp():DisplayObject{
  53. return null;
  54. }
  55. /**
  56. * Override this method in sub-class to draw border on the specified mc.
  57. * @param c the component for which this border is being painted
  58. * @param g the paint graphics
  59. * @param bounds the bounds of border
  60. */
  61. public function updateBorderImp(com:Component, g:Graphics2D, bounds:IntRectangle):void{
  62. throw new ImpMissError();
  63. }
  64. /**
  65. * You should override this method to count this border's insets.
  66. * @see #getBorderInsets
  67. */
  68. public function getBorderInsetsImp(c:Component, bounds:IntRectangle):Insets{
  69. throw new ImpMissError();
  70. return new Insets();
  71. }
  72. /**
  73. * You should override this method to return the display object.
  74. * @see #getDisplayImp()
  75. */
  76. final public function getDisplay(c:Component):DisplayObject
  77. {
  78. var inter:Border = getInterior();
  79. if(inter != null){
  80. var interDis:DisplayObject = inter.getDisplay(c);
  81. var selfDis:DisplayObject = getDisplayImp();
  82. if(interDis == null){
  83. return selfDis;
  84. }else if(selfDis == null){
  85. return interDis;
  86. }else{
  87. if(disContainer == null){
  88. disContainer = new Sprite();
  89. disContainer.mouseEnabled = false;
  90. disContainer.addChild(selfDis);
  91. disContainer.addChild(interDis);
  92. }
  93. return disContainer;
  94. }
  95. }else{
  96. return getDisplayImp();
  97. }
  98. }
  99. /**
  100. * call <code>super.paintBorder</code> paint the border first and then
  101. * paint the interior border on the interior bounds.
  102. * <br>
  103. * Note:subclass should not override this method, should override paintBorderImp.
  104. * @see #paintBorderImp
  105. */
  106. final public function updateBorder(c:Component, g:Graphics2D, bounds:IntRectangle):void{
  107. updateBorderImp(c, g, bounds);
  108. //then paint interior border
  109. if(getInterior() != null){
  110. var interiorBounds:IntRectangle = getBorderInsetsImp(c, bounds).getInsideBounds(bounds);
  111. getInterior().updateBorder(c, g, interiorBounds);
  112. }
  113. }
  114. /**
  115. * Returns the insets of the border.<br>
  116. * Note:subclass should not override this method, should override getBorderInsetsImp.
  117. * @see #getBorderInsetsImp
  118. * @param c the component for which this border insets value applies
  119. * @param bounds the bounds of the border would paint in.
  120. */
  121. final public function getBorderInsets(c:Component, bounds:IntRectangle):Insets{
  122. var insets:Insets = getBorderInsetsImp(c, bounds);
  123. if(getInterior() != null){
  124. var interiorBounds:IntRectangle = insets.getInsideBounds(bounds);
  125. insets.addInsets(getInterior().getBorderInsets(c, interiorBounds));
  126. }
  127. return insets;
  128. }
  129. }
  130. }