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 / DefaultTvEventHandler.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  16.6 KB  |  428 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;
  34.  
  35. import java.awt.*;
  36. import symantec.itools.db.awt.event.*;
  37. import java.awt.event.*;
  38.  
  39. /**
  40.  * The default event handler for the TableView class. This class provides a
  41.  * normal set of TableView functionality. It can be subclassed and extended
  42.  * and only the functions needed to provide the required functionality have
  43.  * to be overriden. This saves some code from having to implement the
  44.  * TvEventHandler interface from scratch. It provides the following
  45.  * behavior: <br>
  46.  * 1) Pushing on a row heading highlights the row.<p>
  47.  * 2) Causing a cell event of any type will cause all hightlighting
  48.  *    to be cleared.<p>
  49.  * 3) Default behavior is provided for the preconfigured toolbar
  50.  *    buttons. The buttons call protected helper functions within the class.
  51.  *    Instead of having to override this class for a preconfigured button and
  52.  *    determine when the one button you are interested in, the helper function
  53.  *    can be overriden instead.<p>
  54.  * 4) Exceptions are simply printed to standard output.
  55.  */
  56. public class DefaultTvEventHandler implements TvEventHandler {
  57.     /**
  58.      * The TableView this DefaultTvEventHandler handles events for.
  59.      */
  60.     protected TableView   view;
  61.  
  62.     int             rowSelectionBase = -1;
  63.  
  64.     /**
  65.      * Constructs a default DefaultTvEventHandler object.
  66.      */
  67.     public DefaultTvEventHandler() {}
  68.  
  69.     /**
  70.      * Called by the TableView when an event handler is installed.  This
  71.      * method provides the event handler a chance to perform any view
  72.      * initialization and set an instance variable for later use.
  73.      */
  74.     public void setupView(TableView v) {
  75.         view = v;
  76.     }
  77.  
  78.     /**
  79.      * Called by the TableView when the user triggers an event that occurs
  80.      * within the context of a normal cell, i.e. not a heading cell.
  81.      * @param e The event object. The source is set to the TableView.
  82.      * @param cell The cell that generated the event.
  83.      */
  84.     public void handleCellEvent(CellEvent e, TableCell cell) {
  85.         if (e.getID() == CellEvent.KEY_RELEASED || e.getID() == CellEvent.LOST_FOCUS) {
  86.             return;
  87.         }
  88.  
  89.         view.clearAllSelections();
  90.     }
  91.  
  92.     /**
  93.      * Called by the TableView when the user triggers an event that occurs
  94.      * within the context of a column heading. This method does not currently perform
  95.      * any actions or add any behavior to the view.
  96.      * @param e The event object. The source is set to the TableView.
  97.      * @param cell The cell that generated the event.
  98.      */
  99.     public void handleColHeadingEvent(CellEvent e, TableCell cell) {
  100.         //remember - rows and columns are measured as 0 relative inside TableView
  101.         //           so must adjust before calling selection functions
  102.  
  103.         //we don't do anything for col heading events
  104.     }
  105.  
  106.     /**
  107.      * Called by the TableView when the user triggers an event that occurs
  108.      * within the context of a row heading heading. This method currently provides
  109.      * for row highlighting with a single mouse press, range selection if the shift
  110.      * key is pressed when the mouse is pressed, or multiple selection if the control
  111.      * button is depressed when the mouse is clicked.
  112.      * @param e The event object. The source is set to the TableView.
  113.      * @param cell The cell that generated the event.
  114.      */
  115.     public void handleRowHeadingEvent(CellEvent e, TableCell cell) {
  116.  
  117.         //remember - rows and columns are measured as 0 relative inside TableView
  118.         //           so must adjust before calling selection functions
  119.         if (e.getID() == CellEvent.BUTTON_UP_EVENT) {
  120.             boolean leftPressed = e.isLeftMouseButtonDown();
  121.             if (e.isShiftDown() && leftPressed) {
  122.                 if (view.isSelectionBaseSet()) {
  123.                     //start a selection
  124.                     view.selectRange(cell.row() + 1, 0);
  125.                 } else {
  126.                     view.setSelectionBase(cell.row()+1, 0);
  127.                     view.selectRange(cell.row() + 1, 0);
  128.                 }
  129.             } else if (e.isControlDown() && leftPressed) {
  130.                 if (!view.isRangeSelected()) {
  131.                     view.setSelectionBase(cell.row()+1, 0);
  132.                 }
  133.  
  134.                 view.toggleRow(cell.row() + 1);
  135.             } else if (leftPressed) {
  136.                 boolean set = view.isRowSet(cell.row()+1);
  137.                 view.clearAllSelections();
  138.                 view.setSelectionBase(cell.row()+1, 0);
  139.                 view.setRow(cell.row() + 1, !set);
  140.             }
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Called by the TableView when the user triggers an event that occurs
  146.      * within the context of the corner cell. This method does not presently
  147.      * add any behavior to the view.
  148.      * @param e The event object. The source is set to the TableView.
  149.      * @param cell The cell that generated the event.
  150.      */
  151.     public void handleCornerCellEvent(CellEvent e, TableCell cell) {
  152.         //remember - rows and columns are measured as 0 relative inside view
  153.         //           so must adjust before calling selection functions
  154.  
  155.         //we don't do anything for this cell
  156.     }
  157.  
  158.    /**
  159.     * Called by the TableView when the users triggers an event outside
  160.     * the scope of any cell, usually a component located on the toolbar.
  161.     * This method calls a series of helper methods depending on which predefined
  162.     * toolbar component with which the user interacted.
  163.     * @param e The unchanged event that was generated.
  164.     */
  165.     public void handleTableEvent(TableEvent e) {
  166.         if (e.getID() != TableEvent.TOOLBAR_EVENT) {
  167.             return;
  168.         }
  169.  
  170.         Component comp = e.getToolbarSource();
  171.         String name = comp.getName();
  172.  
  173.         //check for goto request.
  174.         if (comp instanceof java.awt.TextField
  175.             && name.equals(TableView.gotoName))
  176.         {
  177.             java.awt.TextField go = (java.awt.TextField)e.getToolbarSource();
  178.             try {
  179.                 String rowText = go.getText();
  180.                 if (rowText.equals("")) { return; }
  181.  
  182.                 int row = Integer.parseInt(rowText);
  183.                 view.gotoRow(row);
  184.             } catch(NumberFormatException ex) {
  185.                 handleException(null, ex);
  186.             }
  187.         }
  188.  
  189.         if (!(comp instanceof Button)) {
  190.             //The rest of the components are buttons so no need to go on.
  191.             return;
  192.         }
  193.  
  194.         Button button = (Button)comp;
  195.  
  196.         if (name.equals(TableView.gotoName)) {
  197.             //handle goto request.
  198.             try {
  199.                 String rowText = (String)e.getParamater();
  200.                 if (rowText.equals("")) { return; }
  201.  
  202.                 int row = Integer.parseInt(rowText);
  203.                 view.gotoRow(row);
  204.  
  205.             } catch(NumberFormatException ex) {
  206.                 handleException(null, ex);
  207.             }
  208.         } else if (name.equals(TableView.appendName)) {
  209.             //handle append request.
  210.             try {
  211.                 appendRow(view);
  212.             } catch(TypeNotSupported ex) {
  213.                 handleException(null, ex);
  214.             }
  215.  
  216.             return;
  217.         } else if (name.equals(TableView.deleteName)) {
  218.             //handle delete request.
  219.             int rows[] = view.getSelectedRows();
  220.             boolean auto = view.getAutoRedraw();
  221.             view.setAutoRedraw(false);
  222.  
  223.             try {
  224.                 if (rows.length == 0) {
  225.                     //this is zero relative so must adjust to call API
  226.                     Coordinate c = view.getCurrentCellCoordinates();
  227.                     if (c != null) {
  228.                         deleteRow(c.row(), view);
  229.                     }
  230.                 } else {
  231.                     for(int i=rows.length-1; i>=0; i--) {
  232.                         deleteRow(rows[i], view);
  233.                     }
  234.                 }
  235.             } catch(TypeNotSupported ex) {
  236.                 handleException(null, ex);
  237.             }
  238.  
  239.             view.clearAllSelections();
  240.             view.redrawAsync();
  241.             view.setAutoRedraw(auto);
  242.             return;
  243.         } else if (name.equals(TableView.insertName)) {
  244.             //handle insert row request.
  245.             int row = view.getFirstSelectedRow();
  246.             boolean auto = view.getAutoRedraw();
  247.             view.setAutoRedraw(false);
  248.         try {
  249.                 if (row != -1) {
  250.                     insertRow(row, view);
  251.                 } else {
  252.                     //this is zero relative so must adjust to call API
  253.                     Coordinate c = view.getCurrentCellCoordinates();
  254.                     if (c != null) {
  255.                         insertRow(c.row(), view);
  256.                     }
  257.                     //vj: Insert a row at the top. Ensures that insert works
  258.                     //    even when there are no rows initially
  259.                     else{
  260.                         insertRow(0, view);
  261.                     }
  262.                 }
  263.             } catch(TypeNotSupported ex) {
  264.                 handleException(null, ex);
  265.             }
  266.  
  267.             view.clearAllSelections();
  268.             view.redrawAsync();
  269.             view.setAutoRedraw(auto);
  270.  
  271.             return;
  272.             //BS:added restartbutton support
  273.         } else if (name.equals(TableView.restartName)) {
  274.             view.restart();
  275.             // grid.redrawAsync();
  276.             return;
  277.         } else if (name.equals(TableView.saveName)) {
  278.             //handle a save request.
  279.             int rows[] = view.getSelectedRows();
  280.             boolean auto = view.getAutoRedraw();
  281.             view.setAutoRedraw(false);
  282.  
  283.             if (rows.length == 0) {
  284.                 //save whole view
  285.                 try {
  286.                    saveView(view);
  287.                 } catch(TypeNotSupported ex) {
  288.                     handleException(null, ex);
  289.                 }
  290.             } else {
  291.                 //save selectedRows
  292.                 for(int i=rows.length-1; i>=0; i--) {
  293.                     try {
  294.                         saveRow(rows[i], view);
  295.                     } catch(TypeNotSupported ex) {
  296.                         handleException(null, ex);
  297.                     }
  298.                 }
  299.             }
  300.  
  301.             view.setAutoRedraw(auto);
  302.             view.clearAllSelections();
  303.             return;
  304.         } else if (name.equals(TableView.undeleteName)) {
  305.             //handle an undelete request.
  306.             int rows[] = view.getSelectedRows();
  307.             boolean auto = view.getAutoRedraw();
  308.             view.setAutoRedraw(false);
  309.  
  310.             try {
  311.                 if (rows.length == 0) {
  312.                     //this is zero relative so must adjust to call API
  313.                     Coordinate c = view.getCurrentCellCoordinates();
  314.                     if (c != null) {
  315.                         undeleteRow(c.row(), view);
  316.                         view.redrawAsync();
  317.                     }
  318.                 } else {
  319.                     for(int i=rows.length-1; i>=0; i--) {
  320.                         undeleteRow(rows[i], view);
  321.                     }
  322.                 }
  323.             } catch(TypeNotSupported ex) {
  324.                 handleException(null, ex);
  325.             }
  326.  
  327.             view.setAutoRedraw(auto);
  328.             view.clearAllSelections();
  329.             return;
  330.         }else if (name.equals(TableView.undoName)) {
  331.             //handle an undelete request.
  332.             int rows[] = view.getSelectedRows();
  333.             boolean auto = view.getAutoRedraw();
  334.             view.setAutoRedraw(false);
  335.  
  336.             try {
  337.                 if (rows.length == 0) {
  338.                     //this is zero relative so must adjust to call API
  339.                     Coordinate c = view.getCurrentCellCoordinates();
  340.                     if (c != null) {
  341.                         undeleteRow(c.row(), view);
  342.                         view.redrawAsync();
  343.                     }
  344.                 } else {
  345.                     for(int i=rows.length-1; i>=0; i--) {
  346.                         undeleteRow(rows[i], view);
  347.                     }
  348.                 }
  349.             } catch(TypeNotSupported ex) {
  350.                 handleException(null, ex);
  351.             }
  352.  
  353.             view.setAutoRedraw(auto);
  354.             view.clearAllSelections();
  355.             return;
  356.         }
  357.     }
  358.  
  359.     /**
  360.      * Helper function called if save toolbar button is pressed and no rows are selected.
  361.      * @exception TypeNotSupported if the data source does not support the type of
  362.      *          action requested or is not successful
  363.      */
  364.     protected void saveView(TableView view) throws TypeNotSupported {
  365.         view.save();
  366.     }
  367.  
  368.     /**
  369.      * Helper function called for each selected row if save toolbar button is pressed.
  370.      * @exception TypeNotSupported if the data source does not support the type of
  371.      *          action requested or is not successful
  372.      */
  373.     protected void saveRow(int row, TableView view) throws TypeNotSupported {
  374.         //no such operation defined at the view level yet!
  375.     }
  376.  
  377.     /**
  378.      * Helper function called if append toolbar button is pressed.
  379.      * @exception TypeNotSupported if the data source does not support the type of
  380.      *          action requested or is not successful
  381.      */
  382.     protected void appendRow(TableView view) throws TypeNotSupported {
  383.         view.appendRow();
  384.     }
  385.  
  386.     /**
  387.      * Helper function called if insert toolbar button is pressed. The row will be
  388.      * the first row selected.
  389.      * @exception TypeNotSupported if the data source does not support the type of
  390.      *          action requested or is not successful
  391.      */
  392.     protected void insertRow(int row, TableView view) throws TypeNotSupported {
  393.         view.insertRow(row);
  394.     }
  395.  
  396.     /**
  397.      * Helper function called for each selected row if delete toolbar button is pressed.
  398.      * @exception TypeNotSupported if the data source does not support the type of
  399.      *          action requested or is not successful
  400.      */
  401.     protected void deleteRow(int row, TableView view) throws TypeNotSupported {
  402.         view.deleteRow(row);
  403.     }
  404.  
  405.     /**
  406.      * Helper function called for each selected row if undelete toolbar button is pressed.
  407.      * @exception TypeNotSupported if the data source does not support the type of
  408.      *          action requested or is not successful
  409.      */
  410.     protected void undeleteRow(int row, TableView view) throws TypeNotSupported {
  411.         view.undeleteRow(row);
  412.     }
  413.  
  414.     /**
  415.      * Method called by TableView whenever an exception occurs within the framework of the
  416.      * grid. This method provides a convenient location to place code to advise the user
  417.      * in some consistent manner when error conditions occur.
  418.      */
  419.     public void handleException(Coordinate pos, Exception exc) {
  420.         if (pos != null) {
  421.             System.out.println("Error occurred at row="+pos.row()+" col="+pos.col());
  422.         }
  423.         //vj
  424.         //exc.printStackTrace();
  425.         System.out.println(exc.toString());
  426.     }
  427. }
  428.