jdk/src/share/classes/javax/swing/plaf/basic/BasicTextFieldUI.java
author darcy
Wed, 02 Jul 2014 23:03:27 -0700
changeset 25565 ce603b34c98d
parent 22574 7f8ce0c8c20a
permissions -rw-r--r--
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.* 8042849: Fix raw and unchecked warnings in com.sun.java.swing Reviewed-by: pchelko, alexsch
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.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.KeyEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.FocusEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.event.InputEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.Reader;
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 javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * Basis of a look and feel for a JTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * 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
    48
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author  Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
    54
@SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class BasicTextFieldUI extends BasicTextUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Creates a UI for a JTextField.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param c the text field
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return the UI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return new BasicTextFieldUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a new BasicTextFieldUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public BasicTextFieldUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Fetches the name used as a key to lookup properties through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * UIManager.  This is used as a prefix to all the standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * text properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @return the name ("TextField")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected String getPropertyPrefix() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return "TextField";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates a view (FieldView) based on an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param elem the element
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @return the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public View create(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Document doc = elem.getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        Object i18nFlag = doc.getProperty("i18n"/*AbstractDocument.I18NProperty*/);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (Boolean.TRUE.equals(i18nFlag)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // To support bidirectional text, we build a more heavyweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // representation of the field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            String kind = elem.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (kind != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (kind.equals(AbstractDocument.ContentElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    return new GlyphView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                } else if (kind.equals(AbstractDocument.ParagraphElementName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    return new I18nFieldView(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            // this shouldn't happen, should probably throw in this case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        return new FieldView(elem);
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
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        View rootView = getRootView((JTextComponent)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (rootView.getViewCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            Insets insets = c.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            height = height - insets.top - insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (height > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                int baseline = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                View fieldView = rootView.getView(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                int vspan = (int)fieldView.getPreferredSpan(View.Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (height != vspan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    int slop = height - vspan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    baseline += slop / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (fieldView instanceof I18nFieldView) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    int fieldBaseline = BasicHTML.getBaseline(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                            fieldView, width - insets.left - insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    if (fieldBaseline < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    baseline += fieldBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    FontMetrics fm = c.getFontMetrics(c.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    baseline += fm.getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return Component.BaselineResizeBehavior.CENTER_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * A field view that support bidirectional text via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * support provided by ParagraphView.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static class I18nFieldView extends ParagraphView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        I18nFieldView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         * Fetch the constraining span to flow against for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
         * the given child index.  There is no limit for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
         * a field since it scrolls, so this is implemented to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * return <code>Integer.MAX_VALUE</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        public int getFlowSpan(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        protected void setJustification(int j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            // Justification is done in adjustAllocation(), so disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            // ParagraphView's justification handling by doing nothing here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        static boolean isLeftToRight( java.awt.Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
         * Adjusts the allocation given to the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
         * to be a suitable allocation for a text field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
         * If the view has been allocated more than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * preferred span vertically, the allocation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * changed to be centered vertically.  Horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * the view is adjusted according to the horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         * alignment property set on the associated JTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * (if that is the type of the hosting component).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * @param a the allocation given to the view, which may need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         *  to be adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * @return the allocation that the superclass should use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Shape adjustAllocation(Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (a != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                Rectangle bounds = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                int vspan = (int) getPreferredSpan(Y_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                int hspan = (int) getPreferredSpan(X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (bounds.height != vspan) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    int slop = bounds.height - vspan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    bounds.y += slop / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    bounds.height -= slop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // horizontal adjustments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (c instanceof JTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    JTextField field = (JTextField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    BoundedRangeModel vis = field.getHorizontalVisibility();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    int max = Math.max(hspan, bounds.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    int value = vis.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    int extent = Math.min(max, bounds.width - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    if ((value + extent) > max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                        value = max - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    vis.setRangeProperties(value, extent, vis.getMinimum(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                           max, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    if (hspan < bounds.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                        // horizontally align the interior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        int slop = bounds.width - 1 - hspan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                        int align = ((JTextField)c).getHorizontalAlignment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                        if(isLeftToRight(c)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                            if(align==LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                                align = LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                            else if(align==TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                                align = RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                            if(align==LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                                align = RIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                            else if(align==TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                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
                        switch (align) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        case SwingConstants.CENTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            bounds.x += slop / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                            bounds.width -= slop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                        case SwingConstants.RIGHT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            bounds.x += slop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                            bounds.width -= slop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        // adjust the allocation to match the bounded range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        bounds.width = hspan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        bounds.x -= vis.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return bounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
         * Update the visibility model with the associated JTextField
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
         * (if there is one) to reflect the current visibility as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * result of changes to the document model.  The bounded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         * range properties are updated.  If the view hasn't yet been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         * shown the extent will be zero and we just set it to be full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         * until determined otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        void updateVisibilityModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            Component c = getContainer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            if (c instanceof JTextField) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                JTextField field = (JTextField) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                BoundedRangeModel vis = field.getHorizontalVisibility();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                int hspan = (int) getPreferredSpan(X_AXIS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                int extent = vis.getExtent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                int maximum = Math.max(hspan, extent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                extent = (extent == 0) ? maximum : extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                int value = maximum - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                int oldValue = vis.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                if ((oldValue + extent) > maximum) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    oldValue = maximum - extent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                value = Math.max(0, Math.min(value, oldValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                vis.setRangeProperties(value, extent, 0, maximum, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // --- View methods -------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
         * Renders using the given rendering surface and area on that surface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
         * The view may need to do layout and create child views to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
         * itself to render into the given allocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            Rectangle r = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            g.clipRect(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            super.paint(g, adjustAllocation(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * Determines the resizability of the view along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * given axis.  A value of 0 or less is not resizable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * @param axis View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         * @return the weight -> 1 for View.X_AXIS, else 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public int getResizeWeight(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (axis == View.X_AXIS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
         * @param pos the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
         * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
         * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
         *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return super.modelToView(pos, adjustAllocation(a), b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
         * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
         * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         * @param p0 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         * @param b0 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         *  next character represented by p0, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * @param p1 the position to convert >= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * @param b1 the bias toward the previous character or the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         *  next character represented by p1, in case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         *  position is a boundary of two views.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * @return the bounding box of the given position is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * @exception BadLocationException  if the given position does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         *   not represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * @exception IllegalArgumentException for an invalid bias argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        public Shape modelToView(int p0, Position.Bias b0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                 int p1, Position.Bias b1, Shape a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throws BadLocationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return super.modelToView(p0, b0, p1, b1, adjustAllocation(a));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         * @param fx the X coordinate >= 0.0f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * @param fy the Y coordinate >= 0.0f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         *  given point in the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public int viewToModel(float fx, float fy, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            return super.viewToModel(fx, fy, adjustAllocation(a), bias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
         * Gives notification that something was inserted into the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         * @see View#insertUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            super.insertUpdate(changes, adjustAllocation(a), f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            updateVisibilityModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
         * Gives notification that something was removed from the document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
         * in a location that this view is responsible for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * @param changes the change information from the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         * @param a the current allocation of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
         * @param f the factory to use to rebuild if the view has children
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
         * @see View#removeUpdate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        public void removeUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            super.removeUpdate(changes, adjustAllocation(a), f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            updateVisibilityModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
}