jdk/src/share/classes/javax/swing/plaf/synth/SynthTextPaneUI.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5597 ab490f66d2cf
child 8156 601a0ec31a79
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5597
diff changeset
     2
 * Copyright (c) 2002, 2010, 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: 4394
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: 4394
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: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Provides the look and feel for a styled text editor in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Synth look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * the same version of Swing.  As of 1.4, support for long term storage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author  Shannon Hickey
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    48
 * @since 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    50
public class SynthTextPaneUI extends SynthEditorPaneUI {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Creates a UI for the JTextPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param c the JTextPane object
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    56
     * @return the UI object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return new SynthTextPaneUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Fetches the name used as a key to lookup properties through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * UIManager.  This is used as a prefix to all the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * text properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return the name ("TextPane")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    69
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return "TextPane";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    74
    /**
5594
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    75
     * Installs the UI for a component.  This does the following
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    76
     * things.
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    77
     * <ol>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    78
     * <li>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    79
     * Sets opaqueness of the associated component according to its style,
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    80
     * if the opaque property has not already been set by the client program.
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    81
     * <li>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    82
     * Installs the default caret and highlighter into the
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    83
     * associated component. These properties are only set if their
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    84
     * current value is either {@code null} or an instance of
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    85
     * {@link UIResource}.
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    86
     * <li>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    87
     * Attaches to the editor and model.  If there is no
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    88
     * model, a default one is created.
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    89
     * <li>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    90
     * Creates the view factory and the view hierarchy used
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    91
     * to represent the model.
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    92
     * </ol>
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    93
     *
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    94
     * @param c the editor component
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    95
     * @see BasicTextUI#installUI
3db39773da2e 6954231: SynthTextPaneUI.installUI() doesn't set component to opaque even if prop was not set by client progr
peterz
parents: 4394
diff changeset
    96
     * @see ComponentUI#installUI
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    97
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
    98
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        super.installUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        updateForeground(c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        updateFont(c.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * This method gets called when a bound property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * on the associated JTextComponent.  This is a hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * which UI implementations may change to reflect how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * UI displays bound properties of JTextComponent subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * If the font, foreground or document has changed, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the appropriate property is set in the default style of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param evt the property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   116
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String name = evt.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (name.equals("foreground")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            updateForeground((Color)evt.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        } else if (name.equals("font")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            updateFont((Font)evt.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else if (name.equals("document")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            JComponent comp = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            updateForeground(comp.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            updateFont(comp.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Update the color in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param color the new color to use or null to remove the color attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *              from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    private void updateForeground(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            style.removeAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            StyleConstants.setForeground(style, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Update the font in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param font the new font to use or null to remove the font attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *             from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private void updateFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            style.removeAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            style.removeAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            style.removeAttribute(StyleConstants.Bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            style.removeAttribute(StyleConstants.Italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            StyleConstants.setFontFamily(style, font.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            StyleConstants.setFontSize(style, font.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            StyleConstants.setBold(style, font.isBold());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            StyleConstants.setItalic(style, font.isItalic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   181
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    void paintBackground(SynthContext context, Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        context.getPainter().paintTextPaneBackground(context, g, 0, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                                  c.getWidth(), c.getHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   187
    /**
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   188
     * @inheritDoc
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   189
     */
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   190
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public void paintBorder(SynthContext context, Graphics g, int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                            int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        context.getPainter().paintTextPaneBorder(context, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}