1 | /*
|
---|
2 | * 12/12/2008
|
---|
3 | *
|
---|
4 | * TokenMakerFactory.java - A factory for TokenMakers.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea;
|
---|
10 |
|
---|
11 | import java.util.Map;
|
---|
12 | import java.util.Set;
|
---|
13 |
|
---|
14 | import org.fife.ui.rsyntaxtextarea.modes.PlainTextTokenMaker;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * A factory that maps syntax styles to {@link TokenMaker}s capable of splitting
|
---|
19 | * text into tokens for those syntax styles.
|
---|
20 | *
|
---|
21 | * @author Robert Futrell
|
---|
22 | * @version 1.0
|
---|
23 | */
|
---|
24 | public abstract class TokenMakerFactory {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * If this system property is set, a custom <code>TokenMakerFactory</code>
|
---|
28 | * of the specified class will be used as the default token maker factory.
|
---|
29 | */
|
---|
30 | public static final String PROPERTY_DEFAULT_TOKEN_MAKER_FACTORY =
|
---|
31 | "TokenMakerFactory";
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * The singleton default <code>TokenMakerFactory</code> instance.
|
---|
35 | */
|
---|
36 | private static TokenMakerFactory DEFAULT_INSTANCE;
|
---|
37 |
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates and returns a mapping from keys to the names of
|
---|
41 | * {@link TokenMaker} implementation classes. When
|
---|
42 | * {@link #getTokenMaker(String)} is called with a key defined in this
|
---|
43 | * map, a <code>TokenMaker</code> of the corresponding type is returned.
|
---|
44 | *
|
---|
45 | * @return The map.
|
---|
46 | */
|
---|
47 | protected abstract Map createTokenMakerKeyToClassNameMap();
|
---|
48 |
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Returns the default <code>TokenMakerFactory</code> instance. This is
|
---|
52 | * the factory used by all {@link RSyntaxDocument}s by default.
|
---|
53 | *
|
---|
54 | * @return The factory.
|
---|
55 | * @see #setDefaultInstance(TokenMakerFactory)
|
---|
56 | */
|
---|
57 | public static synchronized TokenMakerFactory getDefaultInstance() {
|
---|
58 | if (DEFAULT_INSTANCE==null) {
|
---|
59 | String clazz = null;
|
---|
60 | try {
|
---|
61 | clazz= System.getProperty(PROPERTY_DEFAULT_TOKEN_MAKER_FACTORY);
|
---|
62 | } catch (java.security.AccessControlException ace) {
|
---|
63 | clazz = null; // We're in an applet; take default.
|
---|
64 | }
|
---|
65 | if (clazz==null) {
|
---|
66 | clazz = "org.fife.ui.rsyntaxtextarea.DefaultTokenMakerFactory";
|
---|
67 | }
|
---|
68 | try {
|
---|
69 | DEFAULT_INSTANCE = (TokenMakerFactory)Class.forName(clazz).
|
---|
70 | newInstance();
|
---|
71 | } catch (RuntimeException re) { // FindBugs
|
---|
72 | throw re;
|
---|
73 | } catch (Exception e) {
|
---|
74 | e.printStackTrace();
|
---|
75 | throw new InternalError("Cannot find TokenMakerFactory: " +
|
---|
76 | clazz);
|
---|
77 | }
|
---|
78 | }
|
---|
79 | return DEFAULT_INSTANCE;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns a {@link TokenMaker} for the specified key.
|
---|
85 | *
|
---|
86 | * @param key The key.
|
---|
87 | * @return The corresponding <code>TokenMaker</code>, or
|
---|
88 | * {@link PlainTextTokenMaker} if none matches the specified key.
|
---|
89 | */
|
---|
90 | public final TokenMaker getTokenMaker(String key) {
|
---|
91 | TokenMaker tm = getTokenMakerImpl(key);
|
---|
92 | if (tm==null) {
|
---|
93 | tm = new PlainTextTokenMaker();
|
---|
94 | }
|
---|
95 | return tm;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Returns a {@link TokenMaker} for the specified key.
|
---|
101 | *
|
---|
102 | * @param key The key.
|
---|
103 | * @return The corresponding <code>TokenMaker</code>, or <code>null</code>
|
---|
104 | * if none matches the specified key.
|
---|
105 | */
|
---|
106 | protected abstract TokenMaker getTokenMakerImpl(String key);
|
---|
107 |
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Returns the set of keys that this factory maps to token makers.
|
---|
111 | *
|
---|
112 | * return The set of keys.
|
---|
113 | */
|
---|
114 | public abstract Set keySet();
|
---|
115 |
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Sets the default <code>TokenMakerFactory</code> instance. This is
|
---|
119 | * the factory used by all future {@link RSyntaxDocument}s by default.
|
---|
120 | * <code>RSyntaxDocument</code>s that have already been created are not
|
---|
121 | * affected.
|
---|
122 | *
|
---|
123 | * @param tmf The factory.
|
---|
124 | * @throws IllegalArgumentException If <code>tmf</code> is
|
---|
125 | * <code>null</code>.
|
---|
126 | * @see #getDefaultInstance()
|
---|
127 | */
|
---|
128 | public static synchronized void setDefaultInstance(TokenMakerFactory tmf) {
|
---|
129 | if (tmf==null) {
|
---|
130 | throw new IllegalArgumentException("tmf cannot be null");
|
---|
131 | }
|
---|
132 | DEFAULT_INSTANCE = tmf;
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | } |
---|