1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.xnap.commons.gui.completion;
22
23 import java.lang.ref.WeakReference;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27
28 /***
29 * This mode acts as a proxy for another mode that can be set as global
30 * default.
31 *
32 * @author Steffen Pingel
33 */
34 public class GlobalDefaultCompletionMode implements CompletionMode
35 {
36
37 private static String defaultMode = DropDownListCompletionMode.class.getName();
38 private static List<WeakReference<GlobalDefaultCompletionMode>> instances
39 = new ArrayList<WeakReference<GlobalDefaultCompletionMode>>();
40
41 private CompletionMode mode;
42 private Completion completion;
43
44 public GlobalDefaultCompletionMode()
45 {
46 instances.add(new WeakReference<GlobalDefaultCompletionMode>(this));
47 }
48
49 public void enable(Completion completion)
50 {
51 try {
52 mode = CompletionModeFactory.createCompletionMode(defaultMode);
53 }
54 catch (IllegalArgumentException iae) {
55 throw iae;
56 }
57 catch (Exception e) {
58
59 e.printStackTrace();
60 return;
61 }
62
63 mode.enable(completion);
64 this.completion = completion;
65 }
66
67 public void disable()
68 {
69 mode.disable();
70 mode = null;
71 completion = null;
72 }
73
74 public void updateMode()
75 {
76 Completion savedCompletion = completion;
77 if (savedCompletion != null) {
78 disable();
79 enable(savedCompletion);
80 }
81 }
82
83 /***
84 * Returns the class name of the currently used completion mode.
85 */
86 public static String getCompletionMode()
87 {
88 return defaultMode;
89 }
90
91 /***
92 * Calls to this method need to be Swing thread synchronized.
93 * @param className must not be <code>null</code>, must not be
94 * the class name of DefaultCompletionMode itself.
95 * @throws IllegalArgumentException if <code>className</code> violates
96 * one of the requirements stated above
97 */
98 public static void setCompletionMode(String className)
99 {
100 if (className == null) {
101 throw new IllegalArgumentException("class name must not be null");
102 }
103 else if (className.equals(GlobalDefaultCompletionMode.class.getName())) {
104 throw new IllegalArgumentException("delegate mode must not be default completion mode itself");
105 }
106
107 defaultMode = className;
108
109 for (Iterator<WeakReference<GlobalDefaultCompletionMode>> it = instances.iterator(); it.hasNext();) {
110 WeakReference<GlobalDefaultCompletionMode> instance = it.next();
111 GlobalDefaultCompletionMode mode = instance.get();
112 if (mode != null) {
113 mode.updateMode();
114 }
115 else {
116 it.remove();
117 }
118 }
119 }
120
121 }