1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.util;
21
22 import java.awt.Component;
23 import java.awt.Container;
24 import java.awt.event.ActionEvent;
25 import java.util.StringTokenizer;
26
27 import javax.swing.JTabbedPane;
28
29 import org.xnap.gui.XNapFrame;
30 import org.xnap.gui.component.ToggleableIconPane;
31 import org.xnap.util.Preferences;
32
33 /***
34 * Provides a helper class for the focus requests.
35 */
36 public class FocusManager {
37
38
39
40
41
42 private static Preferences prefs = Preferences.getInstance();
43
44
45
46 private FocusManager()
47 {
48 }
49
50
51
52 public static final void setFocusTo(String name, ActionEvent event)
53 {
54 if ((event.getModifiers() & ActionEvent.CTRL_MASK) > 0) {
55 if (!prefs.getFocusOnAllEvents()) {
56 setFocusTo(name);
57 }
58 }
59 else if (prefs.getFocusOnAllEvents()) {
60 setFocusTo(name);
61 }
62 }
63
64 public static final void setFocusTo(String name)
65 {
66 setFocusTo(XNapFrame.getInstance().getPane(), name);
67 }
68
69
70 private static void setFocusTo(Container container, String name)
71 {
72 StringTokenizer t = new StringTokenizer(name, ".");
73 if (t.hasMoreTokens()) {
74 String find = t.nextToken();
75 Component[] c;
76 if (container instanceof ToggleableIconPane) {
77 c = ((ToggleableIconPane)container).getTabs();
78 }
79 else {
80 c = container.getComponents();
81 }
82
83 if (c == null) {
84 return;
85 }
86
87 for (int i = 0; i < c.length; i++) {
88 if (find.equals(c[i].getName())) {
89 if (container instanceof ToggleableIconPane) {
90 ((ToggleableIconPane)container).setSelectedComponent(c[i]);
91 }
92 else if (container instanceof JTabbedPane) {
93 ((JTabbedPane)container).setSelectedComponent(c[i]);
94 }
95 else {
96 c[i].requestFocus();
97 }
98
99 if (t.hasMoreTokens() && c[i] instanceof Container) {
100 setFocusTo((Container)c[i], t.nextToken(""));
101 }
102
103 return;
104 }
105 }
106 }
107 }
108
109 }