KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > EditableView


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
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.io.*;
12 import java.net.MalformedURLException JavaDoc;
13 import java.net.URL JavaDoc;
14 import javax.swing.text.*;
15 import javax.swing.*;
16 import javax.swing.border.*;
17 import javax.swing.event.*;
18 import java.util.*;
19
20 /**
21  * EditableView sets the view it contains to be visible only when the
22  * JTextComponent the view is contained in is editable. The min/pref/max
23  * size is 0 when not visible.
24  *
25  * @author Scott Violet
26  * @version 1.11, 12/19/03
27  */

28 class EditableView extends ComponentView {
29
30     EditableView(Element e) {
31     super(e);
32     }
33
34     public float getMinimumSpan(int axis) {
35     if (isVisible) {
36         return super.getMinimumSpan(axis);
37     }
38     return 0;
39     }
40
41     public float getPreferredSpan(int axis) {
42     if (isVisible) {
43         return super.getPreferredSpan(axis);
44     }
45     return 0;
46     }
47
48     public float getMaximumSpan(int axis) {
49     if (isVisible) {
50         return super.getMaximumSpan(axis);
51     }
52     return 0;
53     }
54
55     public void paint(Graphics g, Shape allocation) {
56     Component c = getComponent();
57     Container host = getContainer();
58     
59     if (host != null &&
60         isVisible != ((JTextComponent)host).isEditable()) {
61         isVisible = ((JTextComponent)host).isEditable();
62         preferenceChanged(null, true, true);
63         host.repaint();
64     }
65     /*
66      * Note: we cannot tweak the visible state of the
67      * component in createComponent() even though it
68      * gets called after the setParent() call where
69      * the value of the boolean is set. This
70      * because, the setComponentParent() in the
71      * superclass, always does a setVisible(false)
72      * after calling createComponent(). We therefore
73      * use this flag in the paint() method to
74      * setVisible() to true if required.
75      */

76     if (isVisible) {
77         super.paint(g, allocation);
78     }
79     else {
80         setSize(0, 0);
81     }
82     if (c != null) {
83         c.setFocusable(isVisible);
84     }
85     }
86
87     public void setParent(View parent) {
88     if (parent != null) {
89         Container host = parent.getContainer();
90         if (host != null) {
91         isVisible = ((JTextComponent)host).isEditable();
92         }
93     }
94     super.setParent(parent);
95     }
96     
97     /**
98      * @return true if the Component is visible.
99      */

100     public boolean isVisible() {
101     return isVisible;
102     }
103
104     /** Set to true if the component is visible. This is based off the
105      * editability of the container. */

106     private boolean isVisible;
107 } // End of EditableView
108
Popular Tags