1/*
2 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package javax.swing.text.html;
26
27import java.awt.*;
28import java.awt.event.*;
29import java.io.*;
30import java.net.MalformedURLException;
31import java.net.URL;
32import javax.swing.text.*;
33import javax.swing.*;
34import javax.swing.border.*;
35import javax.swing.event.*;
36import java.util.*;
37
38/**
39 * EditableView sets the view it contains to be visible only when the
40 * JTextComponent the view is contained in is editable. The min/pref/max
41 * size is 0 when not visible.
42 *
43 * @author  Scott Violet
44 */
45class EditableView extends ComponentView {
46
47    EditableView(Element e) {
48        super(e);
49    }
50
51    public float getMinimumSpan(int axis) {
52        if (isVisible) {
53            return super.getMinimumSpan(axis);
54        }
55        return 0;
56    }
57
58    public float getPreferredSpan(int axis) {
59        if (isVisible) {
60            return super.getPreferredSpan(axis);
61        }
62        return 0;
63    }
64
65    public float getMaximumSpan(int axis) {
66        if (isVisible) {
67            return super.getMaximumSpan(axis);
68        }
69        return 0;
70    }
71
72    public void paint(Graphics g, Shape allocation) {
73        Component c = getComponent();
74        Container host = getContainer();
75
76        if (host instanceof JTextComponent &&
77            isVisible != ((JTextComponent)host).isEditable()) {
78            isVisible = ((JTextComponent)host).isEditable();
79            preferenceChanged(null, true, true);
80            host.repaint();
81        }
82        /*
83         * Note: we cannot tweak the visible state of the
84         * component in createComponent() even though it
85         * gets called after the setParent() call where
86         * the value of the boolean is set.  This
87         * because, the setComponentParent() in the
88         * superclass, always does a setVisible(false)
89         * after calling createComponent().   We therefore
90         * use this flag in the paint() method to
91         * setVisible() to true if required.
92         */
93        if (isVisible) {
94            super.paint(g, allocation);
95        }
96        else {
97            setSize(0, 0);
98        }
99        if (c != null) {
100            c.setFocusable(isVisible);
101        }
102    }
103
104    public void setParent(View parent) {
105        if (parent != null) {
106            Container host = parent.getContainer();
107            if (host != null) {
108                if (host instanceof JTextComponent) {
109                    isVisible = ((JTextComponent)host).isEditable();
110                } else {
111                    isVisible = false;
112                }
113            }
114        }
115        super.setParent(parent);
116    }
117
118    /**
119     * @return true if the Component is visible.
120     */
121    public boolean isVisible() {
122        return isVisible;
123    }
124
125    /** Set to true if the component is visible. This is based off the
126     * editability of the container. */
127    private boolean isVisible;
128} // End of EditableView
129