container.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,024 行 · 第 1/4 页

JAVA
2,024
字号
          gfx.translate (-bounds.x, -bounds.y);        if (clipped)          gfx.setClip (oldClip.x, oldClip.y, oldClip.width, oldClip.height);      }  }  void dispatchEventImpl(AWTEvent e)  {    // Give lightweight dispatcher a chance to handle it.    if (eventTypeEnabled (e.id)        && dispatcher != null         && dispatcher.handleEvent (e))      return;        if ((e.id <= ContainerEvent.CONTAINER_LAST             && e.id >= ContainerEvent.CONTAINER_FIRST)        && (containerListener != null            || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0))      processEvent(e);    else      super.dispatchEventImpl(e);  }  // This is used to implement Component.transferFocus.  Component findNextFocusComponent(Component child)  {    synchronized (getTreeLock ())      {        int start, end;        if (child != null)          {            for (start = 0; start < ncomponents; ++start)              {                if (component[start] == child)                  break;              }            end = start;            // This special case lets us be sure to terminate.            if (end == 0)              end = ncomponents;            ++start;          }        else          {            start = 0;            end = ncomponents;          }        for (int j = start; j != end; ++j)          {            if (j >= ncomponents)              {                // The JCL says that we should wrap here.  However, that                // seems wrong.  To me it seems that focus order should be                // global within in given window.  So instead if we reach                // the end we try to look in our parent, if we have one.                if (parent != null)                  return parent.findNextFocusComponent(this);                j -= ncomponents;              }            if (component[j] instanceof Container)              {                Component c = component[j];                c = c.findNextFocusComponent(null);                if (c != null)                  return c;              }            else if (component[j].isFocusTraversable())              return component[j];          }        return null;      }  }  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())                   {                    if (dispatcher == null)                      dispatcher = new LightweightDispatcher (this);                  }		  		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()      {      }      /**       * 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 mouseEventTarget;  private transient Component pressedComponent;  private transient Component lastComponentEntered;  private transient int pressCount;    LightweightDispatcher(Container c)  {    nativeContainer = c;  }  void acquireComponentForMouseEvent(MouseEvent me)  {    int x = me.getX ();    int y = me.getY ();    // Find the candidate which should receive this event.    Component parent = nativeContainer;    Component candidate = null;    Point p = me.getPoint();    while (candidate == null && parent != null)      {        candidate =          SwingUtilities.getDeepestComponentAt(parent, p.x, p.y);        if (candidate == null || (candidate.eventMask & me.getID()) == 0)        {          candidate = null;          p = SwingUtilities.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 (SwingUtilities.isDescendingFrom(lastComponentEntered, nativeContainer))        {          Point tp =             SwingUtilities.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 = SwingUtilities.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);          }      }    if (me.getID() == MouseEvent.MOUSE_RELEASED        || me.getID() == MouseEvent.MOUSE_PRESSED && pressCount > 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_RELEASED      //   - MOUSE_PRESSED: another button pressed while the first is held down      //   - MOUSE_DRAGGED      if (SwingUtilities.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;        else if (pressCount == 0)          pressedComponent = null;      }  }  boolean handleEvent(AWTEvent e)  {    if (e instanceof MouseEvent)      {        MouseEvent me = (MouseEvent) e;        acquireComponentForMouseEvent(me);	        // Avoid dispatching ENTERED and EXITED events twice.        if (mouseEventTarget != null            && mouseEventTarget.isShowing()            && e.getID() != MouseEvent.MOUSE_ENTERED            && e.getID() != MouseEvent.MOUSE_EXITED)          {            MouseEvent newEvt =               SwingUtilities.convertMouseEvent(nativeContainer, me,                                                mouseEventTarget);            mouseEventTarget.dispatchEvent(newEvt);            switch (e.getID())              {                case MouseEvent.MOUSE_PRESSED:                  if (pressCount++ == 0)                    pressedComponent = mouseEventTarget;                  break;                case MouseEvent.MOUSE_RELEASED:                  // Clear our memory of the original PRESSED event, only if                  // we're not expecting a CLICKED event after this. If                  // there is a CLICKED event after this, it will do clean up.                  if (--pressCount == 0                      && mouseEventTarget != pressedComponent)                    pressedComponent = null;                  break;              }              if (newEvt.isConsumed())                e.consume();          }      }        return e.isConsumed();  }} // class LightweightDispatcher

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?