1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.xnap.gui.menu;
20
21 import java.awt.event.ActionEvent;
22 import java.beans.PropertyChangeEvent;
23 import java.beans.PropertyChangeListener;
24
25 import javax.swing.AbstractAction;
26 import javax.swing.*;
27 import javax.swing.ButtonGroup;
28 import javax.swing.JCheckBoxMenuItem;
29 import javax.swing.JMenu;
30
31 import org.xnap.XNap;
32 import org.xnap.gui.component.Completable;
33 import org.xnap.gui.component.CompletionModeFactory;
34 import org.xnap.gui.util.IconHelper;
35 import org.xnap.util.Preferences;
36 import org.xnap.util.PreferencesProvider;
37
38 public class CompletionModeMenu extends JMenu implements PropertyChangeListener
39 {
40 private Completable comp;
41 private String prefsKey = null;
42 private String currentMode;
43 private PreferencesProvider prefs;
44 private ButtonGroup completionGroup = new ButtonGroup();
45
46 public CompletionModeMenu(Completable comp, String prefsKey,
47 PreferencesProvider prefs)
48 {
49 super(XNap.tr("Text Completion"));
50 setIcon(IconHelper.getMenuIcon("completion.png"));
51 this.comp = comp;
52
53 if (prefsKey != null) {
54 this.prefsKey = prefsKey + "TextCompletion";
55 prefs.addPropertyChangeListener(this.prefsKey, this);
56 }
57
58 this.prefs = prefs;
59
60 currentMode = (prefsKey != null) ? prefs.get(prefsKey) : "Default";
61
62 String[] modes = CompletionModeFactory.DEFAULT_COMPLETION_MODES;
63
64 for (int i = 0; i < modes.length; i++) {
65 addMode(modes[i]);
66 }
67
68 addSeparator();
69 JCheckBoxMenuItem item =
70 new JCheckBoxMenuItem(new DefaultCompletionModeAction());
71 completionGroup.add(item);
72 add(item);
73
74 if (completionGroup.getSelection() == null) {
75 item.doClick();
76 }
77 }
78
79 public CompletionModeMenu(Completable comp, String prefsKey)
80 {
81 this(comp, prefsKey, Preferences.getInstance());
82 }
83
84 public CompletionModeMenu(Completable comp)
85 {
86 this(comp, null);
87 }
88
89 public void setPreferences(String key, PreferencesProvider prefs)
90 {
91 if (prefs != null && prefsKey != null) {
92 prefs.removePropertyChangeListener(prefsKey, this);
93 }
94
95 prefsKey = key + "TextCompletion";
96 this.prefs = prefs;
97
98 prefs.addPropertyChangeListener(prefsKey, this);
99 setModeFromPrefs(prefs.get(prefsKey));
100 }
101
102 private void setModeFromPrefs(String newMode)
103 {
104 if (newMode.length() == 0) {
105 newMode = "Default";
106 }
107
108 if (currentMode.equals(newMode)) {
109 return;
110 }
111 currentMode = newMode;
112 for (int i = 0; i < getItemCount(); i++) {
113 JMenuItem item = getItem(i);
114 if (item == null) {
115 continue;
116 }
117 Action a = item.getAction();
118 if (((String)a.getValue(Action.ACTION_COMMAND_KEY))
119 .equals(currentMode)) {
120 getItem(i).doClick();
121 return;
122 }
123 }
124 }
125
126 public void setPreferences(String prefsKey)
127 {
128 setPreferences(prefsKey, Preferences.getInstance());
129 }
130
131 public void propertyChange(PropertyChangeEvent e)
132 {
133 setModeFromPrefs(prefs.get(prefsKey));
134 }
135
136 private void addMode(String mode)
137 {
138 JCheckBoxMenuItem item = new JCheckBoxMenuItem
139 (new CompletionModeAction(mode));
140 completionGroup.add(item);
141 add(item);
142 if (mode.equals(currentMode)) {
143 item.doClick();
144 }
145 }
146
147 private void setMode(String mode)
148 {
149 currentMode = mode;
150 if (currentMode.equals("Default")) {
151 comp.setCompletionMode
152 (Preferences.getInstance().getDefaultCompletionMode());
153 }
154 else {
155 comp.setCompletionMode(currentMode);
156 }
157 if (prefs != null && prefsKey != null) {
158 prefs.set(prefsKey, currentMode);
159 }
160 }
161
162 private class CompletionModeAction extends AbstractAction
163 {
164 private String key;
165
166 public CompletionModeAction(String key)
167 {
168 this.key = key;
169 putValue(Action.NAME, XNap.tr(key));
170 putValue(Action.ACTION_COMMAND_KEY, key);
171 }
172
173 public void actionPerformed(ActionEvent e)
174 {
175 setMode(key);
176 }
177 }
178
179 private class DefaultCompletionModeAction extends AbstractAction
180 implements PropertyChangeListener
181 {
182 public DefaultCompletionModeAction()
183 {
184 putValue(Action.NAME, XNap.tr("Default"));
185 putValue(Action.ACTION_COMMAND_KEY, "Default");
186 Preferences.getInstance().addPropertyChangeListener
187 ("defaultCompletionMode", this);
188 }
189
190 public void actionPerformed(ActionEvent e)
191 {
192 setMode("Default");
193 }
194
195 public void propertyChange(PropertyChangeEvent evt)
196 {
197 if (currentMode.equals("Default")) {
198 comp.setCompletionMode(evt.getNewValue().toString());
199 }
200 }
201 }
202
203 }
204
205