KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjFocusMeLater


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Component JavaDoc;
33 import java.awt.Container JavaDoc;
34 import java.awt.FocusTraversalPolicy JavaDoc;
35
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComboBox JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JTable JavaDoc;
40 import javax.swing.JTextField JavaDoc;
41 import javax.swing.SwingUtilities JavaDoc;
42 import javax.swing.text.JTextComponent JavaDoc;
43
44 import com.genimen.djeneric.util.DjLogger;
45
46 public class DjFocusMeLater implements Runnable JavaDoc
47 {
48   protected Component JavaDoc _toBeFocused;
49
50   public DjFocusMeLater(Component JavaDoc j)
51   {
52     Component JavaDoc startComp = j;
53
54     if (j instanceof JComponent JavaDoc)
55     {
56       Component JavaDoc c = j;
57       JComponent JavaDoc jc = (JComponent JavaDoc) j;
58       while (!jc.isEnabled() && c != null)
59       {
60         Container JavaDoc nearestRoot = j.getFocusCycleRootAncestor();
61         FocusTraversalPolicy JavaDoc policy = nearestRoot.getFocusTraversalPolicy();
62
63         c = policy.getComponentAfter(nearestRoot, j);
64         if (c == startComp) c = null;
65         // Do not loop endlessly
66
if (c != null)
67         {
68           j = c;
69           jc = (JComponent JavaDoc) c;
70         }
71       }
72     }
73
74     _toBeFocused = j;
75
76     if (j instanceof JTable JavaDoc)
77     {
78       SwingUtilities.invokeLater(this);
79       return;
80     }
81     else if (j instanceof JTextComponent JavaDoc)
82     {
83       // we want the focus (And the caret) to be on this field
84
SwingUtilities.invokeLater(this);
85       return;
86     }
87     else if (j instanceof JButton JavaDoc)
88     {
89       SwingUtilities.invokeLater(this);
90       return;
91     }
92     else if (j instanceof JComboBox JavaDoc)
93     {
94       // if this combobox is editable...
95
JComboBox JavaDoc jco = (JComboBox JavaDoc) j;
96       if (jco.isEditable())
97       {
98         // the focus should be set on the JTextField component
99
for (int i = 0; i < jco.getComponentCount(); i++)
100         {
101           if (jco.getComponent(i) instanceof JTextField JavaDoc)
102           {
103             _toBeFocused = (JComponent JavaDoc) jco.getComponent(i);
104             SwingUtilities.invokeLater(this);
105             return;
106           }
107         }
108       }
109       else
110       {
111         // the focus should be set on the button...
112
for (int i = 0; i < jco.getComponentCount(); i++)
113         {
114           if (jco.getComponent(i) instanceof JButton JavaDoc)
115           {
116             _toBeFocused = (JComponent JavaDoc) jco.getComponent(i);
117             SwingUtilities.invokeLater(this);
118             return;
119           }
120         }
121       }
122     }
123     /*
124      all other cases 'fall thru' and are ignored
125      */

126     if (j != null) DjLogger.log("DjFocusMeLater: " + j.getClass().getName() + ", no focus set");
127     else DjLogger.log("DjFocusMeLater: component == null, no focus set");
128
129   }
130
131   public void run()
132   {
133     if (_toBeFocused != null && _toBeFocused.isVisible() && _toBeFocused.isEnabled()) _toBeFocused.requestFocus();
134   }
135
136   public static JComponent JavaDoc getNeededComponent(JComponent JavaDoc j)
137   {
138     if (j instanceof JComboBox JavaDoc)
139     {
140       // if this combobox is editable...
141
JComboBox JavaDoc jco = (JComboBox JavaDoc) j;
142       if (jco.isEditable())
143       {
144         // the focus should be set on the JTextField component
145
for (int i = 0; i < jco.getComponentCount(); i++)
146         {
147           if (jco.getComponent(i) instanceof JTextField JavaDoc)
148           {
149             return (JComponent JavaDoc) jco.getComponent(i);
150           }
151         }
152       }
153       else
154       {
155         // the focus should be set on the button...
156
for (int i = 0; i < jco.getComponentCount(); i++)
157         {
158           if (jco.getComponent(i) instanceof JButton JavaDoc)
159           {
160             return (JComponent JavaDoc) jco.getComponent(i);
161           }
162         }
163       }
164     }
165     return j;
166   }
167
168 }
Popular Tags