1. /*
  2. * @(#)EditableView.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.text.html;
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import java.io.*;
  11. import java.net.MalformedURLException;
  12. import java.net.URL;
  13. import javax.swing.text.*;
  14. import javax.swing.*;
  15. import javax.swing.border.*;
  16. import javax.swing.event.*;
  17. import java.util.*;
  18. /**
  19. * EditableView sets the view it contains to be visible only when the
  20. * JTextComponent the view is contained in is editable. The min/pref/max
  21. * size is 0 when not visible.
  22. *
  23. * @author Scott Violet
  24. * @version 1.11, 12/19/03
  25. */
  26. class EditableView extends ComponentView {
  27. EditableView(Element e) {
  28. super(e);
  29. }
  30. public float getMinimumSpan(int axis) {
  31. if (isVisible) {
  32. return super.getMinimumSpan(axis);
  33. }
  34. return 0;
  35. }
  36. public float getPreferredSpan(int axis) {
  37. if (isVisible) {
  38. return super.getPreferredSpan(axis);
  39. }
  40. return 0;
  41. }
  42. public float getMaximumSpan(int axis) {
  43. if (isVisible) {
  44. return super.getMaximumSpan(axis);
  45. }
  46. return 0;
  47. }
  48. public void paint(Graphics g, Shape allocation) {
  49. Component c = getComponent();
  50. Container host = getContainer();
  51. if (host != null &&
  52. isVisible != ((JTextComponent)host).isEditable()) {
  53. isVisible = ((JTextComponent)host).isEditable();
  54. preferenceChanged(null, true, true);
  55. host.repaint();
  56. }
  57. /*
  58. * Note: we cannot tweak the visible state of the
  59. * component in createComponent() even though it
  60. * gets called after the setParent() call where
  61. * the value of the boolean is set. This
  62. * because, the setComponentParent() in the
  63. * superclass, always does a setVisible(false)
  64. * after calling createComponent(). We therefore
  65. * use this flag in the paint() method to
  66. * setVisible() to true if required.
  67. */
  68. if (isVisible) {
  69. super.paint(g, allocation);
  70. }
  71. else {
  72. setSize(0, 0);
  73. }
  74. if (c != null) {
  75. c.setFocusable(isVisible);
  76. }
  77. }
  78. public void setParent(View parent) {
  79. if (parent != null) {
  80. Container host = parent.getContainer();
  81. if (host != null) {
  82. isVisible = ((JTextComponent)host).isEditable();
  83. }
  84. }
  85. super.setParent(parent);
  86. }
  87. /**
  88. * @return true if the Component is visible.
  89. */
  90. public boolean isVisible() {
  91. return isVisible;
  92. }
  93. /** Set to true if the component is visible. This is based off the
  94. * editability of the container. */
  95. private boolean isVisible;
  96. } // End of EditableView