1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.Component ; 33 import java.awt.Container ; 34 import java.awt.FocusTraversalPolicy ; 35 36 import javax.swing.JButton ; 37 import javax.swing.JComboBox ; 38 import javax.swing.JComponent ; 39 import javax.swing.JTable ; 40 import javax.swing.JTextField ; 41 import javax.swing.SwingUtilities ; 42 import javax.swing.text.JTextComponent ; 43 44 import com.genimen.djeneric.util.DjLogger; 45 46 public class DjFocusMeLater implements Runnable 47 { 48 protected Component _toBeFocused; 49 50 public DjFocusMeLater(Component j) 51 { 52 Component startComp = j; 53 54 if (j instanceof JComponent ) 55 { 56 Component c = j; 57 JComponent jc = (JComponent ) j; 58 while (!jc.isEnabled() && c != null) 59 { 60 Container nearestRoot = j.getFocusCycleRootAncestor(); 61 FocusTraversalPolicy policy = nearestRoot.getFocusTraversalPolicy(); 62 63 c = policy.getComponentAfter(nearestRoot, j); 64 if (c == startComp) c = null; 65 if (c != null) 67 { 68 j = c; 69 jc = (JComponent ) c; 70 } 71 } 72 } 73 74 _toBeFocused = j; 75 76 if (j instanceof JTable ) 77 { 78 SwingUtilities.invokeLater(this); 79 return; 80 } 81 else if (j instanceof JTextComponent ) 82 { 83 SwingUtilities.invokeLater(this); 85 return; 86 } 87 else if (j instanceof JButton ) 88 { 89 SwingUtilities.invokeLater(this); 90 return; 91 } 92 else if (j instanceof JComboBox ) 93 { 94 JComboBox jco = (JComboBox ) j; 96 if (jco.isEditable()) 97 { 98 for (int i = 0; i < jco.getComponentCount(); i++) 100 { 101 if (jco.getComponent(i) instanceof JTextField ) 102 { 103 _toBeFocused = (JComponent ) jco.getComponent(i); 104 SwingUtilities.invokeLater(this); 105 return; 106 } 107 } 108 } 109 else 110 { 111 for (int i = 0; i < jco.getComponentCount(); i++) 113 { 114 if (jco.getComponent(i) instanceof JButton ) 115 { 116 _toBeFocused = (JComponent ) jco.getComponent(i); 117 SwingUtilities.invokeLater(this); 118 return; 119 } 120 } 121 } 122 } 123 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 getNeededComponent(JComponent j) 137 { 138 if (j instanceof JComboBox ) 139 { 140 JComboBox jco = (JComboBox ) j; 142 if (jco.isEditable()) 143 { 144 for (int i = 0; i < jco.getComponentCount(); i++) 146 { 147 if (jco.getComponent(i) instanceof JTextField ) 148 { 149 return (JComponent ) jco.getComponent(i); 150 } 151 } 152 } 153 else 154 { 155 for (int i = 0; i < jco.getComponentCount(); i++) 157 { 158 if (jco.getComponent(i) instanceof JButton ) 159 { 160 return (JComponent ) jco.getComponent(i); 161 } 162 } 163 } 164 } 165 return j; 166 } 167 168 } | Popular Tags |