< prev index next >

src/java.desktop/share/classes/sun/swing/plaf/synth/DefaultSynthStyle.java

Print this page




  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 sun.swing.plaf.synth;
  26 
  27 import javax.swing.plaf.synth.*;
  28 import java.awt.*;
  29 import java.util.*;
  30 import javax.swing.*;
  31 import javax.swing.plaf.*;
  32 
  33 /**
  34  * Default implementation of SynthStyle. Has setters for the various
  35  * SynthStyle methods. Many of the properties can be specified for all states,
  36  * using SynthStyle directly, or a specific state using one of the StateInfo
  37  * methods.
  38  * <p>
  39  * Beyond the constructor a subclass should override the <code>addTo</code>
  40  * and <code>clone</code> methods, these are used when the Styles are being
  41  * merged into a resulting style.
  42  *
  43  * @author Scott Violet
  44  */
  45 public class DefaultSynthStyle extends SynthStyle implements Cloneable {
  46 
  47     private static final Object PENDING = new Object();
  48 
  49     /**
  50      * Should the component be opaque?
  51      */
  52     private boolean opaque;
  53     /**
  54      * Insets.
  55      */
  56     private Insets insets;
  57     /**
  58      * Information specific to ComponentState.
  59      */
  60     private StateInfo[] states;


 279      * @param context SynthContext identifying requestor
 280      * @return SynthGraphicsUtils
 281      */
 282     public SynthGraphicsUtils getGraphicsUtils(SynthContext context) {
 283         if (synthGraphics == null) {
 284             return super.getGraphicsUtils(context);
 285         }
 286         return synthGraphics;
 287     }
 288 
 289     /**
 290      * Sets the insets.
 291      *
 292      * @param insets the new insets.
 293      */
 294     public void setInsets(Insets insets) {
 295         this.insets = insets;
 296     }
 297 
 298     /**
 299      * Returns the Insets. If <code>to</code> is non-null the resulting
 300      * insets will be placed in it, otherwise a new Insets object will be
 301      * created and returned.
 302      *
 303      * @param state SynthContext identifying requestor
 304      * @param to Where to place Insets
 305      * @return Insets.
 306      */
 307     public Insets getInsets(SynthContext state, Insets to) {
 308         if (to == null) {
 309             to = new Insets(0, 0, 0, 0);
 310         }
 311         if (insets != null) {
 312             to.left = insets.left;
 313             to.right = insets.right;
 314             to.top = insets.top;
 315             to.bottom = insets.bottom;
 316         }
 317         else {
 318             to.left = to.right = to.top = to.bottom = 0;
 319         }


 416                     } catch (InterruptedException ie) {}
 417                     value = stateData.get(key);
 418                 }
 419             }
 420             if (value instanceof UIDefaults.LazyValue) {
 421                 synchronized(stateData) {
 422                     stateData.put(key, PENDING);
 423                 }
 424                 value = ((UIDefaults.LazyValue)value).createValue(null);
 425                 synchronized(stateData) {
 426                     stateData.put(key, value);
 427                     stateData.notifyAll();
 428                 }
 429             }
 430         }
 431         return value;
 432     }
 433 
 434     /**
 435      * Returns the default value for a particular property.  This is only
 436      * invoked if this style doesn't define a property for <code>key</code>.
 437      *
 438      * @param context SynthContext identifying requestor
 439      * @param key Property being requested.
 440      * @return Value of the named property
 441      */
 442     public Object getDefaultValue(SynthContext context, Object key) {
 443         return super.get(context, key);
 444     }
 445 
 446     /**
 447      * Creates a clone of this style.
 448      *
 449      * @return Clone of this style
 450      */
 451     public Object clone() {
 452         DefaultSynthStyle style;
 453         try {
 454             style = (DefaultSynthStyle)super.clone();
 455         } catch (CloneNotSupportedException cnse) {
 456             return null;
 457         }
 458         if (states != null) {
 459             style.states = new StateInfo[states.length];
 460             for (int counter = states.length - 1; counter >= 0; counter--) {
 461                 style.states[counter] = (StateInfo)states[counter].clone();
 462             }
 463         }
 464         if (data != null) {
 465             style.data = new HashMap<>();
 466             style.data.putAll(data);
 467         }
 468         return style;
 469     }
 470 
 471     /**
 472      * Merges the contents of this Style with that of the passed in Style,
 473      * returning the resulting merged syle. Properties of this
 474      * <code>DefaultSynthStyle</code> will take precedence over those of the
 475      * passed in <code>DefaultSynthStyle</code>. For example, if this
 476      * style specifics a non-null font, the returned style will have its
 477      * font so to that regardless of the <code>style</code>'s font.
 478      *
 479      * @param style Style to add our styles to
 480      * @return Merged style.
 481      */
 482     public DefaultSynthStyle addTo(DefaultSynthStyle style) {
 483         if (insets != null) {
 484             style.insets = this.insets;
 485         }
 486         if (font != null) {
 487             style.font = this.font;
 488         }
 489         if (painter != null) {
 490             style.painter = this.painter;
 491         }
 492         if (synthGraphics != null) {
 493             style.synthGraphics = this.synthGraphics;
 494         }
 495         style.opaque = opaque;
 496         if (states != null) {
 497             if (style.states == null) {


 765         /**
 766          * Sets the font for this state.
 767          *
 768          * @param font Font to use for rendering
 769          */
 770         public void setFont(Font font) {
 771             this.font = font;
 772         }
 773 
 774         /**
 775          * Returns the font for this state.
 776          *
 777          * @return Returns the font to use for rendering this state
 778          */
 779         public Font getFont() {
 780             return font;
 781         }
 782 
 783         /**
 784          * Sets the array of colors to use for rendering this state. This
 785          * is indexed by <code>ColorType.getID()</code>.
 786          *
 787          * @param colors Array of colors
 788          */
 789         public void setColors(Color[] colors) {
 790             this.colors = colors;
 791         }
 792 
 793         /**
 794          * Returns the array of colors to use for rendering this state. This
 795          * is indexed by <code>ColorType.getID()</code>.
 796          *
 797          * @return Array of colors
 798          */
 799         public Color[] getColors() {
 800             return colors;
 801         }
 802 
 803         /**
 804          * Returns the Color to used for the specified ColorType.
 805          *
 806          * @return Color.
 807          */
 808         public Color getColor(ColorType type) {
 809             if (colors != null) {
 810                 int id = type.getID();
 811 
 812                 if (id < colors.length) {
 813                     return colors[id];
 814                 }
 815             }
 816             return null;
 817         }
 818 
 819         /**
 820          * Merges the contents of this StateInfo with that of the passed in
 821          * StateInfo, returning the resulting merged StateInfo. Properties of
 822          * this <code>StateInfo</code> will take precedence over those of the
 823          * passed in <code>StateInfo</code>. For example, if this
 824          * StateInfo specifics a non-null font, the returned StateInfo will
 825          * have its font so to that regardless of the <code>StateInfo</code>'s
 826          * font.
 827          *
 828          * @param info StateInfo to add our styles to
 829          * @return Merged StateInfo.
 830          */
 831         public StateInfo addTo(StateInfo info) {
 832             if (font != null) {
 833                 info.font = font;
 834             }
 835             if(data != null) {
 836                 if(info.data == null) {
 837                     info.data = new HashMap<>();
 838                 }
 839                 info.data.putAll(data);
 840             }
 841             if (colors != null) {
 842                 if (info.colors == null) {
 843                     info.colors = new Color[colors.length];
 844                     System.arraycopy(colors, 0, info.colors, 0,
 845                                      colors.length);




  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 sun.swing.plaf.synth;
  26 
  27 import javax.swing.plaf.synth.*;
  28 import java.awt.*;
  29 import java.util.*;
  30 import javax.swing.*;
  31 import javax.swing.plaf.*;
  32 
  33 /**
  34  * Default implementation of SynthStyle. Has setters for the various
  35  * SynthStyle methods. Many of the properties can be specified for all states,
  36  * using SynthStyle directly, or a specific state using one of the StateInfo
  37  * methods.
  38  * <p>
  39  * Beyond the constructor a subclass should override the {@code addTo}
  40  * and {@code clone} methods, these are used when the Styles are being
  41  * merged into a resulting style.
  42  *
  43  * @author Scott Violet
  44  */
  45 public class DefaultSynthStyle extends SynthStyle implements Cloneable {
  46 
  47     private static final Object PENDING = new Object();
  48 
  49     /**
  50      * Should the component be opaque?
  51      */
  52     private boolean opaque;
  53     /**
  54      * Insets.
  55      */
  56     private Insets insets;
  57     /**
  58      * Information specific to ComponentState.
  59      */
  60     private StateInfo[] states;


 279      * @param context SynthContext identifying requestor
 280      * @return SynthGraphicsUtils
 281      */
 282     public SynthGraphicsUtils getGraphicsUtils(SynthContext context) {
 283         if (synthGraphics == null) {
 284             return super.getGraphicsUtils(context);
 285         }
 286         return synthGraphics;
 287     }
 288 
 289     /**
 290      * Sets the insets.
 291      *
 292      * @param insets the new insets.
 293      */
 294     public void setInsets(Insets insets) {
 295         this.insets = insets;
 296     }
 297 
 298     /**
 299      * Returns the Insets. If {@code to} is non-null the resulting
 300      * insets will be placed in it, otherwise a new Insets object will be
 301      * created and returned.
 302      *
 303      * @param state SynthContext identifying requestor
 304      * @param to Where to place Insets
 305      * @return Insets.
 306      */
 307     public Insets getInsets(SynthContext state, Insets to) {
 308         if (to == null) {
 309             to = new Insets(0, 0, 0, 0);
 310         }
 311         if (insets != null) {
 312             to.left = insets.left;
 313             to.right = insets.right;
 314             to.top = insets.top;
 315             to.bottom = insets.bottom;
 316         }
 317         else {
 318             to.left = to.right = to.top = to.bottom = 0;
 319         }


 416                     } catch (InterruptedException ie) {}
 417                     value = stateData.get(key);
 418                 }
 419             }
 420             if (value instanceof UIDefaults.LazyValue) {
 421                 synchronized(stateData) {
 422                     stateData.put(key, PENDING);
 423                 }
 424                 value = ((UIDefaults.LazyValue)value).createValue(null);
 425                 synchronized(stateData) {
 426                     stateData.put(key, value);
 427                     stateData.notifyAll();
 428                 }
 429             }
 430         }
 431         return value;
 432     }
 433 
 434     /**
 435      * Returns the default value for a particular property.  This is only
 436      * invoked if this style doesn't define a property for {@code key}.
 437      *
 438      * @param context SynthContext identifying requestor
 439      * @param key Property being requested.
 440      * @return Value of the named property
 441      */
 442     public Object getDefaultValue(SynthContext context, Object key) {
 443         return super.get(context, key);
 444     }
 445 
 446     /**
 447      * Creates a clone of this style.
 448      *
 449      * @return Clone of this style
 450      */
 451     public Object clone() {
 452         DefaultSynthStyle style;
 453         try {
 454             style = (DefaultSynthStyle)super.clone();
 455         } catch (CloneNotSupportedException cnse) {
 456             return null;
 457         }
 458         if (states != null) {
 459             style.states = new StateInfo[states.length];
 460             for (int counter = states.length - 1; counter >= 0; counter--) {
 461                 style.states[counter] = (StateInfo)states[counter].clone();
 462             }
 463         }
 464         if (data != null) {
 465             style.data = new HashMap<>();
 466             style.data.putAll(data);
 467         }
 468         return style;
 469     }
 470 
 471     /**
 472      * Merges the contents of this Style with that of the passed in Style,
 473      * returning the resulting merged syle. Properties of this
 474      * {@code DefaultSynthStyle} will take precedence over those of the
 475      * passed in {@code DefaultSynthStyle}. For example, if this
 476      * style specifics a non-null font, the returned style will have its
 477      * font so to that regardless of the {@code style}'s font.
 478      *
 479      * @param style Style to add our styles to
 480      * @return Merged style.
 481      */
 482     public DefaultSynthStyle addTo(DefaultSynthStyle style) {
 483         if (insets != null) {
 484             style.insets = this.insets;
 485         }
 486         if (font != null) {
 487             style.font = this.font;
 488         }
 489         if (painter != null) {
 490             style.painter = this.painter;
 491         }
 492         if (synthGraphics != null) {
 493             style.synthGraphics = this.synthGraphics;
 494         }
 495         style.opaque = opaque;
 496         if (states != null) {
 497             if (style.states == null) {


 765         /**
 766          * Sets the font for this state.
 767          *
 768          * @param font Font to use for rendering
 769          */
 770         public void setFont(Font font) {
 771             this.font = font;
 772         }
 773 
 774         /**
 775          * Returns the font for this state.
 776          *
 777          * @return Returns the font to use for rendering this state
 778          */
 779         public Font getFont() {
 780             return font;
 781         }
 782 
 783         /**
 784          * Sets the array of colors to use for rendering this state. This
 785          * is indexed by {@code ColorType.getID()}.
 786          *
 787          * @param colors Array of colors
 788          */
 789         public void setColors(Color[] colors) {
 790             this.colors = colors;
 791         }
 792 
 793         /**
 794          * Returns the array of colors to use for rendering this state. This
 795          * is indexed by {@code ColorType.getID()}.
 796          *
 797          * @return Array of colors
 798          */
 799         public Color[] getColors() {
 800             return colors;
 801         }
 802 
 803         /**
 804          * Returns the Color to used for the specified ColorType.
 805          *
 806          * @return Color.
 807          */
 808         public Color getColor(ColorType type) {
 809             if (colors != null) {
 810                 int id = type.getID();
 811 
 812                 if (id < colors.length) {
 813                     return colors[id];
 814                 }
 815             }
 816             return null;
 817         }
 818 
 819         /**
 820          * Merges the contents of this StateInfo with that of the passed in
 821          * StateInfo, returning the resulting merged StateInfo. Properties of
 822          * this {@code StateInfo} will take precedence over those of the
 823          * passed in {@code StateInfo}. For example, if this
 824          * StateInfo specifics a non-null font, the returned StateInfo will
 825          * have its font so to that regardless of the {@code StateInfo}'s
 826          * font.
 827          *
 828          * @param info StateInfo to add our styles to
 829          * @return Merged StateInfo.
 830          */
 831         public StateInfo addTo(StateInfo info) {
 832             if (font != null) {
 833                 info.font = font;
 834             }
 835             if(data != null) {
 836                 if(info.data == null) {
 837                     info.data = new HashMap<>();
 838                 }
 839                 info.data.putAll(data);
 840             }
 841             if (colors != null) {
 842                 if (info.colors == null) {
 843                     info.colors = new Color[colors.length];
 844                     System.arraycopy(colors, 0, info.colors, 0,
 845                                      colors.length);


< prev index next >