1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.gui.completion;
21
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.InvocationTargetException;
24 import org.xnap.commons.i18n.I18n;
25 import org.xnap.commons.i18n.I18nFactory;
26
27 /***
28 * Factory class that manages all installed completion modes and creates
29 * them.
30 *
31 * @author Felix Berger
32 */
33 public class CompletionModeFactory
34 {
35 static final I18n I18N = I18nFactory.getI18n(CompletionModeFactory.class);
36
37 private static CompletionModeInfo[] infos;
38 static {
39 infos = new CompletionModeInfo[] {
40 new CompletionModeInfo(I18N.tr("No Completion"),
41 NoCompletionMode.class.getName()),
42 new CompletionModeInfo(I18N.tr("Automatic"),
43 AutomaticCompletionMode.class.getName()),
44 new CompletionModeInfo(I18N.tr("Automatic Dropdown"),
45 AutomaticDropDownCompletionMode.class.getName()),
46 new CompletionModeInfo(I18N.tr("Dropdown List"),
47 DropDownListCompletionMode.class.getName()),
48 new CompletionModeInfo(I18N.tr("Emacs Completion"),
49 EmacsCompletionMode.class.getName()),
50 new CompletionModeInfo(I18N.tr("Manual"),
51 ManualCompletionMode.class.getName()),
52 new CompletionModeInfo(I18N.tr("Short Automatic"),
53 ShortAutomaticCompletionMode.class.getName()),
54 };
55 }
56
57 /***
58 * Returns all installed completion mode info objects.
59 * @return an array that contains the objects; if no completion modes have
60 * been installed an empty array is returned, never returns null
61 */
62 public static CompletionModeInfo[] getInstalledCompletionModes()
63 {
64 CompletionModeInfo[] result = new CompletionModeInfo[infos.length];
65 System.arraycopy(infos, 0, result, 0, result.length);
66 return result;
67 }
68
69 /***
70 * Returns the completion mode info object for the <code>className</code>.
71 *
72 * @return <code>null</code> if no object for that class name was found
73 */
74 public static CompletionModeInfo getCompletionModeInfoByClassName(String className)
75 {
76 for (CompletionModeInfo info : infos) {
77 if (info.getClassName().equals(className)) {
78 return info;
79 }
80 }
81 return null;
82 }
83
84 /***
85 * Installs a completion mode info object.
86 * <p>
87 * It is then returned in {@link #getInstalledCompletionModes()}.
88 *
89 * Completion modes can not be uninstalled, since they may already be
90 * actively used in {@link Completion} objects and provided as possible options
91 * in {@link CompletionModeMenu CompletionModeMenus}.
92 * @param info
93 */
94 public static void installCompletionMode(CompletionModeInfo info)
95 {
96 CompletionModeInfo[] newInfos
97 = new CompletionModeInfo[infos.length + 1];
98 System.arraycopy(infos, 0, newInfos, 0, infos.length);
99 newInfos[infos.length] = info;
100 infos = newInfos;
101 }
102
103 /***
104 * Creates a completion mode using the class name information from info.
105 * @param info
106 * @throws ClassNotFoundException
107 * @throws NoSuchMethodException
108 * @throws NoSuchMethodException
109 * @throws InvocationTargetException
110 * @throws InstantiationException
111 * @throws IllegalAccessException
112 * @throws InvocationTargetException
113 */
114 public static CompletionMode createCompletionMode(CompletionModeInfo info)
115 throws ClassNotFoundException, NoSuchMethodException,
116 NoSuchMethodException, InvocationTargetException,
117 InstantiationException, IllegalAccessException,
118 InvocationTargetException
119 {
120 return createCompletionMode(info.getClassName());
121 }
122
123 /***
124 * Creates a completion mode object with the given class name.
125 * @param className
126 * @throws IllegalArgumentException if the created object is not an
127 * instance of type {@link CompletionMode}.
128 * @throws ClassNotFoundException
129 * @throws NoSuchMethodException
130 * @throws NoSuchMethodException
131 * @throws InvocationTargetException
132 * @throws InstantiationException
133 * @throws IllegalAccessException
134 * @throws InvocationTargetException
135 */
136 public static CompletionMode createCompletionMode(String className)
137 throws ClassNotFoundException, NoSuchMethodException,
138 NoSuchMethodException, InvocationTargetException,
139 InstantiationException, IllegalAccessException,
140 InvocationTargetException
141 {
142 Class clazz = Class.forName(className);
143 Constructor constructor = clazz.getConstructor(new Class[0]);
144 Object obj;
145 obj = constructor.newInstance(new Object[0]);
146 if (obj instanceof CompletionMode) {
147 return (CompletionMode)obj;
148 }
149 else {
150 throw new IllegalArgumentException("Set completion mode is not a CompetionMode");
151 }
152 }
153
154 /***
155 * Info class that associates a {@link CompletionMode} with a name that
156 * can be shown to the user.
157 *
158 * @author Felix Berger
159 */
160 public static class CompletionModeInfo
161 {
162 private String name;
163 private String className;
164
165 /***
166 * Constructs a new completion mode info object which can then be
167 * installed using
168 * {@link CompletionModeFactory#installCompletionMode(CompletionModeInfo)}.
169 * @param name a possibly localized descriptive name of the completion
170 * mode, like {@link i18n#tr(String) i18n.tr("Drop down Completion")}.
171 * @param className the class name of the completion mode
172 * @throws NullPointerException if one of the arguments is <code>null</code>
173 */
174 public CompletionModeInfo(String name, String className)
175 {
176 if (name == null) {
177 throw new NullPointerException("name must not be null");
178 }
179 if (className == null) {
180 throw new NullPointerException("className must not be null");
181 }
182 this.name = name;
183 this.className = className;
184 }
185
186 /***
187 * Returns the descriptive name of the completion mode.
188 */
189 public String getName()
190 {
191 return name;
192 }
193
194 /***
195 * Returns the class name of the completion mode.
196 */
197 public String getClassName()
198 {
199 return className;
200 }
201
202 /***
203 * Returns the descriptive name of the completion mode, thus
204 * CompletionModeInfo object can be directly used in comobobox or list
205 * models without the need of providing a CellRenderer.
206 */
207 public String toString()
208 {
209 return name;
210 }
211 }
212 }