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