src/share/classes/java/awt/Container.java

Print this page




4410      */
4411     private static final int  LWD_MOUSE_DRAGGED_OVER = 1500;
4412 
4413     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.LightweightDispatcher");
4414 
4415     LightweightDispatcher(Container nativeContainer) {
4416         this.nativeContainer = nativeContainer;
4417         mouseEventTarget = null;
4418         eventMask = 0;
4419     }
4420 
4421     /*
4422      * Clean up any resources allocated when dispatcher was created;
4423      * should be called from Container.removeNotify
4424      */
4425     void dispose() {
4426         //System.out.println("Disposing lw dispatcher");
4427         stopListeningForOtherDrags();
4428         mouseEventTarget = null;
4429         targetLastEntered = null;

4430     }
4431 
4432     /**
4433      * Enables events to subcomponents.
4434      */
4435     void enableEvents(long events) {
4436         eventMask |= events;
4437     }
4438 
4439     /**
4440      * Dispatches an event to a sub-component if necessary, and
4441      * returns whether or not the event was forwarded to a
4442      * sub-component.
4443      *
4444      * @param e the event
4445      */
4446     boolean dispatchEvent(AWTEvent e) {
4447         boolean ret = false;
4448 
4449         /*


4600         Component mouseOver =   // not necessarily sensitive to mouse events
4601             nativeContainer.getDropTargetEventTarget(x, y,
4602                                                      Container.INCLUDE_SELF);
4603         trackMouseEnterExit(mouseOver, e);
4604 
4605         if (mouseOver != nativeContainer && mouseOver != null) {
4606             switch (id) {
4607             case SunDropTargetEvent.MOUSE_ENTERED:
4608             case SunDropTargetEvent.MOUSE_EXITED:
4609                 break;
4610             default:
4611                 retargetMouseEvent(mouseOver, id, e);
4612                 e.consume();
4613                 break;
4614             }
4615         }
4616         return e.isConsumed();
4617     }
4618 
4619     /*
4620      * Generates enter/exit events as mouse moves over lw components
4621      * @param targetOver        Target mouse is over (including native container)
4622      * @param e                 Mouse event in native container
4623      */
4624     private void trackMouseEnterExit(Component targetOver, MouseEvent e) {
4625         Component       targetEnter = null;
4626         int             id = e.getID();
4627 
4628         if (e instanceof SunDropTargetEvent &&
4629             id == MouseEvent.MOUSE_ENTERED &&
4630             isMouseInNativeContainer == true) {
4631             // This can happen if a lightweight component which initiated the
4632             // drag has an associated drop target. MOUSE_ENTERED comes when the
4633             // mouse is in the native container already. To propagate this event
4634             // properly we should null out targetLastEntered.
4635             targetLastEntered = null;
4636         } else if ( id != MouseEvent.MOUSE_EXITED &&






















4637              id != MouseEvent.MOUSE_DRAGGED &&
4638              id != LWD_MOUSE_DRAGGED_OVER &&
4639              isMouseInNativeContainer == false ) {
4640             // any event but an exit or drag means we're in the native container
4641             isMouseInNativeContainer = true;
4642             startListeningForOtherDrags();
4643         } else if ( id == MouseEvent.MOUSE_EXITED ) {
4644             isMouseInNativeContainer = false;
4645             stopListeningForOtherDrags();
4646         }
4647 
4648         if (isMouseInNativeContainer) {
4649             targetEnter = targetOver;
4650         }
4651 
4652         if (targetLastEntered == targetEnter) {
4653                 return;
4654         }


4655 
4656         if (targetLastEntered != null) {
4657             retargetMouseEvent(targetLastEntered, MouseEvent.MOUSE_EXITED, e);

4658         }
4659         if (id == MouseEvent.MOUSE_EXITED) {
4660             // consume native exit event if we generate one
4661             e.consume();
4662         }
4663 
4664         if (targetEnter != null) {
4665             retargetMouseEvent(targetEnter, MouseEvent.MOUSE_ENTERED, e);
4666         }
4667         if (id == MouseEvent.MOUSE_ENTERED) {
4668             // consume native enter event if we generate one
4669             e.consume();
4670         }
4671 
4672         targetLastEntered = targetEnter;
4673     }
4674 
4675     /*
4676      * Listens to global mouse drag events so even drags originating
4677      * from other heavyweight containers will generate enter/exit
4678      * events in this container
4679      */
4680     private void startListeningForOtherDrags() {
4681         //System.out.println("Adding AWTEventListener");
4682         java.security.AccessController.doPrivileged(
4683             new java.security.PrivilegedAction<Object>() {
4684                 public Object run() {
4685                     nativeContainer.getToolkit().addAWTEventListener(
4686                         LightweightDispatcher.this,
4687                         AWTEvent.MOUSE_EVENT_MASK |
4688                         AWTEvent.MOUSE_MOTION_EVENT_MASK);
4689                     return null;
4690                 }
4691             }
4692         );


4891     /**
4892      * The windowed container that might be hosting events for
4893      * subcomponents.
4894      */
4895     private Container nativeContainer;
4896 
4897     /**
4898      * This variable is not used, but kept for serialization compatibility
4899      */
4900     private Component focus;
4901 
4902     /**
4903      * The current subcomponent being hosted by this windowed
4904      * component that has events being forwarded to it.  If this
4905      * is null, there are currently no events being forwarded to
4906      * a subcomponent.
4907      */
4908     private transient Component mouseEventTarget;
4909 
4910     /**
4911      * The last component entered
4912      */
4913     private transient Component targetLastEntered;
4914 
4915     /**





4916      * Indicates whether {@code mouseEventTarget} was removed and nulled
4917      */
4918     private transient boolean isCleaned;
4919 
4920     /**
4921      * Is the mouse over the native container
4922      */
4923     private transient boolean isMouseInNativeContainer = false;
4924 
4925     /**





4926      * This variable is not used, but kept for serialization compatibility
4927      */
4928     private Cursor nativeCursor;
4929 
4930     /**
4931      * The event mask for contained lightweight components.  Lightweight
4932      * components need a windowed container to host window-related
4933      * events.  This separate mask indicates events that have been
4934      * requested by contained lightweight components without effecting
4935      * the mask of the windowed component itself.
4936      */
4937     private long eventMask;
4938 
4939     /**
4940      * The kind of events routed to lightweight components from windowed
4941      * hosts.
4942      */
4943     private static final long PROXY_EVENT_MASK =
4944         AWTEvent.FOCUS_EVENT_MASK |
4945         AWTEvent.KEY_EVENT_MASK |
4946         AWTEvent.MOUSE_EVENT_MASK |
4947         AWTEvent.MOUSE_MOTION_EVENT_MASK |
4948         AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4949 
4950     private static final long MOUSE_MASK =
4951         AWTEvent.MOUSE_EVENT_MASK |
4952         AWTEvent.MOUSE_MOTION_EVENT_MASK |
4953         AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4954 
4955     void removeReferences(Component removedComponent) {
4956         if (mouseEventTarget == removedComponent) {
4957             isCleaned = true;
4958             mouseEventTarget = null;
4959         }
4960         if (targetLastEntered == removedComponent) {
4961             targetLastEntered = null;



4962         }
4963     }
4964 }


4410      */
4411     private static final int  LWD_MOUSE_DRAGGED_OVER = 1500;
4412 
4413     private static final PlatformLogger eventLog = PlatformLogger.getLogger("java.awt.event.LightweightDispatcher");
4414 
4415     LightweightDispatcher(Container nativeContainer) {
4416         this.nativeContainer = nativeContainer;
4417         mouseEventTarget = null;
4418         eventMask = 0;
4419     }
4420 
4421     /*
4422      * Clean up any resources allocated when dispatcher was created;
4423      * should be called from Container.removeNotify
4424      */
4425     void dispose() {
4426         //System.out.println("Disposing lw dispatcher");
4427         stopListeningForOtherDrags();
4428         mouseEventTarget = null;
4429         targetLastEntered = null;
4430         targetLastEnteredDT = null;
4431     }
4432 
4433     /**
4434      * Enables events to subcomponents.
4435      */
4436     void enableEvents(long events) {
4437         eventMask |= events;
4438     }
4439 
4440     /**
4441      * Dispatches an event to a sub-component if necessary, and
4442      * returns whether or not the event was forwarded to a
4443      * sub-component.
4444      *
4445      * @param e the event
4446      */
4447     boolean dispatchEvent(AWTEvent e) {
4448         boolean ret = false;
4449 
4450         /*


4601         Component mouseOver =   // not necessarily sensitive to mouse events
4602             nativeContainer.getDropTargetEventTarget(x, y,
4603                                                      Container.INCLUDE_SELF);
4604         trackMouseEnterExit(mouseOver, e);
4605 
4606         if (mouseOver != nativeContainer && mouseOver != null) {
4607             switch (id) {
4608             case SunDropTargetEvent.MOUSE_ENTERED:
4609             case SunDropTargetEvent.MOUSE_EXITED:
4610                 break;
4611             default:
4612                 retargetMouseEvent(mouseOver, id, e);
4613                 e.consume();
4614                 break;
4615             }
4616         }
4617         return e.isConsumed();
4618     }
4619 
4620     /*
4621      * Generates dnd enter/exit events as mouse moves over lw components
4622      * @param targetOver       Target mouse is over (including native container)
4623      * @param e                SunDropTarget mouse event in native container
4624      */
4625     private void trackMouseDTEnterExit(Component targetOver, MouseEvent e) {

4626         int id = e.getID();
4627         if (id == MouseEvent.MOUSE_ENTERED && isMouseDTInNativeContainer) {



4628             // This can happen if a lightweight component which initiated the
4629             // drag has an associated drop target. MOUSE_ENTERED comes when the
4630             // mouse is in the native container already. To propagate this event
4631             // properly we should null out targetLastEntered.
4632             targetLastEnteredDT = null;
4633         } else if (id == MouseEvent.MOUSE_ENTERED) {
4634             isMouseDTInNativeContainer = true;
4635         } else if (id == MouseEvent.MOUSE_EXITED) {
4636             isMouseDTInNativeContainer = false;
4637         }
4638         targetLastEnteredDT = retargetMouseEnterExit(targetOver, e,
4639                                                      targetLastEnteredDT,
4640                                                      isMouseDTInNativeContainer);
4641     }
4642 
4643     /*
4644      * Generates enter/exit events as mouse moves over lw components
4645      * @param targetOver        Target mouse is over (including native container)
4646      * @param e                 Mouse event in native container
4647      */
4648     private void trackMouseEnterExit(Component targetOver, MouseEvent e) {
4649         if (e instanceof SunDropTargetEvent) {
4650             trackMouseDTEnterExit(targetOver, e);
4651             return;
4652         }
4653         int id = e.getID();
4654 
4655         if ( id != MouseEvent.MOUSE_EXITED &&
4656              id != MouseEvent.MOUSE_DRAGGED &&
4657              id != LWD_MOUSE_DRAGGED_OVER &&
4658                 !isMouseInNativeContainer) {
4659             // any event but an exit or drag means we're in the native container
4660             isMouseInNativeContainer = true;
4661             startListeningForOtherDrags();
4662         } else if (id == MouseEvent.MOUSE_EXITED) {
4663             isMouseInNativeContainer = false;
4664             stopListeningForOtherDrags();
4665         }
4666         targetLastEntered = retargetMouseEnterExit(targetOver, e,
4667                                                    targetLastEntered,
4668                                                    isMouseInNativeContainer);
4669     }
4670 
4671     private Component retargetMouseEnterExit(Component targetOver, MouseEvent e,
4672                                              Component lastEntered,
4673                                              boolean inNativeContainer) {
4674         int id = e.getID();
4675         Component targetEnter = inNativeContainer ? targetOver : null;
4676 
4677         if (lastEntered != targetEnter) {
4678             if (lastEntered != null) {
4679                 retargetMouseEvent(lastEntered, MouseEvent.MOUSE_EXITED, e);
4680             }
4681             if (id == MouseEvent.MOUSE_EXITED) {
4682                 // consume native exit event if we generate one
4683                 e.consume();
4684             }
4685 
4686             if (targetEnter != null) {
4687                 retargetMouseEvent(targetEnter, MouseEvent.MOUSE_ENTERED, e);
4688             }
4689             if (id == MouseEvent.MOUSE_ENTERED) {
4690                 // consume native enter event if we generate one
4691                 e.consume();
4692             }
4693         }
4694         return targetEnter;
4695     }
4696 
4697     /*
4698      * Listens to global mouse drag events so even drags originating
4699      * from other heavyweight containers will generate enter/exit
4700      * events in this container
4701      */
4702     private void startListeningForOtherDrags() {
4703         //System.out.println("Adding AWTEventListener");
4704         java.security.AccessController.doPrivileged(
4705             new java.security.PrivilegedAction<Object>() {
4706                 public Object run() {
4707                     nativeContainer.getToolkit().addAWTEventListener(
4708                         LightweightDispatcher.this,
4709                         AWTEvent.MOUSE_EVENT_MASK |
4710                         AWTEvent.MOUSE_MOTION_EVENT_MASK);
4711                     return null;
4712                 }
4713             }
4714         );


4913     /**
4914      * The windowed container that might be hosting events for
4915      * subcomponents.
4916      */
4917     private Container nativeContainer;
4918 
4919     /**
4920      * This variable is not used, but kept for serialization compatibility
4921      */
4922     private Component focus;
4923 
4924     /**
4925      * The current subcomponent being hosted by this windowed
4926      * component that has events being forwarded to it.  If this
4927      * is null, there are currently no events being forwarded to
4928      * a subcomponent.
4929      */
4930     private transient Component mouseEventTarget;
4931 
4932     /**
4933      * The last component entered by the {@code MouseEvent}.
4934      */
4935     private transient Component targetLastEntered;
4936 
4937     /**
4938      * The last component entered by the {@code SunDropTargetEvent}.
4939      */
4940     private transient Component targetLastEnteredDT;
4941 
4942     /**
4943      * Indicates whether {@code mouseEventTarget} was removed and nulled
4944      */
4945     private transient boolean isCleaned;
4946 
4947     /**
4948      * Is the mouse over the native container.
4949      */
4950     private transient boolean isMouseInNativeContainer = false;
4951 
4952     /**
4953      * Is DnD over the native container.
4954      */
4955     private transient boolean isMouseDTInNativeContainer = false;
4956 
4957     /**
4958      * This variable is not used, but kept for serialization compatibility
4959      */
4960     private Cursor nativeCursor;
4961 
4962     /**
4963      * The event mask for contained lightweight components.  Lightweight
4964      * components need a windowed container to host window-related
4965      * events.  This separate mask indicates events that have been
4966      * requested by contained lightweight components without effecting
4967      * the mask of the windowed component itself.
4968      */
4969     private long eventMask;
4970 
4971     /**
4972      * The kind of events routed to lightweight components from windowed
4973      * hosts.
4974      */
4975     private static final long PROXY_EVENT_MASK =
4976         AWTEvent.FOCUS_EVENT_MASK |
4977         AWTEvent.KEY_EVENT_MASK |
4978         AWTEvent.MOUSE_EVENT_MASK |
4979         AWTEvent.MOUSE_MOTION_EVENT_MASK |
4980         AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4981 
4982     private static final long MOUSE_MASK =
4983         AWTEvent.MOUSE_EVENT_MASK |
4984         AWTEvent.MOUSE_MOTION_EVENT_MASK |
4985         AWTEvent.MOUSE_WHEEL_EVENT_MASK;
4986 
4987     void removeReferences(Component removedComponent) {
4988         if (mouseEventTarget == removedComponent) {
4989             isCleaned = true;
4990             mouseEventTarget = null;
4991         }
4992         if (targetLastEntered == removedComponent) {
4993             targetLastEntered = null;
4994         }
4995         if (targetLastEnteredDT == removedComponent) {
4996             targetLastEnteredDT = null;
4997         }
4998     }
4999 }