1. /*
  2. * @(#)EditableView.java 1.5 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text.html;
  11. import java.awt.*;
  12. import java.awt.event.*;
  13. import java.io.*;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import javax.swing.text.*;
  17. import javax.swing.*;
  18. import javax.swing.border.*;
  19. import javax.swing.event.*;
  20. import java.util.*;
  21. /**
  22. * EditableView sets the view it contains to be visible only when the
  23. * JTextComponent the view is contained in is editable. The min/pref/max
  24. * size is 0 when not visible.
  25. *
  26. * @author Scott Violet
  27. * @version 1.5, 02/02/00
  28. */
  29. class EditableView extends ComponentView {
  30. EditableView(Element e) {
  31. super(e);
  32. }
  33. public float getMinimumSpan(int axis) {
  34. if (isVisible) {
  35. return super.getMinimumSpan(axis);
  36. }
  37. return 0;
  38. }
  39. public float getPreferredSpan(int axis) {
  40. if (isVisible) {
  41. return super.getPreferredSpan(axis);
  42. }
  43. return 0;
  44. }
  45. public float getMaximumSpan(int axis) {
  46. if (isVisible) {
  47. return super.getMaximumSpan(axis);
  48. }
  49. return 0;
  50. }
  51. public void paint(Graphics g, Shape allocation) {
  52. Component c = getComponent();
  53. Container host = getContainer();
  54. if (host != null &&
  55. isVisible != ((JTextComponent)host).isEditable()) {
  56. isVisible = ((JTextComponent)host).isEditable();
  57. preferenceChanged(null, true, true);
  58. host.repaint();
  59. }
  60. /*
  61. * Note: we cannot tweak the visible state of the
  62. * component in createComponent() even though it
  63. * gets called after the setParent() call where
  64. * the value of the boolean is set. This
  65. * because, the setComponentParent() in the
  66. * superclass, always does a setVisible(false)
  67. * after calling createComponent(). We therefore
  68. * use this flag in the paint() method to
  69. * setVisible() to true if required.
  70. */
  71. if (isVisible) {
  72. super.paint(g, allocation);
  73. }
  74. else {
  75. setSize(0, 0);
  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