1. /*
  2. * @(#)SynthViewportUI.java 1.11 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 java.beans.*;
  10. import javax.swing.*;
  11. import javax.swing.border.*;
  12. import javax.swing.plaf.*;
  13. import java.awt.*;
  14. import java.awt.event.*;
  15. import sun.swing.plaf.synth.SynthUI;
  16. /**
  17. * Synth's ViewportUI.
  18. *
  19. * @version 1.11, 12/19/03
  20. */
  21. class SynthViewportUI extends ViewportUI implements
  22. PropertyChangeListener, SynthUI {
  23. private SynthStyle style;
  24. public static ComponentUI createUI(JComponent c) {
  25. return new SynthViewportUI();
  26. }
  27. public void installUI(JComponent c) {
  28. super.installUI(c);
  29. installDefaults(c);
  30. installListeners(c);
  31. }
  32. public void uninstallUI(JComponent c) {
  33. super.uninstallUI(c);
  34. uninstallListeners(c);
  35. uninstallDefaults(c);
  36. }
  37. protected void installDefaults(JComponent c) {
  38. updateStyle(c);
  39. }
  40. private void updateStyle(JComponent c) {
  41. SynthContext context = getContext(c, ENABLED);
  42. // Note: JViewport is special cased as it does not allow for
  43. // a border to be set. JViewport.setBorder is overriden to throw
  44. // an IllegalArgumentException. Refer to SynthScrollPaneUI for
  45. // details of this.
  46. SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(),
  47. context.getRegion());
  48. SynthStyle oldStyle = context.getStyle();
  49. if (newStyle != oldStyle) {
  50. if (oldStyle != null) {
  51. oldStyle.uninstallDefaults(context);
  52. }
  53. context.setStyle(newStyle);
  54. newStyle.installDefaults(context);
  55. }
  56. this.style = newStyle;
  57. context.dispose();
  58. }
  59. protected void installListeners(JComponent c) {
  60. c.addPropertyChangeListener(this);
  61. }
  62. protected void uninstallListeners(JComponent c) {
  63. c.removePropertyChangeListener(this);
  64. }
  65. protected void uninstallDefaults(JComponent c) {
  66. SynthContext context = getContext(c, ENABLED);
  67. style.uninstallDefaults(context);
  68. context.dispose();
  69. style = null;
  70. }
  71. public SynthContext getContext(JComponent c) {
  72. return getContext(c, getComponentState(c));
  73. }
  74. private SynthContext getContext(JComponent c, int state) {
  75. return SynthContext.getContext(SynthContext.class, c,
  76. getRegion(c), style, state);
  77. }
  78. private Region getRegion(JComponent c) {
  79. return SynthLookAndFeel.getRegion(c);
  80. }
  81. private int getComponentState(JComponent c) {
  82. return SynthLookAndFeel.getComponentState(c);
  83. }
  84. public void update(Graphics g, JComponent c) {
  85. SynthContext context = getContext(c);
  86. SynthLookAndFeel.update(context, g);
  87. context.getPainter().paintViewportBackground(context,
  88. g, 0, 0, c.getWidth(), c.getHeight());
  89. paint(context, g);
  90. context.dispose();
  91. }
  92. public void paintBorder(SynthContext context, Graphics g, int x,
  93. int y, int w, int h) {
  94. // This does nothing on purpose, JViewport doesn't allow a border
  95. // and therefor this will NEVER be called.
  96. }
  97. public void paint(Graphics g, JComponent c) {
  98. SynthContext context = getContext(c);
  99. paint(context, g);
  100. context.dispose();
  101. }
  102. protected void paint(SynthContext context, Graphics g) {
  103. }
  104. public void propertyChange(PropertyChangeEvent e) {
  105. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  106. updateStyle((JComponent)e.getSource());
  107. }
  108. }
  109. }