home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Event.java < prev    next >
Text File  |  1996-05-03  |  12KB  |  498 lines

  1. /*
  2.  * @(#)Event.java    1.36 96/03/28 Arthur van Hoff
  3.  *
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19. package java.awt;
  20.  
  21. import java.io.*;
  22.  
  23. /**
  24.  * Event is a platform-independent class that encapsulates events from
  25.  * the local Graphical User Interface(GUI) platform.<p>
  26.  *
  27.  * The event contains an <a href="#id"> id</a> which indicates the type
  28.  * of event it is and which other Event variables are relavent for the event.
  29.  * For keyboard events, <a href="#key"> key</a> will contain a value indicating
  30.  * the key that was activated and <a href="#modifiers"> modifiers</a> will
  31.  * contain the modifiers.  For KEY_PRESS and KEY_RELEASE event ids, the value of key
  32.  * will be the unicode character code for the key; for KEY_ACTION and KEY_ACTION_RELEASE, 
  33.  * the value of key will be one of the defined action-key identifiers in the Event 
  34.  * class (PGUP, PGDN, F1, F2, etc).
  35.  *
  36.  * @version 1.36 28 Mar 1996
  37.  * @author Sami Shaio
  38.  */
  39. public class Event {
  40.     private int data;
  41.  
  42.     /* Modifier constants */
  43.  
  44.     /**
  45.      * The shift modifier constant.
  46.      */
  47.     public static final int SHIFT_MASK         = 1 << 0;
  48.  
  49.     /**
  50.      * The control modifier constant.
  51.      */
  52.     public static final int CTRL_MASK         = 1 << 1;
  53.  
  54.     /** 
  55.      * The meta modifier constant.
  56.      */
  57.     public static final int META_MASK         = 1 << 2;
  58.  
  59.     /** 
  60.      * The alt modifier constant.
  61.      */
  62.     public static final int ALT_MASK         = 1 << 3;
  63.  
  64.     /* Action keys */
  65.     /** 
  66.      * The home action-key.
  67.      */
  68.     public static final int HOME         = 1000;
  69.  
  70.     /** 
  71.      * The end action-key. 
  72.      */
  73.     public static final int END         = 1001;
  74.  
  75.     /**
  76.      * The page up action-key.
  77.      */
  78.     public static final int PGUP         = 1002;
  79.  
  80.     /**
  81.      * The page down action-key.
  82.      */
  83.     public static final int PGDN         = 1003;
  84.  
  85.     /**
  86.      * The up arrow action-key.
  87.      */
  88.     public static final int UP             = 1004;
  89.  
  90.     /**
  91.      * The down arrow action-key.
  92.      */
  93.     public static final int DOWN         = 1005;
  94.  
  95.     /**
  96.      * The left arrow action-key.
  97.      */
  98.     public static final int LEFT         = 1006;
  99.  
  100.     /**
  101.      * The right arrow action-key.
  102.      */
  103.     public static final int RIGHT         = 1007;
  104.  
  105.     /**
  106.      * The F1 function action-key.
  107.      */
  108.     public static final int F1            = 1008;
  109.  
  110.     /**
  111.      * The F2 function action-key.
  112.      */
  113.     public static final int F2            = 1009;
  114.  
  115.     /**
  116.      * The F3 function action-key.
  117.      */
  118.     public static final int F3            = 1010;
  119.  
  120.     /**
  121.      * The F4 function action-key.
  122.      */
  123.     public static final int F4            = 1011;
  124.  
  125.     /**
  126.      * The F5 function action-key.
  127.      */
  128.     public static final int F5            = 1012;
  129.  
  130.     /**
  131.      * The F6 function action-key.
  132.      */
  133.     public static final int F6            = 1013;
  134.  
  135.     /**
  136.      * The F7 function action-key.
  137.      */
  138.     public static final int F7            = 1014;
  139.  
  140.     /**
  141.      * The F8 function action-key.
  142.      */
  143.     public static final int F8            = 1015;
  144.  
  145.     /**
  146.      * The F9 function action-key.
  147.      */
  148.     public static final int F9            = 1016;
  149.  
  150.     /**
  151.      * The F10 function action-key.
  152.      */
  153.     public static final int F10            = 1017;
  154.  
  155.     /**
  156.      * The F11 function action-key.
  157.      */
  158.     public static final int F11            = 1018;
  159.  
  160.     /**
  161.      * The F12 function action-key.
  162.      */
  163.     public static final int F12            = 1019;
  164.  
  165.  
  166.     /* Base for all window events. */
  167.     private static final int WINDOW_EVENT     = 200;
  168.  
  169.     /**
  170.      * The destroy window event.
  171.      */
  172.     public static final int WINDOW_DESTROY     = 1 + WINDOW_EVENT;
  173.  
  174.     /**
  175.      * The expose window event. 
  176.      */
  177.     public static final int WINDOW_EXPOSE     = 2 + WINDOW_EVENT;
  178.  
  179.     /** 
  180.      * The iconify window event. 
  181.      */
  182.     public static final int WINDOW_ICONIFY    = 3 + WINDOW_EVENT;
  183.  
  184.     /** 
  185.      * The de-iconify window event.
  186.      */
  187.     public static final int WINDOW_DEICONIFY    = 4 + WINDOW_EVENT;
  188.  
  189.     /**
  190.      * The move window event.
  191.      */
  192.     public static final int WINDOW_MOVED    = 5 + WINDOW_EVENT;
  193.  
  194.     /* Base for all keyboard events. */
  195.     private static final int KEY_EVENT         = 400;
  196.  
  197.     /**
  198.      * The key press keyboard event.
  199.      */
  200.     public static final int KEY_PRESS         = 1 + KEY_EVENT;
  201.  
  202.     /**
  203.      * The key release keyboard event.
  204.      */
  205.     public static final int KEY_RELEASE     = 2 + KEY_EVENT;
  206.  
  207.     /** 
  208.      * The action-key press keyboard event. 
  209.      */
  210.     public static final int KEY_ACTION         = 3 + KEY_EVENT;
  211.  
  212.     /** 
  213.      * The action-key release keyboard event. 
  214.      */
  215.     public static final int KEY_ACTION_RELEASE    = 4 + KEY_EVENT;
  216.  
  217.     /* Base for all mouse events. */
  218.     private static final int MOUSE_EVENT     = 500;
  219.  
  220.     /**
  221.      * The mouse down event.
  222.      */
  223.     public static final int MOUSE_DOWN         = 1 + MOUSE_EVENT;
  224.  
  225.     /**
  226.      *  The mouse up event.
  227.      */
  228.     public static final int MOUSE_UP         = 2 + MOUSE_EVENT;
  229.  
  230.     /**
  231.      * The mouse move event.
  232.      */
  233.     public static final int MOUSE_MOVE         = 3 + MOUSE_EVENT;
  234.  
  235.     /**
  236.      * The mouse enter event.
  237.      */
  238.     public static final int MOUSE_ENTER     = 4 + MOUSE_EVENT;
  239.  
  240.     /**
  241.      * The mouse exit event.
  242.      */
  243.     public static final int MOUSE_EXIT         = 5 + MOUSE_EVENT;
  244.  
  245.     /** 
  246.      * The mouse drag event.
  247.      */
  248.     public static final int MOUSE_DRAG         = 6 + MOUSE_EVENT;
  249.  
  250.  
  251.     /* Scrolling events */
  252.     private static final int SCROLL_EVENT     = 600;
  253.  
  254.     /** 
  255.      * The line up scroll event. 
  256.      */
  257.     public static final int SCROLL_LINE_UP    = 1 + SCROLL_EVENT;
  258.  
  259.     /**
  260.      * The line down scroll event.
  261.      */
  262.     public static final int SCROLL_LINE_DOWN    = 2 + SCROLL_EVENT;
  263.  
  264.     /**
  265.      * The page up scroll event.
  266.      */
  267.     public static final int SCROLL_PAGE_UP    = 3 + SCROLL_EVENT;
  268.  
  269.     /**
  270.      * The page down scroll event.
  271.      */
  272.     public static final int SCROLL_PAGE_DOWN    = 4 + SCROLL_EVENT;
  273.  
  274.     /**
  275.      * The absolute scroll event.
  276.      */
  277.     public static final int SCROLL_ABSOLUTE    = 5 + SCROLL_EVENT;
  278.     
  279.     /* List Events */
  280.     private static final int LIST_EVENT        = 700;
  281.  
  282.     /* Event sent when an item has been selected */
  283.     public static final int LIST_SELECT        = 1 + LIST_EVENT;
  284.  
  285.     /* Event sent when an item has been deselected */
  286.     public static final int LIST_DESELECT    = 2 + LIST_EVENT;
  287.  
  288.     /* Misc Event */
  289.     private static final int MISC_EVENT        = 1000;
  290.  
  291.     /**
  292.      * An action event.
  293.      */
  294.     public static final int ACTION_EVENT    = 1 + MISC_EVENT;
  295.  
  296.     /**
  297.      * A file loading event.
  298.      */
  299.     public static final int LOAD_FILE        = 2 + MISC_EVENT;
  300.  
  301.     /**
  302.      * A file saving event.
  303.      */
  304.     public static final int SAVE_FILE        = 3 + MISC_EVENT;
  305.  
  306.     /**
  307.      * A component gained the focus.
  308.      */
  309.     public static final int GOT_FOCUS        = 4 + MISC_EVENT;
  310.  
  311.     /**
  312.      * A component lost the focus.
  313.      */
  314.     public static final int LOST_FOCUS        = 5 + MISC_EVENT;
  315.     
  316.     /**
  317.      * The target component.
  318.      */
  319.     public Object target;
  320.  
  321.     /**
  322.      * The time stamp.
  323.      */
  324.     public long when;
  325.  
  326.     /**
  327.      * The type of this event. 
  328.      */
  329.     public int id;
  330.  
  331.     /** 
  332.      * The x coordinate of the event.
  333.      */
  334.     public int x;
  335.  
  336.     /** 
  337.      * The y coordinate of the event. 
  338.      */
  339.     public int y;
  340.  
  341.     /** 
  342.      * The key that was pressed in a keyboard event. 
  343.      */
  344.     public int key;
  345.  
  346.     /** 
  347.      * The state of the modifier keys.
  348.      */
  349.     public int modifiers;
  350.  
  351.     /**
  352.      * The number of consecutive clicks. This field is relevant only for
  353.      * MOUSE_DOWN events. If the field isn't set it will be 0. Otherwise,
  354.      * it will be 1 for single-clicks, 2 for double-clicks, and so on.
  355.      */
  356.     public int clickCount;
  357.  
  358.     /**
  359.      * An arbitrary argument.
  360.      */
  361.     public Object arg;
  362.  
  363.     /**
  364.      * The next event. Used when putting events into a linked list.
  365.      */
  366.     public Event evt;
  367.  
  368.     /**
  369.      * Constructs an event with the specified target component, time stamp,
  370.      * event type, x and y coordinates, keyboard key, state of the modifier
  371.      * keys and argument.
  372.      * @param target the target component
  373.      * @param when the time stamp
  374.      * @param id the event type
  375.      * @param x the x coordinate
  376.      * @param y the y coordinate
  377.      * @param key the key pressed in a keyboard event
  378.      * @param modifiers the state of the modifier keys
  379.      * @param arg the specified argument
  380.      */
  381.     public Event(Object target, long when, int id, int x, int y, int key,
  382.          int modifiers, Object arg) {
  383.     this.target = target;
  384.     this.when = when;
  385.     this.id = id;
  386.     this.x = x;
  387.     this.y = y;
  388.     this.key = key;
  389.     this.modifiers = modifiers;
  390.     this.arg = arg;
  391.     this.data = 0;
  392.     this.clickCount = 0;
  393.     }
  394.  
  395.     /**
  396.      * Constructs an event with the specified target component, time stamp,
  397.      * event type, x and y coordinates, keyboard key, state of the modifier
  398.      * keys and an argument set to null. 
  399.      * @param target the target component
  400.      * @param when the time stamp
  401.      * @param id the event type
  402.      * @param x the x coordinate
  403.      * @param y the y coordinate
  404.      * @param key the key pressed in a keyboard event
  405.      * @param modifiers the state of the modifier keys
  406.      */
  407.     public Event(Object target, long when, int id, int x, int y, int key, int modifiers) {
  408.     this(target, when, id, x, y, key, modifiers, null);
  409.     }
  410.  
  411.     /**
  412.      * Constructs an event with the specified target component, 
  413.      * event type, and argument. 
  414.      * @param target the target component
  415.      * @param id the event type
  416.      * @param arg the specified argument
  417.      */
  418.     public Event(Object target, int id, Object arg) {
  419.     this(target, 0, id, 0, 0, 0, 0, arg);
  420.     }
  421.  
  422.     /** 
  423.      * Translates an event relative to the given component. This
  424.      * involves at a minimum translating the coordinates so they make
  425.      * sense within the given component. It may also involve
  426.      * translating a region in the case of an expose event.
  427.      * @param x the x coordinate
  428.      * @param y the y coordinate
  429.      */
  430.     public void translate(int x, int y) {
  431.     this.x += x;
  432.     this.y += y;
  433.     }
  434.  
  435.     /**
  436.      * Checks if the shift key is down.
  437.      * @see #modifiers
  438.      * @see #controlDown
  439.      * @see #metaDown
  440.      */
  441.     public boolean shiftDown() {
  442.     return (modifiers & SHIFT_MASK) != 0;
  443.     }
  444.  
  445.     /**
  446.      * Checks if the control key is down.
  447.      * @see #modifiers
  448.      * @see #shiftDown
  449.      * @see #metaDown
  450.      */
  451.     public boolean controlDown() {
  452.     return (modifiers & CTRL_MASK) != 0;
  453.     }
  454.  
  455.     /**
  456.      * Checks if the meta key is down.
  457.      * @see #modifiers
  458.      * @see #shiftDown
  459.      * @see #controlDown
  460.      */
  461.     public boolean metaDown() {
  462.     return (modifiers & META_MASK) != 0;
  463.     }
  464.  
  465.     /**
  466.      * Returns the parameter String of this Event. 
  467.      */
  468.     protected String paramString() {
  469.     String str = "id=" + id + ",x=" + x + ",y=" + y;
  470.     if (key != 0) {
  471.         str += ",key=" + key;
  472.     }
  473.     if (shiftDown()) {
  474.         str += ",shift";
  475.     }
  476.     if (controlDown()) {
  477.         str += ",control";
  478.     }
  479.     if (metaDown()) {
  480.         str += ",meta";
  481.     }
  482.     if (target != null) {
  483.         str += ",target=" + target;
  484.     }
  485.     if (arg != null) {
  486.         str += ",arg=" + arg;
  487.     }
  488.     return str;
  489.     }
  490.  
  491.     /**
  492.      * Returns the String representation of this Event's values.
  493.      */
  494.     public String toString() {
  495.     return getClass().getName() + "[" + paramString() + "]";
  496.     }
  497. }
  498.