jdk/src/java.desktop/share/classes/javax/swing/text/IconView.java
author serb
Mon, 06 Mar 2017 22:52:45 +0300
changeset 44155 1bf93336ea45
parent 25859 3317bb8137f4
permissions -rw-r--r--
8158209: Editing in TableView breaks the layout, when the document is I18n Reviewed-by: serb, alexsch Contributed-by: Abossolo Foh Guy <guy.abossolo.foh@scientificware.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20158
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text;
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 javax.swing.Icon;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Icon decorator that implements the view interface.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * entire element is used to represent the icon.  This acts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * as a gateway from the display-only View implementations to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * interactive lightweight icons (that is, it allows icons
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to be embedded into the View hierarchy.  The parent of the icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is the container that is handed out by the associated view
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Timothy Prinzing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class IconView extends View  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Creates a new icon view that represents an element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * @param elem the element to create a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public IconView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        c = StyleConstants.getIcon(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // --- View methods ---------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Paints the icon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The real paint behavior occurs naturally from the association
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * that the icon has with its parent container (the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * container hosting this view), so this simply allows us to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * position the icon properly relative to the view.  Since
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * the coordinate system for the view is simply the parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * containers, positioning the child icon is easy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        Rectangle alloc = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        c.paintIcon(getContainer(), g, alloc.x, alloc.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param axis may be either View.X_AXIS or View.Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @return  the span the view would like to be rendered into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *           Typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *           that is returned, although there is no guarantee.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *           The parent may choose to resize or break the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception IllegalArgumentException for an invalid axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return c.getIconWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return c.getIconHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Determines the desired alignment for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * axis.  This is implemented to give the alignment to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * bottom of the icon along the y axis, and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * along the x axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param axis may be either View.X_AXIS or View.Y_AXIS
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   104
     * @return the desired alignment &gt;= 0.0f &amp;&amp; &lt;= 1.0f.  This should be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *   a value between 0.0 and 1.0 where 0 indicates alignment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *   origin and 1.0 indicates alignment to the full span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *   away from the origin.  An alignment of 0.5 would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *   center of the view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            return super.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   123
     * @param pos the position to convert &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception BadLocationException  if the given position does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *   represent a valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if ((pos >= p0) && (pos <= p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Rectangle r = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (pos == p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                r.x += r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            r.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throw new BadLocationException(pos + " not in range " + p0 + "," + p1, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   148
     * @param x the X coordinate &gt;= 0
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   149
     * @param y the Y coordinate &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return the location within the model that best represents the
20158
1c5d22e5b898 8025117: [cleanup] Eliminate doclint errors in javax/swing/text classes
yan
parents: 5506
diff changeset
   152
     *  given point of view &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (x < alloc.x + (alloc.width / 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        bias[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    // --- member variables ------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    private Icon c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}