CompatibilityFocusTraversalPolicy.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* CompatibilityFocusTraversalPolicy.java -- Provides compatibility to old
  2. focus API
  3. Copyright (C) 2006 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package javax.swing;
  33. import java.awt.Component;
  34. import java.awt.Container;
  35. import java.awt.FocusTraversalPolicy;
  36. import java.util.HashMap;
  37. /**
  38. * Provides compatibility to the older focus API in
  39. * {@link JComponent#setNextFocusableComponent(Component)}.
  40. *
  41. * @author Roman Kennke (kennke@aicas.com)
  42. */
  43. class CompatibilityFocusTraversalPolicy
  44. extends FocusTraversalPolicy
  45. {
  46. /**
  47. * The focus traversal policy that has been installed on the focus cycle
  48. * root before, and to which we fall back.
  49. */
  50. private FocusTraversalPolicy fallback;
  51. /**
  52. * Maps components to their next focused components.
  53. */
  54. private HashMap forward;
  55. /**
  56. * Maps components to their previous focused components.
  57. */
  58. private HashMap backward;
  59. /**
  60. * Creates a new CompatibilityFocusTraversalPolicy with the specified
  61. * policy as fallback.
  62. *
  63. * @param p the fallback policy
  64. */
  65. CompatibilityFocusTraversalPolicy(FocusTraversalPolicy p)
  66. {
  67. fallback = p;
  68. forward = new HashMap();
  69. backward = new HashMap();
  70. }
  71. public Component getComponentAfter(Container root, Component current)
  72. {
  73. Component next = (Component) forward.get(current);
  74. if (next == null && fallback != null)
  75. next = fallback.getComponentAfter(root, current);
  76. return next;
  77. }
  78. public Component getComponentBefore(Container root, Component current)
  79. {
  80. Component previous = (Component) backward.get(current);
  81. if (previous == null && fallback != null)
  82. previous = fallback.getComponentAfter(root, current);
  83. return previous;
  84. }
  85. public Component getFirstComponent(Container root)
  86. {
  87. Component first = null;
  88. if (fallback != null)
  89. first = fallback.getFirstComponent(root);
  90. return first;
  91. }
  92. public Component getLastComponent(Container root)
  93. {
  94. Component last = null;
  95. if (fallback != null)
  96. last = fallback.getLastComponent(root);
  97. return last;
  98. }
  99. public Component getDefaultComponent(Container root)
  100. {
  101. Component def = null;
  102. if (fallback != null)
  103. def = fallback.getDefaultComponent(root);
  104. return def;
  105. }
  106. /**
  107. * Sets a next focused component for a specified component. This is called
  108. * by {@link JComponent#setNextFocusableComponent(Component)}.
  109. *
  110. * @param current the current component
  111. * @param next the next focused component
  112. */
  113. void setNextFocusableComponent(Component current, Component next)
  114. {
  115. forward.put(current, next);
  116. backward.put(next, current);
  117. }
  118. /**
  119. * Sets a next focused component for a specified component. This is called
  120. * by {@link JComponent#setNextFocusableComponent(Component)}.
  121. *
  122. * @param current the current component
  123. * @param next the next focused component
  124. */
  125. void addNextFocusableComponent(Component current, Component next)
  126. {
  127. forward.put(current, next);
  128. backward.put(next, current);
  129. }
  130. /**
  131. * Removes a focused component mapping. This is called
  132. * by {@link JComponent#setNextFocusableComponent(Component)}.
  133. *
  134. * @param current the current component
  135. * @param next the next focused component
  136. */
  137. void removeNextFocusableComponent(Component current, Component next)
  138. {
  139. forward.remove(current);
  140. backward.remove(next);
  141. }
  142. }