jdk/src/share/classes/javax/swing/text/html/CommentView.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, 2004, 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
 * CommentView subclasses HiddenTagView to contain a JTextArea showing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * a comment. When the textarea is edited the comment 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 textarea 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 CommentView extends HiddenTagView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    CommentView(Element e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected Component createComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        Container host = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (host != null && !((JTextComponent)host).isEditable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        JTextArea ta = new JTextArea(getRepresentedText());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (doc instanceof StyledDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            font = ((StyledDocument)doc).getFont(getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            ta.setFont(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            font = ta.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        updateYAlign(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ta.setBorder(CBorder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ta.getDocument().addDocumentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ta.setFocusable(isVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        return ta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    void resetBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * This is subclassed to put the text on the Comment attribute of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * the Element's AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    void _updateModelFromText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        JTextComponent textC = getTextComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (textC != null && doc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            String text = textC.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            SimpleAttributeSet sas = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            isSettingAttributes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                sas.addAttribute(HTML.Attribute.COMMENT, text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                ((StyledDocument)doc).setCharacterAttributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    (getStartOffset(), getEndOffset() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                     getStartOffset(), sas, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                isSettingAttributes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    JTextComponent getTextComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return (JTextComponent)getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    String getRepresentedText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        AttributeSet as = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (as != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Object comment = as.getAttribute(HTML.Attribute.COMMENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (comment instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                return (String)comment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    static final Border CBorder = new CommentBorder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static final int commentPadding = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static final int commentPaddingD = commentPadding * 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static class CommentBorder extends LineBorder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        CommentBorder() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            super(Color.black, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        public void paintBorder(Component c, Graphics g, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            super.paintBorder(c, g, x + commentPadding, y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                              width - commentPaddingD, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        public Insets getBorderInsets(Component c, Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            Insets retI = super.getBorderInsets(c, insets);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            retI.left += commentPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            retI.right += commentPadding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return retI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        public boolean isBorderOpaque() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    } // End of class CommentView.CommentBorder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
} // End of CommentView