1. /*
  2. * @(#)SynthViewportUI.java 1.8 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 com.sun.java.swing.plaf.gtk;
  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. /**
  16. * BasicViewport implementation
  17. *
  18. * @version 1.8, 01/23/03 (based on BasicViewportUI v 1.6)
  19. */
  20. class SynthViewportUI extends ViewportUI implements
  21. PropertyChangeListener, SynthUI {
  22. private SynthStyle style;
  23. public static ComponentUI createUI(JComponent c) {
  24. return new SynthViewportUI();
  25. }
  26. public void installUI(JComponent c) {
  27. super.installUI(c);
  28. installDefaults(c);
  29. installListeners(c);
  30. }
  31. public void uninstallUI(JComponent c) {
  32. super.uninstallUI(c);
  33. uninstallListeners(c);
  34. uninstallDefaults(c);
  35. }
  36. protected void installDefaults(JComponent c) {
  37. fetchStyle(c);
  38. }
  39. private void fetchStyle(JComponent c) {
  40. SynthContext context = getContext(c, ENABLED);
  41. // Note: JViewport is special cased as it does not allow for
  42. // a border to be set. JViewport.setBorder is overriden to throw
  43. // an IllegalArgumentException.
  44. SynthStyle newStyle = SynthLookAndFeel.getStyle(context.getComponent(),
  45. context.getRegion());
  46. SynthStyle oldStyle = context.getStyle();
  47. if (newStyle != oldStyle) {
  48. if (oldStyle != null) {
  49. oldStyle.uninstallDefaults(context);
  50. }
  51. context.setStyle(newStyle);
  52. newStyle.installDefaults(context);
  53. }
  54. this.style = newStyle;
  55. context.dispose();
  56. }
  57. protected void installListeners(JComponent c) {
  58. c.addPropertyChangeListener(this);
  59. }
  60. protected void uninstallListeners(JComponent c) {
  61. c.removePropertyChangeListener(this);
  62. }
  63. protected void uninstallDefaults(JComponent c) {
  64. SynthContext context = getContext(c, ENABLED);
  65. style.uninstallDefaults(context);
  66. context.dispose();
  67. style = null;
  68. }
  69. public SynthContext getContext(JComponent c) {
  70. return getContext(c, getComponentState(c));
  71. }
  72. private SynthContext getContext(JComponent c, int state) {
  73. return SynthContext.getContext(SynthContext.class, c,
  74. getRegion(c), style, state);
  75. }
  76. private Region getRegion(JComponent c) {
  77. return SynthLookAndFeel.getRegion(c);
  78. }
  79. private int getComponentState(JComponent c) {
  80. return SynthLookAndFeel.getComponentState(c);
  81. }
  82. public void update(Graphics g, JComponent c) {
  83. SynthContext context = getContext(c);
  84. SynthLookAndFeel.update(context, g);
  85. paint(context, g);
  86. context.dispose();
  87. }
  88. public void paint(Graphics g, JComponent c) {
  89. SynthContext context = getContext(c);
  90. paint(context, g);
  91. context.dispose();
  92. }
  93. protected void paint(SynthContext context, Graphics g) {
  94. // do actual painting
  95. }
  96. public void propertyChange(PropertyChangeEvent e) {
  97. if (SynthLookAndFeel.shouldUpdateStyle(e)) {
  98. fetchStyle((JComponent)e.getSource());
  99. }
  100. }
  101. }