1 | /*
|
---|
2 | * 12/14/08
|
---|
3 | *
|
---|
4 | * AbstractTokenMakerFactory.java - Base class for TokenMaker implementations.
|
---|
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 |
|
---|
15 | /**
|
---|
16 | * Base class for {@link TokenMakerFactory} implementations. A
|
---|
17 | * <code>java.util.Map</code> maps keys to the names of {@link TokenMaker}
|
---|
18 | * classes.
|
---|
19 | *
|
---|
20 | * @author Robert Futrell
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | public abstract class AbstractTokenMakerFactory extends TokenMakerFactory {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * A mapping from keys to the names of {@link TokenMaker} implementation
|
---|
27 | * class names. When {@link #getTokenMaker(String)} is called with a key
|
---|
28 | * defined in this map, a <code>TokenMaker</code> of the corresponding type
|
---|
29 | * is returned.
|
---|
30 | */
|
---|
31 | private Map tokenMakerMap;
|
---|
32 |
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructor.
|
---|
36 | */
|
---|
37 | protected AbstractTokenMakerFactory() {
|
---|
38 | tokenMakerMap = createTokenMakerKeyToClassNameMap();
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates and returns a mapping from keys to the names of
|
---|
45 | * {@link TokenMaker} implementation classes. When
|
---|
46 | * {@link #getTokenMaker(String)} is called with a key defined in this
|
---|
47 | * map, a <code>TokenMaker</code> of the corresponding type is returned.
|
---|
48 | *
|
---|
49 | * @return The map.
|
---|
50 | */
|
---|
51 | protected abstract Map createTokenMakerKeyToClassNameMap();
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Returns a {@link TokenMaker} for the specified key.
|
---|
56 | *
|
---|
57 | * @param key The key.
|
---|
58 | * @return The corresponding <code>TokenMaker</code>, or <code>null</code>
|
---|
59 | * if none matches the specified key.
|
---|
60 | */
|
---|
61 | protected TokenMaker getTokenMakerImpl(String key) {
|
---|
62 | String clazz = (String)tokenMakerMap.get(key);
|
---|
63 | if (clazz!=null) {
|
---|
64 | try {
|
---|
65 | return (TokenMaker)Class.forName(clazz).newInstance();
|
---|
66 | } catch (RuntimeException re) { // FindBugs
|
---|
67 | throw re;
|
---|
68 | } catch (Exception e) {
|
---|
69 | e.printStackTrace();
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return null;
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * {@inheritDoc}
|
---|
78 | */
|
---|
79 | public Set keySet() {
|
---|
80 | return tokenMakerMap.keySet();
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Adds a mapping from a key to a <code>TokenMaker</code> implementation
|
---|
86 | * class name.
|
---|
87 | *
|
---|
88 | * @param key The key.
|
---|
89 | * @param className The <code>TokenMaker</code> class name.
|
---|
90 | * @return The previous value for the specified key, or <code>null</code>
|
---|
91 | * if there was none.
|
---|
92 | */
|
---|
93 | public String putMapping(String key, String className) {
|
---|
94 | return (String)tokenMakerMap.put(key, className);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | } |
---|