src/share/classes/java/awt/ContainerOrderFocusTraversalPolicy.java

Print this page




  68     /*
  69      * JDK 1.4 serialVersionUID
  70      */
  71     private static final long serialVersionUID = 486933713763926351L;
  72 
  73     private boolean implicitDownCycleTraversal = true;
  74 
  75     /**
  76      * Used by getComponentAfter and getComponentBefore for efficiency. In
  77      * order to maintain compliance with the specification of
  78      * FocusTraversalPolicy, if traversal wraps, we should invoke
  79      * getFirstComponent or getLastComponent. These methods may be overriden in
  80      * subclasses to behave in a non-generic way. However, in the generic case,
  81      * these methods will simply return the first or last Components of the
  82      * sorted list, respectively. Since getComponentAfter and
  83      * getComponentBefore have already built the list before determining
  84      * that they need to invoke getFirstComponent or getLastComponent, the
  85      * list should be reused if possible.
  86      */
  87     transient private Container cachedRoot;
  88     transient private List cachedCycle;
  89 
  90     /*
  91      * We suppose to use getFocusTraversalCycle & getComponentIndex methods in order
  92      * to divide the policy into two parts:
  93      * 1) Making the focus traversal cycle.
  94      * 2) Traversing the cycle.
  95      * The 1st point assumes producing a list of components representing the focus
  96      * traversal cycle. The two methods mentioned above should implement this logic.
  97      * The 2nd point assumes implementing the common concepts of operating on the
  98      * cycle: traversing back and forth, retrieving the initial/default/first/last
  99      * component. These concepts are described in the AWT Focus Spec and they are
 100      * applied to the FocusTraversalPolicy in general.
 101      * Thus, a descendant of this policy may wish to not reimplement the logic of
 102      * the 2nd point but just override the implementation of the 1st one.
 103      * A striking example of such a descendant is the javax.swing.SortingFocusTraversalPolicy.
 104      */
 105     /*protected*/ private List<Component> getFocusTraversalCycle(Container aContainer) {
 106         List<Component> cycle = new ArrayList<Component>();
 107         enumerateCycle(aContainer, cycle);
 108         return cycle;
 109     }
 110     /*protected*/ private int getComponentIndex(List<Component> cycle, Component aComponent) {
 111         return cycle.indexOf(aComponent);
 112     }
 113 
 114     private void enumerateCycle(Container container, List cycle) {
 115         if (!(container.isVisible() && container.isDisplayable())) {
 116             return;
 117         }
 118 
 119         cycle.add(container);
 120 
 121         Component[] components = container.getComponents();
 122         for (int i = 0; i < components.length; i++) {
 123             Component comp = components[i];
 124             if (comp instanceof Container) {
 125                 Container cont = (Container)comp;
 126 
 127                 if (!cont.isFocusCycleRoot() && !cont.isFocusTraversalPolicyProvider()) {
 128                     enumerateCycle(cont, cycle);
 129                     continue;
 130                 }
 131             }
 132             cycle.add(comp);
 133         }
 134     }




  68     /*
  69      * JDK 1.4 serialVersionUID
  70      */
  71     private static final long serialVersionUID = 486933713763926351L;
  72 
  73     private boolean implicitDownCycleTraversal = true;
  74 
  75     /**
  76      * Used by getComponentAfter and getComponentBefore for efficiency. In
  77      * order to maintain compliance with the specification of
  78      * FocusTraversalPolicy, if traversal wraps, we should invoke
  79      * getFirstComponent or getLastComponent. These methods may be overriden in
  80      * subclasses to behave in a non-generic way. However, in the generic case,
  81      * these methods will simply return the first or last Components of the
  82      * sorted list, respectively. Since getComponentAfter and
  83      * getComponentBefore have already built the list before determining
  84      * that they need to invoke getFirstComponent or getLastComponent, the
  85      * list should be reused if possible.
  86      */
  87     transient private Container cachedRoot;
  88     transient private List<Component> cachedCycle;
  89 
  90     /*
  91      * We suppose to use getFocusTraversalCycle & getComponentIndex methods in order
  92      * to divide the policy into two parts:
  93      * 1) Making the focus traversal cycle.
  94      * 2) Traversing the cycle.
  95      * The 1st point assumes producing a list of components representing the focus
  96      * traversal cycle. The two methods mentioned above should implement this logic.
  97      * The 2nd point assumes implementing the common concepts of operating on the
  98      * cycle: traversing back and forth, retrieving the initial/default/first/last
  99      * component. These concepts are described in the AWT Focus Spec and they are
 100      * applied to the FocusTraversalPolicy in general.
 101      * Thus, a descendant of this policy may wish to not reimplement the logic of
 102      * the 2nd point but just override the implementation of the 1st one.
 103      * A striking example of such a descendant is the javax.swing.SortingFocusTraversalPolicy.
 104      */
 105     /*protected*/ private List<Component> getFocusTraversalCycle(Container aContainer) {
 106         List<Component> cycle = new ArrayList<Component>();
 107         enumerateCycle(aContainer, cycle);
 108         return cycle;
 109     }
 110     /*protected*/ private int getComponentIndex(List<Component> cycle, Component aComponent) {
 111         return cycle.indexOf(aComponent);
 112     }
 113 
 114     private void enumerateCycle(Container container, List<Component> cycle) {
 115         if (!(container.isVisible() && container.isDisplayable())) {
 116             return;
 117         }
 118 
 119         cycle.add(container);
 120 
 121         Component[] components = container.getComponents();
 122         for (int i = 0; i < components.length; i++) {
 123             Component comp = components[i];
 124             if (comp instanceof Container) {
 125                 Container cont = (Container)comp;
 126 
 127                 if (!cont.isFocusCycleRoot() && !cont.isFocusTraversalPolicyProvider()) {
 128                     enumerateCycle(cont, cycle);
 129                     continue;
 130                 }
 131             }
 132             cycle.add(comp);
 133         }
 134     }