Source Code Cross Referenced for DefaultSynthStyleFactory.java in  » 6.0-JDK-Core » swing » javax » swing » plaf » synth » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » swing » javax.swing.plaf.synth 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.