jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthContext.java
changeset 37698 4d798c873df0
parent 25859 3317bb8137f4
equal deleted inserted replaced
37697:9bdfdb90249b 37698:4d798c873df0
    22  * or visit www.oracle.com if you need additional information or have any
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 package javax.swing.plaf.synth;
    25 package javax.swing.plaf.synth;
    26 
    26 
    27 import java.util.Queue;
       
    28 import java.util.concurrent.ConcurrentLinkedQueue;
       
    29 import javax.swing.JComponent;
    27 import javax.swing.JComponent;
    30 
    28 
    31 /**
    29 /**
    32  * An immutable transient object containing contextual information about
    30  * An immutable transient object containing contextual information about
    33  * a <code>Region</code>. A <code>SynthContext</code> should only be
    31  * a <code>Region</code>. A <code>SynthContext</code> should only be
    38  *
    36  *
    39  * @since 1.5
    37  * @since 1.5
    40  * @author Scott Violet
    38  * @author Scott Violet
    41  */
    39  */
    42 public class SynthContext {
    40 public class SynthContext {
    43     private static final Queue<SynthContext> queue = new ConcurrentLinkedQueue<>();
       
    44 
    41 
    45     private JComponent component;
    42     private JComponent component;
    46     private Region region;
    43     private Region region;
    47     private SynthStyle style;
    44     private SynthStyle style;
    48     private int state;
    45     private int state;
    52     }
    49     }
    53 
    50 
    54     static SynthContext getContext(JComponent component,
    51     static SynthContext getContext(JComponent component,
    55                                    Region region, SynthStyle style,
    52                                    Region region, SynthStyle style,
    56                                    int state) {
    53                                    int state) {
    57         SynthContext context = queue.poll();
    54         SynthContext context = new SynthContext();
    58         if (context == null) {
    55         context.component = component;
    59             context = new SynthContext();
    56         context.region = region;
    60         }
    57         context.style = style;
    61         context.reset(component, region, style, state);
    58         context.state = state;
    62         return context;
    59         return context;
    63     }
    60     }
    64 
    61 
    65     static void releaseContext(SynthContext context) {
    62     private SynthContext() {
    66         queue.offer(context);
       
    67     }
       
    68 
       
    69     SynthContext() {
       
    70     }
    63     }
    71 
    64 
    72     /**
    65     /**
    73      * Creates a SynthContext with the specified values. This is meant
    66      * Creates a SynthContext with the specified values. This is meant
    74      * for subclasses and custom UI implementors. You very rarely need to
    67      * for subclasses and custom UI implementors. You very rarely need to
    84                         int state) {
    77                         int state) {
    85         if (component == null || region == null || style == null) {
    78         if (component == null || region == null || style == null) {
    86             throw new NullPointerException(
    79             throw new NullPointerException(
    87                 "You must supply a non-null component, region and style");
    80                 "You must supply a non-null component, region and style");
    88         }
    81         }
    89         reset(component, region, style, state);
    82 
       
    83         this.component = component;
       
    84         this.region = region;
       
    85         this.style = style;
       
    86         this.state = state;
    90     }
    87     }
    91 
    88 
    92 
    89 
    93     /**
    90     /**
    94      * Returns the hosting component containing the region.
    91      * Returns the hosting component containing the region.
   145     public int getComponentState() {
   142     public int getComponentState() {
   146         return state;
   143         return state;
   147     }
   144     }
   148 
   145 
   149     /**
   146     /**
   150      * Resets the state of the Context.
       
   151      */
       
   152     void reset(JComponent component, Region region, SynthStyle style,
       
   153                int state) {
       
   154         this.component = component;
       
   155         this.region = region;
       
   156         this.style = style;
       
   157         this.state = state;
       
   158     }
       
   159 
       
   160     void dispose() {
       
   161         this.component = null;
       
   162         this.style = null;
       
   163         releaseContext(this);
       
   164     }
       
   165 
       
   166     /**
       
   167      * Convenience method to get the Painter from the current SynthStyle.
   147      * Convenience method to get the Painter from the current SynthStyle.
   168      * This will NEVER return null.
   148      * This will NEVER return null.
   169      */
   149      */
   170     SynthPainter getPainter() {
   150     SynthPainter getPainter() {
   171         SynthPainter painter = getStyle().getPainter(this);
   151         SynthPainter painter = getStyle().getPainter(this);