jdk/src/share/classes/sun/swing/SwingUtilities2.java
author serb
Mon, 07 Apr 2014 15:34:27 +0400
changeset 24163 d56df89854c4
parent 23298 1c729daf92a0
child 24969 afa6934dd8e8
permissions -rw-r--r--
8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F Reviewed-by: pchelko, flar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4278
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: 4278
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: 4278
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4278
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4278
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import static java.awt.RenderingHints.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.font.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.print.PrinterGraphics;
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
    34
import java.text.CharacterIterator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.text.AttributedCharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.text.AttributedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.*;
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
    39
import javax.swing.event.TreeModelEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.text.Highlighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.text.JTextComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.text.DefaultHighlighter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.swing.text.DefaultCaret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.table.TableCellRenderer;
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
    45
import javax.swing.table.TableColumnModel;
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
    46
import javax.swing.tree.TreeModel;
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
    47
import javax.swing.tree.TreePath;
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
    48
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.print.ProxyPrintGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.font.FontDesignMetrics;
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 1743
diff changeset
    54
import sun.font.FontUtilities;
1724
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
    55
import sun.java2d.SunGraphicsEnvironment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.util.concurrent.Callable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.util.concurrent.Future;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.concurrent.FutureTask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * A collection of utility methods for Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <b>WARNING:</b> While this class is public, it should not be treated as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * public API and its API may change in incompatable ways between dot dot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * releases and even patch releases. You should not rely on this class even
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * existing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class SwingUtilities2 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The <code>AppContext</code> key for our one <code>LAFState</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public static final Object LAF_STATE_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            new StringBuffer("LookAndFeel State");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
23298
1c729daf92a0 7094099: DropDown List of JComboBox detached
anashaty
parents: 23297
diff changeset
    78
    public static final Object MENU_SELECTION_MANAGER_LISTENER_KEY =
1c729daf92a0 7094099: DropDown List of JComboBox detached
anashaty
parents: 23297
diff changeset
    79
            new StringBuffer("MenuSelectionManager listener key");
1c729daf92a0 7094099: DropDown List of JComboBox detached
anashaty
parents: 23297
diff changeset
    80
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    81
    // Maintain a cache of CACHE_SIZE fonts and the left side bearing
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    82
     // of the characters falling into the range MIN_CHAR_INDEX to
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    83
     // MAX_CHAR_INDEX. The values in fontCache are created as needed.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    84
     private static LSBCacheEntry[] fontCache;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    85
     // Windows defines 6 font desktop properties, we will therefore only
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    86
     // cache the metrics for 6 fonts.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    87
     private static final int CACHE_SIZE = 6;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    88
     // nextIndex in fontCache to insert a font into.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    89
     private static int nextIndex;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    90
     // LSBCacheEntry used to search in fontCache to see if we already
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    91
     // have an entry for a particular font
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    92
     private static LSBCacheEntry searchKey;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    93
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    94
     // getLeftSideBearing will consult all characters that fall in the
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    95
     // range MIN_CHAR_INDEX to MAX_CHAR_INDEX.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    96
     private static final int MIN_CHAR_INDEX = (int)'W';
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
    97
     private static final int MAX_CHAR_INDEX = (int)'W' + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final FontRenderContext DEFAULT_FRC =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        new FontRenderContext(null, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * A JComponent client property is used to determine text aa settings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * To avoid having this property persist between look and feels changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * the value of the property is set to null in JComponent.setUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static final Object AA_TEXT_PROPERTY_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                          new StringBuffer("AATextInfoPropertyKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
5763
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   111
     * Attribute key for the content elements.  If it is set on an element, the
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   112
     * element is considered to be a line break.
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   113
     */
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   114
    public static final String IMPLIED_CR = "CR";
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   115
9a942d6fb0aa 6636983: Japanese text does not display correctly in a JEditorPane
rupashka
parents: 5597
diff changeset
   116
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Used to tell a text component, being used as an editor for table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * or tree, how many clicks it took to start editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final StringBuilder SKIP_CLICK_COUNT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        new StringBuilder("skipClickCount");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* Presently this class assumes default fractional metrics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * This may need to change to emulate future platform L&Fs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static class AATextInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   128
        private static AATextInfo getAATextInfoFromMap(Map<java.awt.RenderingHints.Key, Object> hints) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            Object aaHint   = hints.get(KEY_TEXT_ANTIALIASING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            Object contHint = hints.get(KEY_TEXT_LCD_CONTRAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (aaHint == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                return new AATextInfo(aaHint, (Integer)contHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   142
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public static AATextInfo getAATextInfo(boolean lafCondition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            SunToolkit.setAAFontSettingsCondition(lafCondition);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            Toolkit tk = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Object map = tk.getDesktopProperty(SunToolkit.DESKTOPFONTHINTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (map instanceof Map) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   148
                return getAATextInfoFromMap((Map<java.awt.RenderingHints.Key, Object>)map);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Object aaHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        Integer lcdContrastHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        FontRenderContext frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /* These are rarely constructed objects, and only when a complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * UI is being updated, so the cost of the tests here is minimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * and saves tests elsewhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         * We test that the values are ones we support/expect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        public AATextInfo(Object aaHint, Integer lcdContrastHint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (aaHint == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                throw new InternalError("null not allowed here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            if (aaHint == VALUE_TEXT_ANTIALIAS_OFF ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                aaHint == VALUE_TEXT_ANTIALIAS_DEFAULT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new InternalError("AA must be on");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            this.aaHint = aaHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            this.lcdContrastHint = lcdContrastHint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            this.frc = new FontRenderContext(null, aaHint,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                                             VALUE_FRACTIONALMETRICS_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Key used in client properties used to indicate that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <code>ComponentUI</code> of the JComponent instance should be returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static final Object COMPONENT_UI_PROPERTY_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                            new StringBuffer("ComponentUIPropertyKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /** Client Property key for the text maximal offsets for BasicMenuItemUI */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static final StringUIClientPropertyKey BASICMENUITEMUI_MAX_TEXT_OFFSET =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        new StringUIClientPropertyKey ("maxTextOffset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // security stuff
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private static final String UntrustedClipboardAccess =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        "UNTRUSTED_CLIPBOARD_ACCESS_KEY";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    //all access to  charsBuffer is to be synchronized on charsBufferLock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static final int CHAR_BUFFER_SIZE = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static final Object charsBufferLock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static char[] charsBuffer = new char[CHAR_BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   198
    static {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   199
        fontCache = new LSBCacheEntry[CACHE_SIZE];
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   200
    }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   201
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /**
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   203
     * Fill the character buffer cache.  Return the buffer length.
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   204
     */
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   205
    private static int syncCharsBuffer(String s) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   206
        int length = s.length();
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   207
        if ((charsBuffer == null) || (charsBuffer.length < length)) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   208
            charsBuffer = s.toCharArray();
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   209
        } else {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   210
            s.getChars(0, length, charsBuffer, 0);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   211
        }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   212
        return length;
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   213
    }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   214
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   215
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * checks whether TextLayout is required to handle characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param text characters to be tested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * @param start start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param limit limit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @return <tt>true</tt>  if TextLayout is required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *         <tt>false</tt> if TextLayout is not required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static final boolean isComplexLayout(char[] text, int start, int limit) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 1743
diff changeset
   225
        return FontUtilities.isComplexText(text, start, limit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // WARNING WARNING WARNING WARNING WARNING WARNING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // Many of the following methods are invoked from older API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    // As this older API was not passed a Component, a null Component may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // now be passsed in.  For example, SwingUtilities.computeStringWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    // is implemented to call SwingUtilities2.stringWidth, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    // SwingUtilities variant does not take a JComponent, as such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    // SwingUtilities2.stringWidth can be passed a null Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // In other words, if you add new functionality to these methods you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // need to gracefully handle null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * Returns whether or not text should be drawn antialiased.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param c JComponent to test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return Whether or not text should be drawn antialiased for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         specified component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    public static AATextInfo drawTextAntialiased(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            /* a non-null property implies some form of AA requested */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return (AATextInfo)c.getClientProperty(AA_TEXT_PROPERTY_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        // No component, assume aa is off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Returns the left side bearing of the first character of string. The
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   258
     * left side bearing is calculated from the passed in
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   259
     * FontMetrics.  If the passed in String is less than one
3980
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   260
     * character {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param c JComponent that will display the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param fm FontMetrics used to measure the String width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param string String to get the left side bearing for.
3980
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   265
     * @throws NullPointerException if {@code string} is {@code null}
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   266
     *
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   267
     * @return the left side bearing of the first character of string
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   268
     * or {@code 0} if the string is empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public static int getLeftSideBearing(JComponent c, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                         String string) {
9231
99c8b3f7a8d8 7036148: NullPointerException with null JMenu name
alexp
parents: 7668
diff changeset
   272
        if ((string == null) || (string.length() == 0)) {
99c8b3f7a8d8 7036148: NullPointerException with null JMenu name
alexp
parents: 7668
diff changeset
   273
            return 0;
3980
b41744c10146 6883341: SWAT: jdk7-b72 swat build(2009-09-17) threw exceptions when running Java2D demo by clicking Paint ta
alexp
parents: 3976
diff changeset
   274
        }
9231
99c8b3f7a8d8 7036148: NullPointerException with null JMenu name
alexp
parents: 7668
diff changeset
   275
        return getLeftSideBearing(c, fm, string.charAt(0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   279
     * Returns the left side bearing of the first character of string. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * left side bearing is calculated from the passed in FontMetrics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param c JComponent that will display the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @param fm FontMetrics used to measure the String width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param firstChar Character to get the left side bearing for.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public static int getLeftSideBearing(JComponent c, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                         char firstChar) {
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   288
        int charIndex = (int) firstChar;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   289
        if (charIndex < MAX_CHAR_INDEX && charIndex >= MIN_CHAR_INDEX) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   290
            byte[] lsbs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   292
            FontRenderContext frc = getFontRenderContext(c, fm);
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   293
            Font font = fm.getFont();
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   294
            synchronized (SwingUtilities2.class) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   295
                LSBCacheEntry entry = null;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   296
                if (searchKey == null) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   297
                    searchKey = new LSBCacheEntry(frc, font);
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   298
                } else {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   299
                    searchKey.reset(frc, font);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   301
                // See if we already have an entry for this pair
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   302
                for (LSBCacheEntry cacheEntry : fontCache) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if (searchKey.equals(cacheEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                        entry = cacheEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   308
                if (entry == null) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   309
                    // No entry for this pair, add it.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   310
                    entry = searchKey;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   311
                    fontCache[nextIndex] = searchKey;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   312
                    searchKey = null;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   313
                    nextIndex = (nextIndex + 1) % CACHE_SIZE;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   314
                }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   315
                return entry.getLeftSideBearing(firstChar);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
   318
        return 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the FontMetrics for the current Font of the passed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * in Graphics.  This method is used when a Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * is available, typically when painting.  If a Graphics is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * available the JComponent method of the same name should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Callers should pass in a non-null JComponent, the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * to this is if a JComponent is not readily available at the time of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * This does not necessarily return the FontMetrics from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @param c JComponent requesting FontMetrics, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @param g Graphics Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static FontMetrics getFontMetrics(JComponent c, Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        return getFontMetrics(c, g, g.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Returns the FontMetrics for the specified Font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * This method is used when a Graphics is available, typically when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * painting.  If a Graphics is not available the JComponent method of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * the same name should be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * Callers should pass in a non-null JComonent, the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * to this is if a JComponent is not readily available at the time of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * This does not necessarily return the FontMetrics from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param c JComponent requesting FontMetrics, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param c Graphics Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @param font Font to get FontMetrics for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static FontMetrics getFontMetrics(JComponent c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                                             Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            // Note: We assume that we're using the FontMetrics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            // from the widget to layout out text, otherwise we can get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            // mismatches when printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return c.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        return Toolkit.getDefaultToolkit().getFontMetrics(font);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Returns the width of the passed in String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * If the passed String is <code>null</code>, returns zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param c JComponent that will display the string, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param fm FontMetrics used to measure the String width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param string String to get the width of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    public static int stringWidth(JComponent c, FontMetrics fm, String string){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (string == null || string.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   383
        boolean needsTextLayout = ((c != null) &&
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   384
                (c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null));
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   385
        if (needsTextLayout) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   386
            synchronized(charsBufferLock) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   387
                int length = syncCharsBuffer(string);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   388
                needsTextLayout = isComplexLayout(charsBuffer, 0, length);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   389
            }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   390
        }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   391
        if (needsTextLayout) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   392
            TextLayout layout = createTextLayout(c, string,
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   393
                                    fm.getFont(), fm.getFontRenderContext());
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   394
            return (int) layout.getAdvance();
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   395
        } else {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   396
            return fm.stringWidth(string);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   397
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * Clips the passed in String to the space provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @param c JComponent that will display the string, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @param fm FontMetrics used to measure the String width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * @param string String to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param availTextWidth Amount of space that the string can be drawn in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return Clipped string that can fit in the provided space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    public static String clipStringIfNecessary(JComponent c, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                                               String string,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                               int availTextWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        if ((string == null) || (string.equals("")))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        int textWidth = SwingUtilities2.stringWidth(c, fm, string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (textWidth > availTextWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            return SwingUtilities2.clipString(c, fm, string, availTextWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        return string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * Clips the passed in String to the space provided.  NOTE: this assumes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * the string does not fit in the available space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param c JComponent that will display the string, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param fm FontMetrics used to measure the String width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param string String to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * @param availTextWidth Amount of space that the string can be drawn in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return Clipped string that can fit in the provided space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    public static String clipString(JComponent c, FontMetrics fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                    String string, int availTextWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        // c may be null here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        String clipString = "...";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        availTextWidth -= SwingUtilities2.stringWidth(c, fm, clipString);
5948
bd26e23a33e0 6959260: javax/swing/JLabel/6501991/bug6501991.java failed on build 1.7.0-ea-b96
peterz
parents: 5763
diff changeset
   439
        if (availTextWidth <= 0) {
bd26e23a33e0 6959260: javax/swing/JLabel/6501991/bug6501991.java failed on build 1.7.0-ea-b96
peterz
parents: 5763
diff changeset
   440
            //can not fit any characters
bd26e23a33e0 6959260: javax/swing/JLabel/6501991/bug6501991.java failed on build 1.7.0-ea-b96
peterz
parents: 5763
diff changeset
   441
            return clipString;
bd26e23a33e0 6959260: javax/swing/JLabel/6501991/bug6501991.java failed on build 1.7.0-ea-b96
peterz
parents: 5763
diff changeset
   442
        }
bd26e23a33e0 6959260: javax/swing/JLabel/6501991/bug6501991.java failed on build 1.7.0-ea-b96
peterz
parents: 5763
diff changeset
   443
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   444
        boolean needsTextLayout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        synchronized (charsBufferLock) {
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   446
            int stringLength = syncCharsBuffer(string);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            needsTextLayout =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                isComplexLayout(charsBuffer, 0, stringLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (!needsTextLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                int width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                for (int nChars = 0; nChars < stringLength; nChars++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                    width += fm.charWidth(charsBuffer[nChars]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    if (width > availTextWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        string = string.substring(0, nChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        break;
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
        if (needsTextLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            FontRenderContext frc = getFontRenderContext(c, fm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            AttributedString aString = new AttributedString(string);
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   463
            if (c != null) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   464
                aString.addAttribute(TextAttribute.NUMERIC_SHAPING,
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   465
                        c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   466
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            LineBreakMeasurer measurer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                new LineBreakMeasurer(aString.getIterator(), frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            int nChars = measurer.nextOffset(availTextWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            string = string.substring(0, nChars);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        return string + clipString;
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
     * Draws the string at the specified location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @param c JComponent that will display the string, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @param g Graphics to draw the text to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @param text String to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @param x X coordinate to draw the text at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @param y Y coordinate to draw the text at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public static void drawString(JComponent c, Graphics g, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                  int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // c may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // All non-editable widgets that draw strings call into this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        // methods.  By non-editable that means widgets like JLabel, JButton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // but NOT JTextComponents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        if ( text == null || text.length() <= 0 ) { //no need to paint empty strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (isPrinting(g)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            Graphics2D g2d = getGraphics2D(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            if (g2d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                /* The printed text must scale linearly with the UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                 * Calculate the width on screen, obtain a TextLayout with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                 * advances for the printer graphics FRC, and then justify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                 * it to fit in the screen width. This distributes the spacing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                 * more evenly than directly laying out to the screen advances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                 */
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   505
                String trimmedText = trimTrailingSpaces(text);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   506
                if (!trimmedText.isEmpty()) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   507
                    float screenWidth = (float) g2d.getFont().getStringBounds
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   508
                            (trimmedText, DEFAULT_FRC).getWidth();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   509
                    TextLayout layout = createTextLayout(c, text, g2d.getFont(),
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   510
                                                       g2d.getFontRenderContext());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   512
                    layout = layout.getJustifiedLayout(screenWidth);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   513
                    /* Use alternate print color if specified */
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   514
                    Color col = g2d.getColor();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   515
                    if (col instanceof PrintColorUIResource) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   516
                        g2d.setColor(((PrintColorUIResource)col).getPrintColor());
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   517
                    }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   518
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   519
                    layout.draw(g2d, x, y);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   520
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   521
                    g2d.setColor(col);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        // If we get here we're not printing
10869
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   529
        if (g instanceof Graphics2D) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   530
            AATextInfo info = drawTextAntialiased(c);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            Graphics2D g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   533
            boolean needsTextLayout = ((c != null) &&
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   534
                (c.getClientProperty(TextAttribute.NUMERIC_SHAPING) != null));
10869
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   535
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   536
            if (needsTextLayout) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   537
                synchronized(charsBufferLock) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   538
                    int length = syncCharsBuffer(text);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   539
                    needsTextLayout = isComplexLayout(charsBuffer, 0, length);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   540
                }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   541
            }
10869
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   542
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   543
            if (info != null) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   544
                Object oldContrast = null;
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   545
                Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   546
                if (info.aaHint != oldAAValue) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   547
                    g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   548
                } else {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   549
                    oldAAValue = null;
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   550
                }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   551
                if (info.lcdContrastHint != null) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   552
                    oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   553
                    if (info.lcdContrastHint.equals(oldContrast)) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   554
                        oldContrast = null;
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   555
                    } else {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   556
                        g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   557
                                            info.lcdContrastHint);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   558
                    }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   559
                }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   560
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   561
                if (needsTextLayout) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   562
                    TextLayout layout = createTextLayout(c, text, g2.getFont(),
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   563
                                                    g2.getFontRenderContext());
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   564
                    layout.draw(g2, x, y);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   565
                } else {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   566
                    g.drawString(text, x, y);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   567
                }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   568
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   569
                if (oldAAValue != null) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   570
                    g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   571
                }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   572
                if (oldContrast != null) {
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   573
                    g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   574
                }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   575
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   576
                return;
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   577
            }
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   578
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   579
            if (needsTextLayout){
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   580
                TextLayout layout = createTextLayout(c, text, g2.getFont(),
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   581
                                                    g2.getFontRenderContext());
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   582
                layout.draw(g2, x, y);
10869
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   583
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        }
10869
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   586
df518de6f6fd 7077293: javax/swing/JComponent/4337267/bug4337267.java failed on windows 2003
rupashka
parents: 9231
diff changeset
   587
        g.drawString(text, x, y);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Draws the string at the specified location underlining the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @param c JComponent that will display the string, may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @param g Graphics to draw the text to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param text String to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @param underlinedIndex Index of a character in the string to underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @param x X coordinate to draw the text at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * @param y Y coordinate to draw the text at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    public static void drawStringUnderlineCharAt(JComponent c,Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                           String text, int underlinedIndex, int x,int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        if (text == null || text.length() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        SwingUtilities2.drawString(c, g, text, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        int textLength = text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (underlinedIndex >= 0 && underlinedIndex < textLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            int underlineRectY = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            int underlineRectHeight = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            int underlineRectX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            int underlineRectWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            boolean isPrinting = isPrinting(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            boolean needsTextLayout = isPrinting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            if (!needsTextLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                synchronized (charsBufferLock) {
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   617
                    syncCharsBuffer(text);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    needsTextLayout =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        isComplexLayout(charsBuffer, 0, textLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (!needsTextLayout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                FontMetrics fm = g.getFontMetrics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                underlineRectX = x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    SwingUtilities2.stringWidth(c,fm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                        text.substring(0,underlinedIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                underlineRectWidth = fm.charWidth(text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                                                  charAt(underlinedIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                Graphics2D g2d = getGraphics2D(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                if (g2d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    TextLayout layout =
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   633
                        createTextLayout(c, text, g2d.getFont(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                                       g2d.getFontRenderContext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    if (isPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                        float screenWidth = (float)g2d.getFont().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                            getStringBounds(text, DEFAULT_FRC).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                        layout = layout.getJustifiedLayout(screenWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    TextHitInfo leading =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                        TextHitInfo.leading(underlinedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    TextHitInfo trailing =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                        TextHitInfo.trailing(underlinedIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    Shape shape =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        layout.getVisualHighlightShape(leading, trailing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    Rectangle rect = shape.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    underlineRectX = x + rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    underlineRectWidth = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            g.fillRect(underlineRectX, underlineRectY + 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                       underlineRectWidth, underlineRectHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * A variation of locationToIndex() which only returns an index if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Point is within the actual bounds of a list item (not just in the cell)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * and if the JList has the "List.isFileList" client property set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * Otherwise, this method returns -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * This is used to make WindowsL&F JFileChooser act like native dialogs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     */
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   664
    public static int loc2IndexFileList(JList<?> list, Point point) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        int index = list.locationToIndex(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            Object bySize = list.getClientProperty("List.isFileList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if (bySize instanceof Boolean && ((Boolean)bySize).booleanValue() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                !pointIsInActualBounds(list, index, point)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * Returns true if the given point is within the actual bounds of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * JList item at index (not just inside the cell).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   681
    private static <T> boolean pointIsInActualBounds(JList<T> list, int index,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                                                Point point) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   683
        ListCellRenderer<? super T> renderer = list.getCellRenderer();
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 22060
diff changeset
   684
        T value = list.getModel().getElementAt(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        Component item = renderer.getListCellRendererComponent(list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                          value, index, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        Dimension itemSize = item.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (!item.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            cellBounds.x += (cellBounds.width - itemSize.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        cellBounds.width = itemSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        return cellBounds.contains(point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * Returns true if the given point is outside the preferredSize of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * item at the given row of the table.  (Column must be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Does not check the "Table.isFileList" property. That should be checked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * before calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * This is used to make WindowsL&F JFileChooser act like native dialogs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public static boolean pointOutsidePrefSize(JTable table, int row, int column, Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (table.convertColumnIndexToModel(column) != 0 || row == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        TableCellRenderer tcr = table.getCellRenderer(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        Object value = table.getValueAt(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        Component cell = tcr.getTableCellRendererComponent(table, value, false,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                false, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        Dimension itemSize = cell.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        Rectangle cellBounds = table.getCellRect(row, column, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        cellBounds.width = itemSize.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        cellBounds.height = itemSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // See if coords are inside
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        // ASSUME: mouse x,y will never be < cell's x,y
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        assert (p.x >= cellBounds.x && p.y >= cellBounds.y);
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   721
        return p.x > cellBounds.x + cellBounds.width ||
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   722
                p.y > cellBounds.y + cellBounds.height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * Set the lead and anchor without affecting selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public static void setLeadAnchorWithoutSelection(ListSelectionModel model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                                     int lead, int anchor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        if (anchor == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
            anchor = lead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        if (lead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            model.setAnchorSelectionIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            model.setLeadSelectionIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (model.isSelectedIndex(lead)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                model.addSelectionInterval(lead, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                model.removeSelectionInterval(lead, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            model.setAnchorSelectionIndex(anchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     * Ignore mouse events if the component is null, not enabled, the event
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * is not associated with the left mouse button, or the event has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * consumed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    public static boolean shouldIgnore(MouseEvent me, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        return c == null || !c.isEnabled()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                         || !SwingUtilities.isLeftMouseButton(me)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                         || me.isConsumed();
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
     * Request focus on the given component if it doesn't already have it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * and <code>isRequestFocusEnabled()</code> returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public static void adjustFocus(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (!c.hasFocus() && c.isRequestFocusEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            c.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * The following draw functions have the same semantic as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * Graphics methods with the same names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * this is used for printing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    public static int drawChars(JComponent c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                 char[] data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                                 int offset,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                 int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                                 int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                                 int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        if ( length <= 0 ) { //no need to paint empty strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            return x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        int nextX = x + getFontMetrics(c, g).charsWidth(data, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (isPrinting(g)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            Graphics2D g2d = getGraphics2D(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            if (g2d != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                FontRenderContext deviceFontRenderContext = g2d.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                    getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                FontRenderContext frc = getFontRenderContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                if (frc != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                    !isFontRenderContextPrintCompatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                    (deviceFontRenderContext, frc)) {
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   792
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   793
                    String text = new String(data, offset, length);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   794
                    TextLayout layout = new TextLayout(text, g2d.getFont(),
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   795
                                    deviceFontRenderContext);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   796
                    String trimmedText = trimTrailingSpaces(text);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   797
                    if (!trimmedText.isEmpty()) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   798
                        float screenWidth = (float)g2d.getFont().
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   799
                            getStringBounds(trimmedText, frc).getWidth();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   800
                        layout = layout.getJustifiedLayout(screenWidth);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   802
                        /* Use alternate print color if specified */
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   803
                        Color col = g2d.getColor();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   804
                        if (col instanceof PrintColorUIResource) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   805
                            g2d.setColor(((PrintColorUIResource)col).getPrintColor());
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   806
                        }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   807
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   808
                        layout.draw(g2d,x,y);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   809
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   810
                        g2d.setColor(col);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                    return nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        // Assume we're not printing if we get here, or that we are invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        // via Swing text printing which is laid out for the printer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        AATextInfo info = drawTextAntialiased(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        if (info != null && (g instanceof Graphics2D)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            Graphics2D g2 = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            Object oldContrast = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            Object oldAAValue = g2.getRenderingHint(KEY_TEXT_ANTIALIASING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            if (info.aaHint != null && info.aaHint != oldAAValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                g2.setRenderingHint(KEY_TEXT_ANTIALIASING, info.aaHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
                oldAAValue = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            if (info.lcdContrastHint != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
                oldContrast = g2.getRenderingHint(KEY_TEXT_LCD_CONTRAST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                if (info.lcdContrastHint.equals(oldContrast)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                    oldContrast = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                    g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                                        info.lcdContrastHint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            g.drawChars(data, offset, length, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            if (oldAAValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                g2.setRenderingHint(KEY_TEXT_ANTIALIASING, oldAAValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            if (oldContrast != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                g2.setRenderingHint(KEY_TEXT_LCD_CONTRAST, oldContrast);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            g.drawChars(data, offset, length, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        return nextX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * see documentation for drawChars
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * returns the advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    public static float drawString(JComponent c, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                                   AttributedCharacterIterator iterator,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                                   int x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                                   int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        float retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        boolean isPrinting = isPrinting(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        Color col = g.getColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if (isPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            /* Use alternate print color if specified */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            if (col instanceof PrintColorUIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                g.setColor(((PrintColorUIResource)col).getPrintColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        Graphics2D g2d = getGraphics2D(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        if (g2d == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            g.drawString(iterator,x,y); //for the cases where advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                                        //matters it should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            retVal = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            FontRenderContext frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            if (isPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                frc = getFontRenderContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                if (frc.isAntiAliased() || frc.usesFractionalMetrics()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    frc = new FontRenderContext(frc.getTransform(), false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            } else if ((frc = getFRCProperty(c)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                /* frc = frc; ! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                frc = g2d.getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            }
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   893
            TextLayout layout;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            if (isPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                FontRenderContext deviceFRC = g2d.getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                if (!isFontRenderContextPrintCompatible(frc, deviceFRC)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                    layout = new TextLayout(iterator, deviceFRC);
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   898
                    AttributedCharacterIterator trimmedIt =
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   899
                            getTrimmedTrailingSpacesIterator(iterator);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   900
                    if (trimmedIt != null) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   901
                        float screenWidth = new TextLayout(trimmedIt, frc).
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   902
                                getAdvance();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   903
                        layout = layout.getJustifiedLayout(screenWidth);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   904
                    }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   905
                } else {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   906
                    layout = new TextLayout(iterator, frc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                }
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   908
            } else {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
   909
                layout = new TextLayout(iterator, frc);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            layout.draw(g2d, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            retVal = layout.getAdvance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        if (isPrinting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            g.setColor(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
24163
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   922
    /**
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   923
     * This method should be used for drawing a borders over a filled rectangle.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   924
     * Draws vertical line, using the current color, between the points {@code
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   925
     * (x, y1)} and {@code (x, y2)} in graphics context's coordinate system.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   926
     * Note: it use {@code Graphics.fillRect()} internally.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   927
     *
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   928
     * @param g  Graphics to draw the line to.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   929
     * @param x  the <i>x</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   930
     * @param y1 the first point's <i>y</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   931
     * @param y2 the second point's <i>y</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   932
     */
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   933
    public static void drawVLine(Graphics g, int x, int y1, int y2) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   934
        if (y2 < y1) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   935
            final int temp = y2;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   936
            y2 = y1;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   937
            y1 = temp;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   938
        }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   939
        g.fillRect(x, y1, 1, y2 - y1 + 1);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   940
    }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   941
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   942
    /**
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   943
     * This method should be used for drawing a borders over a filled rectangle.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   944
     * Draws horizontal line, using the current color, between the points {@code
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   945
     * (x1, y)} and {@code (x2, y)} in graphics context's coordinate system.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   946
     * Note: it use {@code Graphics.fillRect()} internally.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   947
     *
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   948
     * @param g  Graphics to draw the line to.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   949
     * @param x1 the first point's <i>x</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   950
     * @param x2 the second point's <i>x</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   951
     * @param y  the <i>y</i> coordinate.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   952
     */
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   953
    public static void drawHLine(Graphics g, int x1, int x2, int y) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   954
        if (x2 < x1) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   955
            final int temp = x2;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   956
            x2 = x1;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   957
            x1 = temp;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   958
        }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   959
        g.fillRect(x1, y, x2 - x1 + 1, 1);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   960
    }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   961
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   962
    /**
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   963
     * This method should be used for drawing a borders over a filled rectangle.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   964
     * Draws the outline of the specified rectangle. The left and right edges of
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   965
     * the rectangle are at {@code x} and {@code x + w}. The top and bottom
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   966
     * edges are at {@code y} and {@code y + h}. The rectangle is drawn using
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   967
     * the graphics context's current color. Note: it use {@code
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   968
     * Graphics.fillRect()} internally.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   969
     *
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   970
     * @param g Graphics to draw the rectangle to.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   971
     * @param x the <i>x</i> coordinate of the rectangle to be drawn.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   972
     * @param y the <i>y</i> coordinate of the rectangle to be drawn.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   973
     * @param w the w of the rectangle to be drawn.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   974
     * @param h the h of the rectangle to be drawn.
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   975
     * @see SwingUtilities2#drawVLine(java.awt.Graphics, int, int, int)
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   976
     * @see SwingUtilities2#drawHLine(java.awt.Graphics, int, int, int)
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   977
     */
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   978
    public static void drawRect(Graphics g, int x, int y, int w, int h) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   979
        if (w < 0 || h < 0) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   980
            return;
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   981
        }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   982
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   983
        if (h == 0 || w == 0) {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   984
            g.fillRect(x, y, w + 1, h + 1);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   985
        } else {
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   986
            g.fillRect(x, y, w, 1);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   987
            g.fillRect(x + w, y, 1, h);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   988
            g.fillRect(x + 1, y + h, w, 1);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   989
            g.fillRect(x, y + 1, 1, h);
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   990
        }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   991
    }
d56df89854c4 8032219: [macosx] Scrollbars looks bad under retina in Motif and Metal L&F
serb
parents: 23298
diff changeset
   992
3976
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   993
    private static TextLayout createTextLayout(JComponent c, String s,
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   994
                                            Font f, FontRenderContext frc) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   995
        Object shaper = (c == null ?
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   996
                    null : c.getClientProperty(TextAttribute.NUMERIC_SHAPING));
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   997
        if (shaper == null) {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   998
            return new TextLayout(s, f, frc);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
   999
        } else {
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1000
            Map<TextAttribute, Object> a = new HashMap<TextAttribute, Object>();
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1001
            a.put(TextAttribute.FONT, f);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1002
            a.put(TextAttribute.NUMERIC_SHAPING, shaper);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1003
            return new TextLayout(s, a, frc);
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1004
        }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1005
    }
6758b915b581 4337267: Arabic Numeral Shaping
gsm
parents: 3749
diff changeset
  1006
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * Checks if two given FontRenderContexts are compatible for printing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * We can't just use equals as we want to exclude from the comparison :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     * + whether AA is set as irrelevant for printing and shouldn't affect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * printed metrics anyway
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * + any translation component in the transform of either FRC, as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * does not affect metrics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * Compatible means no special handling needed for text painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
    private static boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
        isFontRenderContextPrintCompatible(FontRenderContext frc1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                                           FontRenderContext frc2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
        if (frc1 == frc2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
        if (frc1 == null || frc2 == null) { // not supposed to happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        if (frc1.getFractionalMetricsHint() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            frc2.getFractionalMetricsHint()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        /* If both are identity, return true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (!frc1.isTransformed() && !frc2.isTransformed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        /* That's the end of the cheap tests, need to get and compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
         * the transform matrices. We don't care about the translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
         * components, so return true if they are otherwise identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        double[] mat1 = new double[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        double[] mat2 = new double[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        frc1.getTransform().getMatrix(mat1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        frc2.getTransform().getMatrix(mat2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            mat1[0] == mat2[0] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
            mat1[1] == mat2[1] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            mat1[2] == mat2[2] &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            mat1[3] == mat2[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * Tries it best to get Graphics2D out of the given Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * returns null if can not derive it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    public static Graphics2D getGraphics2D(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if (g instanceof Graphics2D) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            return (Graphics2D) g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        } else if (g instanceof ProxyPrintGraphics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
            return (Graphics2D)(((ProxyPrintGraphics)g).getGraphics());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * Returns FontRenderContext associated with Component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * FontRenderContext from Component.getFontMetrics is associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * with the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Uses Component.getFontMetrics to get the FontRenderContext from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * see JComponent.getFontMetrics and TextLayoutStrategy.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
    public static FontRenderContext getFontRenderContext(Component c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        assert c != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            return DEFAULT_FRC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            return c.getFontMetrics(c.getFont()).getFontRenderContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * A convenience method to get FontRenderContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Returns the FontRenderContext for the passed in FontMetrics or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * for the passed in Component if FontMetrics is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    private static FontRenderContext getFontRenderContext(Component c, FontMetrics fm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        assert fm != null || c!= null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        return (fm != null) ? fm.getFontRenderContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            : getFontRenderContext(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * This method is to be used only for JComponent.getFontMetrics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     * In all other places to get FontMetrics we need to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * JComponent.getFontMetrics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    public static FontMetrics getFontMetrics(JComponent c, Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        FontRenderContext  frc = getFRCProperty(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        if (frc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            frc = DEFAULT_FRC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        return FontDesignMetrics.getMetrics(font, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    /* Get any FontRenderContext associated with a JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * - may return null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
    private static FontRenderContext getFRCProperty(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
            AATextInfo info =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                (AATextInfo)c.getClientProperty(AA_TEXT_PROPERTY_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                return info.frc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * returns true if the Graphics is print Graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    static boolean isPrinting(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        return (g instanceof PrinterGraphics || g instanceof PrintGraphics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
20113
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1132
    private static String trimTrailingSpaces(String s) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1133
        int i = s.length() - 1;
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1134
        while(i >= 0 && Character.isWhitespace(s.charAt(i))) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1135
            i--;
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1136
        }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1137
        return s.substring(0, i + 1);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1138
    }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1139
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1140
    private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1141
            (AttributedCharacterIterator iterator) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1142
        int curIdx = iterator.getIndex();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1143
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1144
        char c = iterator.last();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1145
        while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1146
            c = iterator.previous();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1147
        }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1148
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1149
        if (c != CharacterIterator.DONE) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1150
            int endIdx = iterator.getIndex();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1151
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1152
            if (endIdx == iterator.getEndIndex() - 1) {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1153
                iterator.setIndex(curIdx);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1154
                return iterator;
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1155
            } else {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1156
                AttributedString trimmedText = new AttributedString(iterator,
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1157
                        iterator.getBeginIndex(), endIdx + 1);
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1158
                return trimmedText.getIterator();
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1159
            }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1160
        } else {
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1161
            return null;
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1162
        }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1163
    }
ee0defbadbb1 8023392: Swing text components printed with spaces between chars
mcherkas
parents: 18133
diff changeset
  1164
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * Determines whether the SelectedTextColor should be used for painting text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     * foreground for the specified highlight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     * Returns true only if the highlight painter for the specified highlight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * is null or equals to the selection color of the text component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * This is a hack for fixing both bugs 4761990 and 5003294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        Highlighter.HighlightPainter painter = h.getPainter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        String painterClass = painter.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            DefaultHighlighter.DefaultHighlightPainter defPainter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
                    (DefaultHighlighter.DefaultHighlightPainter) painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            if (defPainter.getColor() != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                    !defPainter.getColor().equals(c.getSelectionColor())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        } catch (ClassCastException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1197
     * LSBCacheEntry is used to cache the left side bearing (lsb) for
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1198
     * a particular <code>Font</code> and <code>FontRenderContext</code>.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1199
     * This only caches characters that fall in the range
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1200
     * <code>MIN_CHAR_INDEX</code> to <code>MAX_CHAR_INDEX</code>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     */
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1202
    private static class LSBCacheEntry {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1203
        // Used to indicate a particular entry in lsb has not been set.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1204
        private static final byte UNSET = Byte.MAX_VALUE;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1205
        // Used in creating a GlyphVector to get the lsb
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1206
        private static final char[] oneChar = new char[1];
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1207
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1208
        private byte[] lsbCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        private Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        private FontRenderContext frc;
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1211
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1212
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1213
        public LSBCacheEntry(FontRenderContext frc, Font font) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1214
            lsbCache = new byte[MAX_CHAR_INDEX - MIN_CHAR_INDEX];
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1215
            reset(frc, font);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1217
        }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1218
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1219
        public void reset(FontRenderContext frc, Font font) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1220
            this.font = font;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1221
            this.frc = frc;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1222
            for (int counter = lsbCache.length - 1; counter >= 0; counter--) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1223
                lsbCache[counter] = UNSET;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1224
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        public int getLeftSideBearing(char aChar) {
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1228
            int index = aChar - MIN_CHAR_INDEX;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1229
            assert (index >= 0 && index < (MAX_CHAR_INDEX - MIN_CHAR_INDEX));
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1230
            byte lsb = lsbCache[index];
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1231
            if (lsb == UNSET) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1232
                oneChar[0] = aChar;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1233
                GlyphVector gv = font.createGlyphVector(frc, oneChar);
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1234
                lsb = (byte) gv.getGlyphPixelBounds(0, frc, 0f, 0f).x;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1235
                if (lsb < 0) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1236
                    /* HRGB/HBGR LCD glyph images will always have a pixel
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1237
                     * on the left used in colour fringe reduction.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1238
                     * Text rendering positions this correctly but here
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1239
                     * we are using the glyph image to adjust that position
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1240
                     * so must account for it.
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1241
                     */
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1242
                    Object aaHint = frc.getAntiAliasingHint();
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1243
                    if (aaHint == VALUE_TEXT_ANTIALIAS_LCD_HRGB ||
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1244
                            aaHint == VALUE_TEXT_ANTIALIAS_LCD_HBGR) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1245
                        lsb++;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1246
                    }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1247
                }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1248
                lsbCache[index] = lsb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1250
            return lsb;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        public boolean equals(Object entry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            if (entry == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
            }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1259
            if (!(entry instanceof LSBCacheEntry)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            }
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1262
            LSBCacheEntry oEntry = (LSBCacheEntry) entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            return (font.equals(oEntry.font) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    frc.equals(oEntry.frc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            int result = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
            if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                result = 37 * result + font.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            if (frc != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                result = 37 * result + frc.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * here goes the fix for 4856343 [Problem with applet interaction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * with system selection clipboard]
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * NOTE. In case isTrustedContext() no checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     * are to be performed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    /**
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1288
    * checks the security permissions for accessing system clipboard
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1289
    *
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1290
    * for untrusted context (see isTrustedContext) checks the
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1291
    * permissions for the current event being handled
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1292
    *
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1293
    */
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1294
   public static boolean canAccessSystemClipboard() {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1295
       boolean canAccess = false;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1296
       if (!GraphicsEnvironment.isHeadless()) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1297
           SecurityManager sm = System.getSecurityManager();
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1298
           if (sm == null) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1299
               canAccess = true;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1300
           } else {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1301
               try {
22060
cd4f9d7dbeda 8029886: Change SecurityManager check{TopLevelWindow, SystemClipboardAccessAwtEventQueueAccess} to check AllPermission
alanb
parents: 20851
diff changeset
  1302
                   sm.checkPermission(AWTPermissions.ACCESS_CLIPBOARD_PERMISSION);
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1303
                   canAccess = true;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1304
               } catch (SecurityException e) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1305
               }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1306
               if (canAccess && ! isTrustedContext()) {
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1307
                   canAccess = canCurrentEventAccessSystemClipboard(true);
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1308
               }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1309
           }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1310
       }
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1311
       return canAccess;
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1312
   }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
    /**
3749
d0ecbd7075b1 6797139: JButton title is truncating for some strings irrespective of preferred size.
alexp
parents: 3737
diff changeset
  1314
    * Returns true if EventQueue.getCurrentEvent() has the permissions to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * access the system clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    public static boolean canCurrentEventAccessSystemClipboard() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
        return  isTrustedContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
            || canCurrentEventAccessSystemClipboard(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * Returns true if the given event has permissions to access the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * system clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     * @param e AWTEvent to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    public static boolean canEventAccessSystemClipboard(AWTEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
        return isTrustedContext()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
            || canEventAccessSystemClipboard(e, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * Returns true if the given event is corrent gesture for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * accessing clipboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * @param ie InputEvent to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    private static boolean isAccessClipboardGesture(InputEvent ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        boolean allowedGesture = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        if (ie instanceof KeyEvent) { //we can validate only keyboard gestures
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            KeyEvent ke = (KeyEvent)ie;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
            int keyCode = ke.getKeyCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            int keyModifiers = ke.getModifiers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
            switch(keyCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            case KeyEvent.VK_C:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
            case KeyEvent.VK_V:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
            case KeyEvent.VK_X:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                allowedGesture = (keyModifiers == InputEvent.CTRL_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            case KeyEvent.VK_INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                allowedGesture = (keyModifiers == InputEvent.CTRL_MASK ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
                                  keyModifiers == InputEvent.SHIFT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            case KeyEvent.VK_COPY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
            case KeyEvent.VK_PASTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            case KeyEvent.VK_CUT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                allowedGesture = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            case KeyEvent.VK_DELETE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                allowedGesture = ( keyModifiers == InputEvent.SHIFT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        return allowedGesture;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * Returns true if e has the permissions to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * access the system clipboard and if it is allowed gesture (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * checkGesture is true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * @param e AWTEvent to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * @param checkGesture boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    private static boolean canEventAccessSystemClipboard(AWTEvent e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                                                        boolean checkGesture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        if (EventQueue.isDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
             * Checking event permissions makes sense only for event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
             * dispathing thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
            if (e instanceof InputEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                && (! checkGesture || isAccessClipboardGesture((InputEvent)e))) {
23297
2946ef51ee24 8029960: Remove reflection from Swing classes
alexsch
parents: 23281
diff changeset
  1386
                return AWTAccessor.getInputEventAccessor().
2946ef51ee24 8029960: Remove reflection from Swing classes
alexsch
parents: 23281
diff changeset
  1387
                        canAccessSystemClipboard((InputEvent) e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    /**
20800
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1397
     * Utility method that throws SecurityException if SecurityManager is set
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1398
     * and modifiers are not public
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1399
     *
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1400
     * @param modifiers a set of modifiers
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1401
     */
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1402
    public static void checkAccess(int modifiers) {
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1403
        if (System.getSecurityManager() != null
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1404
                && !Modifier.isPublic(modifiers)) {
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1405
            throw new SecurityException("Resource is not accessible");
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1406
        }
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1407
    }
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1408
1f6b7d81410d 8013744: Better tabling for AWT
alexsch
parents: 15316
diff changeset
  1409
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * Returns true if EventQueue.getCurrentEvent() has the permissions to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * access the system clipboard and if it is allowed gesture (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * checkGesture true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     * @param checkGesture boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    private static boolean canCurrentEventAccessSystemClipboard(boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
                                                               checkGesture) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        AWTEvent event = EventQueue.getCurrentEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        return canEventAccessSystemClipboard(event, checkGesture);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * see RFE 5012841 [Per AppContect security permissions] for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * details
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    private static boolean isTrustedContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        return (System.getSecurityManager() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            || (AppContext.getAppContext().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                get(UntrustedClipboardAccess) == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    public static String displayPropertiesToCSS(Font font, Color fg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        StringBuffer rule = new StringBuffer("body {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            rule.append(" font-family: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            rule.append(font.getFamily());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            rule.append(" ; ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            rule.append(" font-size: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            rule.append(font.getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            rule.append("pt ;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            if (font.isBold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                rule.append(" font-weight: 700 ; ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            if (font.isItalic()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                rule.append(" font-style: italic ; ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        if (fg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            rule.append(" color: #");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            if (fg.getRed() < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                rule.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            rule.append(Integer.toHexString(fg.getRed()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            if (fg.getGreen() < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                rule.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            rule.append(Integer.toHexString(fg.getGreen()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
            if (fg.getBlue() < 16) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                rule.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            rule.append(Integer.toHexString(fg.getBlue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
            rule.append(" ; ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        rule.append(" }");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        return rule.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * Utility method that creates a <code>UIDefaults.LazyValue</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * creates an <code>ImageIcon</code> <code>UIResource</code> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * specified image file name. The image is loaded using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * <code>getResourceAsStream</code>, starting with a call to that method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     * on the base class parameter. If it cannot be found, searching will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     * continue through the base class' inheritance hierarchy, up to and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * including <code>rootClass</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * @param baseClass the first class to use in searching for the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     * @param rootClass an ancestor of <code>baseClass</code> to finish the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     *                  search at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * @param imageFile the name of the file to be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * @return a lazy value that creates the <code>ImageIcon</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     *         <code>UIResource</code> for the image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *         or null if it cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    public static Object makeIcon(final Class<?> baseClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                                  final Class<?> rootClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                                  final String imageFile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        return new UIDefaults.LazyValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            public Object createValue(UIDefaults table) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                /* Copy resource into a byte array.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                 * necessary because several browsers consider
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                 * Class.getResource a security risk because it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                 * can be used to load additional classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                 * Class.getResourceAsStream just returns raw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                 * bytes, which we can convert to an image.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                 */
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1499
                byte[] buffer =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
                    java.security.AccessController.doPrivileged(
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1501
                        new java.security.PrivilegedAction<byte[]>() {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
  1502
                    public byte[] run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                            InputStream resource = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                            Class<?> srchClass = baseClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                            while (srchClass != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                                resource = srchClass.getResourceAsStream(imageFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                                if (resource != null || srchClass == rootClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                                srchClass = srchClass.getSuperclass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                            if (resource == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
                                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
                            BufferedInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                                new BufferedInputStream(resource);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                            ByteArrayOutputStream out =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                                new ByteArrayOutputStream(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                            byte[] buffer = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                            while ((n = in.read(buffer)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                                out.write(buffer, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                            return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                            System.err.println(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                if (buffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                if (buffer.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    System.err.println("warning: " + imageFile +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                       " is zero-length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                return new ImageIconUIResource(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    /* Used to help decide if AA text rendering should be used, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
     * this local display test should be additionally qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
     * against whether we have XRender support on both ends of the wire,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
     * as with that support remote performance may be good enough to turn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * on by default. An additional complication there is XRender does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * appear capable of performing gamma correction needed for LCD text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    public static boolean isLocalDisplay() {
1724
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1562
        boolean isLocal;
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1563
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1564
        if (ge instanceof SunGraphicsEnvironment) {
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1565
            isLocal = ((SunGraphicsEnvironment) ge).isDisplayLocal();
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1566
        } else {
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1567
            isLocal = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        }
1724
a22a286aa16f 6748082: remove platform-specific code from SwingUtilities2.isDisplayLocal
tdv
parents: 2
diff changeset
  1569
        return isLocal;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * Returns an integer from the defaults table. If <code>key</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * not map to a valid <code>Integer</code>, or can not be convered from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * a <code>String</code> to an integer, the value 0 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     * @param key  an <code>Object</code> specifying the int.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * @return the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    public static int getUIDefaultsInt(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        return getUIDefaultsInt(key, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * Returns an integer from the defaults table that is appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * for the given locale. If <code>key</code> does not map to a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * <code>Integer</code>, or can not be convered from a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * to an integer, the value 0 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * @param key  an <code>Object</code> specifying the int. Returned value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *             is 0 if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @param l the <code>Locale</code> for which the int is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * @return the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
    public static int getUIDefaultsInt(Object key, Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
        return getUIDefaultsInt(key, l, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * Returns an integer from the defaults table. If <code>key</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * not map to a valid <code>Integer</code>, or can not be convered from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * a <code>String</code> to an integer, <code>default</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * @param key  an <code>Object</code> specifying the int. Returned value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *             is 0 if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * @param defaultValue Returned value if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     *                     or is not an Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @return the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    public static int getUIDefaultsInt(Object key, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        return getUIDefaultsInt(key, null, defaultValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * Returns an integer from the defaults table that is appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * for the given locale. If <code>key</code> does not map to a valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * <code>Integer</code>, or can not be convered from a <code>String</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * to an integer, <code>default</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * @param key  an <code>Object</code> specifying the int. Returned value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *             is 0 if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * @param l the <code>Locale</code> for which the int is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * @param defaultValue Returned value if <code>key</code> is not available,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *                     or is not an Integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * @return the int
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        Object value = UIManager.get(key, l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        if (value instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            return ((Integer)value).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        if (value instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
                return Integer.parseInt((String)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            } catch (NumberFormatException nfe) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        return defaultValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
    // At this point we need this method here. But we assume that there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    // will be a common method for this purpose in the future releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
    public static Component compositeRequestFocus(Component component) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        if (component instanceof Container) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            Container container = (Container)component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            if (container.isFocusCycleRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                FocusTraversalPolicy policy = container.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
                Component comp = policy.getDefaultComponent(container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
                if (comp!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
            Container rootAncestor = container.getFocusCycleRootAncestor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
            if (rootAncestor!=null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
                FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
                Component comp = policy.getComponentAfter(rootAncestor, container);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
                if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
                    comp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                    return comp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        if (component.isFocusable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            component.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * Change focus to the visible component in {@code JTabbedPane}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * This is not a general-purpose method and is here only to permit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * sharing code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    public static boolean tabbedPaneChangeFocusTo(Component comp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        if (comp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            if (comp.isFocusTraversable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                SwingUtilities2.compositeRequestFocus(comp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
            } else if (comp instanceof JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
                       && ((JComponent)comp).requestDefaultFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
                 return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     * Submits a value-returning task for execution on the EDT and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     * returns a Future representing the pending results of the task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * @param task the task to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * @return a Future representing pending completion of the task
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * @throws NullPointerException if the task is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    public static <V> Future<V> submit(Callable<V> task) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        if (task == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
        FutureTask<V> future = new FutureTask<V>(task);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        execute(future);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        return future;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * Submits a Runnable task for execution on the EDT and returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * Future representing that task.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * @param task the task to submit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * @param result the result to return upon successful completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * @return a Future representing pending completion of the task,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *         and whose <tt>get()</tt> method will return the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     *         result value upon completion
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * @throws NullPointerException if the task is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    public static <V> Future<V> submit(Runnable task, V result) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        if (task == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        FutureTask<V> future = new FutureTask<V>(task, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        execute(future);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        return future;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * Sends a Runnable to the EDT for the execution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    private static void execute(Runnable command) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        SwingUtilities.invokeLater(command);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * Sets the {@code SKIP_CLICK_COUNT} client property on the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * if it is an instance of {@code JTextComponent} with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * {@code DefaultCaret}. This property, used for text components acting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * as editors in a table or tree, tells {@code DefaultCaret} how many
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * clicks to skip before starting selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    public static void setSkipClickCount(Component comp, int count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        if (comp instanceof JTextComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                && ((JTextComponent) comp).getCaret() instanceof DefaultCaret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
            ((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * Return the MouseEvent's click count, possibly reduced by the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * the component's {@code SKIP_CLICK_COUNT} client property. Clears
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * the {@code SKIP_CLICK_COUNT} property if the mouse event's click count
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * is 1. In order for clearing of the property to work correctly, there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * must be a mousePressed implementation on the caller with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * call as the first line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    public static int getAdjustedClickCount(JTextComponent comp, MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        int cc = e.getClickCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        if (cc == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
            comp.putClientProperty(SKIP_CLICK_COUNT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
            Integer sub = (Integer) comp.getClientProperty(SKIP_CLICK_COUNT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
            if (sub != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                return cc - sub;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        return cc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * Used by the {@code liesIn} method to return which section
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * the point lies in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * @see #liesIn
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
    public enum Section {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        /** The leading section */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        LEADING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        /** The middle section */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        MIDDLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        /** The trailing section */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        TRAILING
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * This method divides a rectangle into two or three sections along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * the specified axis and determines which section the given point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * lies in on that axis; used by drag and drop when calculating drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     * locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
     * For two sections, the rectangle is divided equally and the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
     * returns whether the point lies in {@code Section.LEADING} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * {@code Section.TRAILING}. For horizontal divisions, the calculation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
     * respects component orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
     * For three sections, if the rectangle is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     * 30 pixels in length along the axis, the calculation gives 10 pixels
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
     * to each of the leading and trailing sections and the remainder to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     * middle. For smaller sizes, the rectangle is divided equally into three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
     * sections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * Note: This method assumes that the point is within the bounds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * the given rectangle on the specified axis. However, in cases where
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * it isn't, the results still have meaning: {@code Section.MIDDLE}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * remains the same, {@code Section.LEADING} indicates that the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * is in or somewhere before the leading section, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     * {@code Section.TRAILING} indicates that the point is in or somewhere
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * after the trailing section.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @param rect the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     * @param p the point the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
     * @param horizontal {@code true} to use the horizontal axis,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
     *        or {@code false} for the vertical axis
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
     * @param ltr {@code true} for left to right orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
     *        or {@code false} for right to left orientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
     *        only used for horizontal calculations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
     * @param three {@code true} for three sections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
     *        or {@code false} for two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
     * @return the {@code Section} where the point lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
     * @throws NullPointerException if {@code rect} or {@code p} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
    private static Section liesIn(Rectangle rect, Point p, boolean horizontal,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                                  boolean ltr, boolean three) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        /* beginning of the rectangle on the axis */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
        int p0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        /* point on the axis we're interested in */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        int pComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        /* length of the rectangle on the axis */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
        /* value of ltr if horizontal, else true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        boolean forward;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
        if (horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            p0 = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            pComp = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
            length = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            forward = ltr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            p0 = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            pComp = p.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            length = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            forward = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        if (three) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            int boundary = (length >= 30) ? 10 : length / 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
            if (pComp < p0 + boundary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
               return forward ? Section.LEADING : Section.TRAILING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
           } else if (pComp >= p0 + length - boundary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
               return forward ? Section.TRAILING : Section.LEADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
           return Section.MIDDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
            int middle = p0 + length / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            if (forward) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
                return pComp >= middle ? Section.TRAILING : Section.LEADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                return pComp < middle ? Section.TRAILING : Section.LEADING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     * This method divides a rectangle into two or three sections along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
     * the horizontal axis and determines which section the given point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
     * lies in; used by drag and drop when calculating drop locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * See the documentation for {@link #liesIn} for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * on how the section is calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * @param rect the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @param p the point the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * @param ltr {@code true} for left to right orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     *        or {@code false} for right to left orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     * @param three {@code true} for three sections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *        or {@code false} for two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * @return the {@code Section} where the point lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * @throws NullPointerException if {@code rect} or {@code p} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    public static Section liesInHorizontal(Rectangle rect, Point p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
                                           boolean ltr, boolean three) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        return liesIn(rect, p, true, ltr, three);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * This method divides a rectangle into two or three sections along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * the vertical axis and determines which section the given point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * lies in; used by drag and drop when calculating drop locations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     * See the documentation for {@link #liesIn} for more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * on how the section is calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     * @param rect the rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * @param p the point the check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     * @param three {@code true} for three sections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     *        or {@code false} for two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * @return the {@code Section} where the point lies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     * @throws NullPointerException if {@code rect} or {@code p} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
     *         {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    public static Section liesInVertical(Rectangle rect, Point p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                                         boolean three) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        return liesIn(rect, p, false, false, three);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    }
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1927
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1928
    /**
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1929
     * Maps the index of the column in the view at
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1930
     * {@code viewColumnIndex} to the index of the column
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1931
     * in the table model.  Returns the index of the corresponding
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1932
     * column in the model.  If {@code viewColumnIndex}
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1933
     * is less than zero, returns {@code viewColumnIndex}.
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1934
     *
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1935
     * @param cm the table model
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1936
     * @param   viewColumnIndex     the index of the column in the view
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1937
     * @return  the index of the corresponding column in the model
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1938
     *
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1939
     * @see JTable#convertColumnIndexToModel(int)
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1940
     * @see javax.swing.plaf.basic.BasicTableHeaderUI
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1941
     */
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1942
    public static int convertColumnIndexToModel(TableColumnModel cm,
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1943
                                                int viewColumnIndex) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1944
        if (viewColumnIndex < 0) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1945
            return viewColumnIndex;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1946
        }
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1947
        return cm.getColumn(viewColumnIndex).getModelIndex();
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1948
    }
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1949
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1950
    /**
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1951
     * Maps the index of the column in the {@code cm} at
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1952
     * {@code modelColumnIndex} to the index of the column
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1953
     * in the view.  Returns the index of the
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1954
     * corresponding column in the view; returns {@code -1} if this column
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1955
     * is not being displayed. If {@code modelColumnIndex} is less than zero,
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1956
     * returns {@code modelColumnIndex}.
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1957
     *
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1958
     * @param cm the table model
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1959
     * @param modelColumnIndex the index of the column in the model
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1960
     * @return the index of the corresponding column in the view
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1961
     *
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1962
     * @see JTable#convertColumnIndexToView(int)
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1963
     * @see javax.swing.plaf.basic.BasicTableHeaderUI
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1964
     */
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1965
    public static int convertColumnIndexToView(TableColumnModel cm,
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1966
                                        int modelColumnIndex) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1967
        if (modelColumnIndex < 0) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1968
            return modelColumnIndex;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1969
        }
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1970
        for (int column = 0; column < cm.getColumnCount(); column++) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1971
            if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1972
                return column;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1973
            }
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1974
        }
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1975
        return -1;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4278
diff changeset
  1976
    }
15316
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1977
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1978
    public static int getSystemMnemonicKeyMask() {
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1979
        Toolkit toolkit = Toolkit.getDefaultToolkit();
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1980
        if (toolkit instanceof SunToolkit) {
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1981
            return ((SunToolkit) toolkit).getFocusAcceleratorKeyMask();
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1982
        }
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1983
        return InputEvent.ALT_MASK;
0dcb6f02678d 8003978: closed/javax/swing/JRootPane/bug4670486.java fails since jdk7u12b01 on macosx
alexsch
parents: 10869
diff changeset
  1984
    }
18133
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1985
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1986
    /**
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1987
     * Returns the {@link TreePath} that identifies the changed nodes.
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1988
     *
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1989
     * @param event  changes in a tree model
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1990
     * @param model  corresponing tree model
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1991
     * @return  the path to the changed nodes
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1992
     */
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1993
    public static TreePath getTreePath(TreeModelEvent event, TreeModel model) {
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1994
        TreePath path = event.getTreePath();
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1995
        if ((path == null) && (model != null)) {
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1996
            Object root = model.getRoot();
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1997
            if (root != null) {
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1998
                path = new TreePath(root);
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  1999
            }
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  2000
        }
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  2001
        return path;
41b4b144eaff 8013571: TreeModelEvent doesn't accept "null" for root as Javadoc specifies.
malenkov
parents: 15316
diff changeset
  2002
    }
23280
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2003
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2004
    /**
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2005
     * Used to listen to "blit" repaints in RepaintManager.
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2006
     */
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2007
    public interface RepaintListener {
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2008
        void repaintPerformed(JComponent c, int x, int y, int w, int h);
df31f522531f 8033233: [JLightweightFrame] support default JViewport BLIT_SCROLL_MODE
ant
parents: 22060
diff changeset
  2009
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
}