diff options
| author | Roman Kennke <roman@kennke.org> | 2006-09-18 13:22:57 +0000 |
|---|---|---|
| committer | Roman Kennke <roman@kennke.org> | 2006-09-18 13:22:57 +0000 |
| commit | 7253664e2485d0214743b00983b2cb6017bd3a78 (patch) | |
| tree | c489cb3b729bed745b9687713d309bc3ec61b20f /java | |
| parent | d30cd18fb409cfe8d30ea69f1443c79918c0b5bc (diff) | |
| download | classpath-7253664e2485d0214743b00983b2cb6017bd3a78.tar.gz | |
2006-09-18 Roman Kennke <kennke@aicas.com>
* java/awt/Component.java
(show): Test for the peer beeing lightweight directly.
(paintAll): Validate before painting. Don't paint when not
showing. Call peer.paint() when the component is heavyweight.
(repaint): Delegate to the parent when lightweight, rather
than skipping to the nearest heavyweight.
(createImage): Added null check to prevent NPE.
(dispatchEvent): Moved old event dispatching and toolkit
event dispatching to dispatchEventImpl.
(addComponentListener): Don't enable event. Only add listener
when not null. Switch to new event dispatching only.
(addFocusListener): Likewise.
(addHierarchyListener): Likewise.
(addHierarchyBoundsListener): Likewise.
(addKeyListener): Likewise.
(addMouseListener): Likewise.
(addMouseMotionListener): Likewise.
(addMouseWheelListener): Likewise.
(addInputMethodListener): Likewise.
(coalesceEvents): For mouse events coalesce them only when
their modifiers are equal. For paint events coalesce the events
when one contains the other, without going through complicated
heuristics.
(dispatchEventImpl): Moved old event dispatching and toolkit
event dispatching to dispatchEventImpl.
(coalescePaintEvents): Removed.
(HeavyweightInLightweightListener.componentHidden):
Fixed condition.
* java/awt/Container.java
(addImpl): Don't enable events on lightweights.
(remove): Reordered operations. Don't remove any listeners.
Throw ArrayIndexOutOfBoundsException when index >= ncomponents.
Only removeNotify() when peer is != null. Only invalidate if
not already invalid. Only fire ContainerEvent if there is
an interested listener or the event is enabled. Dispatch this
event directly without the event queue.
(removeAll): Likewise.
(paintComponents): Only paint when showing. Also paint heavyweights.
Don't paint the container itself.
(removeNotify): Create local variables for improved thread safety.
(addNotifyContainerChildren): Don't enable events for lightweights.
Diffstat (limited to 'java')
| -rw-r--r-- | java/awt/Component.java | 262 | ||||
| -rw-r--r-- | java/awt/Container.java | 101 |
2 files changed, 180 insertions, 183 deletions
diff --git a/java/awt/Component.java b/java/awt/Component.java index cab1df3f9..070f610a3 100644 --- a/java/awt/Component.java +++ b/java/awt/Component.java @@ -971,14 +971,14 @@ public abstract class Component // case lightweight components are not initially painted -- // Container.paint first calls isShowing () before painting itself // and its children. - if(!isVisible()) + if(! visible) { // Need to lock the tree here to avoid races and inconsistencies. synchronized (getTreeLock()) { visible = true; // Avoid NullPointerExceptions by creating a local reference. - ComponentPeer currentPeer=peer; + ComponentPeer currentPeer = peer; if (currentPeer != null) { currentPeer.show(); @@ -990,7 +990,7 @@ public abstract class Component // The JDK repaints the component before invalidating the parent. // So do we. - if (isLightweight()) + if (peer instanceof LightweightPeer) repaint(); } @@ -1037,7 +1037,7 @@ public abstract class Component */ public void hide() { - if (isVisible()) + if (visible) { // Need to lock the tree here to avoid races and inconsistencies. synchronized (getTreeLock()) @@ -2229,9 +2229,14 @@ public abstract class Component */ public void paintAll(Graphics g) { - if (! visible) - return; - paint(g); + if (isShowing()) + { + validate(); + if (peer instanceof LightweightPeer) + paint(g); + else + peer.paint(g); + } } /** @@ -2302,36 +2307,32 @@ public abstract class Component // Let the nearest heavyweight parent handle repainting for lightweight // components. - // This goes up the hierarchy until we hit - // a heavyweight component that handles this and translates the - // rectangle while doing so. - - // We perform some boundary checking to restrict the paint - // region to this component. - int px = (x < 0 ? 0 : x); - int py = (y < 0 ? 0 : y); - int pw = width; - int ph = height; - Component par = this; - while (par != null && p instanceof LightweightPeer) + // We need to recursivly call repaint() on the parent here, since + // a (lightweight) parent component might have overridden repaint() + // to perform additional custom tasks. + + if (p instanceof LightweightPeer) { - px += par.x; - py += par.y; // We perform some boundary checking to restrict the paint // region to this component. - pw = Math.min(pw, par.width); - ph = Math.min(ph, par.height); - par = par.parent; - p = par == null ? null : par.peer; + if (parent != null) + { + int px = this.x + Math.max(0, x); + int py = this.y + Math.max(0, y); + int pw = Math.min(this.width, width); + int ph = Math.min(this.height, height); + parent.repaint(tm, px, py, pw, ph); + } } - - // Now send an UPDATE event to the heavyweight component that we've found. - if (par != null && par.isVisible() && p != null && pw > 0 && ph > 0) + else { - assert ! (p instanceof LightweightPeer); - PaintEvent pe = new PaintEvent(par, PaintEvent.UPDATE, - new Rectangle(px, py, pw, ph)); - getToolkit().getSystemEventQueue().postEvent(pe); + // Now send an UPDATE event to the heavyweight component that we've found. + if (isVisible() && p != null && width > 0 && height > 0) + { + PaintEvent pe = new PaintEvent(this, PaintEvent.UPDATE, + new Rectangle(x, y, width, height)); + getToolkit().getSystemEventQueue().postEvent(pe); + } } } @@ -2459,7 +2460,8 @@ public abstract class Component p = comp == null ? null : comp.peer; } - returnValue = p.createImage(width, height); + if (p != null) + returnValue = p.createImage(width, height); } return returnValue; } @@ -2756,14 +2758,6 @@ public abstract class Component */ public final void dispatchEvent(AWTEvent e) { - Event oldEvent = translateEvent(e); - if (oldEvent != null) - postEvent (oldEvent); - - // Give toolkit a chance to dispatch the event - // to globally registered listeners. - Toolkit.getDefaultToolkit().globalDispatchEvent(e); - // Some subclasses in the AWT package need to override this behavior, // hence the use of dispatchEventImpl(). dispatchEventImpl(e); @@ -2814,9 +2808,12 @@ public abstract class Component */ public synchronized void addComponentListener(ComponentListener listener) { - componentListener = AWTEventMulticaster.add(componentListener, listener); - if (componentListener != null) - enableEvents(AWTEvent.COMPONENT_EVENT_MASK); + if (listener != null) + { + componentListener = AWTEventMulticaster.add(componentListener, + listener); + newEventsOnly = true; + } } /** @@ -2862,9 +2859,11 @@ public abstract class Component */ public synchronized void addFocusListener(FocusListener listener) { - focusListener = AWTEventMulticaster.add(focusListener, listener); - if (focusListener != null) - enableEvents(AWTEvent.FOCUS_EVENT_MASK); + if (listener != null) + { + focusListener = AWTEventMulticaster.add(focusListener, listener); + newEventsOnly = true; + } } /** @@ -2909,16 +2908,19 @@ public abstract class Component */ public synchronized void addHierarchyListener(HierarchyListener listener) { - hierarchyListener = AWTEventMulticaster.add(hierarchyListener, listener); - if (hierarchyListener != null) - enableEvents(AWTEvent.HIERARCHY_EVENT_MASK); - - // Need to lock the tree, otherwise we might end up inconsistent. - synchronized (getTreeLock()) + if (listener != null) { - numHierarchyListeners++; - if (parent != null) - parent.updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK, 1); + hierarchyListener = AWTEventMulticaster.add(hierarchyListener, + listener); + newEventsOnly = true; + // Need to lock the tree, otherwise we might end up inconsistent. + synchronized (getTreeLock()) + { + numHierarchyListeners++; + if (parent != null) + parent.updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK, + 1); + } } } @@ -2975,19 +2977,20 @@ public abstract class Component public synchronized void addHierarchyBoundsListener(HierarchyBoundsListener listener) { - hierarchyBoundsListener = - AWTEventMulticaster.add(hierarchyBoundsListener, listener); - if (hierarchyBoundsListener != null) - enableEvents(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK); - - // Need to lock the tree, otherwise we might end up inconsistent. - synchronized (getTreeLock()) + if (listener != null) { - numHierarchyBoundsListeners++; - if (parent != null) - parent.updateHierarchyListenerCount - (AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, - 1); + hierarchyBoundsListener = + AWTEventMulticaster.add(hierarchyBoundsListener, listener); + newEventsOnly = true; + + // Need to lock the tree, otherwise we might end up inconsistent. + synchronized (getTreeLock()) + { + numHierarchyBoundsListeners++; + if (parent != null) + parent.updateHierarchyListenerCount + (AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, 1); + } } } @@ -3080,9 +3083,11 @@ public abstract class Component */ public synchronized void addKeyListener(KeyListener listener) { - keyListener = AWTEventMulticaster.add(keyListener, listener); - if (keyListener != null) - enableEvents(AWTEvent.KEY_EVENT_MASK); + if (listener != null) + { + keyListener = AWTEventMulticaster.add(keyListener, listener); + newEventsOnly = true; + } } /** @@ -3127,9 +3132,11 @@ public abstract class Component */ public synchronized void addMouseListener(MouseListener listener) { - mouseListener = AWTEventMulticaster.add(mouseListener, listener); - if (mouseListener != null) - enableEvents(AWTEvent.MOUSE_EVENT_MASK); + if (listener != null) + { + mouseListener = AWTEventMulticaster.add(mouseListener, listener); + newEventsOnly = true; + } } /** @@ -3174,9 +3181,12 @@ public abstract class Component */ public synchronized void addMouseMotionListener(MouseMotionListener listener) { - mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, listener); - if (mouseMotionListener != null) - enableEvents(AWTEvent.MOUSE_MOTION_EVENT_MASK); + if (listener != null) + { + mouseMotionListener = AWTEventMulticaster.add(mouseMotionListener, + listener); + newEventsOnly = true; + } } /** @@ -3223,9 +3233,12 @@ public abstract class Component */ public synchronized void addMouseWheelListener(MouseWheelListener listener) { - mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, listener); - if (mouseWheelListener != null) - enableEvents(AWTEvent.MOUSE_WHEEL_EVENT_MASK); + if (listener != null) + { + mouseWheelListener = AWTEventMulticaster.add(mouseWheelListener, + listener); + newEventsOnly = true; + } } /** @@ -3273,9 +3286,12 @@ public abstract class Component */ public synchronized void addInputMethodListener(InputMethodListener listener) { - inputMethodListener = AWTEventMulticaster.add(inputMethodListener, listener); - if (inputMethodListener != null) - enableEvents(AWTEvent.INPUT_METHOD_EVENT_MASK); + if (listener != null) + { + inputMethodListener = AWTEventMulticaster.add(inputMethodListener, + listener); + newEventsOnly = true; + } } /** @@ -3512,19 +3528,36 @@ public abstract class Component */ protected AWTEvent coalesceEvents(AWTEvent existingEvent, AWTEvent newEvent) { + AWTEvent coalesced = null; switch (existingEvent.id) { case MouseEvent.MOUSE_MOVED: case MouseEvent.MOUSE_DRAGGED: // Just drop the old (intermediate) event and return the new one. - return newEvent; + MouseEvent me1 = (MouseEvent) existingEvent; + MouseEvent me2 = (MouseEvent) newEvent; + if (me1.getModifiers() == me2.getModifiers()) + coalesced = newEvent; + break; case PaintEvent.PAINT: case PaintEvent.UPDATE: - return coalescePaintEvents((PaintEvent) existingEvent, - (PaintEvent) newEvent); + // For heavyweights the EventQueue should ask the peer. + if (peer == null || peer instanceof LightweightPeer) + { + PaintEvent pe1 = (PaintEvent) existingEvent; + PaintEvent pe2 = (PaintEvent) newEvent; + Rectangle r1 = pe1.getUpdateRect(); + Rectangle r2 = pe2.getUpdateRect(); + if (r1.contains(r2)) + coalesced = existingEvent; + else if (r2.contains(r1)) + coalesced = newEvent; + } + break; default: - return null; + coalesced = null; } + return coalesced; } /** @@ -5599,11 +5632,21 @@ p * <li>the set of backward traversal keys if (! dispatched) { - if (eventTypeEnabled (e.id)) + // Give toolkit a chance to dispatch the event + // to globally registered listeners. + Toolkit.getDefaultToolkit().globalDispatchEvent(e); + + if (newEventsOnly) { - if (e.id != PaintEvent.PAINT && e.id != PaintEvent.UPDATE) + if (eventTypeEnabled(e.id)) processEvent(e); } + else + { + Event oldEvent = translateEvent(e); + if (oldEvent != null) + postEvent (oldEvent); + } if (peer != null) peer.handleEvent(e); } @@ -5696,45 +5739,6 @@ p * <li>the set of backward traversal keys } /** - * Coalesce paint events. Current heuristic is: Merge if the union of - * areas is less than twice that of the sum of the areas. The X server - * tend to create a lot of paint events that are adjacent but not - * overlapping. - * - * <pre> - * +------+ - * | +-----+ ...will be merged - * | | | - * | | | - * +------+ | - * +-----+ - * - * +---------------+--+ - * | | | ...will not be merged - * +---------------+ | - * | | - * | | - * | | - * | | - * | | - * +--+ - * </pre> - * - * @param queuedEvent the first paint event - * @param newEvent the second paint event - * @return the combined paint event, or null - */ - private PaintEvent coalescePaintEvents(PaintEvent queuedEvent, - PaintEvent newEvent) - { - Rectangle r1 = queuedEvent.getUpdateRect(); - Rectangle r2 = newEvent.getUpdateRect(); - Rectangle union = r1.union(r2); - newEvent.setUpdateRect(union); - return newEvent; - } - - /** * This method is used to implement transferFocus(). CHILD is the child * making the request. This is overridden by Container; when called for an * ordinary component there is no child and so we always return null. @@ -5874,7 +5878,7 @@ p * <li>the set of backward traversal keys */ public void componentHidden(ComponentEvent event) { - if (!isShowing()) + if (isShowing()) peer.hide(); } } diff --git a/java/awt/Container.java b/java/awt/Container.java index db755bbb7..403a6a473 100644 --- a/java/awt/Container.java +++ b/java/awt/Container.java @@ -330,13 +330,6 @@ public class Container extends Component { // Notify the component that it has a new parent. comp.addNotify(); - - if (comp.isLightweight ()) - { - enableEvents (comp.eventMask); - if (!isLightweight ()) - enableEvents (AWTEvent.PAINT_EVENT_MASK); - } } // Invalidate the layout of the added component and its ancestors. @@ -417,17 +410,15 @@ public class Container extends Component { synchronized (getTreeLock ()) { - Component r = component[index]; + if (index < 0 || index >= ncomponents) + throw new ArrayIndexOutOfBoundsException(); - ComponentListener[] list = r.getComponentListeners(); - for (int j = 0; j < list.length; j++) - r.removeComponentListener(list[j]); - - r.removeNotify(); + Component r = component[index]; + if (peer != null) + r.removeNotify(); - System.arraycopy(component, index + 1, component, index, - ncomponents - index - 1); - component[--ncomponents] = null; + if (layoutMgr != null) + layoutMgr.removeLayoutComponent(r); // Update the counter for Hierarchy(Bounds)Listeners. int childHierarchyListeners = r.numHierarchyListeners; @@ -439,20 +430,23 @@ public class Container extends Component updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, -childHierarchyListeners); - invalidate(); + r.parent = null; - if (layoutMgr != null) - layoutMgr.removeLayoutComponent(r); + System.arraycopy(component, index + 1, component, index, + ncomponents - index - 1); + component[--ncomponents] = null; - r.parent = null; + if (valid) + invalidate(); - if (isShowing ()) + if (containerListener != null + || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0) { // Post event to notify of removing the component. ContainerEvent ce = new ContainerEvent(this, - ContainerEvent.COMPONENT_REMOVED, - r); - getToolkit().getSystemEventQueue().postEvent(ce); + ContainerEvent.COMPONENT_REMOVED, + r); + dispatchEvent(ce); } // Notify hierarchy listeners. @@ -496,25 +490,14 @@ public class Container extends Component // super.removeAll() ). // By doing it this way, user code cannot prevent the correct // removal of components. - for ( int index = 0; index < ncomponents; index++) + while (ncomponents > 0) { - Component r = component[index]; + ncomponents--; + Component r = component[ncomponents]; + component[ncomponents] = null; - ComponentListener[] list = r.getComponentListeners(); - for (int j = 0; j < list.length; j++) - r.removeComponentListener(list[j]); - - r.removeNotify(); - - // Update the counter for Hierarchy(Bounds)Listeners. - int childHierarchyListeners = r.numHierarchyListeners; - if (childHierarchyListeners > 0) - updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK, - -childHierarchyListeners); - int childHierarchyBoundsListeners = r.numHierarchyBoundsListeners; - if (childHierarchyBoundsListeners > 0) - updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, - -childHierarchyListeners); + if (peer != null) + r.removeNotify(); if (layoutMgr != null) layoutMgr.removeLayoutComponent(r); @@ -533,6 +516,17 @@ public class Container extends Component dispatchEvent(ce); } + // Update the counter for Hierarchy(Bounds)Listeners. + int childHierarchyListeners = r.numHierarchyListeners; + if (childHierarchyListeners > 0) + updateHierarchyListenerCount(AWTEvent.HIERARCHY_EVENT_MASK, + -childHierarchyListeners); + int childHierarchyBoundsListeners = r.numHierarchyBoundsListeners; + if (childHierarchyBoundsListeners > 0) + updateHierarchyListenerCount(AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK, + -childHierarchyListeners); + + // Send HierarchyEvent if necessary. fireHierarchyEvent(HierarchyEvent.HIERARCHY_CHANGED, r, this, HierarchyEvent.PARENT_CHANGED); @@ -541,8 +535,6 @@ public class Container extends Component if (valid) invalidate(); - - ncomponents = 0; } } @@ -795,8 +787,9 @@ public class Container extends Component LayoutManager l = layoutMgr; if (l instanceof LayoutManager2) maxSize = ((LayoutManager2) l).maximumLayoutSize(this); - else + else { maxSize = super.maximumSizeImpl(); + } size = maxSize; } } @@ -931,8 +924,8 @@ public class Container extends Component */ public void paintComponents(Graphics g) { - paint(g); - visitChildren(g, GfxPaintAllVisitor.INSTANCE, true); + if (isShowing()) + visitChildren(g, GfxPaintAllVisitor.INSTANCE, false); } /** @@ -1258,8 +1251,14 @@ public class Container extends Component { synchronized (getTreeLock ()) { - for (int i = 0; i < ncomponents; ++i) - component[i].removeNotify(); + int ncomps = ncomponents; + Component[] comps = component; + for (int i = ncomps - 1; i >= 0; --i) + { + Component comp = comps[i]; + if (comp != null) + comp.removeNotify(); + } super.removeNotify(); } } @@ -2059,12 +2058,6 @@ public class Container extends Component for (int i = ncomponents; --i >= 0; ) { component[i].addNotify(); - if (component[i].isLightweight ()) - { - enableEvents(component[i].eventMask); - if (peer != null && !isLightweight ()) - enableEvents (AWTEvent.PAINT_EVENT_MASK); - } } } } |
