001 /*
002 * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.plaf.synth;
026
027 import javax.swing.*;
028 import javax.swing.plaf.FontUIResource;
029 import java.awt.Font;
030 import java.util.*;
031 import java.util.regex.*;
032 import sun.swing.plaf.synth.*;
033 import sun.swing.BakedArrayList;
034
035 /**
036 * Factory used for obtaining styles. Supports associating a style based on
037 * the name of the component as returned by <code>Component.getName()</code>,
038 * and the <code>Region</code> associated with the <code>JComponent</code>.
039 * Lookup is done using regular expressions.
040 *
041 * @version 1.15, 05/05/07
042 * @author Scott Violet
043 */
044 class DefaultSynthStyleFactory extends SynthStyleFactory {
045 /**
046 * Used to indicate the lookup should be done based on Component name.
047 */
048 public static final int NAME = 0;
049 /**
050 * Used to indicate the lookup should be done based on region.
051 */
052 public static final int REGION = 1;
053
054 /**
055 * List containing set of StyleAssociations used in determining matching
056 * styles.
057 */
058 private List<StyleAssociation> _styles;
059 /**
060 * Used during lookup.
061 */
062 private BakedArrayList _tmpList;
063
064 /**
065 * Maps from a List (BakedArrayList to be precise) to the merged style.
066 */
067 private Map _resolvedStyles;
068
069 /**
070 * Used if there are no styles matching a widget.
071 */
072 private SynthStyle _defaultStyle;
073
074 DefaultSynthStyleFactory() {
075 _tmpList = new BakedArrayList(5);
076 _styles = new ArrayList<StyleAssociation>();
077 _resolvedStyles = new HashMap();
078 }
079
080 public synchronized void addStyle(DefaultSynthStyle style,
081 String path, int type) throws PatternSyntaxException {
082 if (path == null) {
083 // Make an empty path match all.
084 path = ".*";
085 }
086 if (type == NAME) {
087 _styles.add(StyleAssociation.createStyleAssociation(path,
088 style, type));
089 } else if (type == REGION) {
090 _styles.add(StyleAssociation.createStyleAssociation(path
091 .toLowerCase(), style, type));
092 }
093 }
094
095 /**
096 * Returns the style for the specified Component.
097 *
098 * @param c Component asking for
099 * @param id ID of the Component
100 */
101 public synchronized SynthStyle getStyle(JComponent c, Region id) {
102 BakedArrayList matches = _tmpList;
103
104 matches.clear();
105 getMatchingStyles(matches, c, id);
106
107 if (matches.size() == 0) {
108 return getDefaultStyle();
109 }
110 // Use a cached Style if possible, otherwise create a new one.
111 matches.cacheHashCode();
112 SynthStyle style = getCachedStyle(matches);
113
114 if (style == null) {
115 style = mergeStyles(matches);
116
117 if (style != null) {
118 cacheStyle(matches, style);
119 }
120 }
121 return style;
122 }
123
124 /**
125 * Returns the style to use if there are no matching styles.
126 */
127 private SynthStyle getDefaultStyle() {
128 if (_defaultStyle == null) {
129 _defaultStyle = new DefaultSynthStyle();
130 ((DefaultSynthStyle) _defaultStyle)
131 .setFont(new FontUIResource(Font.DIALOG,
132 Font.PLAIN, 12));
133 }
134 return _defaultStyle;
135 }
136
137 /**
138 * Fetches any styles that match the passed into arguments into
139 * <code>matches</code>.
140 */
141 private void getMatchingStyles(java.util.List matches,
142 JComponent c, Region id) {
143 String idName = id.getLowerCaseName();
144 String cName = c.getName();
145
146 if (cName == null) {
147 cName = "";
148 }
149 for (int counter = _styles.size() - 1; counter >= 0; counter--) {
150 StyleAssociation sa = _styles.get(counter);
151 String path;
152
153 if (sa.getID() == NAME) {
154 path = cName;
155 } else {
156 path = idName;
157 }
158
159 if (sa.matches(path)
160 && matches.indexOf(sa.getStyle()) == -1) {
161 matches.add(sa.getStyle());
162 }
163 }
164 }
165
166 /**
167 * Caches the specified style.
168 */
169 private void cacheStyle(java.util.List styles, SynthStyle style) {
170 BakedArrayList cachedStyles = new BakedArrayList(styles);
171
172 _resolvedStyles.put(cachedStyles, style);
173 }
174
175 /**
176 * Returns the cached style from the passed in arguments.
177 */
178 private SynthStyle getCachedStyle(java.util.List styles) {
179 if (styles.size() == 0) {
180 return null;
181 }
182 return (SynthStyle) _resolvedStyles.get(styles);
183 }
184
185 /**
186 * Creates a single Style from the passed in styles. The passed in List
187 * is reverse sorted, that is the most recently added style found to
188 * match will be first.
189 */
190 private SynthStyle mergeStyles(java.util.List styles) {
191 int size = styles.size();
192
193 if (size == 0) {
194 return null;
195 } else if (size == 1) {
196 return (SynthStyle) ((DefaultSynthStyle) styles.get(0))
197 .clone();
198 }
199 // NOTE: merging is done backwards as DefaultSynthStyleFactory reverses
200 // order, that is, the most specific style is first.
201 DefaultSynthStyle style = (DefaultSynthStyle) styles
202 .get(size - 1);
203
204 style = (DefaultSynthStyle) style.clone();
205 for (int counter = size - 2; counter >= 0; counter--) {
206 style = ((DefaultSynthStyle) styles.get(counter))
207 .addTo(style);
208 }
209 return style;
210 }
211 }
|