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 static org.xnap.commons.gui.completion.CompletionModeFactory.I18N;
23 import java.awt.event.ActionEvent;
24 import java.util.Enumeration;
25 import javax.swing.AbstractAction;
26 import javax.swing.AbstractButton;
27 import javax.swing.Action;
28 import javax.swing.ButtonGroup;
29 import javax.swing.JCheckBoxMenuItem;
30 import javax.swing.JMenu;
31 import org.xnap.commons.gui.completion.CompletionModeFactory.CompletionModeInfo;
32 import org.xnap.commons.gui.util.IconHelper;
33
34 /***
35 * Provides a graphical interface to the user for
36 * selecting his/her favorite completion mode.
37 * <p>
38 * It offers all the completion modes installed in the {@link CompletionModeFactory}
39 * as options to the user.
40 *
41 * It can be used as follows:
42 *
43 * <pre>
44 * JTextField jtf = new JTextField();
45 * Completion comp = new Completion(jtf);
46 * jtf.addMouseListener(new PopupListener(new CompletionModeMenu(comp)));
47 * </pre>
48 *
49 * See also {@link Completion} and {@link org.xnap.commons.gui.util.PopupListener}.
50 *
51 * @author Felix Berger
52 */
53 public class CompletionModeMenu extends JMenu
54 {
55
56 private Completion completion;
57 private ButtonGroup completionGroup = new ButtonGroup();
58
59 public CompletionModeMenu(Completion comp)
60 {
61 super(I18N.tr("Text Completion"));
62 setIcon(IconHelper.getMenuIcon("completion.png"));
63 completion = comp;
64
65 CompletionModeInfo[] infos = CompletionModeFactory.getInstalledCompletionModes();
66 for (CompletionModeInfo info : infos) {
67 addMode(info);
68 }
69
70 addSeparator();
71 addMode(new CompletionModeInfo
72 (I18N.tr("Application Default"),
73 GlobalDefaultCompletionMode.class.getName()));
74
75 completion.addCompletionModeListener(new CompletionModeChangeHandler());
76 }
77
78 private void addMode(CompletionModeInfo info)
79 {
80 JCheckBoxMenuItem item = new JCheckBoxMenuItem(
81 new CompletionModeAction(info));
82 completionGroup.add(item);
83 add(item);
84 if (info.getClassName().equals(completion.getMode().getClass().getName())) {
85 item.setSelected(true);
86 }
87 }
88
89 private void setMode(CompletionModeInfo info)
90 {
91 try {
92 CompletionMode mode
93 = CompletionModeFactory.createCompletionMode(info);
94 completion.setMode(mode);
95 }
96 catch (IllegalArgumentException iae) {
97 throw iae;
98 }
99 catch (Exception e) {
100
101 }
102 }
103
104 private class CompletionModeAction extends AbstractAction
105 {
106 CompletionModeInfo info;
107
108 public CompletionModeAction(CompletionModeInfo info)
109 {
110 this.info = info;
111
112 putValue(Action.NAME, info.getName());
113 putValue(Action.ACTION_COMMAND_KEY, info.getClassName());
114 }
115
116 public void actionPerformed(ActionEvent e)
117 {
118 setMode(info);
119 }
120 }
121
122 private class CompletionModeChangeHandler implements CompletionModeListener {
123
124 public void modeChanged(Class oldMode, Class newMode)
125 {
126 for (Enumeration e = completionGroup.getElements();
127 e.hasMoreElements();) {
128 AbstractButton b = (AbstractButton)e.nextElement();
129 Action a = b.getAction();
130 if (a != null && a.getValue(Action.ACTION_COMMAND_KEY).equals
131 (newMode.getName())) {
132 b.setSelected(true);
133 return;
134 }
135 }
136 }
137 }
138 }