View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
39  
40      //--- Data field(s) ---
41  
42      private static Preferences prefs = Preferences.getInstance();
43  
44      //--- Constructor(s) ---
45  
46      private FocusManager()
47      {
48      }
49  
50      //--- Method(s) ---
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 }