home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VCafe / prosrc.bin / CellEvent.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  11.4 KB  |  359 lines

  1. /*
  2.  * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
  3.  *
  4.  * www.krumel.com - controls@krumel.com
  5.  *
  6.  * Permission is given to the buyer of this package for one software
  7.  * developer to use this software on one CPU (one workstation) and to make
  8.  * one backup copy.  You may uitilize and/or modify this class for use in your
  9.  * projects.  You may distribute or sell any executable which results from
  10.  * using this code in yur application, except a utility or class of similar
  11.  * nature to this product.  You may distribute this product in compiled
  12.  * form only, but soley to be used with your cmpiled executable product
  13.  * for the puposes of dynamic loading. You may NOT redistribute the source
  14.  * code in any form or make it accessible through a network or other
  15.  * distribution media to others. Please refer to the file "copyright.html"
  16.  * for further important copyright and licensing information.
  17.  *
  18.  * The source code is the confidential and proprietary information
  19.  * of Krumel & Associates, Inc. ("Confidential Information").  You shall
  20.  * not disclose such Confidential Information and shall use it only in
  21.  * accordance with the terms of the license agreement you entered into
  22.  * with Krumel & Associates, Inc..
  23.  
  24.  * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
  25.  * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
  26.  * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  27.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
  28.  * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
  29.  * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  30.  */
  31.  
  32.  
  33. package symantec.itools.db.awt.event;
  34.  
  35. import java.awt.*;
  36. import symantec.itools.db.awt.*;
  37. import java.awt.event.*;
  38.  
  39. /**
  40.  * Event type generated by a TableView when the user interacts with a cell, whether
  41.  * the cell is a heading or in the view proper. The original AWT event that caused
  42.  * a cell event is maintained internally so it can be inspected to determine
  43.  * the what caused the cell event and allow for very specific customization of the
  44.  * the table view's response to cell interactions.  All cell events are routed to the
  45.  * TableView's event handler, the data source, and finally any registered objects.
  46.  */
  47. public class CellEvent extends AWTEvent {
  48.     TableCell cell;
  49.     TableView view;
  50.     int id;
  51.  
  52.     int x, y;
  53.     AWTEvent event;
  54.  
  55.  
  56.     /**
  57.      * Base cell level event ID
  58.      */
  59.     public final static int CELL_EVENT = 50;
  60.     /**
  61.      * Cell level event for when cell is button an pushed down
  62.      */
  63.     public final static int BUTTON_DOWN_EVENT = CELL_EVENT;
  64.     /**
  65.      * Cell level event for when cell is button a mouse is released over button
  66.      */
  67.     public final static int BUTTON_UP_EVENT = CELL_EVENT + 1;
  68.     /**
  69.      * Button depressed but mouse moved off button then back on
  70.      */
  71.     public final static int BUTTON_FLICKER_DOWN_EVENT = CELL_EVENT + 2;
  72.     /**
  73.      * Button depressed but mouse moved off button
  74.      */
  75.     public final static int BUTTON_FLICKER_UP_EVENT = CELL_EVENT + 3;
  76.     /**
  77.      * User requested the most recent changes to be undone and the latest
  78.      * committed data be reset
  79.      */
  80.     public final static int UNDO_CELL_EVENT = CELL_EVENT + 4;
  81.     /**
  82.      * Cell level event for when cell gets focus
  83.      */
  84.     public final static int GOT_FOCUS = CELL_EVENT + 5;
  85.     /**
  86.      * Cell level event for when cell loses focus
  87.      */
  88.     public final static int LOST_FOCUS = CELL_EVENT + 6;
  89.     /**
  90.      * User performed action to change contents of a cell (like a key press)
  91.      */
  92.     public final static int CELL_CONTENT_CHANGE = CELL_EVENT + 7;
  93.     /**
  94.      * Cell level event for when key is pressed in cell
  95.      */
  96.     public final static int KEY_PRESSED = CELL_EVENT + 8;
  97.     /**
  98.      * Cell level event for when key is released in cell
  99.      */
  100.     public final static int KEY_RELEASED = CELL_EVENT + 9;
  101.     /**
  102.      * Cell level event for when mouse is released in cell
  103.      */
  104.     public final static int MOUSE_RELEASED = CELL_EVENT + 10;
  105.     /**
  106.      * Cell level event for when mouse is pushed down in cell
  107.      */
  108.     public final static int MOUSE_PRESSED = CELL_EVENT + 11;
  109.     /**
  110.      * Cell level event for when mouse is dragged in cell
  111.      */
  112.     public final static int MOUSE_DRAGGED = CELL_EVENT + 12;
  113.     /**
  114.      * Double click within cell
  115.      */
  116.     public final static int MOUSE_DOUBLE = CELL_EVENT + 13;
  117.     /**
  118.      * User cell level event ID
  119.      */
  120.     public final static int USER_CELL_EVENT = CELL_EVENT +14;
  121.  
  122.     /**
  123.      * Constructs a cell event using an AWTEvent as a basis. If the AWTEvent
  124.      * is an instance of MouseEvent, the x and y coordinates are obtained and
  125.      * translated to cell relative.
  126.      */
  127.     public CellEvent(TableView v, TableCell c, AWTEvent e, int id) {
  128.         super(v, id);
  129.         view = v;
  130.         cell = c;
  131.         event = e;
  132.         this.id = id;
  133.  
  134.         if (e instanceof MouseEvent) {
  135.             setXandY((MouseEvent)e, true);
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * Gets whether the left mouse button is pushed for when the CellEvent
  141.      * contains a MouseEvent.
  142.      */
  143.     public boolean isLeftMouseButtonDown() {
  144.         if (event instanceof InputEvent) {
  145.             MouseEvent me = (MouseEvent)event;
  146.             return !me.isMetaDown() && (me.getModifiers() & me.ALT_MASK)==0;
  147.         }
  148.  
  149.         return false;
  150.     }
  151.  
  152.     /**
  153.      * Gets whether the middle mouse button is pushed for when the CellEvent
  154.      * contains a MouseEvent.
  155.      */
  156.     public boolean isMiddleMouseButtonDown() {
  157.         if (event instanceof InputEvent) {
  158.             MouseEvent me = (MouseEvent)event;
  159.             return (me.getModifiers() & me.ALT_MASK)!=0;
  160.         }
  161.  
  162.         return false;
  163.     }
  164.  
  165.     /**
  166.      * Gets whether the right mouse button is pushed for when the CellEvent
  167.      * contains a MouseEvent.
  168.      */
  169.     public boolean isRightMouseButtonDown() {
  170.         if (event instanceof InputEvent) {
  171.             MouseEvent me = (MouseEvent)event;
  172.             return me.isMetaDown();
  173.         }
  174.  
  175.         return false;
  176.     }
  177.  
  178.     /**
  179.      * Gets whether the shift key is pushed for when the CellEvent
  180.      * contains a InputEvent.
  181.      */
  182.    public boolean isShiftDown() {
  183.         if (event instanceof InputEvent) {
  184.             return ((InputEvent)event).isShiftDown();
  185.         }
  186.  
  187.         return false;
  188.     }
  189.  
  190.     /**
  191.      * Gets whether the control key is pushed for when the CellEvent
  192.      * contains a InputEvent.
  193.      */
  194.     public boolean isControlDown() {
  195.           if (event instanceof InputEvent) {
  196.             return ((InputEvent)event).isControlDown();
  197.         }
  198.  
  199.         return false;
  200.     }
  201.  
  202.     /**
  203.      * Gets whether the modifiers for when the CellEvent contains a InputEvent.
  204.      */
  205.     public int getModifiers() {
  206.         if (event instanceof InputEvent) {
  207.             return ((InputEvent)event).getModifiers();
  208.         }
  209.  
  210.         return 0;
  211.     }
  212.  
  213.     /**
  214.      * Inherited from java.awt.AWTEvent.
  215.      */
  216.     public int getID() { return id; }
  217.  
  218.     /**
  219.      * Sets the ID for the event.
  220.      */
  221.     public void setID(int id) {
  222.         this.id = id;
  223.     }
  224.  
  225.     /**
  226.      * Gets the Object that generated the event. This is usually a TableView object.
  227.      */
  228.     public TableView getView() { return view; }
  229.  
  230.     /**
  231.      * Gets the TableCell that the event occurred upon.
  232.      */
  233.     public TableCell getCell() { return cell; }
  234.  
  235.     /**
  236.      * Sets the target for the cell that the user interacted with to trigger the
  237.      * event.
  238.      */
  239.     public void setCell(TableCell target) { cell = target; }
  240.  
  241.     /**
  242.      * Gets whether the target cell is a column heading.
  243.      */
  244.     public boolean isTargetColumnHeading() { return view.colHeadingCell(cell); }
  245.  
  246.     /**
  247.      * Gets whether the target cell is a row heading.
  248.      */
  249.     public boolean isTargetRowHeading() { return view.rowHeadingCell(cell); }
  250.  
  251.     /**
  252.      * Gets whether the target cell is corner cell.
  253.      */
  254.     public boolean isTargetCornerCell() { return view.cornerCell(cell); }
  255.  
  256.     /**
  257.      * Sets the x and y coordinates for the event using a mouse event object.
  258.      * @param e The mouse event to get the event coordinates from.
  259.      * @param translate Determines if coordinates should be translated from
  260.      *      view relative to cell relative.
  261.      */
  262.     public void setXandY(MouseEvent e, boolean translate) {
  263.         setXandY(e.getX(), e.getY(), translate);
  264.     }
  265.  
  266.     /**
  267.      * Sets the x and y coordinates for the event.  Usually set for mouse events
  268.      * only.
  269.      * @param x The x coordinate.
  270.      * @param y The y coordinate.
  271.      * @param translate Determines if coordinates should be translated from
  272.      *      view relative to cell relative.
  273.      */
  274.     public void setXandY(int x, int y, boolean translate) {
  275.         this.x = x;
  276.         this.y = y;
  277.         if (translate) {
  278.             translateToCell();
  279.         }
  280.     }
  281.  
  282.     /**
  283.      * Gets the x coordinate for the event. Usually on set for mouse events and it has
  284.      * been translated to be relative to upper-left corner of cell.
  285.      */
  286.     public int getX() { return x; }
  287.  
  288.     /**
  289.      * Gets the y coordinate for the event. Usually on set for mouse events and it has
  290.      * been translated to be relative to upper-left corner of cell.
  291.      */
  292.     public int getY() { return y; }
  293.  
  294.     /**
  295.      * Returns the integer key-code associated with the key in this event. For KEY_TYPED
  296.      * events, keyCode is VK_UNDEFINED.
  297.      */
  298.     public int getKeyCode() {
  299.         if (event instanceof KeyEvent) {
  300.             return ((KeyEvent)event).getKeyCode();
  301.         }
  302.  
  303.         return KeyEvent.VK_UNDEFINED;
  304.     }
  305.  
  306.     /**
  307.      * The CellEvent class stores a reference to the actual event sent to the TableView.
  308.      * This method attempts to cast that reference to a KeyEvent type and return it.
  309.      * The event object is stored as an AWTEvent internally.
  310.      */
  311.     public KeyEvent getAsKeyEvent() {
  312.         return (KeyEvent)event;
  313.     }
  314.  
  315.     /**
  316.      * The CellEvent class stores a reference to the actual event sent to the TableView.
  317.      * This method attempts to cast that reference to a MouseEvent type and return it.
  318.      * The event object is stored as an AWTEvent internally. The x and y coordinates
  319.      * stored by this mouse event are relative to the TableView, NOT the cell.
  320.      */
  321.     public MouseEvent getAsMouseEvent() {
  322.         return (MouseEvent)event;
  323.     }
  324.  
  325.     /**
  326.      * The CellEvent class stores a reference to the actual event sent to the TableView.
  327.      * The event object is stored as an AWTEvent internally.
  328.      */
  329.     public AWTEvent getAsAWTEvent() {
  330.         return event;
  331.     }
  332.  
  333.     void translateToCell() {
  334.         Rectangle b = view.getCellBounds(cell, null);
  335.  
  336.         x -= b.x;
  337.         y -= b.y;
  338.     }
  339.  
  340.     /**
  341.      * Creates a cell event based on a view's mouse event.
  342.      */
  343.         public static void
  344.     generateMouseEvent(TableView v, TableCell c, MouseEvent e, int id) {
  345.         CellEvent event = new CellEvent(v, c, e, id);
  346.         c.mouseEvent(event);
  347.     }
  348.  
  349.     /**
  350.      * Creates a cell event based on a view's mouse event.
  351.      */
  352.         public static void
  353.     generateKeyEvent(TableView v, TableCell c, KeyEvent e, int id) {
  354.         CellEvent event = new CellEvent(v, c, e, id);
  355.         c.keyEvent(event);
  356.     }
  357.  
  358. }
  359.