FileDocCategorySizeDatePackage
AlphaButtonPolicy.javaAPI DocExample3317Thu Oct 24 20:14:28 BST 2002None

AlphaButtonPolicy

public class AlphaButtonPolicy extends FocusTraversalPolicy

Fields Summary
Constructors Summary
Methods Summary
public java.awt.ComponentgetComponentAfter(java.awt.Container focusCycleRoot, java.awt.Component aComponent)

        if (!(aComponent instanceof JButton)) { return null; }
        SortedMap buttons = getSortedButtons(focusCycleRoot);
        // Find all buttons after the current one.
        String nextName = ((JButton)aComponent).getText() + "\0";
        SortedMap nextButtons = buttons.tailMap(nextName);
        if (nextButtons.isEmpty()) {  // Wrapped back to beginning
            if (!buttons.isEmpty()) {
                return (Component)buttons.get(buttons.firstKey());
            }
            return null;  // Degenerate case of no buttons.
        }
        return (Component)nextButtons.get(nextButtons.firstKey());
    
public java.awt.ComponentgetComponentBefore(java.awt.Container focusCycleRoot, java.awt.Component aComponent)

        if (!(aComponent instanceof JButton)) { return null; }

        SortedMap buttons = getSortedButtons(focusCycleRoot);
        SortedMap prevButtons =  // Find all buttons before this one.
            buttons.headMap(((JButton)aComponent).getText());
        if (prevButtons.isEmpty()) {  // Wrapped back to end.
            if (!buttons.isEmpty()) {
                return (Component)buttons.get(buttons.lastKey());
            }
            return null;  // Degenerate case of no buttons.
        }
        return (Component)prevButtons.get(prevButtons.lastKey());
    
public java.awt.ComponentgetDefaultComponent(java.awt.Container focusCycleRoot)

        return getFirstComponent(focusCycleRoot);
    
public java.awt.ComponentgetFirstComponent(java.awt.Container focusCycleRoot)

        SortedMap buttons = getSortedButtons(focusCycleRoot);
        if (buttons.isEmpty()) { return null; }
        return (Component)buttons.get(buttons.firstKey());
    
public java.awt.ComponentgetLastComponent(java.awt.Container focusCycleRoot)

        SortedMap buttons = getSortedButtons(focusCycleRoot);
        if (buttons.isEmpty()) { return null; }
        return (Component)buttons.get(buttons.lastKey());
    
private java.util.SortedMapgetSortedButtons(java.awt.Container focusCycleRoot)

        if (focusCycleRoot == null) {
            throw new IllegalArgumentException("focusCycleRoot can't be null");
        }
        SortedMap result = new TreeMap();  // Will sort all buttons by text.
        sortRecursive(result, focusCycleRoot);
        return result;
    
private voidsortRecursive(java.util.Map buttons, java.awt.Container container)

        for (int i = 0; i < container.getComponentCount(); i++) {
            Component c = container.getComponent(i);
            if (c instanceof JButton) {  // Found another button to sort.
                buttons.put(((JButton)c).getText(), c);
            }
            if (c instanceof Container) {  // Found a container to search.
                sortRecursive(buttons, (Container)c);
            }
        }