jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthContext.java
author alexsch
Wed, 18 Nov 2015 19:13:42 +0400
changeset 34407 2b40f400a30b
parent 25859 3317bb8137f4
child 37698 4d798c873df0
permissions -rw-r--r--
8081411: Add an API for painting an icon with a SynthContext Reviewed-by: serb, azvegint
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 2002, 2008, 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 javax.swing.plaf.synth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    27
import java.util.Queue;
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    28
import java.util.concurrent.ConcurrentLinkedQueue;
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    29
import javax.swing.JComponent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * An immutable transient object containing contextual information about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a <code>Region</code>. A <code>SynthContext</code> should only be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * considered valid for the duration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of the method it is passed to. In other words you should not cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * a <code>SynthContext</code> that is passed to you and expect it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * remain valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class SynthContext {
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    43
    private static final Queue<SynthContext> queue = new ConcurrentLinkedQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private JComponent component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Region region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private SynthStyle style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private int state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    50
    static SynthContext getContext(JComponent c, SynthStyle style, int state) {
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    51
        return getContext(c, SynthLookAndFeel.getRegion(c), style, state);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    54
    static SynthContext getContext(JComponent component,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                   Region region, SynthStyle style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                                   int state) {
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    57
        SynthContext context = queue.poll();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (context == null) {
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    59
            context = new SynthContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        context.reset(component, region, style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return context;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static void releaseContext(SynthContext context) {
25100
d527cc827d7d 8043627: NPE in SynthContext in plugin mode
malenkov
parents: 25088
diff changeset
    66
        queue.offer(context);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    SynthContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a SynthContext with the specified values. This is meant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * for subclasses and custom UI implementors. You very rarely need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * construct a SynthContext, though some methods will take one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param component JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param region Identifies the portion of the JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param style Style associated with the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param state State of the component as defined in SynthConstants.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @throws NullPointerException if component, region of style is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public SynthContext(JComponent component, Region region, SynthStyle style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (component == null || region == null || style == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                "You must supply a non-null component, region and style");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        reset(component, region, style, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns the hosting component containing the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return Hosting Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public JComponent getComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Returns the Region identifying this state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @return Region of the hosting component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public Region getRegion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * A convenience method for <code>getRegion().isSubregion()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    boolean isSubregion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return getRegion().isSubregion();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    void setStyle(SynthStyle style) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        this.style = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Returns the style associated with this Region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @return SynthStyle associated with the region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public SynthStyle getStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    void setComponentState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        this.state = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Returns the state of the widget, which is a bitmask of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * values defined in <code>SynthConstants</code>. A region will at least
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * be in one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * or <code>DISABLED</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @see SynthConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return State of Component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public int getComponentState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Resets the state of the Context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    void reset(JComponent component, Region region, SynthStyle style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
               int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.component = component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.region = region;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.style = style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.state = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        this.component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.style = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        releaseContext(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Convenience method to get the Painter from the current SynthStyle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * This will NEVER return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    SynthPainter getPainter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        SynthPainter painter = getStyle().getPainter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (painter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            return painter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        return SynthPainter.NULL_PAINTER;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
}