jdk/src/share/classes/javax/swing/text/html/HiddenTagView.java
author darcy
Wed, 22 Jan 2014 23:20:58 -0800
changeset 22567 5816a47fa4dd
parent 5506 202f599c92aa
child 23697 e556a715949f
permissions -rw-r--r--
8032047: Fix static lint warnings in client libraries 8032048: Add static lint warning to build of jdk repository Reviewed-by: pchelko, serb, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2006, 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.text.html;
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * HiddenTagView subclasses EditableView to contain a JTextField showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the element name. When the textfield is edited the element name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * reset. As this inherits from EditableView if the JTextComponent is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * not editable, the textfield will not be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class HiddenTagView extends EditableView implements DocumentListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    HiddenTagView(Element e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        yAlign = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected Component createComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        JTextField tf = new JTextField(getElement().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            font = ((StyledDocument)doc).getFont(getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            tf.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            font = tf.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        tf.getDocument().addDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        updateYAlign(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        // Create a panel to wrap the textfield so that the textfields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // laf border shows through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        JPanel panel = new JPanel(new BorderLayout());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        panel.setBackground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (isEndTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            panel.setBorder(EndBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            panel.setBorder(StartBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        panel.add(tf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return panel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (axis == View.Y_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return yAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return 0.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public float getMinimumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (axis == View.X_AXIS && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            // Default to preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return Math.max(30, super.getPreferredSpan(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return super.getMinimumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (axis == View.X_AXIS && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            return Math.max(30, super.getPreferredSpan(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return super.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public float getMaximumSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (axis == View.X_AXIS && isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // Default to preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return Math.max(30, super.getMaximumSpan(axis));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return super.getMaximumSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // DocumentListener methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public void insertUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        updateModelFromText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public void removeUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        updateModelFromText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void changedUpdate(DocumentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        updateModelFromText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // View method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (!isSettingAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            setTextFromModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    // local methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    void updateYAlign(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        Container c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        FontMetrics fm = (c != null) ? c.getFontMetrics(font) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Toolkit.getDefaultToolkit().getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        float h = fm.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        float d = fm.getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        yAlign = (h > 0) ? (h - d) / h : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    void resetBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        Component comp = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (isEndTag()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ((JPanel)comp).setBorder(EndBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                ((JPanel)comp).setBorder(StartBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
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
     * This resets the text on the text component we created to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * that of the AttributeSet for the Element we represent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <p>If this is invoked on the event dispatching thread, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * directly invokes <code>_setTextFromModel</code>, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <code>SwingUtilities.invokeLater</code> is used to schedule execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * of <code>_setTextFromModel</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    void setTextFromModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            _setTextFromModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    _setTextFromModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * This resets the text on the text component we created to match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * that of the AttributeSet for the Element we represent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    void _setTextFromModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            isSettingAttributes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            JTextComponent text = getTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                text.setText(getRepresentedText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                resetBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                Container host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                if (host != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    preferenceChanged(this, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    host.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            isSettingAttributes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * This copies the text from the text component we've created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * to the Element's AttributeSet we represent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <p>If this is invoked on the event dispatching thread, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * directly invokes <code>_updateModelFromText</code>, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>SwingUtilities.invokeLater</code> is used to schedule execution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * of <code>_updateModelFromText</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    void updateModelFromText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (!isSettingAttributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                _updateModelFromText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                        _updateModelFromText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * This copies the text from the text component we've created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * to the Element's AttributeSet we represent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    void _updateModelFromText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        Object name = getElement().getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            (StyleConstants.NameAttribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if ((name instanceof HTML.UnknownTag) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            (doc instanceof StyledDocument)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            JTextComponent textComponent = getTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (textComponent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                String text = textComponent.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                isSettingAttributes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    sas.addAttribute(StyleConstants.NameAttribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                                     new HTML.UnknownTag(text));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    ((StyledDocument)doc).setCharacterAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                        (getStartOffset(), getEndOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                         getStartOffset(), sas, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    isSettingAttributes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    JTextComponent getTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        Component comp = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        return (comp == null) ? null : (JTextComponent)((Container)comp).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                       getComponent(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    String getRepresentedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        String retValue = getElement().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return (retValue == null) ? "" : retValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    boolean isEndTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        AttributeSet as = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (as != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            Object end = as.getAttribute(HTML.Attribute.ENDTAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (end != null && (end instanceof String) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                ((String)end).equals("true")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /** Alignment along the y axis, based on the font of the textfield. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    float yAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /** Set to true when setting attributes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    boolean isSettingAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    // Following are for Borders that used for Unknown tags and comments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    // Border defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    static final int circleR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    static final int circleD = circleR * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    static final int tagSize = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    static final int padding = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    static final Color UnknownTagBorderColor = Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    static final Border StartBorder = new StartTagBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    static final Border EndBorder = new EndTagBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    static class StartTagBorder implements Border, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        public void paintBorder(Component c, Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            g.setColor(UnknownTagBorderColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            x += padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            width -= (padding * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            g.drawLine(x, y + circleR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                       x, y + height - circleR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            g.drawArc(x, y + height - circleD - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                      circleD, circleD, 180, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            g.drawArc(x, y, circleD, circleD, 90, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            g.drawLine(x + circleR, y, x + width - tagSize, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            g.drawLine(x + circleR, y + height - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                       x + width - tagSize, y + height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            g.drawLine(x + width - tagSize, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                       x + width - 1, y + height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            g.drawLine(x + width - tagSize, y + height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                       x + width - 1, y + height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        public Insets getBorderInsets(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            return new Insets(2, 2 + padding, 2, tagSize + 2 + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    } // End of class HiddenTagView.StartTagBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    static class EndTagBorder implements Border, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public void paintBorder(Component c, Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            g.setColor(UnknownTagBorderColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            x += padding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            width -= (padding * 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            g.drawLine(x + width - 1, y + circleR,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                       x + width - 1, y + height - circleR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            g.drawArc(x + width - circleD - 1, y + height - circleD - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                      circleD, circleD, 270, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            g.drawArc(x + width - circleD - 1, y, circleD, circleD, 0, 90);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            g.drawLine(x + tagSize, y, x + width - circleR, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            g.drawLine(x + tagSize, y + height - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                       x + width - circleR, y + height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            g.drawLine(x + tagSize, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                       x, y + height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            g.drawLine(x + tagSize, y + height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                       x, y + height / 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        public Insets getBorderInsets(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            return new Insets(2, tagSize + 2 + padding, 2, 2 + padding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    } // End of class HiddenTagView.EndTagBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
} // End of HiddenTagView