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