jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicEditorPaneUI.java
author weijun
Thu, 11 Dec 2014 15:19:12 +0800
changeset 28228 be83f404724d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8067207: Replace concat String to append in StringBuilder parameters (client) Reviewed-by: serb Contributed-by: Otavio Santana <otaviojava@java.net>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.html.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Provides the look and feel for a JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 5506
diff changeset
    47
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    53
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
public class BasicEditorPaneUI extends BasicTextUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Creates a UI for the JTextPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param c the JTextPane component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @return the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return new BasicEditorPaneUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Creates a new BasicEditorPaneUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public BasicEditorPaneUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Fetches the name used as a key to lookup properties through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * UIManager.  This is used as a prefix to all the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * text properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @return the name ("EditorPane")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        return "EditorPane";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *{@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        super.installUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        updateDisplayProperties(c.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                c.getForeground());
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
     *{@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        cleanDisplayProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        super.uninstallUI(c);
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
     * Fetches the EditorKit for the UI.  This is whatever is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * currently set in the associated JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @return the editor capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see TextUI#getEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public EditorKit getEditorKit(JTextComponent tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        JEditorPane pane = (JEditorPane) getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return pane.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Fetch an action map to use.  The map for a JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * is not shared because it changes with the EditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    ActionMap getActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        ActionMap am = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        am.put("requestFocus", new FocusAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        EditorKit editorKit = getEditorKit(getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (editorKit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            Action[] actions = editorKit.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                addActions(am, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        am.put(TransferHandler.getCutAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        am.put(TransferHandler.getCopyAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        am.put(TransferHandler.getPasteAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return am;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This method gets called when a bound property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * on the associated JTextComponent.  This is a hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * which UI implementations may change to reflect how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * UI displays bound properties of JTextComponent subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * This is implemented to rebuild the ActionMap based upon an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * EditorKit change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param evt the property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    protected void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        super.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        String name = evt.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if ("editorKit".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            ActionMap map = SwingUtilities.getUIActionMap(getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                Object oldValue = evt.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (oldValue instanceof EditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    Action[] actions = ((EditorKit)oldValue).getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        removeActions(map, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                Object newValue = evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (newValue instanceof EditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    Action[] actions = ((EditorKit)newValue).getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        addActions(map, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            updateFocusTraversalKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else if ("editable".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            updateFocusTraversalKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        } else if ("foreground".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                   || "font".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                   || "document".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                   || JEditorPane.W3C_LENGTH_UNITS.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                   || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                   ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            JComponent c = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            updateDisplayProperties(c.getFont(), c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if ( JEditorPane.W3C_LENGTH_UNITS.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                 || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if ("foreground".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                Object honorDisplayPropertiesObject = c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                boolean honorDisplayProperties = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if (honorDisplayPropertiesObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    honorDisplayProperties =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        ((Boolean)honorDisplayPropertiesObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    void removeActions(ActionMap map, Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        int n = actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            Action a = actions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            map.remove(a.getValue(Action.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    void addActions(ActionMap map, Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int n = actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            Action a = actions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            map.put(a.getValue(Action.NAME), a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    void updateDisplayProperties(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        JComponent c = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        Object honorDisplayPropertiesObject = c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        boolean honorDisplayProperties = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Object w3cLengthUnitsObject = c.getClientProperty(JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                                                          W3C_LENGTH_UNITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        boolean w3cLengthUnits = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (honorDisplayPropertiesObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            honorDisplayProperties =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                ((Boolean)honorDisplayPropertiesObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (w3cLengthUnitsObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            w3cLengthUnits = ((Boolean)w3cLengthUnitsObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (this instanceof BasicTextPaneUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            || honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
             //using equals because can not use UIResource for Boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if (doc instanceof HTMLDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    && honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    updateCSS(font, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    updateStyle(font, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            cleanDisplayProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if ( w3cLengthUnits ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    ((HTMLDocument)doc).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                documentStyleSheet.addRule("W3C_LENGTH_UNITS_ENABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    ((HTMLDocument)doc).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                documentStyleSheet.addRule("W3C_LENGTH_UNITS_DISABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Attribute key to reference the default font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * used in javax.swing.text.StyleContext.getFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * to resolve the default font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    private static final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    void cleanDisplayProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Document document = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (document instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                ((HTMLDocument)document).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (styleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                for (StyleSheet s : styleSheets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    if (s instanceof StyleSheetUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        documentStyleSheet.removeStyleSheet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        documentStyleSheet.addRule("BASE_SIZE_DISABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (style.getAttribute(FONT_ATTRIBUTE_KEY) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                style.removeAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static class StyleSheetUIResource extends StyleSheet implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    private void updateCSS(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        JTextComponent component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        Document document = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (document instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            StyleSheet styleSheet = new StyleSheetUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                ((HTMLDocument)document).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (styleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                for (StyleSheet s : styleSheets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    if (s instanceof StyleSheetUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        documentStyleSheet.removeStyleSheet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            String cssRule = sun.swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                SwingUtilities2.displayPropertiesToCSS(font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                                       fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            styleSheet.addRule(cssRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            documentStyleSheet.addStyleSheet(styleSheet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            documentStyleSheet.addRule("BASE_SIZE " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                       component.getFont().getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            if (! font.equals(style.getAttribute(FONT_ATTRIBUTE_KEY))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                style.addAttribute(FONT_ATTRIBUTE_KEY, font);
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
    private void updateStyle(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        updateFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        updateForeground(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Update the color in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param color the new color to use or null to remove the color attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *              from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private void updateForeground(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (style.getAttribute(StyleConstants.Foreground) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                style.removeAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (! color.equals(StyleConstants.getForeground(style))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                StyleConstants.setForeground(style, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Update the font in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param font the new font to use or null to remove the font attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *             from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private void updateFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        String fontFamily = (String) style.getAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Integer fontSize = (Integer) style.getAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Boolean isBold = (Boolean) style.getAttribute(StyleConstants.Bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Boolean isItalic = (Boolean) style.getAttribute(StyleConstants.Italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        Font  fontAttribute = (Font) style.getAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            if (fontFamily != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                style.removeAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            if (fontSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                style.removeAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            if (isBold != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                style.removeAttribute(StyleConstants.Bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (isItalic != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                style.removeAttribute(StyleConstants.Italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            if (fontAttribute != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                style.removeAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (! font.getName().equals(fontFamily)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                StyleConstants.setFontFamily(style, font.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            if (fontSize == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                  || fontSize.intValue() != font.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                StyleConstants.setFontSize(style, font.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (isBold == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                  || isBold.booleanValue() != font.isBold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                StyleConstants.setBold(style, font.isBold());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (isItalic == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                  || isItalic.booleanValue() != font.isItalic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                StyleConstants.setItalic(style, font.isItalic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            if (! font.equals(fontAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                style.addAttribute(FONT_ATTRIBUTE_KEY, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
}