View Javadoc
1   /*
2    * Copyright (c) 2003, 2004, 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
23   * questions.
24   */
25  package sun.swing.plaf.synth;
26  
27  import javax.swing.plaf.synth.*;
28  import java.util.*;
29  import java.util.regex.*;
30  
31  /**
32   * <b>WARNING:</b> This class is an implementation detail and is only
33   * public so that it can be used by two packages. You should NOT consider
34   * this public API.
35   * <p>
36   * StyleAssociation is used to lookup a style for a particular
37   * component (or region).
38   *
39   * @author Scott Violet
40   */
41  public class StyleAssociation {
42      /**
43       * The style
44       */
45      private SynthStyle _style;
46  
47      /**
48       * Pattern used for matching.
49       */
50      private Pattern _pattern;
51      /**
52       * Matcher used for testing if path matches that of pattern.
53       */
54      private Matcher _matcher;
55  
56      /**
57       * Identifier for this association.
58       */
59      private int _id;
60  
61  
62      /**
63       * Returns a StyleAssociation that can be used to determine if
64       * a particular string matches the returned association.
65       */
66      public static StyleAssociation createStyleAssociation(
67          String text, SynthStyle style)
68          throws PatternSyntaxException {
69          return createStyleAssociation(text, style, 0);
70      }
71  
72      /**
73       * Returns a StyleAssociation that can be used to determine if
74       * a particular string matches the returned association.
75       */
76      public static StyleAssociation createStyleAssociation(
77          String text, SynthStyle style, int id)
78          throws PatternSyntaxException {
79          return new StyleAssociation(text, style, id);
80      }
81  
82  
83      private StyleAssociation(String text, SynthStyle style, int id)
84                   throws PatternSyntaxException {
85          _style = style;
86          _pattern = Pattern.compile(text);
87          _id = id;
88      }
89  
90      /**
91       * Returns the developer specified identifier for this association, will
92       * be <code>0</code> if an identifier was not specified when this
93       * <code>StyleAssociation</code> was created.
94       */
95      public int getID() {
96          return _id;
97      }
98  
99      /**
100      * Returns true if this <code>StyleAssociation</code> matches the
101      * passed in CharSequence.
102      *
103      * @return true if this <code>StyleAssociation</code> matches the
104      * passed in CharSequence.if this StyleAssociation.
105      */
106     public synchronized boolean matches(CharSequence path) {
107         if (_matcher == null) {
108             _matcher = _pattern.matcher(path);
109         }
110         else {
111             _matcher.reset(path);
112         }
113         return _matcher.matches();
114     }
115 
116     /**
117      * Returns the text used in matching the string.
118      *
119      * @return the text used in matching the string.
120      */
121     public String getText() {
122         return _pattern.pattern();
123     }
124 
125     /**
126      * Returns the style this association is mapped to.
127      *
128      * @return the style this association is mapped to.
129      */
130     public SynthStyle getStyle() {
131         return _style;
132     }
133 }