📄 container.java
字号:
private void addNotifyContainerChildren() { synchronized (getTreeLock ()) { for (int i = ncomponents; --i >= 0; ) { component[i].addNotify(); if (component[i].isLightweight ()) { // If we're not lightweight, and we just got a lightweight // child, we need a lightweight dispatcher to feed it events. if (!this.isLightweight() && dispatcher == null) dispatcher = new LightweightDispatcher (this); if (dispatcher != null) dispatcher.enableEvents(component[i].eventMask); enableEvents(component[i].eventMask); if (peer != null && !isLightweight ()) enableEvents (AWTEvent.PAINT_EVENT_MASK); } } } } /** * Deserialize this Container: * <ol> * <li>Read from the stream the default serializable fields.</li> * <li>Read a list of serializable ContainerListeners as optional * data. If the list is null, no listeners will be registered.</li> * <li>Read this Container's FocusTraversalPolicy as optional data. * If this is null, then this Container will use a * DefaultFocusTraversalPolicy.</li> * </ol> * * @param s the stream to read from * @throws ClassNotFoundException if deserialization fails * @throws IOException if the stream fails */ private void readObject (ObjectInputStream s) throws ClassNotFoundException, IOException { s.defaultReadObject (); String key = (String) s.readObject (); while (key != null) { Object object = s.readObject (); if ("containerL".equals (key)) addContainerListener((ContainerListener) object); // FIXME: under what key is the focus traversal policy stored? else if ("focusTraversalPolicy".equals (key)) setFocusTraversalPolicy ((FocusTraversalPolicy) object); key = (String) s.readObject(); } } /** * Serialize this Container: * <ol> * <li>Write to the stream the default serializable fields.</li> * <li>Write the list of serializable ContainerListeners as optional * data.</li> * <li>Write this Container's FocusTraversalPolicy as optional data.</li> * </ol> * * @param s the stream to write to * @throws IOException if the stream fails */ private void writeObject (ObjectOutputStream s) throws IOException { s.defaultWriteObject (); AWTEventMulticaster.save (s, "containerL", containerListener); if (focusTraversalPolicy instanceof Serializable) s.writeObject (focusTraversalPolicy); else s.writeObject (null); } // Nested classes. /* The following classes are used in concert with the visitChildren() method to implement all the graphics operations that requires traversal of the containment hierarchy. */ abstract static class GfxVisitor { public abstract void visit(Component c, Graphics gfx); } static class GfxPaintVisitor extends GfxVisitor { public static final GfxVisitor INSTANCE = new GfxPaintVisitor(); public void visit(Component c, Graphics gfx) { c.paint(gfx); } } static class GfxPrintVisitor extends GfxVisitor { public static final GfxVisitor INSTANCE = new GfxPrintVisitor(); public void visit(Component c, Graphics gfx) { c.print(gfx); } } static class GfxPaintAllVisitor extends GfxVisitor { public static final GfxVisitor INSTANCE = new GfxPaintAllVisitor(); public void visit(Component c, Graphics gfx) { c.paintAll(gfx); } } static class GfxPrintAllVisitor extends GfxVisitor { public static final GfxVisitor INSTANCE = new GfxPrintAllVisitor(); public void visit(Component c, Graphics gfx) { c.printAll(gfx); } } /** * This class provides accessibility support for subclasses of container. * * @author Eric Blake (ebb9@email.byu.edu) * * @since 1.3 */ protected class AccessibleAWTContainer extends AccessibleAWTComponent { /** * Compatible with JDK 1.4+. */ private static final long serialVersionUID = 5081320404842566097L; /** * The handler to fire PropertyChange when children are added or removed. * * @serial the handler for property changes */ protected ContainerListener accessibleContainerHandler = new AccessibleContainerHandler(); /** * The default constructor. */ protected AccessibleAWTContainer() { Container.this.addContainerListener(accessibleContainerHandler); } /** * Return the number of accessible children of the containing accessible * object (at most the total number of its children). * * @return the number of accessible children */ public int getAccessibleChildrenCount() { synchronized (getTreeLock ()) { int count = 0; int i = component == null ? 0 : component.length; while (--i >= 0) if (component[i] instanceof Accessible) count++; return count; } } /** * Return the nth accessible child of the containing accessible object. * * @param i the child to grab, zero-based * @return the accessible child, or null */ public Accessible getAccessibleChild(int i) { synchronized (getTreeLock ()) { if (component == null) return null; int index = -1; while (i >= 0 && ++index < component.length) if (component[index] instanceof Accessible) i--; if (i < 0) return (Accessible) component[index]; return null; } } /** * Return the accessible child located at point (in the parent's * coordinates), if one exists. * * @param p the point to look at * * @return an accessible object at that point, or null * * @throws NullPointerException if p is null */ public Accessible getAccessibleAt(Point p) { Component c = getComponentAt(p.x, p.y); return c != Container.this && c instanceof Accessible ? (Accessible) c : null; } /** * This class fires a <code>PropertyChange</code> listener, if registered, * when children are added or removed from the enclosing accessible object. * * @author Eric Blake (ebb9@email.byu.edu) * * @since 1.3 */ protected class AccessibleContainerHandler implements ContainerListener { /** * Default constructor. */ protected AccessibleContainerHandler() { // Nothing to do here. } /** * Fired when a component is added; forwards to the PropertyChange * listener. * * @param e the container event for adding */ public void componentAdded(ContainerEvent e) { AccessibleAWTContainer.this.firePropertyChange (ACCESSIBLE_CHILD_PROPERTY, null, e.getChild()); } /** * Fired when a component is removed; forwards to the PropertyChange * listener. * * @param e the container event for removing */ public void componentRemoved(ContainerEvent e) { AccessibleAWTContainer.this.firePropertyChange (ACCESSIBLE_CHILD_PROPERTY, e.getChild(), null); } } // class AccessibleContainerHandler } // class AccessibleAWTContainer} // class Container/** * There is a helper class implied from stack traces called * LightweightDispatcher, but since it is not part of the public API, * rather than mimic it exactly we write something which does "roughly * the same thing". */class LightweightDispatcher implements Serializable{ private static final long serialVersionUID = 5184291520170872969L; private Container nativeContainer; private Cursor nativeCursor; private long eventMask; private transient Component pressedComponent; private transient Component lastComponentEntered; private transient int pressCount; LightweightDispatcher(Container c) { nativeContainer = c; } void enableEvents(long l) { eventMask |= l; } /** * Returns the deepest visible descendent of parent that contains the * specified location and that is not transparent and MouseListener-less. * @param parent the root component to begin the search * @param x the x coordinate * @param y the y coordinate * @return null if <code>parent</code> doesn't contain the location, * parent if parent is not a container or has no child that contains the * location, otherwise the appropriate component from the conditions * above. */ Component getDeepestComponentForMouseEventAt(Component parent, int x, int y) { if (parent == null || (! parent.contains(x, y))) return null; if (! (parent instanceof Container)) return parent; Container c = (Container) parent; return c.findComponentForMouseEventAt(x, y); } Component acquireComponentForMouseEvent(MouseEvent me) { int x = me.getX (); int y = me.getY (); Component mouseEventTarget = null; // Find the candidate which should receive this event. Component parent = nativeContainer; Component candidate = null; Point p = me.getPoint(); while (candidate == null && parent != null) { candidate = getDeepestComponentForMouseEventAt(parent, p.x, p.y); if (candidate == null || (candidate.eventMask & me.getID()) == 0) { candidate = null; p = AWTUtilities.convertPoint(parent, p.x, p.y, parent.parent); parent = parent.parent; } } // If the only candidate we found was the native container itself, // don't dispatch any event at all. We only care about the lightweight // children here. if (candidate == nativeContainer) candidate = null; // If our candidate is new, inform the old target we're leaving. if (lastComponentEntered != null && lastComponentEntered.isShowing() && lastComponentEntered != candidate) { // Old candidate could have been removed from // the nativeContainer so we check first. if (AWTUtilities.isDescendingFrom(lastComponentEntered, nativeContainer)) { Point tp = AWTUtilities.convertPoint(nativeContainer, x, y, lastComponentEntered); MouseEvent exited = new MouseEvent (lastComponentEntered, MouseEvent.MOUSE_EXITED, me.getWhen (), me.getModifiersEx (), tp.x, tp.y, me.getClickCount (), me.isPopupTrigger (), me.getButton ()); lastComponentEntered.dispatchEvent (exited); } lastComponentEntered = null; } // If we have a candidate, maybe enter it. if (candidate != null) { mouseEventTarget = candidate; if (candidate.isLightweight() && candidate.isShowing() && candidate != nativeContainer && candidate != lastComponentEntered) { lastComponentEntered = mouseEventTarget; Point cp = AWTUtilities.convertPoint(nativeContainer, x, y, lastComponentEntered); MouseEvent entered = new MouseEvent (lastComponentEntered, MouseEvent.MOUSE_ENTERED, me.getWhen (), me.getModifiersEx (), cp.x, cp.y, me.getClickCount (), me.isPopupTrigger (), me.getButton ()); lastComponentEntered.dispatchEvent (entered); } } // Check which buttons where pressed except the last button that // changed state. int modifiers = me.getModifiersEx() & (MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK); switch(me.getButton()) { case MouseEvent.BUTTON1: modifiers &= ~MouseEvent.BUTTON1_DOWN_MASK; break; case MouseEvent.BUTTON2: modifiers &= ~MouseEvent.BUTTON2_DOWN_MASK; break; case MouseEvent.BUTTON3: modifiers &= ~MouseEvent.BUTTON3_DOWN_MASK; break; } if (me.getID() == MouseEvent.MOUSE_PRESSED && modifiers > 0 || me.getID() == MouseEvent.MOUSE_DRAGGED) { // If any of the following events occur while a button is held down, // they should be dispatched to the same component to which the // original MOUSE_PRESSED event was dispatched: // - MOUSE_PRESSED: another button pressed while the first is held // down // - MOUSE_DRAGGED if (AWTUtilities.isDescendingFrom(pressedComponent, nativeContainer)) mouseEventTarget = pressedComponent; } else if (me.getID() == MouseEvent.MOUSE_CLICKED) { // Don't dispatch CLICKED events whose target is not the same as the // target for the original PRESSED event. if (candidate != pressedComponent) { mouseEventTarget = null; pressCount = 0; } else if (pressCount == 0) pressedComponent = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -