1. /*
  2. * @(#)CompoundBorder.java 1.14 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.border;
  8. import java.awt.Graphics;
  9. import java.awt.Insets;
  10. import java.awt.Component;
  11. /**
  12. * A composite Border class used to compose two Border objects
  13. * into a single border by nesting an inside Border object within
  14. * the insets of an outside Border object.
  15. *
  16. * For example, this class may be used to add blank margin space
  17. * to a component with an existing decorative border:
  18. * <p>
  19. * <code><pre>
  20. * Border border = comp.getBorder();
  21. * Border margin = new EmptyBorder(10,10,10,10);
  22. * comp.setBorder(new CompoundBorder(border, margin));
  23. * </pre></code>
  24. * <p>
  25. * <strong>Warning:</strong>
  26. * Serialized objects of this class will not be compatible with
  27. * future Swing releases. The current serialization support is appropriate
  28. * for short term storage or RMI between applications running the same
  29. * version of Swing. A future release of Swing will provide support for
  30. * long term persistence.
  31. *
  32. * @version 1.14 11/29/01
  33. * @author David Kloba
  34. */
  35. public class CompoundBorder extends AbstractBorder {
  36. protected Border outsideBorder;
  37. protected Border insideBorder;
  38. /**
  39. * Creates a compound border with null outside and inside borders.
  40. */
  41. public CompoundBorder() {
  42. this.outsideBorder = null;
  43. this.insideBorder = null;
  44. }
  45. /**
  46. * Creates a compound border with the specified outside and
  47. * inside borders. Either border may be null.
  48. * @param outsideBorder the outside border
  49. * @param insideBorder the inside border to be nested
  50. */
  51. public CompoundBorder(Border outsideBorder, Border insideBorder) {
  52. this.outsideBorder = outsideBorder;
  53. this.insideBorder = insideBorder;
  54. }
  55. /**
  56. * Returns whether or not this compound border is opaque.
  57. * Returns true if both the inside and outside borders are
  58. * non-null and opaque; returns false otherwise.
  59. */
  60. public boolean isBorderOpaque() {
  61. return (outsideBorder != null && outsideBorder.isBorderOpaque()) &&
  62. (insideBorder != null && insideBorder.isBorderOpaque());
  63. }
  64. /**
  65. * Paints the compound border by painting the outside border
  66. * with the specified position and size and then painting the
  67. * inside border at the specified position and size offset by
  68. * the insets of the outside border.
  69. * @param c the component for which this border is being painted
  70. * @param g the paint graphics
  71. * @param x the x position of the painted border
  72. * @param y the y position of the painted border
  73. * @param width the width of the painted border
  74. * @param height the height of the painted border
  75. */
  76. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  77. Insets nextInsets;
  78. int px, py, pw, ph;
  79. px = x;
  80. py = y;
  81. pw = width;
  82. ph = height;
  83. if(outsideBorder != null) {
  84. outsideBorder.paintBorder(c, g, px, py, pw, ph);
  85. nextInsets = outsideBorder.getBorderInsets(c);
  86. px += nextInsets.left;
  87. py += nextInsets.top;
  88. pw = pw - nextInsets.right - nextInsets.left;
  89. ph = ph - nextInsets.bottom - nextInsets.top;
  90. }
  91. if(insideBorder != null)
  92. insideBorder.paintBorder(c, g, px, py, pw, ph);
  93. }
  94. /**
  95. * Reinitialize the insets parameter with this Border's current Insets.
  96. * @param c the component for which this border insets value applies
  97. * @param insets the object to be reinitialized
  98. */
  99. public Insets getBorderInsets(Component c, Insets insets) {
  100. Insets nextInsets;
  101. insets.top = insets.left = insets.right = insets.bottom = 0;
  102. if(outsideBorder != null) {
  103. nextInsets = outsideBorder.getBorderInsets(c);
  104. insets.top += nextInsets.top;
  105. insets.left += nextInsets.left;
  106. insets.right += nextInsets.right;
  107. insets.bottom += nextInsets.bottom;
  108. }
  109. if(insideBorder != null) {
  110. nextInsets = insideBorder.getBorderInsets(c);
  111. insets.top += nextInsets.top;
  112. insets.left += nextInsets.left;
  113. insets.right += nextInsets.right;
  114. insets.bottom += nextInsets.bottom;
  115. }
  116. return insets;
  117. }
  118. /**
  119. * Returns the insets of the composite border by adding
  120. * the insets of the outside border to the insets of the
  121. * inside border.
  122. * @param c the component for which this border insets value applies
  123. */
  124. public Insets getBorderInsets(Component c) {
  125. return getBorderInsets(c, new Insets(0,0,0,0));
  126. }
  127. /**
  128. * Returns the outside border object.
  129. */
  130. public Border getOutsideBorder() {
  131. return outsideBorder;
  132. }
  133. /**
  134. * Returns the inside border object.
  135. */
  136. public Border getInsideBorder() {
  137. return insideBorder;
  138. }
  139. }