LightweightRedirector.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Copyright (C) 2000 Free Software Foundation
  2. This file is part of libgcj.
  3. This software is copyrighted work licensed under the terms of the
  4. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  5. details. */
  6. package gnu.awt;
  7. import java.awt.AWTEvent;
  8. import java.awt.AWTError;
  9. import java.awt.Component;
  10. import java.awt.Container;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.InputEvent;
  13. /**
  14. * Encapsulates the logic required to dispatch events to the correct
  15. * component in a component tree that may contain lightweight
  16. * components. Toolkits typically only identify heavyweight components
  17. * as the source of events. This class redirects the events to the
  18. * appropriate lightweight children of the heavyweight component.
  19. */
  20. public class LightweightRedirector
  21. {
  22. final static int LAST_BUTTON_NUMBER = 3;
  23. /* We sacrifice one array element to allow the button number to
  24. match the index of this array. */
  25. Component[] releaseTargets = new Component[LAST_BUTTON_NUMBER+1];
  26. /**
  27. *
  28. * Modifies or replaces the given event with an event that has been
  29. * properly redirected. State of button presses are kept so that
  30. * button releases can be redirected to the same component as the
  31. * button press. It is required that all events are sent through
  32. * this method in chronological order.
  33. */
  34. public AWTEvent redirect(AWTEvent event)
  35. {
  36. if (event instanceof MouseEvent)
  37. return redirectMouse((MouseEvent) event);
  38. /* In case we don't know how to redirect the event, simply return
  39. the event unchanged. */
  40. return event;
  41. }
  42. MouseEvent redirectMouse(MouseEvent event)
  43. {
  44. int button = getButtonNumber(event);
  45. int id = event.getID();
  46. Component heavySource = (Component) event.getSource();
  47. Component source = heavySource;
  48. int x = event.getX();
  49. int y = event.getY();
  50. if (id == MouseEvent.MOUSE_RELEASED)
  51. {
  52. Component target = releaseTargets[button];
  53. if (target != null)
  54. {
  55. releaseTargets[button] = null;
  56. source = target;
  57. Component child = source;
  58. while (child != heavySource)
  59. {
  60. x -= child.getX();
  61. y -= child.getY();
  62. child = child.getParent();
  63. if (child == null)
  64. System.err.println("warning, orphaned release target");
  65. }
  66. }
  67. }
  68. else
  69. {
  70. /* Find real component, and adjust source, x and y
  71. accordingly. */
  72. while (true)
  73. {
  74. Component parent = source;
  75. Component child = parent.getComponentAt(x, y);
  76. if (parent == child)
  77. break;
  78. // maybe ignoring would be better?
  79. if (child == null)
  80. {
  81. String msg = "delivered event not within component. " +
  82. "Heavyweight source was " + heavySource + ". " +
  83. "Component was " + parent;
  84. throw new AWTError(msg);
  85. }
  86. if (child.isLightweight())
  87. {
  88. // descend down to child
  89. source = child;
  90. x -= child.getX();
  91. y -= child.getY();
  92. }
  93. else
  94. {
  95. System.err.println("warning: event delivered to wrong " +
  96. "heavyweight component. Was " +
  97. "delivered to " + source + ". " +
  98. "Should have been delivered to " +
  99. child + ". Maybe the native window " +
  100. "system is bubbling events up the " +
  101. "containment hierarchy.");
  102. break;
  103. }
  104. }
  105. /* ensure that the release event is delivered to the same
  106. component as the press event. For most toolkits this is
  107. only necessary for lightweight components, since the
  108. underlying windowing system takes care of its heavyweight
  109. components. */
  110. if (id == MouseEvent.MOUSE_PRESSED)
  111. releaseTargets[button] = source;
  112. }
  113. if (source == heavySource)
  114. return event; // no change in event
  115. // print warning for heavyweights
  116. /* this warning can safely be removed if a toolkit that
  117. needs heavyweight redirection support is ever created. */
  118. if (!source.isLightweight())
  119. System.err.println("warning: redirecting to heavyweight");
  120. MouseEvent redirected = new MouseEvent(source, event.getID(),
  121. event.getWhen(),
  122. event.getModifiersEx(),
  123. x, y,
  124. event.getClickCount(),
  125. event.isPopupTrigger());
  126. return redirected;
  127. }
  128. /**
  129. * Identifies the button number for an input event.
  130. *
  131. * @returns the button number, or 0 if no button modifier was set
  132. * for the event.
  133. */
  134. int getButtonNumber(InputEvent event)
  135. {
  136. int modifiers = event.getModifiersEx();
  137. modifiers &=
  138. InputEvent.BUTTON1_DOWN_MASK |
  139. InputEvent.BUTTON2_DOWN_MASK |
  140. InputEvent.BUTTON3_DOWN_MASK;
  141. switch (modifiers)
  142. {
  143. case InputEvent.BUTTON1_DOWN_MASK:
  144. return 1;
  145. case InputEvent.BUTTON2_DOWN_MASK:
  146. return 2;
  147. case InputEvent.BUTTON3_DOWN_MASK:
  148. return 3;
  149. case 0:
  150. return 0;
  151. default:
  152. System.err.println("FIXME: multibutton event");
  153. return 0;
  154. }
  155. }
  156. }