1. /*
  2. * @(#)SynthBorder.java 1.12 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.synth;
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.border.*;
  11. import javax.swing.plaf.UIResource;
  12. import sun.swing.plaf.synth.SynthUI;
  13. /**
  14. * SynthBorder is a border that delegates to a Painter. The Insets
  15. * are determined at construction time.
  16. *
  17. * @version 1.12, 12/19/03
  18. * @author Scott Violet
  19. */
  20. class SynthBorder extends AbstractBorder implements UIResource {
  21. private SynthUI ui;
  22. private Insets insets;
  23. SynthBorder(SynthUI ui, Insets insets) {
  24. this.ui = ui;
  25. this.insets = insets;
  26. }
  27. SynthBorder(SynthUI ui) {
  28. this(ui, null);
  29. }
  30. public void paintBorder(Component c, Graphics g, int x, int y,
  31. int width, int height) {
  32. JComponent jc = (JComponent)c;
  33. SynthContext context = ui.getContext(jc);
  34. SynthStyle style = context.getStyle();
  35. if (style == null) {
  36. assert false: "SynthBorder is being used outside after the UI " +
  37. "has been uninstalled";
  38. return;
  39. }
  40. ui.paintBorder(context, g, x, y, width, height);
  41. context.dispose();
  42. }
  43. /**
  44. * This default implementation returns a new <code>Insets</code>
  45. * instance where the <code>top</code>, <code>left</code>,
  46. * <code>bottom</code>, and
  47. * <code>right</code> fields are set to <code>0</code>.
  48. * @param c the component for which this border insets value applies
  49. * @return the new <code>Insets</code> object initialized to 0
  50. */
  51. public Insets getBorderInsets(Component c) {
  52. return getBorderInsets(c, null);
  53. }
  54. /**
  55. * Reinitializes the insets parameter with this Border's current Insets.
  56. * @param c the component for which this border insets value applies
  57. * @param insets the object to be reinitialized
  58. * @return the <code>insets</code> object
  59. */
  60. public Insets getBorderInsets(Component c, Insets insets) {
  61. if (this.insets != null) {
  62. if (insets == null) {
  63. insets = new Insets(this.insets.top, this.insets.left,
  64. this.insets.bottom, this.insets.right);
  65. }
  66. else {
  67. insets.top = this.insets.top;
  68. insets.bottom = this.insets.bottom;
  69. insets.left = this.insets.left;
  70. insets.right = this.insets.left;
  71. }
  72. }
  73. else if (insets == null) {
  74. insets = new Insets(0, 0, 0, 0);
  75. }
  76. else {
  77. insets.top = insets.bottom = insets.left = insets.right = 0;
  78. }
  79. if (c instanceof JComponent) {
  80. Region region = Region.getRegion((JComponent)c);
  81. Insets margin = null;
  82. if ((region == Region.ARROW_BUTTON || region == Region.BUTTON ||
  83. region == Region.CHECK_BOX ||
  84. region == Region.CHECK_BOX_MENU_ITEM ||
  85. region == Region.MENU || region == Region.MENU_ITEM ||
  86. region == Region.RADIO_BUTTON ||
  87. region == Region.RADIO_BUTTON_MENU_ITEM ||
  88. region == Region.TOGGLE_BUTTON) &&
  89. (c instanceof AbstractButton)) {
  90. margin = ((AbstractButton)c).getMargin();
  91. }
  92. else if (region == Region.TOOL_BAR && (c instanceof JToolBar)) {
  93. margin = ((JToolBar)c).getMargin();
  94. }
  95. else if (region == Region.MENU_BAR && (c instanceof JMenuBar)) {
  96. margin = ((JMenuBar)c).getMargin();
  97. }
  98. if (margin != null) {
  99. insets.top += margin.top;
  100. insets.bottom += margin.bottom;
  101. insets.left += margin.left;
  102. insets.right += margin.right;
  103. }
  104. }
  105. return insets;
  106. }
  107. /**
  108. * This default implementation returns false.
  109. * @return false
  110. */
  111. public boolean isBorderOpaque() {
  112. return false;
  113. }
  114. }