1 /*
   2  * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing.plaf.synth;
  26 
  27 import java.util.Queue;
  28 import java.util.concurrent.ConcurrentLinkedQueue;
  29 import javax.swing.JComponent;
  30 
  31 /**
  32  * An immutable transient object containing contextual information about
  33  * a <code>Region</code>. A <code>SynthContext</code> should only be
  34  * considered valid for the duration
  35  * of the method it is passed to. In other words you should not cache
  36  * a <code>SynthContext</code> that is passed to you and expect it to
  37  * remain valid.
  38  *
  39  * @since 1.5
  40  * @author Scott Violet
  41  */
  42 public class SynthContext {
  43     private static final Queue<SynthContext> queue = new ConcurrentLinkedQueue<>();
  44 
  45     private JComponent component;
  46     private Region region;
  47     private SynthStyle style;
  48     private int state;
  49 
  50     static SynthContext getContext(JComponent c, SynthStyle style, int state) {
  51         return getContext(c, SynthLookAndFeel.getRegion(c), style, state);
  52     }
  53 
  54     static SynthContext getContext(JComponent component,
  55                                    Region region, SynthStyle style,
  56                                    int state) {
  57         SynthContext context = queue.poll();
  58         if (context == null) {
  59             context = new SynthContext();
  60         }
  61         context.reset(component, region, style, state);
  62         return context;
  63     }
  64 
  65     static void releaseContext(SynthContext context) {
  66         queue.offer(context);
  67     }
  68 
  69     SynthContext() {
  70     }
  71 
  72     /**
  73      * Creates a SynthContext with the specified values. This is meant
  74      * for subclasses and custom UI implementors. You very rarely need to
  75      * construct a SynthContext, though some methods will take one.
  76      *
  77      * @param component JComponent
  78      * @param region Identifies the portion of the JComponent
  79      * @param style Style associated with the component
  80      * @param state State of the component as defined in SynthConstants.
  81      * @throws NullPointerException if component, region of style is null.
  82      */
  83     public SynthContext(JComponent component, Region region, SynthStyle style,
  84                         int state) {
  85         if (component == null || region == null || style == null) {
  86             throw new NullPointerException(
  87                 "You must supply a non-null component, region and style");
  88         }
  89         reset(component, region, style, state);
  90     }
  91 
  92 
  93     /**
  94      * Returns the hosting component containing the region.
  95      *
  96      * @return Hosting Component
  97      */
  98     public JComponent getComponent() {
  99         return component;
 100     }
 101 
 102     /**
 103      * Returns the Region identifying this state.
 104      *
 105      * @return Region of the hosting component
 106      */
 107     public Region getRegion() {
 108         return region;
 109     }
 110 
 111     /**
 112      * A convenience method for <code>getRegion().isSubregion()</code>.
 113      */
 114     boolean isSubregion() {
 115         return getRegion().isSubregion();
 116     }
 117 
 118     void setStyle(SynthStyle style) {
 119         this.style = style;
 120     }
 121 
 122     /**
 123      * Returns the style associated with this Region.
 124      *
 125      * @return SynthStyle associated with the region.
 126      */
 127     public SynthStyle getStyle() {
 128         return style;
 129     }
 130 
 131     void setComponentState(int state) {
 132         this.state = state;
 133     }
 134 
 135     /**
 136      * Returns the state of the widget, which is a bitmask of the
 137      * values defined in <code>SynthConstants</code>. A region will at least
 138      * be in one of
 139      * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
 140      * or <code>DISABLED</code>.
 141      *
 142      * @see SynthConstants
 143      * @return State of Component
 144      */
 145     public int getComponentState() {
 146         return state;
 147     }
 148 
 149     /**
 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.
 168      * This will NEVER return null.
 169      */
 170     SynthPainter getPainter() {
 171         SynthPainter painter = getStyle().getPainter(this);
 172 
 173         if (painter != null) {
 174             return painter;
 175         }
 176         return SynthPainter.NULL_PAINTER;
 177     }
 178 }
--- EOF ---