src/share/classes/java/awt/Component.java

Print this page




  50 import java.beans.PropertyChangeListener;
  51 import java.beans.PropertyChangeSupport;
  52 import java.beans.Transient;
  53 import java.awt.im.InputContext;
  54 import java.awt.im.InputMethodRequests;
  55 import java.awt.dnd.DropTarget;
  56 import java.lang.reflect.InvocationTargetException;
  57 import java.lang.reflect.Method;
  58 import java.security.AccessController;
  59 import java.security.PrivilegedAction;
  60 import java.security.AccessControlContext;
  61 import javax.accessibility.*;
  62 import java.applet.Applet;
  63 
  64 import sun.security.action.GetPropertyAction;
  65 import sun.awt.AppContext;
  66 import sun.awt.AWTAccessor;
  67 import sun.awt.ConstrainableGraphics;
  68 import sun.awt.SubRegionShowable;
  69 import sun.awt.SunToolkit;
  70 import sun.awt.WindowClosingListener;
  71 import sun.awt.CausedFocusEvent;
  72 import sun.awt.EmbeddedFrame;
  73 import sun.awt.dnd.SunDropTargetEvent;
  74 import sun.awt.im.CompositionArea;
  75 import sun.font.FontManager;
  76 import sun.font.FontManagerFactory;
  77 import sun.font.SunFontManager;
  78 import sun.java2d.SunGraphics2D;
  79 import sun.java2d.pipe.Region;
  80 import sun.awt.image.VSyncedBSManager;
  81 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  82 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
  83 import sun.awt.RequestFocusController;
  84 import sun.java2d.SunGraphicsEnvironment;
  85 import sun.util.logging.PlatformLogger;
  86 
  87 /**
  88  * A <em>component</em> is an object having a graphical representation
  89  * that can be displayed on the screen and that can interact with the
  90  * user. Examples of components are the buttons, checkboxes, and scrollbars


 527      * one of the event types enabled for the component.
 528      * It will then allow for normal processing to
 529      * continue.  If it is false the event is passed
 530      * to the component's parent and up the ancestor
 531      * tree until the event has been consumed.
 532      *
 533      * @serial
 534      * @see #dispatchEvent
 535      */
 536     boolean newEventsOnly = false;
 537     transient ComponentListener componentListener;
 538     transient FocusListener focusListener;
 539     transient HierarchyListener hierarchyListener;
 540     transient HierarchyBoundsListener hierarchyBoundsListener;
 541     transient KeyListener keyListener;
 542     transient MouseListener mouseListener;
 543     transient MouseMotionListener mouseMotionListener;
 544     transient MouseWheelListener mouseWheelListener;
 545     transient InputMethodListener inputMethodListener;
 546 
 547     transient RuntimeException windowClosingException = null;
 548 
 549     /** Internal, constants for serialization */
 550     final static String actionListenerK = "actionL";
 551     final static String adjustmentListenerK = "adjustmentL";
 552     final static String componentListenerK = "componentL";
 553     final static String containerListenerK = "containerL";
 554     final static String focusListenerK = "focusL";
 555     final static String itemListenerK = "itemL";
 556     final static String keyListenerK = "keyL";
 557     final static String mouseListenerK = "mouseL";
 558     final static String mouseMotionListenerK = "mouseMotionL";
 559     final static String mouseWheelListenerK = "mouseWheelL";
 560     final static String textListenerK = "textL";
 561     final static String ownedWindowK = "ownedL";
 562     final static String windowListenerK = "windowL";
 563     final static String inputMethodListenerK = "inputMethodL";
 564     final static String hierarchyListenerK = "hierarchyL";
 565     final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
 566     final static String windowStateListenerK = "windowStateL";
 567     final static String windowFocusListenerK = "windowFocusL";
 568 


4849         switch(id) {
4850             // Handling of the PAINT and UPDATE events is now done in the
4851             // peer's handleEvent() method so the background can be cleared
4852             // selectively for non-native components on Windows only.
4853             // - Fred.Ecks@Eng.sun.com, 5-8-98
4854 
4855           case KeyEvent.KEY_PRESSED:
4856           case KeyEvent.KEY_RELEASED:
4857               Container p = (Container)((this instanceof Container) ? this : parent);
4858               if (p != null) {
4859                   p.preProcessKeyEvent((KeyEvent)e);
4860                   if (e.isConsumed()) {
4861                         if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
4862                             focusLog.finest("Pre-process consumed event");
4863                         }
4864                       return;
4865                   }
4866               }
4867               break;
4868 
4869           case WindowEvent.WINDOW_CLOSING:
4870               if (toolkit instanceof WindowClosingListener) {
4871                   windowClosingException = ((WindowClosingListener)
4872                                             toolkit).windowClosingNotify((WindowEvent)e);
4873                   if (checkWindowClosingException()) {
4874                       return;
4875                   }
4876               }
4877               break;
4878 
4879           default:
4880               break;
4881         }
4882 
4883         /*
4884          * 6. Deliver event for normal processing
4885          */
4886         if (newEventsOnly) {
4887             // Filtering needs to really be moved to happen at a lower
4888             // level in order to get maximum performance gain;  it is
4889             // here temporarily to ensure the API spec is honored.
4890             //
4891             if (eventEnabled(e)) {
4892                 processEvent(e);
4893             }
4894         } else if (id == MouseEvent.MOUSE_WHEEL) {
4895             // newEventsOnly will be false for a listenerless ScrollPane, but
4896             // MouseWheelEvents still need to be dispatched to it so scrolling
4897             // can be done.
4898             autoProcessMouseWheel((MouseWheelEvent)e);


4914                 //
4915                 switch(olde.id) {
4916                   case Event.KEY_PRESS:
4917                   case Event.KEY_RELEASE:
4918                   case Event.KEY_ACTION:
4919                   case Event.KEY_ACTION_RELEASE:
4920                       if (olde.key != key) {
4921                           ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
4922                       }
4923                       if (olde.modifiers != modifiers) {
4924                           ((KeyEvent)e).setModifiers(olde.modifiers);
4925                       }
4926                       break;
4927                   default:
4928                       break;
4929                 }
4930             }
4931         }
4932 
4933         /*
4934          * 8. Special handling for 4061116 : Hook for browser to close modal
4935          *    dialogs.
4936          */
4937         if (id == WindowEvent.WINDOW_CLOSING && !e.isConsumed()) {
4938             if (toolkit instanceof WindowClosingListener) {
4939                 windowClosingException =
4940                     ((WindowClosingListener)toolkit).
4941                     windowClosingDelivered((WindowEvent)e);
4942                 if (checkWindowClosingException()) {
4943                     return;
4944                 }
4945             }
4946         }
4947 
4948         /*
4949          * 9. Allow the peer to process the event.
4950          * Except KeyEvents, they will be processed by peer after
4951          * all KeyEventPostProcessors
4952          * (see DefaultKeyboardFocusManager.dispatchKeyEvent())
4953          */
4954         if (!(e instanceof KeyEvent)) {
4955             ComponentPeer tpeer = peer;
4956             if (e instanceof FocusEvent && (tpeer == null || tpeer instanceof LightweightPeer)) {
4957                 // if focus owner is lightweight then its native container
4958                 // processes event
4959                 Component source = (Component)e.getSource();
4960                 if (source != null) {
4961                     Container target = source.getNativeContainer();
4962                     if (target != null) {
4963                         tpeer = target.getPeer();
4964                     }
4965                 }
4966             }
4967             if (tpeer != null) {
4968                 tpeer.handleEvent(e);


5035                                              e.getScrollType(),
5036                                              e.getScrollAmount(),
5037                                              e.getWheelRotation(),
5038                                              e.getPreciseWheelRotation());
5039                 ((AWTEvent)e).copyPrivateDataInto(newMWE);
5040                 // When dispatching a wheel event to
5041                 // ancestor, there is no need trying to find descendant
5042                 // lightweights to dispatch event to.
5043                 // If we dispatch the event to toplevel ancestor,
5044                 // this could encolse the loop: 6480024.
5045                 anc.dispatchEventToSelf(newMWE);
5046                 if (newMWE.isConsumed()) {
5047                     e.consume();
5048                 }
5049                 return true;
5050             }
5051         }
5052         return false;
5053     }
5054 
5055     boolean checkWindowClosingException() {
5056         if (windowClosingException != null) {
5057             if (this instanceof Dialog) {
5058                 ((Dialog)this).interruptBlocking();
5059             } else {
5060                 windowClosingException.fillInStackTrace();
5061                 windowClosingException.printStackTrace();
5062                 windowClosingException = null;
5063             }
5064             return true;
5065         }
5066         return false;
5067     }
5068 
5069     boolean areInputMethodsEnabled() {
5070         // in 1.2, we assume input method support is required for all
5071         // components that handle key events, but components can turn off
5072         // input methods by calling enableInputMethods(false).
5073         return ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) &&
5074             ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null);
5075     }
5076 
5077     // REMIND: remove when filtering is handled at lower level
5078     boolean eventEnabled(AWTEvent e) {
5079         return eventTypeEnabled(e.id);
5080     }
5081 
5082     boolean eventTypeEnabled(int type) {
5083         switch(type) {
5084           case ComponentEvent.COMPONENT_MOVED:
5085           case ComponentEvent.COMPONENT_RESIZED:
5086           case ComponentEvent.COMPONENT_SHOWN:
5087           case ComponentEvent.COMPONENT_HIDDEN:
5088               if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||




  50 import java.beans.PropertyChangeListener;
  51 import java.beans.PropertyChangeSupport;
  52 import java.beans.Transient;
  53 import java.awt.im.InputContext;
  54 import java.awt.im.InputMethodRequests;
  55 import java.awt.dnd.DropTarget;
  56 import java.lang.reflect.InvocationTargetException;
  57 import java.lang.reflect.Method;
  58 import java.security.AccessController;
  59 import java.security.PrivilegedAction;
  60 import java.security.AccessControlContext;
  61 import javax.accessibility.*;
  62 import java.applet.Applet;
  63 
  64 import sun.security.action.GetPropertyAction;
  65 import sun.awt.AppContext;
  66 import sun.awt.AWTAccessor;
  67 import sun.awt.ConstrainableGraphics;
  68 import sun.awt.SubRegionShowable;
  69 import sun.awt.SunToolkit;

  70 import sun.awt.CausedFocusEvent;
  71 import sun.awt.EmbeddedFrame;
  72 import sun.awt.dnd.SunDropTargetEvent;
  73 import sun.awt.im.CompositionArea;
  74 import sun.font.FontManager;
  75 import sun.font.FontManagerFactory;
  76 import sun.font.SunFontManager;
  77 import sun.java2d.SunGraphics2D;
  78 import sun.java2d.pipe.Region;
  79 import sun.awt.image.VSyncedBSManager;
  80 import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
  81 import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*;
  82 import sun.awt.RequestFocusController;
  83 import sun.java2d.SunGraphicsEnvironment;
  84 import sun.util.logging.PlatformLogger;
  85 
  86 /**
  87  * A <em>component</em> is an object having a graphical representation
  88  * that can be displayed on the screen and that can interact with the
  89  * user. Examples of components are the buttons, checkboxes, and scrollbars


 526      * one of the event types enabled for the component.
 527      * It will then allow for normal processing to
 528      * continue.  If it is false the event is passed
 529      * to the component's parent and up the ancestor
 530      * tree until the event has been consumed.
 531      *
 532      * @serial
 533      * @see #dispatchEvent
 534      */
 535     boolean newEventsOnly = false;
 536     transient ComponentListener componentListener;
 537     transient FocusListener focusListener;
 538     transient HierarchyListener hierarchyListener;
 539     transient HierarchyBoundsListener hierarchyBoundsListener;
 540     transient KeyListener keyListener;
 541     transient MouseListener mouseListener;
 542     transient MouseMotionListener mouseMotionListener;
 543     transient MouseWheelListener mouseWheelListener;
 544     transient InputMethodListener inputMethodListener;
 545 


 546     /** Internal, constants for serialization */
 547     final static String actionListenerK = "actionL";
 548     final static String adjustmentListenerK = "adjustmentL";
 549     final static String componentListenerK = "componentL";
 550     final static String containerListenerK = "containerL";
 551     final static String focusListenerK = "focusL";
 552     final static String itemListenerK = "itemL";
 553     final static String keyListenerK = "keyL";
 554     final static String mouseListenerK = "mouseL";
 555     final static String mouseMotionListenerK = "mouseMotionL";
 556     final static String mouseWheelListenerK = "mouseWheelL";
 557     final static String textListenerK = "textL";
 558     final static String ownedWindowK = "ownedL";
 559     final static String windowListenerK = "windowL";
 560     final static String inputMethodListenerK = "inputMethodL";
 561     final static String hierarchyListenerK = "hierarchyL";
 562     final static String hierarchyBoundsListenerK = "hierarchyBoundsL";
 563     final static String windowStateListenerK = "windowStateL";
 564     final static String windowFocusListenerK = "windowFocusL";
 565 


4846         switch(id) {
4847             // Handling of the PAINT and UPDATE events is now done in the
4848             // peer's handleEvent() method so the background can be cleared
4849             // selectively for non-native components on Windows only.
4850             // - Fred.Ecks@Eng.sun.com, 5-8-98
4851 
4852           case KeyEvent.KEY_PRESSED:
4853           case KeyEvent.KEY_RELEASED:
4854               Container p = (Container)((this instanceof Container) ? this : parent);
4855               if (p != null) {
4856                   p.preProcessKeyEvent((KeyEvent)e);
4857                   if (e.isConsumed()) {
4858                         if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
4859                             focusLog.finest("Pre-process consumed event");
4860                         }
4861                       return;
4862                   }
4863               }
4864               break;
4865 










4866           default:
4867               break;
4868         }
4869 
4870         /*
4871          * 6. Deliver event for normal processing
4872          */
4873         if (newEventsOnly) {
4874             // Filtering needs to really be moved to happen at a lower
4875             // level in order to get maximum performance gain;  it is
4876             // here temporarily to ensure the API spec is honored.
4877             //
4878             if (eventEnabled(e)) {
4879                 processEvent(e);
4880             }
4881         } else if (id == MouseEvent.MOUSE_WHEEL) {
4882             // newEventsOnly will be false for a listenerless ScrollPane, but
4883             // MouseWheelEvents still need to be dispatched to it so scrolling
4884             // can be done.
4885             autoProcessMouseWheel((MouseWheelEvent)e);


4901                 //
4902                 switch(olde.id) {
4903                   case Event.KEY_PRESS:
4904                   case Event.KEY_RELEASE:
4905                   case Event.KEY_ACTION:
4906                   case Event.KEY_ACTION_RELEASE:
4907                       if (olde.key != key) {
4908                           ((KeyEvent)e).setKeyChar(olde.getKeyEventChar());
4909                       }
4910                       if (olde.modifiers != modifiers) {
4911                           ((KeyEvent)e).setModifiers(olde.modifiers);
4912                       }
4913                       break;
4914                   default:
4915                       break;
4916                 }
4917             }
4918         }
4919 
4920         /*















4921          * 9. Allow the peer to process the event.
4922          * Except KeyEvents, they will be processed by peer after
4923          * all KeyEventPostProcessors
4924          * (see DefaultKeyboardFocusManager.dispatchKeyEvent())
4925          */
4926         if (!(e instanceof KeyEvent)) {
4927             ComponentPeer tpeer = peer;
4928             if (e instanceof FocusEvent && (tpeer == null || tpeer instanceof LightweightPeer)) {
4929                 // if focus owner is lightweight then its native container
4930                 // processes event
4931                 Component source = (Component)e.getSource();
4932                 if (source != null) {
4933                     Container target = source.getNativeContainer();
4934                     if (target != null) {
4935                         tpeer = target.getPeer();
4936                     }
4937                 }
4938             }
4939             if (tpeer != null) {
4940                 tpeer.handleEvent(e);


5007                                              e.getScrollType(),
5008                                              e.getScrollAmount(),
5009                                              e.getWheelRotation(),
5010                                              e.getPreciseWheelRotation());
5011                 ((AWTEvent)e).copyPrivateDataInto(newMWE);
5012                 // When dispatching a wheel event to
5013                 // ancestor, there is no need trying to find descendant
5014                 // lightweights to dispatch event to.
5015                 // If we dispatch the event to toplevel ancestor,
5016                 // this could encolse the loop: 6480024.
5017                 anc.dispatchEventToSelf(newMWE);
5018                 if (newMWE.isConsumed()) {
5019                     e.consume();
5020                 }
5021                 return true;
5022             }
5023         }
5024         return false;
5025     }
5026 














5027     boolean areInputMethodsEnabled() {
5028         // in 1.2, we assume input method support is required for all
5029         // components that handle key events, but components can turn off
5030         // input methods by calling enableInputMethods(false).
5031         return ((eventMask & AWTEvent.INPUT_METHODS_ENABLED_MASK) != 0) &&
5032             ((eventMask & AWTEvent.KEY_EVENT_MASK) != 0 || keyListener != null);
5033     }
5034 
5035     // REMIND: remove when filtering is handled at lower level
5036     boolean eventEnabled(AWTEvent e) {
5037         return eventTypeEnabled(e.id);
5038     }
5039 
5040     boolean eventTypeEnabled(int type) {
5041         switch(type) {
5042           case ComponentEvent.COMPONENT_MOVED:
5043           case ComponentEvent.COMPONENT_RESIZED:
5044           case ComponentEvent.COMPONENT_SHOWN:
5045           case ComponentEvent.COMPONENT_HIDDEN:
5046               if ((eventMask & AWTEvent.COMPONENT_EVENT_MASK) != 0 ||