Source for gnu.awt.xlib.XEventQueue

   1: /* Copyright (C) 2000  Free Software Foundation
   2: 
   3:    This file is part of libgcj.
   4: 
   5: This software is copyrighted work licensed under the terms of the
   6: Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
   7: details.  */
   8: 
   9: package gnu.awt.xlib;
  10: 
  11: import gnu.gcj.xlib.Display;
  12: import java.awt.AWTEvent;
  13: import java.awt.Component;
  14: import java.awt.Container;
  15: import java.awt.EventQueue;
  16: import java.awt.event.ComponentEvent;
  17: import java.awt.event.ContainerEvent;
  18: 
  19: /**
  20:  * The main difference here from a standard EventQueue is that the X
  21:  * display connection is flushed before waiting for more events.
  22:  */
  23: public class XEventQueue extends EventQueue
  24: {
  25:   Display display;
  26:   
  27:   public XEventQueue(Display display)
  28:   {
  29:     this.display = display;
  30:   }
  31:   
  32:   public AWTEvent getNextEvent() throws InterruptedException
  33:   {
  34:     if ((peekEvent() == null) && (display != null))
  35:       display.flush();
  36:     AWTEvent event = super.getNextEvent();
  37:     if (event != null)
  38:     {
  39:       switch (event.getID ())
  40:       {
  41:         case ContainerEvent.COMPONENT_ADDED:
  42:         {
  43:           /* If a component has been added to a container, it needs to be
  44:            * invalidated, to ensure that it ultimately gets an addNotify.
  45:            * If it's not invalidated, the component will never display in 
  46:            * an already-showing container (probably applies only to CardLayout).
  47:            * Perhaps this code should be in java.awt, but the problem only seems 
  48:            * to happen with xlib peers (not with gtk peers) so it's here instead.
  49:            */
  50:           ContainerEvent ce = (ContainerEvent)event;
  51:           ce.getChild ().invalidate ();
  52:           ce.getContainer ().validate ();
  53:         }
  54:         break;
  55: 
  56:         case ComponentEvent.COMPONENT_RESIZED:
  57:         {
  58:           ComponentEvent ce = (ComponentEvent)event;
  59:           // FIXME: there may be opportunities to coalesce resize events
  60:           ce.getComponent ().validate ();
  61:         }
  62:         break;
  63: 
  64:         case ComponentEvent.COMPONENT_SHOWN:
  65:         {
  66:           ComponentEvent ce = (ComponentEvent)event;
  67:           Component comp = ce.getComponent ();
  68:           if (!comp.isValid ())
  69:           {
  70:             /* Try to validate, going up the tree to the highest-level invalid
  71:              * Container.  The idea is to ensure that addNotify gets called for
  72:              * any non-top-level component being shown, to make it create a peer.
  73:              */
  74:             Container parent = comp.getParent ();
  75:             while (parent != null)
  76:             {
  77:               Container next = parent.getParent ();
  78:               if (next == null || next.isValid ())
  79:               {
  80:                 parent.validate ();
  81:                 break;
  82:               }
  83:               else
  84:                 parent = next;
  85:             }
  86:             if (comp instanceof Container)
  87:               comp.validate ();
  88:           }
  89:           comp.repaint ();
  90:         }
  91:         break;
  92:         
  93:         default:
  94:           break;
  95:       }
  96:     }
  97:     return event;
  98:   }
  99: }