1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
11 import java
.awt
.AWTEvent
;
12 import java
.awt
.AWTError
;
13 import java
.awt
.Component
;
14 import java
.awt
.Container
;
15 import java
.awt
.event
.MouseEvent
;
16 import java
.awt
.event
.InputEvent
;
19 * Encapsulates the logic required to dispatch events to the correct
20 * component in a component tree that may contain lightweight
21 * components. Toolkits typically only identify heavyweight components
22 * as the source of events. This class redirects the events to the
23 * appropriate lightweight children of the heavyweight component.
25 public class LightweightRedirector
27 final static int LAST_BUTTON_NUMBER
= 3;
29 /* We sacrifice one array element to allow the button number to
30 match the index of this array. */
31 Component
[] releaseTargets
= new Component
[LAST_BUTTON_NUMBER
+1];
35 * Modifies or replaces the given event with an event that has been
36 * properly redirected. State of button presses are kept so that
37 * button releases can be redirected to the same component as the
38 * button press. It is required that all events are sent through
39 * this method in chronological order.
41 public AWTEvent
redirect(AWTEvent event
)
43 if (event
instanceof MouseEvent
)
44 return redirectMouse((MouseEvent
) event
);
46 /* In case we don't know how to redirect the event, simply return
47 the event unchanged. */
51 MouseEvent
redirectMouse(MouseEvent event
)
53 int button
= getButtonNumber(event
);
54 int id
= event
.getID();
56 Component heavySource
= (Component
) event
.getSource();
57 Component source
= heavySource
;
61 if (id
== MouseEvent
.MOUSE_RELEASED
)
63 Component target
= releaseTargets
[button
];
67 releaseTargets
[button
] = null;
70 Component child
= source
;
71 while (child
!= heavySource
)
75 child
= child
.getParent();
77 System
.err
.println("warning, orphaned release target");
83 /* Find real component, and adjust source, x and y
88 Component parent
= source
;
90 Component child
= parent
.getComponentAt(x
, y
);
95 // maybe ignoring would be better?
98 String msg
= "delivered event not within component. " +
99 "Heavyweight source was " + heavySource
+ ". " +
100 "Component was " + parent
;
101 throw new AWTError(msg
);
103 if (child
.isLightweight())
105 // descend down to child
112 System
.err
.println("warning: event delivered to wrong " +
113 "heavyweight component. Was " +
114 "delivered to " + source
+ ". " +
115 "Should have been delivered to " +
116 child
+ ". Maybe the native window " +
117 "system is bubbling events up the " +
118 "containment hierarchy.");
123 /* ensure that the release event is delivered to the same
124 component as the press event. For most toolkits this is
125 only necessary for lightweight components, since the
126 underlying windowing system takes care of its heavyweight
128 if (id
== MouseEvent
.MOUSE_PRESSED
)
129 releaseTargets
[button
] = source
;
133 if (source
== heavySource
)
134 return event
; // no change in event
136 // print warning for heavyweights
137 /* this warning can safely be removed if a toolkit that
138 needs heavyweight redirection support is ever created. */
139 if (!source
.isLightweight())
140 System
.err
.println("warning: redirecting to heavyweight");
142 MouseEvent redirected
= new MouseEvent(source
, event
.getID(),
144 event
.getModifiersEx(),
146 event
.getClickCount(),
147 event
.isPopupTrigger());
153 * Identifies the button number for an input event.
155 * @returns the button number, or 0 if no button modifier was set
158 int getButtonNumber(InputEvent event
)
160 int modifiers
= event
.getModifiersEx();
163 InputEvent
.BUTTON1_DOWN_MASK
|
164 InputEvent
.BUTTON2_DOWN_MASK
|
165 InputEvent
.BUTTON3_DOWN_MASK
;
169 case InputEvent
.BUTTON1_DOWN_MASK
:
171 case InputEvent
.BUTTON2_DOWN_MASK
:
173 case InputEvent
.BUTTON3_DOWN_MASK
:
179 System
.err
.println("FIXME: multibutton event");