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