KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > synth > DefaultSynthStyleFactory


1 /*
2  * @(#)DefaultSynthStyleFactory.java 1.7 04/09/15
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.plaf.synth;
8
9 import javax.swing.*;
10 import javax.swing.plaf.FontUIResource JavaDoc;
11 import java.awt.Font JavaDoc;
12 import java.util.*;
13 import java.util.regex.*;
14 import sun.swing.plaf.synth.*;
15 import sun.swing.BakedArrayList;
16
17 /**
18  * Factory used for obtaining styles. Supports associating a style based on
19  * the name of the component as returned by <code>Component.getName()</code>,
20  * and the <code>Region</code> associated with the <code>JComponent</code>.
21  * Lookup is done using regular expressions.
22  *
23  * @version 1.7, 09/15/04
24  * @author Scott Violet
25  */

26 class DefaultSynthStyleFactory extends SynthStyleFactory JavaDoc {
27     /**
28      * Used to indicate the lookup should be done based on Component name.
29      */

30     public static final int NAME = 0;
31     /**
32      * Used to indicate the lookup should be done based on region.
33      */

34     public static final int REGION = 1;
35
36     /**
37      * List containing set of StyleAssociations used in determining matching
38      * styles.
39      */

40     private List<StyleAssociation> _styles;
41     /**
42      * Used during lookup.
43      */

44     private BakedArrayList _tmpList;
45
46     /**
47      * Maps from a List (BakedArrayList to be precise) to the merged style.
48      */

49     private Map _resolvedStyles;
50
51     /**
52      * Used if there are no styles matching a widget.
53      */

54     private SynthStyle JavaDoc _defaultStyle;
55
56
57     DefaultSynthStyleFactory() {
58         _tmpList = new BakedArrayList(5);
59         _styles = new ArrayList<StyleAssociation>();
60         _resolvedStyles = new HashMap();
61     }
62
63     public synchronized void addStyle(DefaultSynthStyle style,
64                          String JavaDoc path, int type) throws PatternSyntaxException {
65         if (path == null) {
66             // Make an empty path match all.
67
path = ".*";
68         }
69         if (type == NAME) {
70             _styles.add(StyleAssociation.createStyleAssociation(
71                             path, style, type));
72         }
73         else if (type == REGION) {
74             _styles.add(StyleAssociation.createStyleAssociation(
75                             path.toLowerCase(), style, type));
76         }
77     }
78
79     /**
80      * Returns the style for the specified Component.
81      *
82      * @param c Component asking for
83      * @param id ID of the Component
84      */

85     public synchronized SynthStyle JavaDoc getStyle(JComponent c, Region JavaDoc id) {
86         BakedArrayList matches = _tmpList;
87
88         matches.clear();
89         getMatchingStyles(matches, c, id);
90
91         if (matches.size() == 0) {
92             return getDefaultStyle();
93         }
94         // Use a cached Style if possible, otherwise create a new one.
95
matches.cacheHashCode();
96         SynthStyle JavaDoc style = getCachedStyle(matches);
97
98         if (style == null) {
99             style = mergeStyles(matches);
100
101             if (style != null) {
102                 cacheStyle(matches, style);
103             }
104         }
105         return style;
106     }
107
108     /**
109      * Returns the style to use if there are no matching styles.
110      */

111     private SynthStyle JavaDoc getDefaultStyle() {
112         if (_defaultStyle == null) {
113             _defaultStyle = new DefaultSynthStyle();
114             ((DefaultSynthStyle)_defaultStyle).setFont(
115                 new FontUIResource JavaDoc("Dialog", Font.PLAIN,12));
116         }
117         return _defaultStyle;
118     }
119
120     /**
121      * Fetches any styles that match the passed into arguments into
122      * <code>matches</code>.
123      */

124     private void getMatchingStyles(java.util.List JavaDoc matches, JComponent c,
125                                    Region JavaDoc id) {
126         String JavaDoc idName = id.getLowerCaseName();
127         String JavaDoc cName = c.getName();
128
129         if (cName == null) {
130             cName = "";
131         }
132         for (int counter = _styles.size() - 1; counter >= 0; counter--){
133             StyleAssociation sa = _styles.get(counter);
134             String JavaDoc path;
135
136             if (sa.getID() == NAME) {
137                 path = cName;
138             }
139             else {
140                 path = idName;
141             }
142
143             if (sa.matches(path) && matches.indexOf(sa.getStyle()) == -1) {
144                 matches.add(sa.getStyle());
145             }
146         }
147     }
148
149     /**
150      * Caches the specified style.
151      */

152     private void cacheStyle(java.util.List JavaDoc styles, SynthStyle JavaDoc style) {
153         BakedArrayList cachedStyles = new BakedArrayList(styles);
154
155         _resolvedStyles.put(cachedStyles, style);
156     }
157
158     /**
159      * Returns the cached style from the passed in arguments.
160      */

161     private SynthStyle JavaDoc getCachedStyle(java.util.List JavaDoc styles) {
162         if (styles.size() == 0) {
163             return null;
164         }
165         return (SynthStyle JavaDoc)_resolvedStyles.get(styles);
166     }
167
168     /**
169      * Creates a single Style from the passed in styles. The passed in List
170      * is reverse sorted, that is the most recently added style found to
171      * match will be first.
172      */

173     private SynthStyle JavaDoc mergeStyles(java.util.List JavaDoc styles) {
174         int size = styles.size();
175
176         if (size == 0) {
177             return null;
178         }
179         else if (size == 1) {
180             return (SynthStyle JavaDoc)((DefaultSynthStyle)styles.get(0)).clone();
181         }
182         // NOTE: merging is done backwards as DefaultSynthStyleFactory reverses
183
// order, that is, the most specific style is first.
184
DefaultSynthStyle style = (DefaultSynthStyle)styles.get(size - 1);
185
186         style = (DefaultSynthStyle)style.clone();
187         for (int counter = size - 2; counter >= 0; counter--) {
188             style = ((DefaultSynthStyle)styles.get(counter)).addTo(style);
189         }
190         return style;
191     }
192 }
193
Popular Tags