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.settings;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.xnap.commons.gui.completion.Completion;
26 import org.xnap.commons.gui.completion.CompletionMode;
27 import org.xnap.commons.gui.completion.CompletionModeFactory;
28 import org.xnap.commons.gui.completion.CompletionModeListener;
29
30
31 /***
32 * This class manages the serialization of the active completion mode for a
33 * {@link Completion} object.
34 *
35 * A CompletionModeSetting can be used as follows:
36 *
37 * <pre>
38 * JTextField jtf = new JTextField();
39 * Completion comp = new Completion(jtf);
40 * CompletionModeSetting cms =
41 * new CompletionModeSetting("prefsKey",
42 * ManualCompletionMode.class.getName(), comp);
43 * </pre>
44 *
45 *
46 *
47 * @author Felix Berger
48 */
49 public class CompletionModeSetting extends StringSetting
50 implements CompletionModeListener
51 {
52
53 private static Log logger = LogFactory.getLog(CompletionModeSetting.class);
54
55 private Completion comp;
56
57 /***
58 * Creates a CompletionModeSetting for a completion object.
59 * @param key the settings key for the properties file
60 * @param defaultMode the mode which is initially set for the completion
61 * object
62 * @param comp
63 */
64 public CompletionModeSetting(SettingResource backend, String key,
65 Class defaultMode, Completion comp)
66
67 {
68 super(backend, key, defaultMode.getName());
69 this.comp = comp;
70 setMode();
71 comp.addCompletionModeListener(this);
72 }
73
74 /***
75 * Reverts the completion mode setting to the default mode and sets it
76 * for the completion object.
77 */
78 @Override
79 public void revert()
80 {
81 super.revert();
82 setMode();
83 }
84
85 private void setMode()
86 {
87 try {
88 CompletionMode mode
89 = CompletionModeFactory.createCompletionMode(getValue());
90 comp.setMode(mode);
91 } catch (IllegalArgumentException iae) {
92 logger.debug(iae);
93 }
94 catch (Exception e) {
95 logger.debug(e);
96 }
97 }
98
99
100
101
102 public void modeChanged(Class oldMode, Class newMode)
103 {
104 logger.info("mode changed from " + oldMode.getName() + " to "
105 + newMode.getName());
106 setValue(newMode.getName());
107 }
108 }