|
31 | 31 | import static java.awt.RenderingHints.KEY_TEXT_ANTIALIASING; |
32 | 32 | import java.awt.Color; |
33 | 33 | import java.awt.Component; |
| 34 | +import java.awt.Container; |
34 | 35 | import java.awt.Dimension; |
| 36 | +import java.awt.FocusTraversalPolicy; |
35 | 37 | import java.awt.Font; |
36 | 38 | import java.awt.FontMetrics; |
37 | 39 | import java.awt.Graphics; |
|
62 | 64 | import javax.swing.ListSelectionModel; |
63 | 65 | import javax.swing.SwingUtilities; |
64 | 66 | import javax.swing.UIManager; |
| 67 | +import javax.swing.event.TreeModelEvent; |
65 | 68 | import javax.swing.table.TableCellRenderer; |
| 69 | +import javax.swing.text.DefaultCaret; |
66 | 70 | import javax.swing.text.DefaultHighlighter; |
67 | 71 | import javax.swing.text.Highlighter; |
68 | 72 | import javax.swing.text.JTextComponent; |
|
83 | 87 | //import java.awt.event.KeyEvent; |
84 | 88 | //import javax.swing.UIDefaults; |
85 | 89 | //import javax.swing.text.DefaultCaret; |
| 90 | +import javax.swing.tree.TreeModel; |
| 91 | +import javax.swing.tree.TreePath; |
86 | 92 |
|
87 | 93 | /** |
88 | 94 | * A collection of utility methods for Swing. |
@@ -122,11 +128,11 @@ public class SwingUtilities2 { |
122 | 128 | */ |
123 | 129 | public static final Object AA_TEXT_PROPERTY_KEY = new Object(); // AATextInfoPropertyKey |
124 | 130 |
|
125 | | -// /** |
126 | | -// * Used to tell a text component, being used as an editor for table |
127 | | -// * or tree, how many clicks it took to start editing. |
128 | | -// */ |
129 | | -// private static final Object SKIP_CLICK_COUNT = new Object(); // skipClickCount |
| 131 | + /** |
| 132 | + * Used to tell a text component, being used as an editor for table |
| 133 | + * or tree, how many clicks it took to start editing. |
| 134 | + */ |
| 135 | + private static final Object SKIP_CLICK_COUNT = new Object(); // skipClickCount |
130 | 136 |
|
131 | 137 | /* Presently this class assumes default fractional metrics. |
132 | 138 | * This may need to change to emulate future platform L&Fs. |
@@ -1611,37 +1617,37 @@ public static int getUIDefaultsInt(Object key, Locale l, int defaultValue) { |
1611 | 1617 | } |
1612 | 1618 |
|
1613 | 1619 | // SwingJS X: Key Focus |
1614 | | -// // At this point we need this method here. But we assume that there |
1615 | | -// // will be a common method for this purpose in the future releases. |
1616 | | -// public static Component compositeRequestFocus(Component component) { |
1617 | | -// if (component instanceof Container) { |
1618 | | -// Container container = (Container)component; |
1619 | | -// if (container.isFocusCycleRoot()) { |
1620 | | -// FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); |
1621 | | -// Component comp = policy.getDefaultComponent(container); |
1622 | | -// if (comp!=null) { |
1623 | | -// comp.requestFocus(); |
1624 | | -// return comp; |
1625 | | -// } |
1626 | | -// } |
1627 | | -// Container rootAncestor = container.getFocusCycleRootAncestor(); |
1628 | | -// if (rootAncestor!=null) { |
1629 | | -// FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); |
1630 | | -// Component comp = policy.getComponentAfter(rootAncestor, container); |
1631 | | -// |
1632 | | -// if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { |
1633 | | -// comp.requestFocus(); |
1634 | | -// return comp; |
1635 | | -// } |
1636 | | -// } |
1637 | | -// } |
1638 | | -// if (component.isFocusable()) { |
1639 | | -// component.requestFocus(); |
1640 | | -// return component; |
1641 | | -// } |
1642 | | -// return null; |
1643 | | -// } |
1644 | | -// |
| 1620 | + // At this point we need this method here. But we assume that there |
| 1621 | + // will be a common method for this purpose in the future releases. |
| 1622 | + public static Component compositeRequestFocus(Component component) { |
| 1623 | + if (component instanceof Container) { |
| 1624 | + Container container = (Container)component; |
| 1625 | + if (container.isFocusCycleRoot()) { |
| 1626 | + FocusTraversalPolicy policy = container.getFocusTraversalPolicy(); |
| 1627 | + Component comp = policy.getDefaultComponent(container); |
| 1628 | + if (comp!=null) { |
| 1629 | + comp.requestFocus(); |
| 1630 | + return comp; |
| 1631 | + } |
| 1632 | + } |
| 1633 | + Container rootAncestor = container.getFocusCycleRootAncestor(); |
| 1634 | + if (rootAncestor!=null) { |
| 1635 | + FocusTraversalPolicy policy = rootAncestor.getFocusTraversalPolicy(); |
| 1636 | + Component comp = policy.getComponentAfter(rootAncestor, container); |
| 1637 | + |
| 1638 | + if (comp!=null && SwingUtilities.isDescendingFrom(comp, container)) { |
| 1639 | + comp.requestFocus(); |
| 1640 | + return comp; |
| 1641 | + } |
| 1642 | + } |
| 1643 | + } |
| 1644 | + if (component.isFocusable()) { |
| 1645 | + component.requestFocus(); |
| 1646 | + return component; |
| 1647 | + } |
| 1648 | + return null; |
| 1649 | + } |
| 1650 | + |
1645 | 1651 | /** |
1646 | 1652 | * Change focus to the visible component in {@code JTabbedPane}. |
1647 | 1653 | * This is not a general-purpose method and is here only to permit |
@@ -1705,20 +1711,20 @@ public static boolean tabbedPaneChangeFocusTo(Component comp) { |
1705 | 1711 | // private static void execute(Runnable command) { |
1706 | 1712 | // SwingUtilities.invokeLater(command); |
1707 | 1713 | // } |
1708 | | -// /** |
1709 | | -// * Sets the {@code SKIP_CLICK_COUNT} client property on the component |
1710 | | -// * if it is an instance of {@code JTextComponent} with a |
1711 | | -// * {@code DefaultCaret}. This property, used for text components acting |
1712 | | -// * as editors in a table or tree, tells {@code DefaultCaret} how many |
1713 | | -// * clicks to skip before starting selection. |
1714 | | -// */ |
1715 | | -// public static void setSkipClickCount(Component comp, int count) { |
1716 | | -// if (comp instanceof JTextComponent |
1717 | | -// && ((JTextComponent) comp).getCaret() instanceof DefaultCaret) { |
1718 | | -// |
1719 | | -// ((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count); |
1720 | | -// } |
1721 | | -// } |
| 1714 | + /** |
| 1715 | + * Sets the {@code SKIP_CLICK_COUNT} client property on the component |
| 1716 | + * if it is an instance of {@code JTextComponent} with a |
| 1717 | + * {@code DefaultCaret}. This property, used for text components acting |
| 1718 | + * as editors in a table or tree, tells {@code DefaultCaret} how many |
| 1719 | + * clicks to skip before starting selection. |
| 1720 | + */ |
| 1721 | + public static void setSkipClickCount(Component comp, int count) { |
| 1722 | + if (comp instanceof JTextComponent |
| 1723 | + && ((JTextComponent) comp).getCaret() instanceof DefaultCaret) { |
| 1724 | + |
| 1725 | + ((JTextComponent) comp).putClientProperty(SKIP_CLICK_COUNT, count); |
| 1726 | + } |
| 1727 | + } |
1722 | 1728 |
|
1723 | 1729 | // /** |
1724 | 1730 | // * Return the MouseEvent's click count, possibly reduced by the value of |
@@ -1896,8 +1902,22 @@ public static Section liesInVertical(Rectangle rect, Point p, |
1896 | 1902 | return liesIn(rect, p, false, false, three); |
1897 | 1903 | } |
1898 | 1904 |
|
1899 | | - public static void compositeRequestFocus(Component editorComponent) { |
1900 | | - // TODO Auto-generated method stub |
1901 | | - |
1902 | | - } |
| 1905 | + /** |
| 1906 | + * Returns the {@link TreePath} that identifies the changed nodes. |
| 1907 | + * |
| 1908 | + * @param event changes in a tree model |
| 1909 | + * @param model corresponing tree model |
| 1910 | + * @return the path to the changed nodes |
| 1911 | + */ |
| 1912 | + public static TreePath getTreePath(TreeModelEvent event, TreeModel model) { |
| 1913 | + TreePath path = event.getTreePath(); |
| 1914 | + if ((path == null) && (model != null)) { |
| 1915 | + Object root = model.getRoot(); |
| 1916 | + if (root != null) { |
| 1917 | + path = new TreePath(root); |
| 1918 | + } |
| 1919 | + } |
| 1920 | + return path; |
| 1921 | + } |
| 1922 | + |
1903 | 1923 | } |
0 commit comments