jdk/src/share/classes/javax/swing/plaf/basic/BasicEditorPaneUI.java
author rupashka
Mon, 02 Jun 2008 19:08:13 +0400
changeset 676 8cf833d60e87
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6709530: There are unnecessary code in slider classes, such as in JSlider and SliderUIs Summary: Removed unnecessary code like unused variables, castings, imports etc Reviewed-by: peterz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * of all JavaBeans<sup><font size="-2">TM</font></sup>
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class BasicEditorPaneUI extends BasicTextUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Creates a UI for the JTextPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param c the JTextPane component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @return the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return new BasicEditorPaneUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Creates a new BasicEditorPaneUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public BasicEditorPaneUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Fetches the name used as a key to lookup properties through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * UIManager.  This is used as a prefix to all the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * text properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return the name ("EditorPane")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return "EditorPane";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *{@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super.installUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        updateDisplayProperties(c.getFont(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                                c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *{@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        cleanDisplayProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super.uninstallUI(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Fetches the EditorKit for the UI.  This is whatever is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * currently set in the associated JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @return the editor capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @see TextUI#getEditorKit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public EditorKit getEditorKit(JTextComponent tc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        JEditorPane pane = (JEditorPane) getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return pane.getEditorKit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Fetch an action map to use.  The map for a JEditorPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is not shared because it changes with the EditorKit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    ActionMap getActionMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ActionMap am = new ActionMapUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        am.put("requestFocus", new FocusAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        EditorKit editorKit = getEditorKit(getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (editorKit != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Action[] actions = editorKit.getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                addActions(am, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        am.put(TransferHandler.getCutAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        am.put(TransferHandler.getCopyAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        am.put(TransferHandler.getPasteAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return am;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This method gets called when a bound property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * on the associated JTextComponent.  This is a hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * which UI implementations may change to reflect how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * UI displays bound properties of JTextComponent subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * This is implemented to rebuild the ActionMap based upon an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * EditorKit change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param evt the property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        super.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        String name = evt.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if ("editorKit".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            ActionMap map = SwingUtilities.getUIActionMap(getComponent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (map != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                Object oldValue = evt.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                if (oldValue instanceof EditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    Action[] actions = ((EditorKit)oldValue).getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        removeActions(map, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                Object newValue = evt.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                if (newValue instanceof EditorKit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    Action[] actions = ((EditorKit)newValue).getActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    if (actions != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        addActions(map, actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            updateFocusTraversalKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else if ("editable".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            updateFocusTraversalKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else if ("foreground".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                   || "font".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                   || "document".equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                   || JEditorPane.W3C_LENGTH_UNITS.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                   || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                   ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            JComponent c = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            updateDisplayProperties(c.getFont(), c.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if ( JEditorPane.W3C_LENGTH_UNITS.equals(name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                 || JEditorPane.HONOR_DISPLAY_PROPERTIES.equals(name) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if ("foreground".equals(name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                Object honorDisplayPropertiesObject = c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                boolean honorDisplayProperties = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (honorDisplayPropertiesObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    honorDisplayProperties =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                        ((Boolean)honorDisplayPropertiesObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                }
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
    void removeActions(ActionMap map, Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        int n = actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            Action a = actions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            map.remove(a.getValue(Action.NAME));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    void addActions(ActionMap map, Action[] actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int n = actions.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            Action a = actions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            map.put(a.getValue(Action.NAME), a);
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
    void updateDisplayProperties(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        JComponent c = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        Object honorDisplayPropertiesObject = c.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            getClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        boolean honorDisplayProperties = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        Object w3cLengthUnitsObject = c.getClientProperty(JEditorPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                                                          W3C_LENGTH_UNITS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        boolean w3cLengthUnits = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if (honorDisplayPropertiesObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            honorDisplayProperties =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                ((Boolean)honorDisplayPropertiesObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (w3cLengthUnitsObject instanceof Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            w3cLengthUnits = ((Boolean)w3cLengthUnitsObject).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (this instanceof BasicTextPaneUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            || honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
             //using equals because can not use UIResource for Boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                if (doc instanceof HTMLDocument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                    && honorDisplayProperties) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    updateCSS(font, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    updateStyle(font, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            cleanDisplayProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if ( w3cLengthUnits ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    ((HTMLDocument)doc).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                documentStyleSheet.addRule("W3C_LENGTH_UNITS_ENABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            Document doc = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            if (doc instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    ((HTMLDocument)doc).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                documentStyleSheet.addRule("W3C_LENGTH_UNITS_DISABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
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
     * Attribute key to reference the default font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * used in javax.swing.text.StyleContext.getFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * to resolve the default font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    private static final String FONT_ATTRIBUTE_KEY = "FONT_ATTRIBUTE_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    void cleanDisplayProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        Document document = getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if (document instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                ((HTMLDocument)document).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if (styleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                for (StyleSheet s : styleSheets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (s instanceof StyleSheetUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        documentStyleSheet.removeStyleSheet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        documentStyleSheet.addRule("BASE_SIZE_DISABLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (style.getAttribute(FONT_ATTRIBUTE_KEY) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                style.removeAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
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
    static class StyleSheetUIResource extends StyleSheet implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void updateCSS(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        JTextComponent component = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Document document = component.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if (document instanceof HTMLDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            StyleSheet styleSheet = new StyleSheetUIResource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            StyleSheet documentStyleSheet =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                ((HTMLDocument)document).getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            StyleSheet[] styleSheets = documentStyleSheet.getStyleSheets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (styleSheets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                for (StyleSheet s : styleSheets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    if (s instanceof StyleSheetUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                        documentStyleSheet.removeStyleSheet(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            String cssRule = sun.swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                SwingUtilities2.displayPropertiesToCSS(font,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                                       fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            styleSheet.addRule(cssRule);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            documentStyleSheet.addStyleSheet(styleSheet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            documentStyleSheet.addRule("BASE_SIZE " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                       component.getFont().getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            Style style = ((StyledDocument) document).getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (! font.equals(style.getAttribute(FONT_ATTRIBUTE_KEY))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                style.addAttribute(FONT_ATTRIBUTE_KEY, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
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
    private void updateStyle(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        updateFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        updateForeground(fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Update the color in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param color the new color to use or null to remove the color attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *              from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private void updateForeground(Color color) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (style.getAttribute(StyleConstants.Foreground) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                style.removeAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (! color.equals(StyleConstants.getForeground(style))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                StyleConstants.setForeground(style, color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
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
     * Update the font in the default style of the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param font the new font to use or null to remove the font attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     *             from the document's style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private void updateFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        StyledDocument doc = (StyledDocument)getComponent().getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        Style style = doc.getStyle(StyleContext.DEFAULT_STYLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        String fontFamily = (String) style.getAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        Integer fontSize = (Integer) style.getAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        Boolean isBold = (Boolean) style.getAttribute(StyleConstants.Bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        Boolean isItalic = (Boolean) style.getAttribute(StyleConstants.Italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        Font  fontAttribute = (Font) style.getAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (font == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (fontFamily != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                style.removeAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            if (fontSize != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                style.removeAttribute(StyleConstants.FontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (isBold != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                style.removeAttribute(StyleConstants.Bold);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            if (isItalic != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                style.removeAttribute(StyleConstants.Italic);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            if (fontAttribute != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                style.removeAttribute(FONT_ATTRIBUTE_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (! font.getName().equals(fontFamily)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                StyleConstants.setFontFamily(style, font.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            if (fontSize == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                  || fontSize.intValue() != font.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                StyleConstants.setFontSize(style, font.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            if (isBold == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                  || isBold.booleanValue() != font.isBold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                StyleConstants.setBold(style, font.isBold());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (isItalic == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                  || isItalic.booleanValue() != font.isItalic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                StyleConstants.setItalic(style, font.isItalic());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            if (! font.equals(fontAttribute)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                style.addAttribute(FONT_ATTRIBUTE_KEY, font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
}