jdk/src/java.desktop/share/classes/sun/swing/plaf/synth/DefaultSynthStyle.java
author serb
Thu, 25 Dec 2014 14:43:49 +0300
changeset 28533 aea5ffcd5b2b
parent 25859 3317bb8137f4
child 30948 0a0972d3b58d
permissions -rw-r--r--
7180976: Pending String deadlocks UIDefaults Reviewed-by: azvegint, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
     2
 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package sun.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.plaf.synth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Default implementation of SynthStyle. Has setters for the various
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * SynthStyle methods. Many of the properties can be specified for all states,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * using SynthStyle directly, or a specific state using one of the StateInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Beyond the constructor a subclass should override the <code>addTo</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and <code>clone</code> methods, these are used when the Styles are being
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * merged into a resulting style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class DefaultSynthStyle extends SynthStyle implements Cloneable {
28533
aea5ffcd5b2b 7180976: Pending String deadlocks UIDefaults
serb
parents: 25859
diff changeset
    46
aea5ffcd5b2b 7180976: Pending String deadlocks UIDefaults
serb
parents: 25859
diff changeset
    47
    private static final Object PENDING = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     * Should the component be opaque?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private boolean opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private Insets insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Information specific to ComponentState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private StateInfo[] states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * User specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
    64
    private Map<Object, Object> data;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Font to use if there is no matching StateInfo, or the StateInfo doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * define one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * SynthGraphics, may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private SynthGraphicsUtils synthGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Painter to use if the StateInfo doesn't have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private SynthPainter painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Nullary constructor, intended for subclassers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public DefaultSynthStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Creates a new DefaultSynthStyle that is a copy of the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * style. Any StateInfo's of the passed in style are clonsed as well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param style Style to duplicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public DefaultSynthStyle(DefaultSynthStyle style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        opaque = style.opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (style.insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            insets = new Insets(style.insets.top, style.insets.left,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                style.insets.bottom, style.insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (style.states != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            states = new StateInfo[style.states.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            for (int counter = style.states.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                     counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                states[counter] = (StateInfo)style.states[counter].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (style.data != null) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   109
            data = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            data.putAll(style.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        font = style.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        synthGraphics = style.synthGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        painter = style.painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Creates a new DefaultSynthStyle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param insets Insets for the Style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param opaque Whether or not the background is completely painted in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *        an opaque color
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param states StateInfos describing properties per state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param data Style specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public DefaultSynthStyle(Insets insets, boolean opaque,
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   127
                             StateInfo[] states, Map<Object, Object> data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.insets = insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.opaque = opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.states = states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public Color getColor(SynthContext context, ColorType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return getColor(context.getComponent(), context.getRegion(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        context.getComponentState(), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public Color getColor(JComponent c, Region id, int state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                          ColorType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // For the enabled state, prefer the widget's colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (!id.isSubregion() && state == SynthConstants.ENABLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            if (type == ColorType.BACKGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                return c.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            else if (type == ColorType.FOREGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return c.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            else if (type == ColorType.TEXT_FOREGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                // If getForeground returns a non-UIResource it means the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                // developer has explicitly set the foreground, use it over
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                // that of TEXT_FOREGROUND as that is typically the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                // behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                Color color = c.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                if (!(color instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // Then use what we've locally defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        Color color = getColorForState(c, id, state, type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (color == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            // No color, fallback to that of the widget.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if (type == ColorType.BACKGROUND ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        type == ColorType.TEXT_BACKGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return c.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            else if (type == ColorType.FOREGROUND ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                     type == ColorType.TEXT_FOREGROUND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return c.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    protected Color getColorForState(SynthContext context, ColorType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return getColorForState(context.getComponent(), context.getRegion(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                context.getComponentState(), type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Returns the color for the specified state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param c JComponent the style is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param id Region identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @param state State of the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @param type Type of color being requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @return Color to render with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected Color getColorForState(JComponent c, Region id, int state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                     ColorType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Use the best state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        StateInfo si = getStateInfo(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Color color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (si != null && (color = si.getColor(type)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (si == null || si.getComponentState() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            si = getStateInfo(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            if (si != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                return si.getColor(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Sets the font that is used if there is no matching StateInfo, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * it does not define a font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param font Font to use for rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public Font getFont(SynthContext state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return getFont(state.getComponent(), state.getRegion(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                       state.getComponentState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public Font getFont(JComponent c, Region id, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!id.isSubregion() && state == SynthConstants.ENABLED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            return c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        Font cFont = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (cFont != null && !(cFont instanceof UIResource)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            return cFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return getFontForState(c, id, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Returns the font for the specified state. This should NOT callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * to the JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param c JComponent the style is associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param id Region identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param state State of the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @return Font to render with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected Font getFontForState(JComponent c, Region id, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (c == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            return this.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // First pass, look for the best match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        StateInfo si = getStateInfo(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (si != null && (font = si.getFont()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (si == null || si.getComponentState() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            si = getStateInfo(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (si != null && (font = si.getFont()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        // Fallback font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return this.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    protected Font getFontForState(SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return getFontForState(context.getComponent(), context.getRegion(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                               context.getComponentState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * Sets the SynthGraphicsUtils that will be used for rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @param graphics SynthGraphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    public void setGraphicsUtils(SynthGraphicsUtils graphics) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.synthGraphics = graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * Returns a SynthGraphicsUtils.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   279
     * @param context SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @return SynthGraphicsUtils
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public SynthGraphicsUtils getGraphicsUtils(SynthContext context) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        if (synthGraphics == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            return super.getGraphicsUtils(context);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return synthGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Sets the insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * @param Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public void setInsets(Insets insets) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        this.insets = insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * Returns the Insets. If <code>to</code> is non-null the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * insets will be placed in it, otherwise a new Insets object will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * created and returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   303
     * @param context SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param to Where to place Insets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return Insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public Insets getInsets(SynthContext state, Insets to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (to == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            to = new Insets(0, 0, 0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            to.left = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            to.right = insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            to.top = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            to.bottom = insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            to.left = to.right = to.top = to.bottom = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        return to;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * Sets the Painter to use for the border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param painter Painter for the Border.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    public void setPainter(SynthPainter painter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        this.painter = painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * Returns the Painter for the passed in Component. This may return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   335
     * @param ss SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @return Painter for the border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public SynthPainter getPainter(SynthContext ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Sets whether or not the JComponent should be opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param opaque Whether or not the JComponent should be opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void setOpaque(boolean opaque) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        this.opaque = opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Returns the value to initialize the opacity property of the Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * to. A Style should NOT assume the opacity will remain this value, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * developer may reset it or override it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   356
     * @param ss SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @return opaque Whether or not the JComponent is opaque.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public boolean isOpaque(SynthContext ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        return opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * Sets style specific values. This does NOT copy the data, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * assigns it directly to this Style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param data Style specific values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     */
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   369
    public void setData(Map<Object, Object> data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * Returns the style specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @return Style specific data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   378
    public Map<Object, Object> getData() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        return data;
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
     * Getter for a region specific style property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   385
     * @param state SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * @param key Property being requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * @return Value of the named property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    public Object get(SynthContext state, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        // Look for the best match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        StateInfo si = getStateInfo(state.getComponentState());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            return getKeyFromData(si.getData(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        si = getStateInfo(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            return getKeyFromData(si.getData(), key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if(getKeyFromData(data, key) != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
          return getKeyFromData(data, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return getDefaultValue(state, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   405
    private Object getKeyFromData(Map<Object, Object> stateData, Object key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
          Object value = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
          if (stateData != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            synchronized(stateData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                value = stateData.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            while (value == PENDING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                synchronized(stateData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                        stateData.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    } catch (InterruptedException ie) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    value = stateData.get(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            if (value instanceof UIDefaults.LazyValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                synchronized(stateData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    stateData.put(key, PENDING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                value = ((UIDefaults.LazyValue)value).createValue(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                synchronized(stateData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                    stateData.put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    stateData.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * Returns the default value for a particular property.  This is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * invoked if this style doesn't define a property for <code>key</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
   438
     * @param state SynthContext identifying requestor
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param key Property being requested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @return Value of the named property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public Object getDefaultValue(SynthContext context, Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        return super.get(context, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Creates a clone of this style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * @return Clone of this style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        DefaultSynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            style = (DefaultSynthStyle)super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        } catch (CloneNotSupportedException cnse) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (states != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            style.states = new StateInfo[states.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            for (int counter = states.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                style.states[counter] = (StateInfo)states[counter].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (data != null) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   465
            style.data = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            style.data.putAll(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Merges the contents of this Style with that of the passed in Style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * returning the resulting merged syle. Properties of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * <code>DefaultSynthStyle</code> will take precedence over those of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * passed in <code>DefaultSynthStyle</code>. For example, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * style specifics a non-null font, the returned style will have its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * font so to that regardless of the <code>style</code>'s font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @param style Style to add our styles to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @return Merged style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    public DefaultSynthStyle addTo(DefaultSynthStyle style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        if (insets != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            style.insets = this.insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            style.font = this.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (painter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            style.painter = this.painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        if (synthGraphics != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            style.synthGraphics = this.synthGraphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        style.opaque = opaque;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        if (states != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (style.states == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                style.states = new StateInfo[states.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                for (int counter = states.length - 1; counter >= 0; counter--){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    if (states[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                        style.states[counter] = (StateInfo)states[counter].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                                                clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // Find the number of new states in unique, merging any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // matching states as we go. Also, move any merge styles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // to the end to give them precedence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                int unique = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                // Number of StateInfos that match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                int matchCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                int maxOStyles = style.states.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                for (int thisCounter = states.length - 1; thisCounter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                         thisCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    int state = states[thisCounter].getComponentState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                    boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                    for (int oCounter = maxOStyles - 1 - matchCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                             oCounter >= 0; oCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                        if (state == style.states[oCounter].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                                           getComponentState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                            style.states[oCounter] = states[thisCounter].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                                        addTo(style.states[oCounter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                            // Move StateInfo to end, giving it precedence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                            StateInfo tmp = style.states[maxOStyles - 1 -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                                         matchCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                            style.states[maxOStyles - 1 - matchCount] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                  style.states[oCounter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                            style.states[oCounter] = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                            matchCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                            found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        unique++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                if (unique != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                    // There are states that exist in this Style that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    // don't exist in the other style, recreate the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                    // and add them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    StateInfo[] newStates = new StateInfo[
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                                   unique + maxOStyles];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    int newIndex = maxOStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    System.arraycopy(style.states, 0, newStates, 0,maxOStyles);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    for (int thisCounter = states.length - 1; thisCounter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                             thisCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                        int state = states[thisCounter].getComponentState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                        boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                        for (int oCounter = maxOStyles - 1; oCounter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                 oCounter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                            if (state == style.states[oCounter].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                                               getComponentState()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                        if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                            newStates[newIndex++] = (StateInfo)states[
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                      thisCounter].clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    style.states = newStates;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            if (style.data == null) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   573
                style.data = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            style.data.putAll(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * Sets the array of StateInfo's which are used to specify properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * specific to a particular style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param states StateInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    public void setStateInfo(StateInfo[] states) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        this.states = states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * Returns the array of StateInfo's that that are used to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * properties specific to a particular style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @return Array of StateInfos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    public StateInfo[] getStateInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * Returns the best matching StateInfo for a particular state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @param state Component state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @return Best matching StateInfo, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    public StateInfo getStateInfo(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        // Use the StateInfo with the most bits that matches that of state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        // If there is none, than fallback to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        // the StateInfo with a state of 0, indicating it'll match anything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        // Consider if we have 3 StateInfos a, b and c with states:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        // SELECTED, SELECTED | ENABLED, 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        // Input                          Return Value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // -----                          ------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        // SELECTED                       a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        // SELECTED | ENABLED             b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        // MOUSE_OVER                     c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        // SELECTED | ENABLED | FOCUSED   b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // ENABLED                        c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (states != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            int bestCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            int bestIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            int wildIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            if (state == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                for (int counter = states.length - 1; counter >= 0;counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    if (states[counter].getComponentState() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        return states[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            for (int counter = states.length - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                int oState = states[counter].getComponentState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                if (oState == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                    if (wildIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                        wildIndex = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                else if ((state & oState) == oState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    // This is key, we need to make sure all bits of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                    // StateInfo match, otherwise a StateInfo with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    // SELECTED | ENABLED would match ENABLED, which we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    // don't want.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    // This comes from BigInteger.bitCnt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                    int bitCount = oState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                    bitCount -= (0xaaaaaaaa & bitCount) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                    bitCount = (bitCount & 0x33333333) + ((bitCount >>> 2) &
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                                                      0x33333333);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    bitCount = bitCount + (bitCount >>> 4) & 0x0f0f0f0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                    bitCount += bitCount >>> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    bitCount += bitCount >>> 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    bitCount = bitCount & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                    if (bitCount > bestCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                        bestIndex = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                        bestCount = bitCount;
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
            if (bestIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                return states[bestIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            if (wildIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                return states[wildIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
          return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   676
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   678
        sb.append(super.toString()).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   680
        sb.append("data=").append(data).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   682
        sb.append("font=").append(font).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   684
        sb.append("insets=").append(insets).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   686
        sb.append("synthGraphics=").append(synthGraphics).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   688
        sb.append("painter=").append(painter).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        StateInfo[] states = getStateInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        if (states != null) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   692
            sb.append("states[");
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   693
            for (StateInfo state : states) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   694
                sb.append(state.toString()).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   696
            sb.append(']').append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        // remove last newline
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   700
        sb.deleteCharAt(sb.length() - 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   702
        return sb.toString();
2
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * StateInfo represents Style information specific to the state of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * a component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    public static class StateInfo {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   711
        private Map<Object, Object> data;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        private Font font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        private Color[] colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * Creates a new StateInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        public StateInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * Creates a new StateInfo with the specified properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * @param state Component state(s) that this StateInfo should be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         * @param painter Painter responsible for rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
         * @param bgPainter Painter responsible for rendering the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
         * @param font Font for this state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
         * @param colors Colors for this state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        public StateInfo(int state, Font font, Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            this.state = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            this.colors = colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         * Creates a new StateInfo that is a copy of the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
         * StateInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * @param info StateInfo to copy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        public StateInfo(StateInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            this.state = info.state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            this.font = info.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            if(info.data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
               if(data == null) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   749
                  data = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
               }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
               data.putAll(info.data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            if (info.colors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                this.colors = new Color[info.colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                System.arraycopy(info.colors, 0, colors, 0,info.colors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   759
        public Map<Object, Object> getData() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   763
        public void setData(Map<Object, Object> data) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            this.data = data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
         * Sets the font for this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
         * @param font Font to use for rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        public void setFont(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            this.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
         * Returns the font for this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * @return Returns the font to use for rendering this state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * Sets the array of colors to use for rendering this state. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * is indexed by <code>ColorType.getID()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         * @param colors Array of colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        public void setColors(Color[] colors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            this.colors = colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         * Returns the array of colors to use for rendering this state. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
         * is indexed by <code>ColorType.getID()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
         * @return Array of colors
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        public Color[] getColors() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            return colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
         * Returns the Color to used for the specified ColorType.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
         * @return Color.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        public Color getColor(ColorType type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            if (colors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                int id = type.getID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (id < colors.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    return colors[id];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
         * Merges the contents of this StateInfo with that of the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
         * StateInfo, returning the resulting merged StateInfo. Properties of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * this <code>StateInfo</code> will take precedence over those of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * passed in <code>StateInfo</code>. For example, if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * StateInfo specifics a non-null font, the returned StateInfo will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         * have its font so to that regardless of the <code>StateInfo</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
         * font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
         * @param info StateInfo to add our styles to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
         * @return Merged StateInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        public StateInfo addTo(StateInfo info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                info.font = font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            if(data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                if(info.data == null) {
22644
965bba13a5f0 8033525: Fix raw and unchecked lint warnings in sun.swing.*
darcy
parents: 21278
diff changeset
   839
                    info.data = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                info.data.putAll(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            if (colors != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                if (info.colors == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                    info.colors = new Color[colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    System.arraycopy(colors, 0, info.colors, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                                     colors.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    if (info.colors.length < colors.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                        Color[] old = info.colors;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                        info.colors = new Color[colors.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                        System.arraycopy(old, 0, info.colors, 0, old.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                    for (int counter = colors.length - 1; counter >= 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                             counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                        if (colors[counter] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                            info.colors[counter] = colors[counter];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            return info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
         * Sets the state this StateInfo corresponds to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
         * @see SynthConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
         * @param state info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        public void setComponentState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            this.state = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
         * Returns the state this StateInfo corresponds to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
         * @see SynthConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
         * @return state info.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        public int getComponentState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
            return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
         * Returns the number of states that are similar between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
         * ComponentState this StateInfo represents and val.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
         */
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   891
        private int getMatchCount(int val) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            // This comes from BigInteger.bitCnt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            val &= state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            val -= (0xaaaaaaaa & val) >>> 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            val = (val & 0x33333333) + ((val >>> 2) & 0x33333333);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            val = val + (val >>> 4) & 0x0f0f0f0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            val += val >>> 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            val += val >>> 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            return val & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         * Creates and returns a copy of this StateInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
         * @return Copy of this StateInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            return new StateInfo(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        public String toString() {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   912
            StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   914
            sb.append(super.toString()).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   916
            sb.append("state=").append(Integer.toString(state)).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   918
            sb.append("font=").append(font).append(',');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            if (colors != null) {
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   921
                sb.append("colors=").append(Arrays.asList(colors)).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                    append(',');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            }
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 22644
diff changeset
   924
            return sb.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
}