1. /*
  2. * @(#)CompoundBorder.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 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
  28. * appropriate for short term storage or RMI between applications running
  29. * the same version of Swing. As of 1.4, support for long term storage
  30. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  31. * has been added to the <code>java.beans</code> package.
  32. * Please see {@link java.beans.XMLEncoder}.
  33. *
  34. * @version 1.19 01/23/03
  35. * @author David Kloba
  36. */
  37. public class CompoundBorder extends AbstractBorder {
  38. protected Border outsideBorder;
  39. protected Border insideBorder;
  40. /**
  41. * Creates a compound border with null outside and inside borders.
  42. */
  43. public CompoundBorder() {
  44. this.outsideBorder = null;
  45. this.insideBorder = null;
  46. }
  47. /**
  48. * Creates a compound border with the specified outside and
  49. * inside borders. Either border may be null.
  50. * @param outsideBorder the outside border
  51. * @param insideBorder the inside border to be nested
  52. */
  53. public CompoundBorder(Border outsideBorder, Border insideBorder) {
  54. this.outsideBorder = outsideBorder;
  55. this.insideBorder = insideBorder;
  56. }
  57. /**
  58. * Returns whether or not this compound border is opaque.
  59. * Returns true if both the inside and outside borders are
  60. * non-null and opaque; returns false otherwise.
  61. */
  62. public boolean isBorderOpaque() {
  63. return (outsideBorder == null || outsideBorder.isBorderOpaque()) &&
  64. (insideBorder == null || insideBorder.isBorderOpaque());
  65. }
  66. /**
  67. * Paints the compound border by painting the outside border
  68. * with the specified position and size and then painting the
  69. * inside border at the specified position and size offset by
  70. * the insets of the outside border.
  71. * @param c the component for which this border is being painted
  72. * @param g the paint graphics
  73. * @param x the x position of the painted border
  74. * @param y the y position of the painted border
  75. * @param width the width of the painted border
  76. * @param height the height of the painted border
  77. */
  78. public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
  79. Insets nextInsets;
  80. int px, py, pw, ph;
  81. px = x;
  82. py = y;
  83. pw = width;
  84. ph = height;
  85. if(outsideBorder != null) {
  86. outsideBorder.paintBorder(c, g, px, py, pw, ph);
  87. nextInsets = outsideBorder.getBorderInsets(c);
  88. px += nextInsets.left;
  89. py += nextInsets.top;
  90. pw = pw - nextInsets.right - nextInsets.left;
  91. ph = ph - nextInsets.bottom - nextInsets.top;
  92. }
  93. if(insideBorder != null)
  94. insideBorder.paintBorder(c, g, px, py, pw, ph);
  95. }
  96. /**
  97. * Reinitialize the insets parameter with this Border's current Insets.
  98. * @param c the component for which this border insets value applies
  99. * @param insets the object to be reinitialized
  100. */
  101. public Insets getBorderInsets(Component c, Insets insets) {
  102. Insets nextInsets;
  103. insets.top = insets.left = insets.right = insets.bottom = 0;
  104. if(outsideBorder != null) {
  105. nextInsets = outsideBorder.getBorderInsets(c);
  106. insets.top += nextInsets.top;
  107. insets.left += nextInsets.left;
  108. insets.right += nextInsets.right;
  109. insets.bottom += nextInsets.bottom;
  110. }
  111. if(insideBorder != null) {
  112. nextInsets = insideBorder.getBorderInsets(c);
  113. insets.top += nextInsets.top;
  114. insets.left += nextInsets.left;
  115. insets.right += nextInsets.right;
  116. insets.bottom += nextInsets.bottom;
  117. }
  118. return insets;
  119. }
  120. /**
  121. * Returns the insets of the composite border by adding
  122. * the insets of the outside border to the insets of the
  123. * inside border.
  124. * @param c the component for which this border insets value applies
  125. */
  126. public Insets getBorderInsets(Component c) {
  127. return getBorderInsets(c, new Insets(0,0,0,0));
  128. }
  129. /**
  130. * Returns the outside border object.
  131. */
  132. public Border getOutsideBorder() {
  133. return outsideBorder;
  134. }
  135. /**
  136. * Returns the inside border object.
  137. */
  138. public Border getInsideBorder() {
  139. return insideBorder;
  140. }
  141. }