AWTEvent

 

  1 package java.awt;
  2 import java.util.EventObject;
  3 import java.awt.event.*;
  4 import java.awt.peer.ComponentPeer;
  5 import java.awt.peer.LightweightPeer;
  6 import java.lang.reflect.Field;
  7 import sun.awt.AWTAccessor;
  8 public abstract class AWTEvent extends EventObject 
  9 {
 10     private byte bdata[];
 11     protected int id;
 12     protected boolean consumed = false;
 13     transient boolean focusManagerIsDispatching = false;
 14     transient boolean isPosted;
 15     private transient boolean isSystemGenerated;
 16     public final static long COMPONENT_EVENT_MASK = 0x01;
 17     public final static long CONTAINER_EVENT_MASK = 0x02;
 18     public final static long FOCUS_EVENT_MASK = 0x04;
 19     public final static long KEY_EVENT_MASK = 0x08;
 20     public final static long MOUSE_EVENT_MASK = 0x10;
 21     public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
 22     public final static long WINDOW_EVENT_MASK = 0x40;
 23     public final static long ACTION_EVENT_MASK = 0x80;
 24     public final static long ADJUSTMENT_EVENT_MASK = 0x100;
 25     public final static long ITEM_EVENT_MASK = 0x200;
 26     public final static long TEXT_EVENT_MASK = 0x400;
 27     public final static long INPUT_METHOD_EVENT_MASK = 0x800;
 28     final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
 29     public final static long PAINT_EVENT_MASK = 0x2000;
 30     public final static long INVOCATION_EVENT_MASK = 0x4000;
 31     public final static long HIERARCHY_EVENT_MASK = 0x8000;
 32     public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
 33     public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;
 34     public final static long WINDOW_STATE_EVENT_MASK = 0x40000;
 35     public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;
 36     public final static int RESERVED_ID_MAX = 1999;
 37     private static Field inputEvent_CanAccessSystemClipboard_Field = null;
 38     private static final long serialVersionUID = -1825314779160409405L;
 39     
 40     static 
 41     {
 42           Toolkit.loadLibraries();
 43         if (!GraphicsEnvironment.isHeadless()) initIDs();
 44         AWTAccessor.setAWTEventAccessor
 45         (
 46             new AWTAccessor.AWTEventAccessor() 
 47             {
 48                 public void setSystemGenerated(AWTEvent ev) 
 49                 {
 50                     ev.isSystemGenerated = true;
 51                 }
 52                 public boolean isSystemGenerated(AWTEvent ev) 
 53                 {
 54                     return ev.isSystemGenerated;
 55                 }
 56             }
 57           );
 58     }
 59     
 60     
 61     private static synchronized Field get_InputEvent_CanAccessSystemClipboard() 
 62     { 
 63         if (inputEvent_CanAccessSystemClipboard_Field == null) 
 64         { 
 65             inputEvent_CanAccessSystemClipboard_Field =(Field)java.security.AccessController.doPrivileged
 66                                                        (
 67                                                                                                 new java.security.PrivilegedAction() 
 68                                                                                                 {
 69                                                                                                 public Object run() 
 70                                                                                                 {
 71                                                                                                     Field field = null;
 72                                                                                                     try 
 73                                                                                                     {
 74                                                                                                         field = InputEvent.class.
 75                                                                                                             getDeclaredField("canAccessSystemClipboard");
 76                                                                                                         field.setAccessible(true);
 77                                                                                                         return field;
 78                                                                                                     } 
 79                                                                                                     catch (SecurityException e) 
 80                                                                                                     {
 81                                                                                                     } 
 82                                                                                                     catch (NoSuchFieldException e) 
 83                                                                                                     {
 84                                                                                                     }
 85                                                                                                     return null;
 86                                                                                                 }
 87                                                                                                  }
 88                                                                                              );
 89         }
 90         return inputEvent_CanAccessSystemClipboard_Field;
 91     }
 92     
 93     
 94     private static native void initIDs();
 95     
 96     
 97     public AWTEvent(Event event) 
 98     {
 99         this(event.target, event.id);
100     }
101     
102     
103     public AWTEvent(Object source, int id) 
104     {
105         super(source);
106           this.id = id;
107         switch(id) 
108         {
109           case ActionEvent.ACTION_PERFORMED:
110           case ItemEvent.ITEM_STATE_CHANGED:
111           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
112           case TextEvent.TEXT_VALUE_CHANGED:
113           consumed = true;
114           break;
115           default:
116         }
117     }
118     
119     
120     public int getID() 
121     {
122         return id;
123     }
124     
125     
126     public String toString() 
127     {
128         String srcName = null;
129         if (source instanceof Component) 
130         {
131             srcName = ((Component)source).getName();
132         } 
133         else if (source instanceof MenuComponent) 
134         {
135             srcName = ((MenuComponent)source).getName();
136         }
137           return getClass().getName() + "[" + paramString() + "] on " +(srcName != null? srcName : source);
138     }
139     
140     
141     public String paramString() 
142     {
143         return "";
144     }
145     
146     
147     protected void consume() 
148     {
149         switch(id) 
150         {
151           case KeyEvent.KEY_PRESSED:
152           case KeyEvent.KEY_RELEASED:
153           case MouseEvent.MOUSE_PRESSED:
154           case MouseEvent.MOUSE_RELEASED:
155           case MouseEvent.MOUSE_MOVED:
156           case MouseEvent.MOUSE_DRAGGED:
157           case MouseEvent.MOUSE_ENTERED:
158           case MouseEvent.MOUSE_EXITED:
159           case MouseEvent.MOUSE_WHEEL:
160           case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
161           case InputMethodEvent.CARET_POSITION_CHANGED:
162           consumed = true;
163           break;
164           default:
165         }
166     }
167     
168     
169     protected boolean isConsumed() 
170     {
171         return consumed;
172     }
173     
174     
175     Event convertToOld() 
176     {
177         Object src = getSource();
178         int newid = id;
179         switch(id) 
180         {
181           case KeyEvent.KEY_PRESSED:
182           case KeyEvent.KEY_RELEASED:
183           KeyEvent ke = (KeyEvent)this;
184           if (ke.isActionKey()) 
185           {
186               newid = (id == KeyEvent.KEY_PRESSED?Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
187           }
188           int keyCode = ke.getKeyCode();
189           if (keyCode == KeyEvent.VK_SHIFT ||keyCode == KeyEvent.VK_CONTROL ||keyCode == KeyEvent.VK_ALT) 
190           {
191               return null;
192           }
193           return new Event(src, ke.getWhen(), newid, 0, 0,Event.getOldEventKey(ke),(ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
194           case MouseEvent.MOUSE_PRESSED:
195           case MouseEvent.MOUSE_RELEASED:
196           case MouseEvent.MOUSE_MOVED:
197           case MouseEvent.MOUSE_DRAGGED:
198           case MouseEvent.MOUSE_ENTERED:
199           case MouseEvent.MOUSE_EXITED:
200           MouseEvent me = (MouseEvent)this;
201           Event olde = new Event(src, me.getWhen(), newid,me.getX(), me.getY(), 0,(me.getModifiers() & ~InputEvent.BUTTON1_MASK));
202           olde.clickCount = me.getClickCount();
203               return olde;
204           case FocusEvent.FOCUS_GAINED:
205               return new Event(src, Event.GOT_FOCUS, null);
206           case FocusEvent.FOCUS_LOST:
207               return new Event(src, Event.LOST_FOCUS, null);
208           case WindowEvent.WINDOW_CLOSING:
209           case WindowEvent.WINDOW_ICONIFIED:
210           case WindowEvent.WINDOW_DEICONIFIED:
211               return new Event(src, newid, null);
212           case ComponentEvent.COMPONENT_MOVED:
213                   if (src instanceof Frame || src instanceof Dialog) 
214                   {
215                         Point p = ((Component)src).getLocation();
216                         return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
217                   }
218                   break;
219           case ActionEvent.ACTION_PERFORMED:
220                   ActionEvent ae = (ActionEvent)this;
221                   String cmd;
222                   if (src instanceof Button) 
223                   {
224                       cmd = ((Button)src).getLabel();
225                   } 
226                   else if (src instanceof MenuItem) 
227                   {
228                       cmd = ((MenuItem)src).getLabel();
229                   } 
230                   else 
231                   {
232                       cmd = ae.getActionCommand();
233                   }
234                   return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
235                   case ItemEvent.ITEM_STATE_CHANGED:
236                   ItemEvent ie = (ItemEvent)this;
237                   Object arg;
238                   if (src instanceof List) 
239                   {
240                       newid = (ie.getStateChange() == ItemEvent.SELECTED?Event.LIST_SELECT : Event.LIST_DESELECT);
241                       arg = ie.getItem();
242                   } 
243                   else 
244                   {
245                       newid = Event.ACTION_EVENT;
246                       if (src instanceof Choice) 
247                       {
248                           arg = ie.getItem();
249                       } 
250                       else 
251                       {
252                           arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED);
253                       }
254                   }
255                   return new Event(src, newid, arg);
256           case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
257               AdjustmentEvent aje = (AdjustmentEvent)this;
258               switch(aje.getAdjustmentType()) 
259               {
260                 case AdjustmentEvent.UNIT_INCREMENT:
261                   newid = Event.SCROLL_LINE_DOWN;
262                   break;
263                 case AdjustmentEvent.UNIT_DECREMENT:
264                   newid = Event.SCROLL_LINE_UP;
265                   break;
266                 case AdjustmentEvent.BLOCK_INCREMENT:
267                   newid = Event.SCROLL_PAGE_DOWN;
268                   break;
269                 case AdjustmentEvent.BLOCK_DECREMENT:
270                   newid = Event.SCROLL_PAGE_UP;
271                   break;
272                 case AdjustmentEvent.TRACK:
273                                   if (aje.getValueIsAdjusting()) 
274                                   {
275                                       newid = Event.SCROLL_ABSOLUTE;
276                                   }
277                                   else 
278                                   {
279                                       newid = Event.SCROLL_END;
280                                   }
281                               break;
282                 default:
283                   return null;
284               }
285               return new Event(src, newid, Integer.valueOf(aje.getValue()));
286           default:
287         }
288         return null;
289     }
290     
291     
292     void copyPrivateDataInto(AWTEvent that) 
293     {
294         that.bdata = this.bdata;
295       if (this instanceof InputEvent && that instanceof InputEvent) 
296       {
297           Field field = get_InputEvent_CanAccessSystemClipboard();
298           if (field != null) 
299           { 
300               try 
301               {
302                   boolean b = field.getBoolean(this);
303                   field.setBoolean(that, b);
304               } 
305               catch(IllegalAccessException e) 
306               {
307               } 
308           }
309       }
310       that.isSystemGenerated = this.isSystemGenerated;
311     }
312     
313     
314     void dispatched() 
315     {
316         if (this instanceof InputEvent) 
317         {
318             Field field = get_InputEvent_CanAccessSystemClipboard();
319             if (field != null) 
320             { 
321                 try 
322                 {
323                     field.setBoolean(this, false);
324                 } 
325                 catch(IllegalAccessException e) 
326                 {
327                 } 
328             }
329         }
330     }            
331 }

 

package java.awt;
import java.util.EventObject;
import java.awt.event.*;
import java.awt.peer.ComponentPeer;
import java.awt.peer.LightweightPeer;
import java.lang.reflect.Field;
import sun.awt.AWTAccessor;
public abstract class AWTEvent extends EventObject
{
    private byte bdata[];
    protected int id;
    protected boolean consumed = false;
    transient boolean focusManagerIsDispatching = false;
    transient boolean isPosted;
    private transient boolean isSystemGenerated;
    public final static long COMPONENT_EVENT_MASK = 0x01;
    public final static long CONTAINER_EVENT_MASK = 0x02;
    public final static long FOCUS_EVENT_MASK = 0x04;
    public final static long KEY_EVENT_MASK = 0x08;
    public final static long MOUSE_EVENT_MASK = 0x10;
    public final static long MOUSE_MOTION_EVENT_MASK = 0x20;
    public final static long WINDOW_EVENT_MASK = 0x40;
    public final static long ACTION_EVENT_MASK = 0x80;
    public final static long ADJUSTMENT_EVENT_MASK = 0x100;
    public final static long ITEM_EVENT_MASK = 0x200;
    public final static long TEXT_EVENT_MASK = 0x400;
    public final static long INPUT_METHOD_EVENT_MASK = 0x800;
    final static long INPUT_METHODS_ENABLED_MASK = 0x1000;
    public final static long PAINT_EVENT_MASK = 0x2000;
    public final static long INVOCATION_EVENT_MASK = 0x4000;
    public final static long HIERARCHY_EVENT_MASK = 0x8000;
    public final static long HIERARCHY_BOUNDS_EVENT_MASK = 0x10000;
    public final static long MOUSE_WHEEL_EVENT_MASK = 0x20000;
    public final static long WINDOW_STATE_EVENT_MASK = 0x40000;
    public final static long WINDOW_FOCUS_EVENT_MASK = 0x80000;
    public final static int RESERVED_ID_MAX = 1999;
    private static Field inputEvent_CanAccessSystemClipboard_Field = null;
    private static final long serialVersionUID = -1825314779160409405L;
    
    static
    {
          Toolkit.loadLibraries();
        if (!GraphicsEnvironment.isHeadless()) initIDs();
        AWTAccessor.setAWTEventAccessor
        (
            new AWTAccessor.AWTEventAccessor()
            {
                public void setSystemGenerated(AWTEvent ev)
                {
                    ev.isSystemGenerated = true;
                }
                public boolean isSystemGenerated(AWTEvent ev)
                {
                    return ev.isSystemGenerated;
                }
            }
          );
    }
    
    
    private static synchronized Field get_InputEvent_CanAccessSystemClipboard()
    {
        if (inputEvent_CanAccessSystemClipboard_Field == null)
        {
            inputEvent_CanAccessSystemClipboard_Field =(Field)java.security.AccessController.doPrivileged
                                                       (
                                                                                                new java.security.PrivilegedAction()
                                                                                                {
                                                                                                public Object run()
                                                                                                {
                                                                                                    Field field = null;
                                                                                                    try
                                                                                                    {
                                                                                                        field = InputEvent.class.
                                                                                                            getDeclaredField("canAccessSystemClipboard");
                                                                                                        field.setAccessible(true);
                                                                                                        return field;
                                                                                                    }
                                                                                                    catch (SecurityException e)
                                                                                                    {
                                                                                                    }
                                                                                                    catch (NoSuchFieldException e)
                                                                                                    {
                                                                                                    }
                                                                                                    return null;
                                                                                                }
                                                                                                 }
                                                                                             );
        }
        return inputEvent_CanAccessSystemClipboard_Field;
    }
    
    
    private static native void initIDs();
    
    
    public AWTEvent(Event event)
    {
        this(event.target, event.id);
    }
    
    
    public AWTEvent(Object source, int id)
    {
        super(source);
          this.id = id;
        switch(id)
        {
          case ActionEvent.ACTION_PERFORMED:
          case ItemEvent.ITEM_STATE_CHANGED:
          case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
          case TextEvent.TEXT_VALUE_CHANGED:
          consumed = true;
          break;
          default:
        }
    }
    
    
    public int getID()
    {
        return id;
    }
    
    
    public String toString()
    {
        String srcName = null;
        if (source instanceof Component)
        {
            srcName = ((Component)source).getName();
        }
        else if (source instanceof MenuComponent)
        {
            srcName = ((MenuComponent)source).getName();
        }
          return getClass().getName() + "[" + paramString() + "] on " +(srcName != null? srcName : source);
    }
    
    
    public String paramString()
    {
        return "";
    }
    
    
    protected void consume()
    {
        switch(id)
        {
          case KeyEvent.KEY_PRESSED:
          case KeyEvent.KEY_RELEASED:
          case MouseEvent.MOUSE_PRESSED:
          case MouseEvent.MOUSE_RELEASED:
          case MouseEvent.MOUSE_MOVED:
          case MouseEvent.MOUSE_DRAGGED:
          case MouseEvent.MOUSE_ENTERED:
          case MouseEvent.MOUSE_EXITED:
          case MouseEvent.MOUSE_WHEEL:
          case InputMethodEvent.INPUT_METHOD_TEXT_CHANGED:
          case InputMethodEvent.CARET_POSITION_CHANGED:
          consumed = true;
          break;
          default:
        }
    }
    
    
    protected boolean isConsumed()
    {
        return consumed;
    }
    
    
    Event convertToOld()
    {
        Object src = getSource();
        int newid = id;
        switch(id)
        {
          case KeyEvent.KEY_PRESSED:
          case KeyEvent.KEY_RELEASED:
          KeyEvent ke = (KeyEvent)this;
          if (ke.isActionKey())
          {
              newid = (id == KeyEvent.KEY_PRESSED?Event.KEY_ACTION : Event.KEY_ACTION_RELEASE);
          }
          int keyCode = ke.getKeyCode();
          if (keyCode == KeyEvent.VK_SHIFT ||keyCode == KeyEvent.VK_CONTROL ||keyCode == KeyEvent.VK_ALT)
          {
              return null;
          }
          return new Event(src, ke.getWhen(), newid, 0, 0,Event.getOldEventKey(ke),(ke.getModifiers() & ~InputEvent.BUTTON1_MASK));
          case MouseEvent.MOUSE_PRESSED:
          case MouseEvent.MOUSE_RELEASED:
          case MouseEvent.MOUSE_MOVED:
          case MouseEvent.MOUSE_DRAGGED:
          case MouseEvent.MOUSE_ENTERED:
          case MouseEvent.MOUSE_EXITED:
          MouseEvent me = (MouseEvent)this;
          Event olde = new Event(src, me.getWhen(), newid,me.getX(), me.getY(), 0,(me.getModifiers() & ~InputEvent.BUTTON1_MASK));
          olde.clickCount = me.getClickCount();
              return olde;
          case FocusEvent.FOCUS_GAINED:
              return new Event(src, Event.GOT_FOCUS, null);
          case FocusEvent.FOCUS_LOST:
              return new Event(src, Event.LOST_FOCUS, null);
          case WindowEvent.WINDOW_CLOSING:
          case WindowEvent.WINDOW_ICONIFIED:
          case WindowEvent.WINDOW_DEICONIFIED:
              return new Event(src, newid, null);
          case ComponentEvent.COMPONENT_MOVED:
                  if (src instanceof Frame || src instanceof Dialog)
                  {
                        Point p = ((Component)src).getLocation();
                        return new Event(src, 0, Event.WINDOW_MOVED, p.x, p.y, 0, 0);
                  }
                  break;
          case ActionEvent.ACTION_PERFORMED:
                  ActionEvent ae = (ActionEvent)this;
                  String cmd;
                  if (src instanceof Button)
                  {
                      cmd = ((Button)src).getLabel();
                  }
                  else if (src instanceof MenuItem)
                  {
                      cmd = ((MenuItem)src).getLabel();
                  }
                  else
                  {
                      cmd = ae.getActionCommand();
                  }
                  return new Event(src, 0, newid, 0, 0, 0, ae.getModifiers(), cmd);
                  case ItemEvent.ITEM_STATE_CHANGED:
                  ItemEvent ie = (ItemEvent)this;
                  Object arg;
                  if (src instanceof List)
                  {
                      newid = (ie.getStateChange() == ItemEvent.SELECTED?Event.LIST_SELECT : Event.LIST_DESELECT);
                      arg = ie.getItem();
                  }
                  else
                  {
                      newid = Event.ACTION_EVENT;
                      if (src instanceof Choice)
                      {
                          arg = ie.getItem();
                      }
                      else
                      {
                          arg = Boolean.valueOf(ie.getStateChange() == ItemEvent.SELECTED);
                      }
                  }
                  return new Event(src, newid, arg);
          case AdjustmentEvent.ADJUSTMENT_VALUE_CHANGED:
              AdjustmentEvent aje = (AdjustmentEvent)this;
              switch(aje.getAdjustmentType())
              {
                case AdjustmentEvent.UNIT_INCREMENT:
                  newid = Event.SCROLL_LINE_DOWN;
                  break;
                case AdjustmentEvent.UNIT_DECREMENT:
                  newid = Event.SCROLL_LINE_UP;
                  break;
                case AdjustmentEvent.BLOCK_INCREMENT:
                  newid = Event.SCROLL_PAGE_DOWN;
                  break;
                case AdjustmentEvent.BLOCK_DECREMENT:
                  newid = Event.SCROLL_PAGE_UP;
                  break;
                case AdjustmentEvent.TRACK:
                                  if (aje.getValueIsAdjusting())
                                  {
                                      newid = Event.SCROLL_ABSOLUTE;
                                  }
                                  else
                                  {
                                      newid = Event.SCROLL_END;
                                  }
                              break;
                default:
                  return null;
              }
              return new Event(src, newid, Integer.valueOf(aje.getValue()));
          default:
        }
        return null;
    }
    
    
    void copyPrivateDataInto(AWTEvent that)
    {
        that.bdata = this.bdata;
      if (this instanceof InputEvent && that instanceof InputEvent)
      {
          Field field = get_InputEvent_CanAccessSystemClipboard();
          if (field != null)
          {
              try
              {
                  boolean b = field.getBoolean(this);
                  field.setBoolean(that, b);
              }
              catch(IllegalAccessException e)
              {
              }
          }
      }
      that.isSystemGenerated = this.isSystemGenerated;
    }
    
    
    void dispatched()
    {
        if (this instanceof InputEvent)
        {
            Field field = get_InputEvent_CanAccessSystemClipboard();
            if (field != null)
            {
                try
                {
                    field.setBoolean(this, false);
                }
                catch(IllegalAccessException e)
                {
                }
            }
        }
    }            
}

posted @ 2012-08-19 23:21  大头鬼  阅读(191)  评论(0编辑  收藏  举报