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