0N/A/*
2362N/A * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text;
0N/A
0N/Aimport java.awt.*;
0N/Aimport javax.swing.event.*;
0N/A
0N/A/**
0N/A * A <code>LabelView</code> is a styled chunk of text
0N/A * that represents a view mapped over an element in the
0N/A * text model. It caches the character level attributes
0N/A * used for rendering.
0N/A *
0N/A * @author Timothy Prinzing
0N/A */
0N/Apublic class LabelView extends GlyphView implements TabableView {
0N/A
0N/A /**
0N/A * Constructs a new view wrapped on an element.
0N/A *
0N/A * @param elem the element
0N/A */
0N/A public LabelView(Element elem) {
0N/A super(elem);
0N/A }
0N/A
0N/A /**
0N/A * Synchronize the view's cached values with the model.
0N/A * This causes the font, metrics, color, etc to be
0N/A * re-cached if the cache has been invalidated.
0N/A */
0N/A final void sync() {
0N/A if (font == null) {
0N/A setPropertiesFromAttributes();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets whether or not the view is underlined.
0N/A * Note that this setter is protected and is really
0N/A * only meant if you need to update some additional
0N/A * state when set.
0N/A *
0N/A * @param u true if the view is underlined, otherwise
0N/A * false
0N/A * @see #isUnderline
0N/A */
0N/A protected void setUnderline(boolean u) {
0N/A underline = u;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether or not the view has a strike/line
0N/A * through it.
0N/A * Note that this setter is protected and is really
0N/A * only meant if you need to update some additional
0N/A * state when set.
0N/A *
0N/A * @param s true if the view has a strike/line
0N/A * through it, otherwise false
0N/A * @see #isStrikeThrough
0N/A */
0N/A protected void setStrikeThrough(boolean s) {
0N/A strike = s;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets whether or not the view represents a
0N/A * superscript.
0N/A * Note that this setter is protected and is really
0N/A * only meant if you need to update some additional
0N/A * state when set.
0N/A *
0N/A * @param s true if the view represents a
0N/A * superscript, otherwise false
0N/A * @see #isSuperscript
0N/A */
0N/A protected void setSuperscript(boolean s) {
0N/A superscript = s;
0N/A }
0N/A
0N/A /**
0N/A * Sets whether or not the view represents a
0N/A * subscript.
0N/A * Note that this setter is protected and is really
0N/A * only meant if you need to update some additional
0N/A * state when set.
0N/A *
0N/A * @param s true if the view represents a
0N/A * subscript, otherwise false
0N/A * @see #isSubscript
0N/A */
0N/A protected void setSubscript(boolean s) {
0N/A subscript = s;
0N/A }
0N/A
0N/A /**
0N/A * Sets the background color for the view. This method is typically
0N/A * invoked as part of configuring this <code>View</code>. If you need
0N/A * to customize the background color you should override
0N/A * <code>setPropertiesFromAttributes</code> and invoke this method. A
0N/A * value of null indicates no background should be rendered, so that the
0N/A * background of the parent <code>View</code> will show through.
0N/A *
0N/A * @param bg background color, or null
0N/A * @see #setPropertiesFromAttributes
0N/A * @since 1.5
0N/A */
0N/A protected void setBackground(Color bg) {
0N/A this.bg = bg;
0N/A }
0N/A
0N/A /**
0N/A * Sets the cached properties from the attributes.
0N/A */
0N/A protected void setPropertiesFromAttributes() {
0N/A AttributeSet attr = getAttributes();
0N/A if (attr != null) {
0N/A Document d = getDocument();
0N/A if (d instanceof StyledDocument) {
0N/A StyledDocument doc = (StyledDocument) d;
0N/A font = doc.getFont(attr);
0N/A fg = doc.getForeground(attr);
0N/A if (attr.isDefined(StyleConstants.Background)) {
0N/A bg = doc.getBackground(attr);
0N/A } else {
0N/A bg = null;
0N/A }
0N/A setUnderline(StyleConstants.isUnderline(attr));
0N/A setStrikeThrough(StyleConstants.isStrikeThrough(attr));
0N/A setSuperscript(StyleConstants.isSuperscript(attr));
0N/A setSubscript(StyleConstants.isSubscript(attr));
0N/A } else {
0N/A throw new StateInvariantError("LabelView needs StyledDocument");
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Fetches the <code>FontMetrics</code> used for this view.
0N/A * @deprecated FontMetrics are not used for glyph rendering
0N/A * when running in the JDK.
0N/A */
0N/A @Deprecated
0N/A protected FontMetrics getFontMetrics() {
0N/A sync();
0N/A Container c = getContainer();
0N/A return (c != null) ? c.getFontMetrics(font) :
0N/A Toolkit.getDefaultToolkit().getFontMetrics(font);
0N/A }
0N/A
0N/A /**
0N/A * Fetches the background color to use to render the glyphs.
0N/A * This is implemented to return a cached background color,
0N/A * which defaults to <code>null</code>.
0N/A *
0N/A * @return the cached background color
0N/A * @since 1.3
0N/A */
0N/A public Color getBackground() {
0N/A sync();
0N/A return bg;
0N/A }
0N/A
0N/A /**
0N/A * Fetches the foreground color to use to render the glyphs.
0N/A * This is implemented to return a cached foreground color,
0N/A * which defaults to <code>null</code>.
0N/A *
0N/A * @return the cached foreground color
0N/A * @since 1.3
0N/A */
0N/A public Color getForeground() {
0N/A sync();
0N/A return fg;
0N/A }
0N/A
0N/A /**
0N/A * Fetches the font that the glyphs should be based upon.
0N/A * This is implemented to return a cached font.
0N/A *
0N/A * @return the cached font
0N/A */
0N/A public Font getFont() {
0N/A sync();
0N/A return font;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the glyphs should be underlined. If true,
0N/A * an underline should be drawn through the baseline. This
0N/A * is implemented to return the cached underline property.
0N/A *
0N/A * <p>When you request this property, <code>LabelView</code>
0N/A * re-syncs its state with the properties of the
0N/A * <code>Element</code>'s <code>AttributeSet</code>.
0N/A * If <code>Element</code>'s <code>AttributeSet</code>
0N/A * does not have this property set, it will revert to false.
0N/A *
0N/A * @return the value of the cached
0N/A * <code>underline</code> property
0N/A * @since 1.3
0N/A */
0N/A public boolean isUnderline() {
0N/A sync();
0N/A return underline;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the glyphs should have a strikethrough
0N/A * line. If true, a line should be drawn through the center
0N/A * of the glyphs. This is implemented to return the
0N/A * cached <code>strikeThrough</code> property.
0N/A *
0N/A * <p>When you request this property, <code>LabelView</code>
0N/A * re-syncs its state with the properties of the
0N/A * <code>Element</code>'s <code>AttributeSet</code>.
0N/A * If <code>Element</code>'s <code>AttributeSet</code>
0N/A * does not have this property set, it will revert to false.
0N/A *
0N/A * @return the value of the cached
0N/A * <code>strikeThrough</code> property
0N/A * @since 1.3
0N/A */
0N/A public boolean isStrikeThrough() {
0N/A sync();
0N/A return strike;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the glyphs should be rendered as superscript.
0N/A * @return the value of the cached subscript property
0N/A *
0N/A * <p>When you request this property, <code>LabelView</code>
0N/A * re-syncs its state with the properties of the
0N/A * <code>Element</code>'s <code>AttributeSet</code>.
0N/A * If <code>Element</code>'s <code>AttributeSet</code>
0N/A * does not have this property set, it will revert to false.
0N/A *
0N/A * @return the value of the cached
0N/A * <code>subscript</code> property
0N/A * @since 1.3
0N/A */
0N/A public boolean isSubscript() {
0N/A sync();
0N/A return subscript;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the glyphs should be rendered as subscript.
0N/A *
0N/A * <p>When you request this property, <code>LabelView</code>
0N/A * re-syncs its state with the properties of the
0N/A * <code>Element</code>'s <code>AttributeSet</code>.
0N/A * If <code>Element</code>'s <code>AttributeSet</code>
0N/A * does not have this property set, it will revert to false.
0N/A *
0N/A * @return the value of the cached
0N/A * <code>superscript</code> property
0N/A * @since 1.3
0N/A */
0N/A public boolean isSuperscript() {
0N/A sync();
0N/A return superscript;
0N/A }
0N/A
0N/A // --- View methods ---------------------------------------------
0N/A
0N/A /**
0N/A * Gives notification from the document that attributes were changed
0N/A * in a location that this view is responsible for.
0N/A *
0N/A * @param e the change information from the associated document
0N/A * @param a the current allocation of the view
0N/A * @param f the factory to use to rebuild if the view has children
0N/A * @see View#changedUpdate
0N/A */
0N/A public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
0N/A font = null;
0N/A super.changedUpdate(e, a, f);
0N/A }
0N/A
0N/A // --- variables ------------------------------------------------
0N/A
0N/A private Font font;
0N/A private Color fg;
0N/A private Color bg;
0N/A private boolean underline;
0N/A private boolean strike;
0N/A private boolean superscript;
0N/A private boolean subscript;
0N/A
0N/A}