jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTextAreaUI.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.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.DocumentEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Provides the look and feel for a plain text editor.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * implementation the default UI is extended to act as a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * view factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * 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
    46
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    52
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class BasicTextAreaUI 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 a JTextArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @param ta a text area
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 ta) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return new BasicTextAreaUI();
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
     * Constructs a new BasicTextAreaUI object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public BasicTextAreaUI() {
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 look up 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 ("TextArea")
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 "TextArea";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        super.installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        //the fix for 4785160 is undone
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * This method gets called when a bound property is changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * on the associated JTextComponent.  This is a hook
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * which UI implementations may change to reflect how the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * UI displays bound properties of JTextComponent subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * This is implemented to rebuild the View when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <em>WrapLine</em> or the <em>WrapStyleWord</em> property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param evt the property change event
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    protected void propertyChange(PropertyChangeEvent evt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        super.propertyChange(evt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (evt.getPropertyName().equals("lineWrap") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            evt.getPropertyName().equals("wrapStyleWord") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                evt.getPropertyName().equals("tabSize")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // rebuild the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            modelChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } else if ("editable".equals(evt.getPropertyName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            updateFocusTraversalKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The method is overridden to take into account caret width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @return the preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @throws IllegalArgumentException if invalid value is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return super.getPreferredSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        //the fix for 4785160 is undone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * The method is overridden to take into account caret width.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param c the editor component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @return the minimum size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @throws IllegalArgumentException if invalid value is passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return super.getMinimumSize(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        //the fix for 4785160 is undone
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
     * Creates the view for an element.  Returns a WrappedPlainView or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * PlainView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        Document doc = elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if ((i18nFlag != null) && i18nFlag.equals(Boolean.TRUE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // build a view that support bidi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return createI18N(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            JTextComponent c = getComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (c instanceof JTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                JTextArea area = (JTextArea) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                View v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                if (area.getLineWrap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    v = new WrappedPlainView(elem, area.getWrapStyleWord());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    v = new PlainView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    View createI18N(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        String kind = elem.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (kind != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            if (kind.equals(AbstractDocument.ContentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                return new PlainParagraph(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                return new BoxView(elem, View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Object i18nFlag = ((JTextComponent)c).getDocument().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                              getProperty("i18n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (Boolean.TRUE.equals(i18nFlag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            View rootView = getRootView((JTextComponent)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if (rootView.getViewCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                int baseline = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                int fieldBaseline = BasicHTML.getBaseline(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                        rootView.getView(0), width - insets.left -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                        insets.right, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                if (fieldBaseline < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                return baseline + fieldBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        FontMetrics fm = c.getFontMetrics(c.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return insets.top + fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
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
     * Paragraph for representing plain-text lines that support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * bidirectional text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    static class PlainParagraph extends ParagraphView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        PlainParagraph(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            layoutPool = new LogicalView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            layoutPool.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if ((c != null) && (! c.getComponentOrientation().isLeftToRight())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                setJustification(StyleConstants.ALIGN_RIGHT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                setJustification(StyleConstants.ALIGN_LEFT);
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
         * Fetch the constraining span to flow against for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
         * the given child index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        public int getFlowSpan(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (c instanceof JTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                JTextArea area = (JTextArea) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                if (! area.getLineWrap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    // no limit if unwrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return super.getFlowSpan(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        protected SizeRequirements calculateMinorAxisRequirements(int axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                                                  SizeRequirements r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            SizeRequirements req = super.calculateMinorAxisRequirements(axis, r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            if (c instanceof JTextArea) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                JTextArea area = (JTextArea) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if (! area.getLineWrap()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    // min is pref if unwrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    req.minimum = req.preferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    req.minimum = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    req.preferred = getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    if (req.preferred == Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        // We have been initially set to MAX_VALUE, but we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                        // don't want this as our preferred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                        req.preferred = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            return req;
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
         * Sets the size of the view.  If the size has changed, layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
         * is redone.  The size is the full size of the view including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
         * the inset areas.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
         * @param width the width >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
         * @param height the height >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if ((int) width != getWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            super.setSize(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * This class can be used to represent a logical view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * a flow.  It keeps the children updated to reflect the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * of the model, gives the logical child views access to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         * view hierarchy, and calculates a preferred span.  It doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * do any rendering, layout, or model/view translation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        static class LogicalView extends CompositeView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            LogicalView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            protected int getViewIndexAtPosition(int pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (elem.getElementCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return elem.getElementIndex(pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            protected boolean updateChildren(DocumentEvent.ElementChange ec,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                             DocumentEvent e, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            protected void loadChildren(ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (elem.getElementCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    super.loadChildren(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                    View v = new GlyphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    append(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                if( getViewCount() != 1 )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                    throw new Error("One child view is assumed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                View v = getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return v.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
             * Forward the DocumentEvent to the given child view.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
             * is implemented to reparent the child to the logical view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
             * (the children may have been parented by a row in the flow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
             * if they fit without breaking) and then execute the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             * behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
             * @param v the child view to forward the event to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
             * @param e the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
             * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
             * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
             * @see #forwardUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
             * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            protected void forwardUpdateToView(View v, DocumentEvent e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                               Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                v.setParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                super.forwardUpdateToView(v, e, a, f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            // The following methods don't do anything useful, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            // simply keep the class from being abstract.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            public void paint(Graphics g, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            protected boolean isBefore(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            protected boolean isAfter(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            protected View getViewAtPoint(int x, int y, Rectangle alloc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            protected void childAllocation(int index, Rectangle a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
}