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