src/java.desktop/share/classes/javax/swing/text/html/ImageView.java
author kaddepalli
Wed, 14 Mar 2018 15:41:29 +0530
changeset 49295 8d8f74e84ff6
parent 47216 71c04702a3d5
child 51067 0961485fc686
permissions -rw-r--r--
8195095: Images are not scaled correctly in JEditorPane Reviewed-by: ssadetsky, psadhukhan, mhalder
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: 20169
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: 2493
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: 2493
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: 2493
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2493
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2493
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.html;
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.image.ImageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Dictionary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * View of an Image, intended to support the HTML <IMG> tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Supports scaling via the HEIGHT and WIDTH attributes of the tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * If the image is unable to be loaded any text specified via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>ALT</code> attribute will be rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * While this class has been part of swing for a while now, it is public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * as of 1.4.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author  Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see IconView
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class ImageView extends View {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * If true, when some of the bits are available a repaint is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * This is set to false as swing does not offer a repaint that takes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * delay. If this were true, a bunch of immediate repaints would get
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * generated that end up significantly delaying the loading of the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * (or anything else going on for that matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private static boolean sIsInc = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * Repaint delay when some of the bits are available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static int sIncRate = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Property name for pending image icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private static final String PENDING_IMAGE = "html.pendingImage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Property name for missing image icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private static final String MISSING_IMAGE = "html.missingImage";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Document property for image cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static final String IMAGE_CACHE_PROPERTY = "imageCache";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // Height/width to use before we know the real size, these should at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // the size of <code>sMissingImageIcon</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // <code>sPendingImageIcon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final int DEFAULT_WIDTH = 38;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static final int DEFAULT_HEIGHT= 38;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Default border to use if one is not specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static final int DEFAULT_BORDER = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    // Bitmask values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static final int LOADING_FLAG = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static final int LINK_FLAG = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static final int WIDTH_FLAG = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    private static final int HEIGHT_FLAG = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private static final int RELOAD_FLAG = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static final int RELOAD_IMAGE_FLAG = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final int SYNC_LOAD_FLAG = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    private AttributeSet attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private Image image;
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
    98
    private Image disabledImage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /** Bitmask containing some of the above bitmask values. Because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * image loading notification can happen on another thread access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * this is synchronized (at least for modifying it). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private Container container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private Rectangle fBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private Color borderColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Size of the border, the insets contains this valid. For example, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // the HSPACE attribute was 4 and BORDER 2, leftInset would be 6.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private short borderSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    // Insets, obtained from the painter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    private short leftInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private short rightInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private short topInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private short bottomInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * We don't directly implement ImageObserver, instead we use an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * that calls back to us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private ImageObserver imageObserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Used for alt text. Will be non-null if the image couldn't be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * and there is valid alt text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private View altView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /** Alignment along the vertical (Y) axis. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private float vAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Creates a new view that represents an IMG element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * @param elem the element to create a view for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    public ImageView(Element elem) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        super(elem);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        fBounds = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        imageObserver = new ImageHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        state = RELOAD_FLAG | RELOAD_IMAGE_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   144
     * Returns the text to display if the image cannot be loaded. This is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * obtained from the Elements attribute set with the attribute name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * <code>HTML.Attribute.ALT</code>.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   147
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   148
     * @return the test to display if the image cannot be loaded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public String getAltText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return (String)getElement().getAttributes().getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            (HTML.Attribute.ALT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Return a URL for the image source,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * or null if it could not be determined.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   158
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   159
     * @return the URL for the image source, or null if it could not be determined.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public URL getImageURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        String src = (String)getElement().getAttributes().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                             getAttribute(HTML.Attribute.SRC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (src == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        URL reference = ((HTMLDocument)getDocument()).getBase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            URL u = new URL(reference,src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            return null;
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
    /**
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   178
     * Returns the icon to use if the image could not be found.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   179
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   180
     * @return the icon to use if the image could not be found.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public Icon getNoImageIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return (Icon) UIManager.getLookAndFeelDefaults().get(MISSING_IMAGE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the icon to use while in the process of loading the image.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   188
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   189
     * @return the icon to use while in the process of loading the image.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public Icon getLoadingImageIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return (Icon) UIManager.getLookAndFeelDefaults().get(PENDING_IMAGE);
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
     * Returns the image to render.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   197
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   198
     * @return the image to render.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public Image getImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   205
    private Image getImage(boolean enabled) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   206
        Image img = getImage();
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   207
        if (! enabled) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   208
            if (disabledImage == null) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   209
                disabledImage = GrayFilter.createDisabledImage(img);
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   210
            }
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   211
            img = disabledImage;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   212
        }
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   213
        return img;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   214
    }
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   215
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Sets how the image is loaded. If <code>newValue</code> is true,
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   218
     * the image will be loaded when first asked for, otherwise it will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * be loaded asynchronously. The default is to not load synchronously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * that is to load the image asynchronously.
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   221
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   222
     * @param newValue if {@code true} the image will be loaded when first asked for,
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   223
     *                 otherwise it will be asynchronously.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public void setLoadsSynchronously(boolean newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                state |= SYNC_LOAD_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                state = (state | SYNC_LOAD_FLAG) ^ SYNC_LOAD_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   237
     * Returns {@code true} if the image should be loaded when first asked for.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   238
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   239
     * @return {@code true} if the image should be loaded when first asked for.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public boolean getLoadsSynchronously() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return ((state & SYNC_LOAD_FLAG) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   246
     * Convenient method to get the StyleSheet.
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   247
     *
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   248
     * @return the StyleSheet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    protected StyleSheet getStyleSheet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        HTMLDocument doc = (HTMLDocument) getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        return doc.getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Fetches the attributes to use when rendering.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * implemented to multiplex the attributes specified in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * model with a StyleSheet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public AttributeSet getAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * For images the tooltip text comes from text specified with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * <code>ALT</code> attribute. This is overriden to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <code>getAltText</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see JTextComponent#getToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public String getToolTipText(float x, float y, Shape allocation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return getAltText();
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 any cached values that come from attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    protected void setPropertiesFromAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        StyleSheet sheet = getStyleSheet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this.attr = sheet.getViewAttributes(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        // Gutters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        borderSize = (short)getIntAttr(HTML.Attribute.BORDER, isLink() ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                                       DEFAULT_BORDER : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        leftInset = rightInset = (short)(getIntAttr(HTML.Attribute.HSPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                    0) + borderSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        topInset = bottomInset = (short)(getIntAttr(HTML.Attribute.VSPACE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                    0) + borderSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        borderColor = ((StyledDocument)getDocument()).getForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                      (getAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        AttributeSet attr = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        // Alignment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        // PENDING: This needs to be changed to support the CSS versions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        // when conversion from ALIGN to VERTICAL_ALIGN is complete.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Object alignment = attr.getAttribute(HTML.Attribute.ALIGN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        vAlign = 1.0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (alignment != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            alignment = alignment.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            if ("top".equals(alignment)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                vAlign = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            else if ("middle".equals(alignment)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                vAlign = .5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        AttributeSet anchorAttr = (AttributeSet)attr.getAttribute(HTML.Tag.A);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (anchorAttr != null && anchorAttr.isDefined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            (HTML.Attribute.HREF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                state |= LINK_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                state = (state | LINK_FLAG) ^ LINK_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Establishes the parent view for this view.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * Seize this moment to cache the AWT Container I'm in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    public void setParent(View parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        View oldParent = getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        super.setParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        container = (parent != null) ? getContainer() : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (oldParent != parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                state |= RELOAD_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Invoked when the Elements attributes have changed. Recreates the image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        super.changedUpdate(e,a,f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            state |= RELOAD_FLAG | RELOAD_IMAGE_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        // Assume the worst.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * Paints the View.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @param g the rendering surface to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @see View#paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Rectangle rect = (a instanceof Rectangle) ? (Rectangle)a :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                         a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        fBounds.setBounds(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        paintHighlights(g, a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        paintBorder(g, rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            g.clipRect(rect.x + leftInset, rect.y + topInset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                       rect.width - leftInset - rightInset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                       rect.height - topInset - bottomInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   378
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   379
        Container host = getContainer();
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   380
        Image img = getImage(host == null || host.isEnabled());
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   381
        if (img != null) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   382
            if (! hasPixels(img)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                // No pixels yet, use the default
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   384
                Icon icon = getLoadingImageIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                if (icon != null) {
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   386
                    icon.paintIcon(host, g,
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   387
                            rect.x + leftInset, rect.y + topInset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                // Draw the image
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   392
                g.drawImage(img, rect.x + leftInset, rect.y + topInset,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                            width, height, imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            Icon icon = getNoImageIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (icon != null) {
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   399
                icon.paintIcon(host, g,
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   400
                        rect.x + leftInset, rect.y + topInset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            View view = getAltView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            // Paint the view representing the alt text, if its non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            if (view != null && ((state & WIDTH_FLAG) == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                                 width > DEFAULT_WIDTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                // Assume layout along the y direction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                Rectangle altRect = new Rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    (rect.x + leftInset + DEFAULT_WIDTH, rect.y + topInset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                     rect.width - leftInset - rightInset - DEFAULT_WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                     rect.height - topInset - bottomInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                view.paint(g, altRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (clip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            // Reset clip.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            g.setClip(clip.x, clip.y, clip.width, clip.height);
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
    private void paintHighlights(Graphics g, Shape shape) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (container instanceof JTextComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            JTextComponent tc = (JTextComponent)container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            Highlighter h = tc.getHighlighter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (h instanceof LayeredHighlighter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                ((LayeredHighlighter)h).paintLayeredHighlights
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    (g, getStartOffset(), getEndOffset(), shape, tc, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private void paintBorder(Graphics g, Rectangle rect) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        Color color = borderColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if ((borderSize > 0 || image == null) && color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            int xOffset = leftInset - borderSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            int yOffset = topInset - borderSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            int n = (image == null) ? 1 : borderSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            for (int counter = 0; counter < n; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                g.drawRect(rect.x + xOffset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                           rect.y + yOffset + counter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                           rect.width - counter - counter - xOffset -xOffset-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                           rect.height - counter - counter -yOffset-yOffset-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * Determines the preferred span for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * @return   the span the view would like to be rendered into;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *           typically the view is told to render into the span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *           that is returned, although there is no guarantee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *           the parent may choose to resize or break the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    public float getPreferredSpan(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        // If the attributes specified a width/height, always use it!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (axis == View.X_AXIS && (state & WIDTH_FLAG) == WIDTH_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            getPreferredSpanFromAltView(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return width + leftInset + rightInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (axis == View.Y_AXIS && (state & HEIGHT_FLAG) == HEIGHT_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            getPreferredSpanFromAltView(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return height + topInset + bottomInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        Image image = getImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return width + leftInset + rightInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return height + topInset + bottomInset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            View view = getAltView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            float retValue = 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                retValue = view.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            case View.X_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                return retValue + (float)(width + leftInset + rightInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                return retValue + (float)(height + topInset + bottomInset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                throw new IllegalArgumentException("Invalid axis: " + axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Determines the desired alignment for this view along an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * axis.  This is implemented to give the alignment to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * bottom of the icon along the y axis, and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * along the x axis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * @param axis may be either X_AXIS or Y_AXIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * @return the desired alignment; this should be a value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *   between 0.0 and 1.0 where 0 indicates alignment at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *   origin and 1.0 indicates alignment to the full span
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     *   away from the origin; an alignment of 0.5 would be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *   center of the view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public float getAlignment(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        switch (axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        case View.Y_AXIS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            return vAlign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            return super.getAlignment(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Provides a mapping from the document model coordinate space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * to the coordinate space of the view mapped to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * @param pos the position to convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @return the bounding box of the given position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @exception BadLocationException  if the given position does not represent a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *   valid location in the associated document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @see View#modelToView
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        int p0 = getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        int p1 = getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        if ((pos >= p0) && (pos <= p1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            Rectangle r = a.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            if (pos == p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                r.x += r.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            r.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            return r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Provides a mapping from the view coordinate space to the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * coordinate space of the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @param x the X coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @param y the Y coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param a the allocated region to render into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return the location within the model that best represents the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *  given point of view
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * @see View#viewToModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        Rectangle alloc = (Rectangle) a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        if (x < alloc.x + alloc.width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
            bias[0] = Position.Bias.Forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            return getStartOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        bias[0] = Position.Bias.Backward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        return getEndOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * Sets the size of the view.  This should cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * layout of the view if it has any layout duties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   574
     * @param width the width &gt;= 0
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   575
     * @param height the height &gt;= 0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public void setSize(float width, float height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        sync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        if (getImage() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            View view = getAltView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                view.setSize(Math.max(0f, width - (float)(DEFAULT_WIDTH + leftInset + rightInset)),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                             Math.max(0f, height - (float)(topInset + bottomInset)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns true if this image within a link?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    private boolean isLink() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        return ((state & LINK_FLAG) == LINK_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Returns true if the passed in image has a non-zero width and height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    private boolean hasPixels(Image image) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return image != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            (image.getHeight(imageObserver) > 0) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            (image.getWidth(imageObserver) > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * Returns the preferred span of the View used to display the alt text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * or 0 if the view does not exist.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    private float getPreferredSpanFromAltView(int axis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        if (getImage() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            View view = getAltView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            if (view != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                return view.getPreferredSpan(axis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        return 0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Request that this view be repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * Assumes the view is still at its last-drawn location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private void repaint(long delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        if (container != null && fBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            container.repaint(delay, fBounds.x, fBounds.y, fBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                               fBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    /**
25148
1026dc322690 8046446: Fix doclint warnings in javax.swing.text.html package
yan
parents: 23010
diff changeset
   633
     * Convenient method for getting an integer attribute from the elements
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * AttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    private int getIntAttr(HTML.Attribute name, int deflt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        AttributeSet attr = getElement().getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        if (attr.isDefined(name)) {             // does not check parents!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            String val = (String)attr.getAttribute(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            if (val == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                i = deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                try{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    i = Math.max(0, Integer.parseInt(val));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                }catch( NumberFormatException x ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    i = deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            return deflt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Makes sure the necessary properties and image is loaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    private void sync() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        int s = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if ((s & RELOAD_IMAGE_FLAG) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            refreshImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        s = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        if ((s & RELOAD_FLAG) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                state = (state | RELOAD_FLAG) ^ RELOAD_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            setPropertiesFromAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * Loads the image and updates the size accordingly. This should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * invoked instead of invoking <code>loadImage</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * <code>updateImageSize</code> directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    private void refreshImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // clear out width/height/realoadimage flag and set loading flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            state = (state | LOADING_FLAG | RELOAD_IMAGE_FLAG | WIDTH_FLAG |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                     HEIGHT_FLAG) ^ (WIDTH_FLAG | HEIGHT_FLAG |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                                     RELOAD_IMAGE_FLAG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            width = height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            // Load the image
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            loadImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            // And update the size params
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            updateImageSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                // Clear out state in case someone threw an exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                state = (state | LOADING_FLAG) ^ LOADING_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Loads the image from the URL <code>getImageURL</code>. This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * only be invoked from <code>refreshImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    private void loadImage() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        URL src = getImageURL();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        Image newImage = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        if (src != null) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23010
diff changeset
   711
            @SuppressWarnings("unchecked")
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23010
diff changeset
   712
            Dictionary<URL, Image> cache = (Dictionary)getDocument().
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23010
diff changeset
   713
                getProperty(IMAGE_CACHE_PROPERTY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            if (cache != null) {
25193
187a455af8f8 8043549: Fix raw and unchecked lint warnings in javax.swing.text.*
darcy
parents: 23010
diff changeset
   715
                newImage = cache.get(src);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                newImage = Toolkit.getDefaultToolkit().createImage(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                if (newImage != null && getLoadsSynchronously()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                    // Force the image to be loaded by using an ImageIcon.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    ImageIcon ii = new ImageIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    ii.setImage(newImage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        image = newImage;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * Recreates and reloads the image.  This should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * only be invoked from <code>refreshImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    private void updateImageSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        int newWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        int newHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        int newState = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        Image newImage = getImage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        if (newImage != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            Element elem = getElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            AttributeSet attr = elem.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            // Get the width/height and set the state ivar before calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            // anything that might cause the image to be loaded, and thus the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            // ImageHandler to be called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            newWidth = getIntAttr(HTML.Attribute.WIDTH, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if (newWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                newState |= WIDTH_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            newHeight = getIntAttr(HTML.Attribute.HEIGHT, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            if (newHeight > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                newState |= HEIGHT_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            if (newWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                newWidth = newImage.getWidth(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                if (newWidth <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    newWidth = DEFAULT_WIDTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            if (newHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                newHeight = newImage.getHeight(imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                if (newHeight <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                    newHeight = DEFAULT_HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // Make sure the image starts loading:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if ((newState & (WIDTH_FLAG | HEIGHT_FLAG)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                Toolkit.getDefaultToolkit().prepareImage(newImage, newWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                                                         newHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                                                         imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                Toolkit.getDefaultToolkit().prepareImage(newImage, -1, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                                         imageObserver);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            boolean createText = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                // If imageloading failed, other thread may have called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                // ImageLoader which will null out image, hence we check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                // for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                if (image != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    if ((newState & WIDTH_FLAG) == WIDTH_FLAG || width == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                        width = newWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    if ((newState & HEIGHT_FLAG) == HEIGHT_FLAG ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                        height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        height = newHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                    createText = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                    if ((newState & WIDTH_FLAG) == WIDTH_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        width = newWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    if ((newState & HEIGHT_FLAG) == HEIGHT_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        height = newHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                state = state | newState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                state = (state | LOADING_FLAG) ^ LOADING_FLAG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            if (createText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                // Only reset if this thread determined image is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                updateAltTextView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            width = height = DEFAULT_HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            updateAltTextView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Updates the view representing the alt text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    private void updateAltTextView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        String text = getAltText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        if (text != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            ImageLabelView newView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            newView = new ImageLabelView(getElement(), text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                altView = newView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * Returns the view to use for alternate text. This may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    private View getAltView() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        View view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            view = altView;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        if (view != null && view.getParent() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            view.setParent(getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        return view;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * Invokes <code>preferenceChanged</code> on the event displatching
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    private void safePreferenceChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        if (SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                ((AbstractDocument)doc).readLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            preferenceChanged(null, true, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (doc instanceof AbstractDocument) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                ((AbstractDocument)doc).readUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                        safePreferenceChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * ImageHandler implements the ImageObserver to correctly update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * display as new parts of the image become available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
    private class ImageHandler implements ImageObserver {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        // This can come on any thread. If we are in the process of reloading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        // the image and determining our state (loading == true) we don't fire
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // preference changed, or repaint, we just reset the fWidth/fHeight as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        // necessary and return. This is ok as we know when loading finishes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        // it will pick up the new height/width, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        public boolean imageUpdate(Image img, int flags, int x, int y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                                   int newWidth, int newHeight ) {
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   884
            if (img != image && img != disabledImage ||
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   885
                image == null || getParent() == null) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   886
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // Bail out if there was an error:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            if ((flags & (ABORT|ERROR)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                repaint(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                synchronized(ImageView.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    if (image == img) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        // Be sure image hasn't changed since we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                        // initialy synchronize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                        image = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                        if ((state & WIDTH_FLAG) != WIDTH_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                            width = DEFAULT_WIDTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                        if ((state & HEIGHT_FLAG) != HEIGHT_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                            height = DEFAULT_HEIGHT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        }
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   904
                    } else {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   905
                        disabledImage = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    if ((state & LOADING_FLAG) == LOADING_FLAG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        // No need to resize or repaint, still in the process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        // of loading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                updateAltTextView();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                safePreferenceChanged();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   918
            if (image == img) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   919
                // Resize image if necessary:
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   920
                short changed = 0;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   921
                if ((flags & ImageObserver.HEIGHT) != 0 && !getElement().
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   922
                      getAttributes().isDefined(HTML.Attribute.HEIGHT)) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   923
                    changed |= 1;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   924
                }
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   925
                if ((flags & ImageObserver.WIDTH) != 0 && !getElement().
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   926
                      getAttributes().isDefined(HTML.Attribute.WIDTH)) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   927
                    changed |= 2;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   928
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   930
                synchronized(ImageView.this) {
49295
8d8f74e84ff6 8195095: Images are not scaled correctly in JEditorPane
kaddepalli
parents: 47216
diff changeset
   931
                    if ((changed & 1) == 1 && (state & HEIGHT_FLAG) == 0) {
8d8f74e84ff6 8195095: Images are not scaled correctly in JEditorPane
kaddepalli
parents: 47216
diff changeset
   932
                        height = newHeight;
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   933
                    }
49295
8d8f74e84ff6 8195095: Images are not scaled correctly in JEditorPane
kaddepalli
parents: 47216
diff changeset
   934
                    if ((changed & 2) == 2 && (state & WIDTH_FLAG) == 0) {
8d8f74e84ff6 8195095: Images are not scaled correctly in JEditorPane
kaddepalli
parents: 47216
diff changeset
   935
                        width = newWidth;
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   936
                    }
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   937
                    if ((state & LOADING_FLAG) == LOADING_FLAG) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   938
                        // No need to resize or repaint, still in the process of
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   939
                        // loading.
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   940
                        return true;
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   941
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                }
2493
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   943
                if (changed != 0) {
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   944
                    // May need to resize myself, asynchronously:
93a357c96600 4783068: Components with HTML text should gray out the text when disabled
peterz
parents: 2
diff changeset
   945
                    safePreferenceChanged();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            // Repaint when done or when new pixels arrive:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            if ((flags & (FRAMEBITS|ALLBITS)) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                repaint(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            else if ((flags & SOMEBITS) != 0 && sIsInc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                repaint(sIncRate);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            return ((flags & ALLBITS) == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * ImageLabelView is used if the image can't be loaded, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     * the attribute specified an alt attribute. It overriden a handle of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * methods as the text is hardcoded and does not come from the document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
    private class ImageLabelView extends InlineView {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        private Segment segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        private Color fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        ImageLabelView(Element e, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            reset(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        public void reset(String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
            segment = new Segment(text.toCharArray(), 0, text.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        public void paint(Graphics g, Shape a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            // Don't use supers paint, otherwise selection will be wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            // as our start/end offsets are fake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            GlyphPainter painter = getGlyphPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            if (painter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                g.setColor(getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                painter.paint(this, g, a, getStartOffset(), getEndOffset());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        public Segment getText(int p0, int p1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (p0 < 0 || p1 > segment.array.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                throw new RuntimeException("ImageLabelView: Stale view");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            segment.offset = p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            segment.count = p1 - p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            return segment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        public int getStartOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        public int getEndOffset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
            return segment.array.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        public View breakView(int axis, int p0, float pos, float len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
            // Don't allow a break
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        public Color getForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
            View parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (fg == null && (parent = getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                Document doc = getDocument();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                AttributeSet attr = parent.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                if (attr != null && (doc instanceof StyledDocument)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    fg = ((StyledDocument)doc).getForeground(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            return fg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
}