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 javax.swing.JComponent;
28 
29 /**
30  * An immutable transient object containing contextual information about
31  * a <code>Region</code>. A <code>SynthContext</code> should only be
32  * considered valid for the duration
33  * of the method it is passed to. In other words you should not cache
34  * a <code>SynthContext</code> that is passed to you and expect it to
35  * remain valid.
36  *
37  * @since 1.5
38  * @author Scott Violet
39  */
40 public class SynthContext {
41 
42     private JComponent component;
43     private Region region;
44     private SynthStyle style;
45     private int state;
46 
getContext(JComponent c, SynthStyle style, int state)47     static SynthContext getContext(JComponent c, SynthStyle style, int state) {
48         return getContext(c, SynthLookAndFeel.getRegion(c), style, state);
49     }
50 
getContext(JComponent component, Region region, SynthStyle style, int state)51     static SynthContext getContext(JComponent component,
52                                    Region region, SynthStyle style,
53                                    int state) {
54         SynthContext context = new SynthContext();
55         context.component = component;
56         context.region = region;
57         context.style = style;
58         context.state = state;
59         return context;
60     }
61 
SynthContext()62     private SynthContext() {
63     }
64 
65     /**
66      * Creates a SynthContext with the specified values. This is meant
67      * for subclasses and custom UI implementors. You very rarely need to
68      * construct a SynthContext, though some methods will take one.
69      *
70      * @param component JComponent
71      * @param region Identifies the portion of the JComponent
72      * @param style Style associated with the component
73      * @param state State of the component as defined in SynthConstants.
74      * @throws NullPointerException if component, region of style is null.
75      */
SynthContext(JComponent component, Region region, SynthStyle style, int state)76     public SynthContext(JComponent component, Region region, SynthStyle style,
77                         int state) {
78         if (component == null || region == null || style == null) {
79             throw new NullPointerException(
80                 "You must supply a non-null component, region and style");
81         }
82 
83         this.component = component;
84         this.region = region;
85         this.style = style;
86         this.state = state;
87     }
88 
89 
90     /**
91      * Returns the hosting component containing the region.
92      *
93      * @return Hosting Component
94      */
getComponent()95     public JComponent getComponent() {
96         return component;
97     }
98 
99     /**
100      * Returns the Region identifying this state.
101      *
102      * @return Region of the hosting component
103      */
getRegion()104     public Region getRegion() {
105         return region;
106     }
107 
108     /**
109      * A convenience method for <code>getRegion().isSubregion()</code>.
110      */
isSubregion()111     boolean isSubregion() {
112         return getRegion().isSubregion();
113     }
114 
setStyle(SynthStyle style)115     void setStyle(SynthStyle style) {
116         this.style = style;
117     }
118 
119     /**
120      * Returns the style associated with this Region.
121      *
122      * @return SynthStyle associated with the region.
123      */
getStyle()124     public SynthStyle getStyle() {
125         return style;
126     }
127 
setComponentState(int state)128     void setComponentState(int state) {
129         this.state = state;
130     }
131 
132     /**
133      * Returns the state of the widget, which is a bitmask of the
134      * values defined in <code>SynthConstants</code>. A region will at least
135      * be in one of
136      * <code>ENABLED</code>, <code>MOUSE_OVER</code>, <code>PRESSED</code>
137      * or <code>DISABLED</code>.
138      *
139      * @see SynthConstants
140      * @return State of Component
141      */
getComponentState()142     public int getComponentState() {
143         return state;
144     }
145 
146     /**
147      * Convenience method to get the Painter from the current SynthStyle.
148      * This will NEVER return null.
149      */
getPainter()150     SynthPainter getPainter() {
151         SynthPainter painter = getStyle().getPainter(this);
152 
153         if (painter != null) {
154             return painter;
155         }
156         return SynthPainter.NULL_PAINTER;
157     }
158 }
159