FocusTraversalOnArray.java 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*******************************************************************************
  2. * Copyright (c) 2011 Google, Inc.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * Google, Inc. - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.wb.swing;
  12. import java.awt.Component;
  13. import java.awt.Container;
  14. import java.awt.FocusTraversalPolicy;
  15. /**
  16. * Cyclic focus traversal policy based on array of components.
  17. * <p>
  18. * This class may be freely distributed as part of any application or plugin.
  19. *
  20. * @author scheglov_ke
  21. */
  22. public class FocusTraversalOnArray extends FocusTraversalPolicy {
  23. private final Component m_Components[];
  24. ////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Constructor
  27. //
  28. ////////////////////////////////////////////////////////////////////////////
  29. public FocusTraversalOnArray(Component components[]) {
  30. m_Components = components;
  31. }
  32. ////////////////////////////////////////////////////////////////////////////
  33. //
  34. // Utilities
  35. //
  36. ////////////////////////////////////////////////////////////////////////////
  37. private int indexCycle(int index, int delta) {
  38. int size = m_Components.length;
  39. int next = (index + delta + size) % size;
  40. return next;
  41. }
  42. private Component cycle(Component currentComponent, int delta) {
  43. int index = -1;
  44. loop : for (int i = 0; i < m_Components.length; i++) {
  45. Component component = m_Components[i];
  46. for (Component c = currentComponent; c != null; c = c.getParent()) {
  47. if (component == c) {
  48. index = i;
  49. break loop;
  50. }
  51. }
  52. }
  53. // try to find enabled component in "delta" direction
  54. int initialIndex = index;
  55. while (true) {
  56. int newIndex = indexCycle(index, delta);
  57. if (newIndex == initialIndex) {
  58. break;
  59. }
  60. index = newIndex;
  61. //
  62. Component component = m_Components[newIndex];
  63. if (component.isEnabled() && component.isVisible() && component.isFocusable()) {
  64. return component;
  65. }
  66. }
  67. // not found
  68. return currentComponent;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////
  71. //
  72. // FocusTraversalPolicy
  73. //
  74. ////////////////////////////////////////////////////////////////////////////
  75. public Component getComponentAfter(Container container, Component component) {
  76. return cycle(component, 1);
  77. }
  78. public Component getComponentBefore(Container container, Component component) {
  79. return cycle(component, -1);
  80. }
  81. public Component getFirstComponent(Container container) {
  82. return m_Components[0];
  83. }
  84. public Component getLastComponent(Container container) {
  85. return m_Components[m_Components.length - 1];
  86. }
  87. public Component getDefaultComponent(Container container) {
  88. return getFirstComponent(container);
  89. }
  90. }