jdk/src/share/classes/javax/swing/plaf/basic/BasicHTML.java
author darcy
Wed, 02 Jul 2014 23:03:27 -0700
changeset 25565 ce603b34c98d
parent 23697 e556a715949f
child 25761 c408b10ef757
permissions -rw-r--r--
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.* 8042849: Fix raw and unchecked warnings in com.sun.java.swing Reviewed-by: pchelko, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.html.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Support for providing html views for the swing components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This translates a simple html string to a javax.swing.text.View
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * implementation that can render the html and provide the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * layout semantics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class BasicHTML {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Create an html renderer for the given component and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * string of html.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static View createHTMLView(JComponent c, String html) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        BasicEditorKit kit = getFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Document doc = kit.createDefaultDocument(c.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                                 c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Object base = c.getClientProperty(documentBaseKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (base instanceof URL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            ((HTMLDocument)doc).setBase((URL)base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Reader r = new StringReader(html);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            kit.read(r, doc, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        ViewFactory f = kit.getViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        View hview = f.create(doc.getDefaultRootElement());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        View v = new Renderer(c, f, hview);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Returns the baseline for the html renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param view the View to get the baseline for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * @param w the width to get the baseline for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param h the height to get the baseline for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @throws IllegalArgumentException if width or height is < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return baseline or a value < 0 indicating there is no reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *                  baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @see java.awt.FontMetrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @see javax.swing.JComponent#getBaseline(int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static int getHTMLBaseline(View view, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (w < 0 || h < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    "Width and height must be >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (view instanceof Renderer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return getBaseline(view.getView(0), w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Gets the baseline for the specified component.  This digs out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * the View client property, and if non-null the baseline is calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * from it.  Otherwise the baseline is the value <code>y + ascent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static int getBaseline(JComponent c, int y, int ascent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                  int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        View view = (View)c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            int baseline = getHTMLBaseline(view, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (baseline < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return y + baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return y + ascent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Gets the baseline for the specified View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static int getBaseline(View view, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (hasParagraph(view)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            view.setSize(w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            return getBaseline(view, new Rectangle(0, 0, w, h));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static int getBaseline(View view, Shape bounds) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (view.getViewCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        AttributeSet attributes = view.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Object name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            name = attributes.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // For html on widgets the header is not visible, skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        bounds = view.getChildAllocation(index, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (bounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        View child = view.getView(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (view instanceof javax.swing.text.ParagraphView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            Rectangle rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (bounds instanceof Rectangle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                rect = (Rectangle)bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                rect = bounds.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return rect.y + (int)(rect.height *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                  child.getAlignment(View.Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return getBaseline(child, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private static boolean hasParagraph(View view) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (view instanceof javax.swing.text.ParagraphView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (view.getViewCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        AttributeSet attributes = view.getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        Object name = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (attributes != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            name = attributes.getAttribute(StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (name == HTML.Tag.HTML && view.getViewCount() > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            // For html on widgets the header is not visible, skip it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            index = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return hasParagraph(view.getView(index));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Check the given string to see if it should trigger the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * html rendering logic in a non-text component that supports
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * html rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static boolean isHTMLString(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if ((s.length() >= 6) && (s.charAt(0) == '<') && (s.charAt(5) == '>')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                String tag = s.substring(1,5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return tag.equalsIgnoreCase(propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Stash the HTML render for the given text into the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * properties of the given JComponent. If the given text is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <em>NOT HTML</em> the property will be cleared of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * This method is useful for ComponentUI implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * that are static (i.e. shared) and get their state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * entirely from the JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static void updateRenderer(JComponent c, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        View value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        View oldValue = (View)c.getClientProperty(BasicHTML.propertyKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Boolean htmlDisabled = (Boolean) c.getClientProperty(htmlDisable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (htmlDisabled != Boolean.TRUE && BasicHTML.isHTMLString(text)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            value = BasicHTML.createHTMLView(c, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (value != oldValue && oldValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            for (int i = 0; i < oldValue.getViewCount(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                oldValue.getView(i).setParent(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        c.putClientProperty(BasicHTML.propertyKey, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * If this client property of a JComponent is set to Boolean.TRUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * the component's 'text' property is never treated as HTML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    private static final String htmlDisable = "html.disable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Key to use for the html renderer when stored as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * client property of a JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static final String propertyKey = "html";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Key stored as a client property to indicate the base that relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * references are resolved against. For example, lets say you keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * your images in the directory resources relative to the code path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * you would use the following the set the base:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *   jComponent.putClientProperty(documentBaseKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *                                xxx.class.getResource("resources/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public static final String documentBaseKey = "html.base";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    static BasicEditorKit getFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (basicHTMLFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            basicHTMLViewFactory = new BasicHTMLViewFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            basicHTMLFactory = new BasicEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return basicHTMLFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * The source of the html renderers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static BasicEditorKit basicHTMLFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Creates the Views that visually represent the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    private static ViewFactory basicHTMLViewFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Overrides to the default stylesheet.  Should consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * just creating a completely fresh stylesheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    private static final String styleChanges =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    "p { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    "body { margin-top: 0; margin-bottom: 0; margin-left: 0; margin-right: 0 }";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * The views produced for the ComponentUI implementations aren't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * going to be edited and don't need full html support.  This kit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * alters the HTMLEditorKit to try and trim things down a bit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * It does the following:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * <li>It doesn't produce Views for things like comments,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * head, title, unknown tags, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <li>It installs a different set of css settings from the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * provided by HTMLEditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
   279
    @SuppressWarnings("serial") // JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    static class BasicEditorKit extends HTMLEditorKit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        /** Shared base style for all documents created by us use. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        private static StyleSheet defaultStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         * Overriden to return our own slimmed down style sheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        public StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (defaultStyles == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                defaultStyles = new StyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                StringReader r = new StringReader(styleChanges);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    defaultStyles.loadRules(r, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                } catch (Throwable e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    // don't want to die in static initialization...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    // just display things wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                defaultStyles.addStyleSheet(super.getStyleSheet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            return defaultStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
         * Sets the async policy to flush everything in one chunk, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
         * to not display unknown tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        public Document createDefaultDocument(Font defaultFont,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                              Color foreground) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            StyleSheet styles = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            StyleSheet ss = new StyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            ss.addStyleSheet(styles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            BasicDocument doc = new BasicDocument(ss, defaultFont, foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            doc.setAsynchronousLoadPriority(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            doc.setPreservesUnknownTags(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return doc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         * Returns the ViewFactory that is used to make sure the Views don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
         * load in the background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            return basicHTMLViewFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * BasicHTMLViewFactory extends HTMLFactory to force images to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * synchronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    static class BasicHTMLViewFactory extends HTMLEditorKit.HTMLFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            View view = super.create(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (view instanceof ImageView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                ((ImageView)view).setLoadsSynchronously(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * The subclass of HTMLDocument that is used as the model. getForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * is overridden to return the foreground property from the Component this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * was created for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
   349
    @SuppressWarnings("serial") // Superclass is not serializable across versions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    static class BasicDocument extends HTMLDocument {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        /** The host, that is where we are rendering. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // private JComponent host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        BasicDocument(StyleSheet s, Font defaultFont, Color foreground) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            super(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            setPreservesUnknownTags(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            setFontAndColor(defaultFont, foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         * Sets the default font and default color. These are set by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * adding a rule for the body that specifies the font and color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * This allows the html to override these should it wish to have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * a custom font or color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        private void setFontAndColor(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            getStyleSheet().addRule(sun.swing.SwingUtilities2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                                    displayPropertiesToCSS(font,fg));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Root text view that acts as an HTML renderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    static class Renderer extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        Renderer(JComponent c, ViewFactory f, View v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            super(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            host = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            factory = f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            view = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            view.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            // initially layout to the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            setSize(view.getPreferredSpan(X_AXIS), view.getPreferredSpan(Y_AXIS));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
         * Fetches the attributes to use when rendering.  At the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * level there are no attributes.  If an attribute is resolved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         * up the view hierarchy this is the end of the line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * Determines the preferred span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (axis == X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                // width currently laid out to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                return width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            return view.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
         * Determines the minimum span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return view.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
         * Determines the maximum span for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
         * @return the span the view would like to be rendered into.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
         *         Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
         *         that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
         *         The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
         * Specifies that a preference has changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         * Child views can call this on the parent to indicate that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         * the preference has changed.  The root view routes this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         * invalidate on the hosting component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
         * This can be called on a different thread from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
         * event dispatching thread and is basically unsafe to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * propagate into the component.  To make this safe,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * the operation is transferred over to the event dispatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         * thread for completion.  It is a design goal that all view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         * methods be safe to call without concern for concurrency,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
         * and this behavior helps make that true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
         * @param child the child view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
         * @param width true if the width preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
         * @param height true if the height preference has changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        public void preferenceChanged(View child, boolean width, boolean height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            host.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
         * Determines the desired alignment for this view along an axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
         * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
         * @return the desired alignment, where 0.0 indicates the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
         *     and 1.0 the full span away from the origin
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            return view.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
         * Renders the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
         * @param g the graphics context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
         * @param allocation the region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            Rectangle alloc = allocation.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            view.setSize(alloc.width, alloc.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            view.paint(g, allocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
         * Sets the view parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
         * @param parent the parent view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            throw new Error("Can't set parent on root view");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
         * Returns the number of views in this view.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
         * this view simply wraps the root of the view hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         * it has exactly one child.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
         * @return the number of views
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
         * @see #getView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        public int getViewCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * Gets the n-th view in this container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * @param n the number of the view to get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        public View getView(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            return view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
         * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            return view.modelToView(pos, a, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
         * @param p0 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
         * @param b0 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
         *  next character represented by p0, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
         * @param p1 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
         * @param b1 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
         *  next character represented by p1, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
         * @exception IllegalArgumentException for an invalid bias argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        public Shape modelToView(int p0, Position.Bias b0, int p1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                 Position.Bias b1, Shape a) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return view.modelToView(p0, b0, p1, b1, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
         * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
         * @param x x coordinate of the view location to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
         * @param y y coordinate of the view location to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
         *    given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return view.viewToModel(x, y, a, bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
         * Returns the document model underlying the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
         * @return the model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        public Document getDocument() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return view.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
         * Returns the starting offset into the model for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
         * @return the starting offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return view.getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * Returns the ending offset into the model for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         * @return the ending offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            return view.getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * Gets the element that this view is mapped to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
         * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        public Element getElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            return view.getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
         * Sets the view size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         * @param width the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
         * @param height the height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            this.width = (int) width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            view.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * Fetches the container hosting the view.  This is useful for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         * things like scheduling a repaint, finding out the host
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
         * components font, etc.  The default implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
         * of this is to forward the query to the parent view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
         * @return the container
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        public Container getContainer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            return host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
         * Fetches the factory to be used for building the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
         * various view fragments that make up the view that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
         * represents the model.  This is what determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
         * how the model will be represented.  This is implemented
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
         * to fetch the factory provided by the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
         * EditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
         * @return the factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        public ViewFactory getViewFactory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            return factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        private View view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        private ViewFactory factory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        private JComponent host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
}