2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / awt / LightweightRedirector.java
bloba0ea7bcff4bbba4ebc6994e0057d69d80f6b5688
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
7 details. */
9 package gnu.awt;
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;
18 /**
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];
33 /**
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. */
48 return event;
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;
58 int x = event.getX();
59 int y = event.getY();
61 if (id == MouseEvent.MOUSE_RELEASED)
63 Component target = releaseTargets[button];
65 if (target != null)
67 releaseTargets[button] = null;
68 source = target;
70 Component child = source;
71 while (child != heavySource)
73 x -= child.getX();
74 y -= child.getY();
75 child = child.getParent();
76 if (child == null)
77 System.err.println("warning, orphaned release target");
81 else
83 /* Find real component, and adjust source, x and y
84 accordingly. */
86 while (true)
88 Component parent = source;
90 Component child = parent.getComponentAt(x, y);
92 if (parent == child)
93 break;
95 // maybe ignoring would be better?
96 if (child == null)
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
106 source = child;
107 x -= child.getX();
108 y -= child.getY();
110 else
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.");
119 break;
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
127 components. */
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(),
143 event.getWhen(),
144 event.getModifiers(),
145 x, y,
146 event.getClickCount(),
147 event.isPopupTrigger());
149 return redirected;
153 * Identifies the button number for an input event.
155 * @returns the button number, or 0 if no button modifier was set
156 * for the event.
158 int getButtonNumber(InputEvent event)
160 int modifiers = event.getModifiers();
162 modifiers &=
163 InputEvent.BUTTON1_MASK |
164 InputEvent.BUTTON2_MASK |
165 InputEvent.BUTTON3_MASK;
167 switch (modifiers)
169 case InputEvent.BUTTON1_MASK:
170 return 1;
171 case InputEvent.BUTTON2_MASK:
172 return 2;
173 case InputEvent.BUTTON3_MASK:
174 return 3;
175 case 0:
176 return 0;
178 default:
179 System.err.println("FIXME: multibutton event");
180 return 0;