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
44 * @author Scott Violet
45 */
46 public class DefaultSynthStyle extends SynthStyle implements Cloneable {
47 private static final String PENDING = "Pending";
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;
61 /**
62 * User specific data.
63 */
64 private Map data;
65
66 /**
67 * Font to use if there is no matching StateInfo, or the StateInfo doesn't
68 * define one.
69 */
70 private Font font;
71
72 /**
73 * SynthGraphics, may be null.
74 */
75 private SynthGraphicsUtils synthGraphics;
76
77 /**
78 * Painter to use if the StateInfo doesn't have one.
79 */
80 private SynthPainter painter;
81
82
83 /**
84 * Nullary constructor, intended for subclassers.
89 /**
90 * Creates a new DefaultSynthStyle that is a copy of the passed in
91 * style. Any StateInfo's of the passed in style are clonsed as well.
92 *
93 * @param style Style to duplicate
94 */
95 public DefaultSynthStyle(DefaultSynthStyle style) {
96 opaque = style.opaque;
97 if (style.insets != null) {
98 insets = new Insets(style.insets.top, style.insets.left,
99 style.insets.bottom, style.insets.right);
100 }
101 if (style.states != null) {
102 states = new StateInfo[style.states.length];
103 for (int counter = style.states.length - 1; counter >= 0;
104 counter--) {
105 states[counter] = (StateInfo)style.states[counter].clone();
106 }
107 }
108 if (style.data != null) {
109 data = new HashMap();
110 data.putAll(style.data);
111 }
112 font = style.font;
113 synthGraphics = style.synthGraphics;
114 painter = style.painter;
115 }
116
117 /**
118 * Creates a new DefaultSynthStyle.
119 *
120 * @param insets Insets for the Style
121 * @param opaque Whether or not the background is completely painted in
122 * an opaque color
123 * @param states StateInfos describing properties per state
124 * @param data Style specific data.
125 */
126 public DefaultSynthStyle(Insets insets, boolean opaque,
127 StateInfo[] states, Map data) {
128 this.insets = insets;
129 this.opaque = opaque;
130 this.states = states;
131 this.data = data;
132 }
133
134 public Color getColor(SynthContext context, ColorType type) {
135 return getColor(context.getComponent(), context.getRegion(),
136 context.getComponentState(), type);
137 }
138
139 public Color getColor(JComponent c, Region id, int state,
140 ColorType type) {
141 // For the enabled state, prefer the widget's colors
142 if (!id.isSubregion() && state == SynthConstants.ENABLED) {
143 if (type == ColorType.BACKGROUND) {
144 return c.getBackground();
145 }
146 else if (type == ColorType.FOREGROUND) {
147 return c.getForeground();
349 }
350
351 /**
352 * Returns the value to initialize the opacity property of the Component
353 * to. A Style should NOT assume the opacity will remain this value, the
354 * developer may reset it or override it.
355 *
356 * @param ss SynthContext identifying requestor
357 * @return opaque Whether or not the JComponent is opaque.
358 */
359 public boolean isOpaque(SynthContext ss) {
360 return opaque;
361 }
362
363 /**
364 * Sets style specific values. This does NOT copy the data, it
365 * assigns it directly to this Style.
366 *
367 * @param data Style specific values
368 */
369 public void setData(Map data) {
370 this.data = data;
371 }
372
373 /**
374 * Returns the style specific data.
375 *
376 * @return Style specific data.
377 */
378 public Map getData() {
379 return data;
380 }
381
382 /**
383 * Getter for a region specific style property.
384 *
385 * @param state SynthContext identifying requestor
386 * @param key Property being requested.
387 * @return Value of the named property
388 */
389 public Object get(SynthContext state, Object key) {
390 // Look for the best match
391 StateInfo si = getStateInfo(state.getComponentState());
392 if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
393 return getKeyFromData(si.getData(), key);
394 }
395 si = getStateInfo(0);
396 if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
397 return getKeyFromData(si.getData(), key);
398 }
399 if(getKeyFromData(data, key) != null)
400 return getKeyFromData(data, key);
401 return getDefaultValue(state, key);
402 }
403
404
405 private Object getKeyFromData(Map stateData, Object key) {
406 Object value = null;
407 if (stateData != null) {
408
409 synchronized(stateData) {
410 value = stateData.get(key);
411 }
412 while (value == PENDING) {
413 synchronized(stateData) {
414 try {
415 stateData.wait();
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) {
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 }
553
554 for (int oCounter = maxOStyles - 1; oCounter >= 0;
555 oCounter--) {
556 if (state == style.states[oCounter].
557 getComponentState()) {
558 found = true;
559 break;
560 }
561 }
562 if (!found) {
563 newStates[newIndex++] = (StateInfo)states[
564 thisCounter].clone();
565 }
566 }
567 style.states = newStates;
568 }
569 }
570 }
571 if (data != null) {
572 if (style.data == null) {
573 style.data = new HashMap();
574 }
575 style.data.putAll(data);
576 }
577 return style;
578 }
579
580 /**
581 * Sets the array of StateInfo's which are used to specify properties
582 * specific to a particular style.
583 *
584 * @param states StateInfos
585 */
586 public void setStateInfo(StateInfo[] states) {
587 this.states = states;
588 }
589
590 /**
591 * Returns the array of StateInfo's that that are used to specify
592 * properties specific to a particular style.
593 *
691 if (states != null) {
692 buf.append("states[");
693 for (StateInfo state : states) {
694 buf.append(state.toString()).append(',');
695 }
696 buf.append(']').append(',');
697 }
698
699 // remove last newline
700 buf.deleteCharAt(buf.length() - 1);
701
702 return buf.toString();
703 }
704
705
706 /**
707 * StateInfo represents Style information specific to the state of
708 * a component.
709 */
710 public static class StateInfo {
711 private Map data;
712 private Font font;
713 private Color[] colors;
714 private int state;
715
716 /**
717 * Creates a new StateInfo.
718 */
719 public StateInfo() {
720 }
721
722 /**
723 * Creates a new StateInfo with the specified properties
724 *
725 * @param state Component state(s) that this StateInfo should be used
726 * for
727 * @param painter Painter responsible for rendering
728 * @param bgPainter Painter responsible for rendering the background
729 * @param font Font for this state
730 * @param colors Colors for this state
731 */
732 public StateInfo(int state, Font font, Color[] colors) {
733 this.state = state;
734 this.font = font;
735 this.colors = colors;
736 }
737
738 /**
739 * Creates a new StateInfo that is a copy of the passed in
740 * StateInfo.
741 *
742 * @param info StateInfo to copy.
743 */
744 public StateInfo(StateInfo info) {
745 this.state = info.state;
746 this.font = info.font;
747 if(info.data != null) {
748 if(data == null) {
749 data = new HashMap();
750 }
751 data.putAll(info.data);
752 }
753 if (info.colors != null) {
754 this.colors = new Color[info.colors.length];
755 System.arraycopy(info.colors, 0, colors, 0,info.colors.length);
756 }
757 }
758
759 public Map getData() {
760 return data;
761 }
762
763 public void setData(Map data) {
764 this.data = data;
765 }
766
767 /**
768 * Sets the font for this state.
769 *
770 * @param font Font to use for rendering
771 */
772 public void setFont(Font font) {
773 this.font = font;
774 }
775
776 /**
777 * Returns the font for this state.
778 *
779 * @return Returns the font to use for rendering this state
780 */
781 public Font getFont() {
782 return font;
783 }
819 }
820
821 /**
822 * Merges the contents of this StateInfo with that of the passed in
823 * StateInfo, returning the resulting merged StateInfo. Properties of
824 * this <code>StateInfo</code> will take precedence over those of the
825 * passed in <code>StateInfo</code>. For example, if this
826 * StateInfo specifics a non-null font, the returned StateInfo will
827 * have its font so to that regardless of the <code>StateInfo</code>'s
828 * font.
829 *
830 * @param info StateInfo to add our styles to
831 * @return Merged StateInfo.
832 */
833 public StateInfo addTo(StateInfo info) {
834 if (font != null) {
835 info.font = font;
836 }
837 if(data != null) {
838 if(info.data == null) {
839 info.data = new HashMap();
840 }
841 info.data.putAll(data);
842 }
843 if (colors != null) {
844 if (info.colors == null) {
845 info.colors = new Color[colors.length];
846 System.arraycopy(colors, 0, info.colors, 0,
847 colors.length);
848 }
849 else {
850 if (info.colors.length < colors.length) {
851 Color[] old = info.colors;
852
853 info.colors = new Color[colors.length];
854 System.arraycopy(old, 0, info.colors, 0, old.length);
855 }
856 for (int counter = colors.length - 1; counter >= 0;
857 counter--) {
858 if (colors[counter] != null) {
859 info.colors[counter] = colors[counter];
|
1 /*
2 * Copyright (c) 2002, 2014, 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
44 * @author Scott Violet
45 */
46 public class DefaultSynthStyle extends SynthStyle implements Cloneable {
47 private static final String PENDING = "Pending";
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;
61 /**
62 * User specific data.
63 */
64 private Map<Object, Object> data;
65
66 /**
67 * Font to use if there is no matching StateInfo, or the StateInfo doesn't
68 * define one.
69 */
70 private Font font;
71
72 /**
73 * SynthGraphics, may be null.
74 */
75 private SynthGraphicsUtils synthGraphics;
76
77 /**
78 * Painter to use if the StateInfo doesn't have one.
79 */
80 private SynthPainter painter;
81
82
83 /**
84 * Nullary constructor, intended for subclassers.
89 /**
90 * Creates a new DefaultSynthStyle that is a copy of the passed in
91 * style. Any StateInfo's of the passed in style are clonsed as well.
92 *
93 * @param style Style to duplicate
94 */
95 public DefaultSynthStyle(DefaultSynthStyle style) {
96 opaque = style.opaque;
97 if (style.insets != null) {
98 insets = new Insets(style.insets.top, style.insets.left,
99 style.insets.bottom, style.insets.right);
100 }
101 if (style.states != null) {
102 states = new StateInfo[style.states.length];
103 for (int counter = style.states.length - 1; counter >= 0;
104 counter--) {
105 states[counter] = (StateInfo)style.states[counter].clone();
106 }
107 }
108 if (style.data != null) {
109 data = new HashMap<>();
110 data.putAll(style.data);
111 }
112 font = style.font;
113 synthGraphics = style.synthGraphics;
114 painter = style.painter;
115 }
116
117 /**
118 * Creates a new DefaultSynthStyle.
119 *
120 * @param insets Insets for the Style
121 * @param opaque Whether or not the background is completely painted in
122 * an opaque color
123 * @param states StateInfos describing properties per state
124 * @param data Style specific data.
125 */
126 public DefaultSynthStyle(Insets insets, boolean opaque,
127 StateInfo[] states, Map<Object, Object> data) {
128 this.insets = insets;
129 this.opaque = opaque;
130 this.states = states;
131 this.data = data;
132 }
133
134 public Color getColor(SynthContext context, ColorType type) {
135 return getColor(context.getComponent(), context.getRegion(),
136 context.getComponentState(), type);
137 }
138
139 public Color getColor(JComponent c, Region id, int state,
140 ColorType type) {
141 // For the enabled state, prefer the widget's colors
142 if (!id.isSubregion() && state == SynthConstants.ENABLED) {
143 if (type == ColorType.BACKGROUND) {
144 return c.getBackground();
145 }
146 else if (type == ColorType.FOREGROUND) {
147 return c.getForeground();
349 }
350
351 /**
352 * Returns the value to initialize the opacity property of the Component
353 * to. A Style should NOT assume the opacity will remain this value, the
354 * developer may reset it or override it.
355 *
356 * @param ss SynthContext identifying requestor
357 * @return opaque Whether or not the JComponent is opaque.
358 */
359 public boolean isOpaque(SynthContext ss) {
360 return opaque;
361 }
362
363 /**
364 * Sets style specific values. This does NOT copy the data, it
365 * assigns it directly to this Style.
366 *
367 * @param data Style specific values
368 */
369 public void setData(Map<Object, Object> data) {
370 this.data = data;
371 }
372
373 /**
374 * Returns the style specific data.
375 *
376 * @return Style specific data.
377 */
378 public Map<Object, Object> getData() {
379 return data;
380 }
381
382 /**
383 * Getter for a region specific style property.
384 *
385 * @param state SynthContext identifying requestor
386 * @param key Property being requested.
387 * @return Value of the named property
388 */
389 public Object get(SynthContext state, Object key) {
390 // Look for the best match
391 StateInfo si = getStateInfo(state.getComponentState());
392 if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
393 return getKeyFromData(si.getData(), key);
394 }
395 si = getStateInfo(0);
396 if (si != null && si.getData() != null && getKeyFromData(si.getData(), key) != null) {
397 return getKeyFromData(si.getData(), key);
398 }
399 if(getKeyFromData(data, key) != null)
400 return getKeyFromData(data, key);
401 return getDefaultValue(state, key);
402 }
403
404
405 private Object getKeyFromData(Map<Object, Object> stateData, Object key) {
406 Object value = null;
407 if (stateData != null) {
408
409 synchronized(stateData) {
410 value = stateData.get(key);
411 }
412 while (value == PENDING) {
413 synchronized(stateData) {
414 try {
415 stateData.wait();
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) {
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 }
553
554 for (int oCounter = maxOStyles - 1; oCounter >= 0;
555 oCounter--) {
556 if (state == style.states[oCounter].
557 getComponentState()) {
558 found = true;
559 break;
560 }
561 }
562 if (!found) {
563 newStates[newIndex++] = (StateInfo)states[
564 thisCounter].clone();
565 }
566 }
567 style.states = newStates;
568 }
569 }
570 }
571 if (data != null) {
572 if (style.data == null) {
573 style.data = new HashMap<>();
574 }
575 style.data.putAll(data);
576 }
577 return style;
578 }
579
580 /**
581 * Sets the array of StateInfo's which are used to specify properties
582 * specific to a particular style.
583 *
584 * @param states StateInfos
585 */
586 public void setStateInfo(StateInfo[] states) {
587 this.states = states;
588 }
589
590 /**
591 * Returns the array of StateInfo's that that are used to specify
592 * properties specific to a particular style.
593 *
691 if (states != null) {
692 buf.append("states[");
693 for (StateInfo state : states) {
694 buf.append(state.toString()).append(',');
695 }
696 buf.append(']').append(',');
697 }
698
699 // remove last newline
700 buf.deleteCharAt(buf.length() - 1);
701
702 return buf.toString();
703 }
704
705
706 /**
707 * StateInfo represents Style information specific to the state of
708 * a component.
709 */
710 public static class StateInfo {
711 private Map<Object, Object> data;
712 private Font font;
713 private Color[] colors;
714 private int state;
715
716 /**
717 * Creates a new StateInfo.
718 */
719 public StateInfo() {
720 }
721
722 /**
723 * Creates a new StateInfo with the specified properties
724 *
725 * @param state Component state(s) that this StateInfo should be used
726 * for
727 * @param painter Painter responsible for rendering
728 * @param bgPainter Painter responsible for rendering the background
729 * @param font Font for this state
730 * @param colors Colors for this state
731 */
732 public StateInfo(int state, Font font, Color[] colors) {
733 this.state = state;
734 this.font = font;
735 this.colors = colors;
736 }
737
738 /**
739 * Creates a new StateInfo that is a copy of the passed in
740 * StateInfo.
741 *
742 * @param info StateInfo to copy.
743 */
744 public StateInfo(StateInfo info) {
745 this.state = info.state;
746 this.font = info.font;
747 if(info.data != null) {
748 if(data == null) {
749 data = new HashMap<>();
750 }
751 data.putAll(info.data);
752 }
753 if (info.colors != null) {
754 this.colors = new Color[info.colors.length];
755 System.arraycopy(info.colors, 0, colors, 0,info.colors.length);
756 }
757 }
758
759 public Map<Object, Object> getData() {
760 return data;
761 }
762
763 public void setData(Map<Object, Object> data) {
764 this.data = data;
765 }
766
767 /**
768 * Sets the font for this state.
769 *
770 * @param font Font to use for rendering
771 */
772 public void setFont(Font font) {
773 this.font = font;
774 }
775
776 /**
777 * Returns the font for this state.
778 *
779 * @return Returns the font to use for rendering this state
780 */
781 public Font getFont() {
782 return font;
783 }
819 }
820
821 /**
822 * Merges the contents of this StateInfo with that of the passed in
823 * StateInfo, returning the resulting merged StateInfo. Properties of
824 * this <code>StateInfo</code> will take precedence over those of the
825 * passed in <code>StateInfo</code>. For example, if this
826 * StateInfo specifics a non-null font, the returned StateInfo will
827 * have its font so to that regardless of the <code>StateInfo</code>'s
828 * font.
829 *
830 * @param info StateInfo to add our styles to
831 * @return Merged StateInfo.
832 */
833 public StateInfo addTo(StateInfo info) {
834 if (font != null) {
835 info.font = font;
836 }
837 if(data != null) {
838 if(info.data == null) {
839 info.data = new HashMap<>();
840 }
841 info.data.putAll(data);
842 }
843 if (colors != null) {
844 if (info.colors == null) {
845 info.colors = new Color[colors.length];
846 System.arraycopy(colors, 0, info.colors, 0,
847 colors.length);
848 }
849 else {
850 if (info.colors.length < colors.length) {
851 Color[] old = info.colors;
852
853 info.colors = new Color[colors.length];
854 System.arraycopy(old, 0, info.colors, 0, old.length);
855 }
856 for (int counter = colors.length - 1; counter >= 0;
857 counter--) {
858 if (colors[counter] != null) {
859 info.colors[counter] = colors[counter];
|