home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 16.6 KB | 428 lines |
- /*
- * Copyright (c) 1997 Krumel & Associates, Inc. All Rights Reserved.
- *
- * www.krumel.com - controls@krumel.com
- *
- * Permission is given to the buyer of this package for one software
- * developer to use this software on one CPU (one workstation) and to make
- * one backup copy. You may uitilize and/or modify this class for use in your
- * projects. You may distribute or sell any executable which results from
- * using this code in yur application, except a utility or class of similar
- * nature to this product. You may distribute this product in compiled
- * form only, but soley to be used with your cmpiled executable product
- * for the puposes of dynamic loading. You may NOT redistribute the source
- * code in any form or make it accessible through a network or other
- * distribution media to others. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * The source code is the confidential and proprietary information
- * of Krumel & Associates, Inc. ("Confidential Information"). You shall
- * not disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Krumel & Associates, Inc..
-
- * KRUMEL & ASSOCIATES MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
- * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT
- * NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. KRUMEL & ASSOCIATES SHALL NOT
- * BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
- * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
-
- package symantec.itools.db.awt;
-
- import java.awt.*;
- import symantec.itools.db.awt.event.*;
- import java.awt.event.*;
-
- /**
- * The default event handler for the TableView class. This class provides a
- * normal set of TableView functionality. It can be subclassed and extended
- * and only the functions needed to provide the required functionality have
- * to be overriden. This saves some code from having to implement the
- * TvEventHandler interface from scratch. It provides the following
- * behavior: <br>
- * 1) Pushing on a row heading highlights the row.<p>
- * 2) Causing a cell event of any type will cause all hightlighting
- * to be cleared.<p>
- * 3) Default behavior is provided for the preconfigured toolbar
- * buttons. The buttons call protected helper functions within the class.
- * Instead of having to override this class for a preconfigured button and
- * determine when the one button you are interested in, the helper function
- * can be overriden instead.<p>
- * 4) Exceptions are simply printed to standard output.
- */
- public class DefaultTvEventHandler implements TvEventHandler {
- /**
- * The TableView this DefaultTvEventHandler handles events for.
- */
- protected TableView view;
-
- int rowSelectionBase = -1;
-
- /**
- * Constructs a default DefaultTvEventHandler object.
- */
- public DefaultTvEventHandler() {}
-
- /**
- * Called by the TableView when an event handler is installed. This
- * method provides the event handler a chance to perform any view
- * initialization and set an instance variable for later use.
- */
- public void setupView(TableView v) {
- view = v;
- }
-
- /**
- * Called by the TableView when the user triggers an event that occurs
- * within the context of a normal cell, i.e. not a heading cell.
- * @param e The event object. The source is set to the TableView.
- * @param cell The cell that generated the event.
- */
- public void handleCellEvent(CellEvent e, TableCell cell) {
- if (e.getID() == CellEvent.KEY_RELEASED || e.getID() == CellEvent.LOST_FOCUS) {
- return;
- }
-
- view.clearAllSelections();
- }
-
- /**
- * Called by the TableView when the user triggers an event that occurs
- * within the context of a column heading. This method does not currently perform
- * any actions or add any behavior to the view.
- * @param e The event object. The source is set to the TableView.
- * @param cell The cell that generated the event.
- */
- public void handleColHeadingEvent(CellEvent e, TableCell cell) {
- //remember - rows and columns are measured as 0 relative inside TableView
- // so must adjust before calling selection functions
-
- //we don't do anything for col heading events
- }
-
- /**
- * Called by the TableView when the user triggers an event that occurs
- * within the context of a row heading heading. This method currently provides
- * for row highlighting with a single mouse press, range selection if the shift
- * key is pressed when the mouse is pressed, or multiple selection if the control
- * button is depressed when the mouse is clicked.
- * @param e The event object. The source is set to the TableView.
- * @param cell The cell that generated the event.
- */
- public void handleRowHeadingEvent(CellEvent e, TableCell cell) {
-
- //remember - rows and columns are measured as 0 relative inside TableView
- // so must adjust before calling selection functions
- if (e.getID() == CellEvent.BUTTON_UP_EVENT) {
- boolean leftPressed = e.isLeftMouseButtonDown();
- if (e.isShiftDown() && leftPressed) {
- if (view.isSelectionBaseSet()) {
- //start a selection
- view.selectRange(cell.row() + 1, 0);
- } else {
- view.setSelectionBase(cell.row()+1, 0);
- view.selectRange(cell.row() + 1, 0);
- }
- } else if (e.isControlDown() && leftPressed) {
- if (!view.isRangeSelected()) {
- view.setSelectionBase(cell.row()+1, 0);
- }
-
- view.toggleRow(cell.row() + 1);
- } else if (leftPressed) {
- boolean set = view.isRowSet(cell.row()+1);
- view.clearAllSelections();
- view.setSelectionBase(cell.row()+1, 0);
- view.setRow(cell.row() + 1, !set);
- }
- }
- }
-
- /**
- * Called by the TableView when the user triggers an event that occurs
- * within the context of the corner cell. This method does not presently
- * add any behavior to the view.
- * @param e The event object. The source is set to the TableView.
- * @param cell The cell that generated the event.
- */
- public void handleCornerCellEvent(CellEvent e, TableCell cell) {
- //remember - rows and columns are measured as 0 relative inside view
- // so must adjust before calling selection functions
-
- //we don't do anything for this cell
- }
-
- /**
- * Called by the TableView when the users triggers an event outside
- * the scope of any cell, usually a component located on the toolbar.
- * This method calls a series of helper methods depending on which predefined
- * toolbar component with which the user interacted.
- * @param e The unchanged event that was generated.
- */
- public void handleTableEvent(TableEvent e) {
- if (e.getID() != TableEvent.TOOLBAR_EVENT) {
- return;
- }
-
- Component comp = e.getToolbarSource();
- String name = comp.getName();
-
- //check for goto request.
- if (comp instanceof java.awt.TextField
- && name.equals(TableView.gotoName))
- {
- java.awt.TextField go = (java.awt.TextField)e.getToolbarSource();
- try {
- String rowText = go.getText();
- if (rowText.equals("")) { return; }
-
- int row = Integer.parseInt(rowText);
- view.gotoRow(row);
- } catch(NumberFormatException ex) {
- handleException(null, ex);
- }
- }
-
- if (!(comp instanceof Button)) {
- //The rest of the components are buttons so no need to go on.
- return;
- }
-
- Button button = (Button)comp;
-
- if (name.equals(TableView.gotoName)) {
- //handle goto request.
- try {
- String rowText = (String)e.getParamater();
- if (rowText.equals("")) { return; }
-
- int row = Integer.parseInt(rowText);
- view.gotoRow(row);
-
- } catch(NumberFormatException ex) {
- handleException(null, ex);
- }
- } else if (name.equals(TableView.appendName)) {
- //handle append request.
- try {
- appendRow(view);
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- return;
- } else if (name.equals(TableView.deleteName)) {
- //handle delete request.
- int rows[] = view.getSelectedRows();
- boolean auto = view.getAutoRedraw();
- view.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = view.getCurrentCellCoordinates();
- if (c != null) {
- deleteRow(c.row(), view);
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- deleteRow(rows[i], view);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- view.clearAllSelections();
- view.redrawAsync();
- view.setAutoRedraw(auto);
- return;
- } else if (name.equals(TableView.insertName)) {
- //handle insert row request.
- int row = view.getFirstSelectedRow();
- boolean auto = view.getAutoRedraw();
- view.setAutoRedraw(false);
- try {
- if (row != -1) {
- insertRow(row, view);
- } else {
- //this is zero relative so must adjust to call API
- Coordinate c = view.getCurrentCellCoordinates();
- if (c != null) {
- insertRow(c.row(), view);
- }
- //vj: Insert a row at the top. Ensures that insert works
- // even when there are no rows initially
- else{
- insertRow(0, view);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- view.clearAllSelections();
- view.redrawAsync();
- view.setAutoRedraw(auto);
-
- return;
- //BS:added restartbutton support
- } else if (name.equals(TableView.restartName)) {
- view.restart();
- // grid.redrawAsync();
- return;
- } else if (name.equals(TableView.saveName)) {
- //handle a save request.
- int rows[] = view.getSelectedRows();
- boolean auto = view.getAutoRedraw();
- view.setAutoRedraw(false);
-
- if (rows.length == 0) {
- //save whole view
- try {
- saveView(view);
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
- } else {
- //save selectedRows
- for(int i=rows.length-1; i>=0; i--) {
- try {
- saveRow(rows[i], view);
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
- }
- }
-
- view.setAutoRedraw(auto);
- view.clearAllSelections();
- return;
- } else if (name.equals(TableView.undeleteName)) {
- //handle an undelete request.
- int rows[] = view.getSelectedRows();
- boolean auto = view.getAutoRedraw();
- view.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = view.getCurrentCellCoordinates();
- if (c != null) {
- undeleteRow(c.row(), view);
- view.redrawAsync();
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- undeleteRow(rows[i], view);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- view.setAutoRedraw(auto);
- view.clearAllSelections();
- return;
- }else if (name.equals(TableView.undoName)) {
- //handle an undelete request.
- int rows[] = view.getSelectedRows();
- boolean auto = view.getAutoRedraw();
- view.setAutoRedraw(false);
-
- try {
- if (rows.length == 0) {
- //this is zero relative so must adjust to call API
- Coordinate c = view.getCurrentCellCoordinates();
- if (c != null) {
- undeleteRow(c.row(), view);
- view.redrawAsync();
- }
- } else {
- for(int i=rows.length-1; i>=0; i--) {
- undeleteRow(rows[i], view);
- }
- }
- } catch(TypeNotSupported ex) {
- handleException(null, ex);
- }
-
- view.setAutoRedraw(auto);
- view.clearAllSelections();
- return;
- }
- }
-
- /**
- * Helper function called if save toolbar button is pressed and no rows are selected.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void saveView(TableView view) throws TypeNotSupported {
- view.save();
- }
-
- /**
- * Helper function called for each selected row if save toolbar button is pressed.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void saveRow(int row, TableView view) throws TypeNotSupported {
- //no such operation defined at the view level yet!
- }
-
- /**
- * Helper function called if append toolbar button is pressed.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void appendRow(TableView view) throws TypeNotSupported {
- view.appendRow();
- }
-
- /**
- * Helper function called if insert toolbar button is pressed. The row will be
- * the first row selected.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void insertRow(int row, TableView view) throws TypeNotSupported {
- view.insertRow(row);
- }
-
- /**
- * Helper function called for each selected row if delete toolbar button is pressed.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void deleteRow(int row, TableView view) throws TypeNotSupported {
- view.deleteRow(row);
- }
-
- /**
- * Helper function called for each selected row if undelete toolbar button is pressed.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- protected void undeleteRow(int row, TableView view) throws TypeNotSupported {
- view.undeleteRow(row);
- }
-
- /**
- * Method called by TableView whenever an exception occurs within the framework of the
- * grid. This method provides a convenient location to place code to advise the user
- * in some consistent manner when error conditions occur.
- */
- public void handleException(Coordinate pos, Exception exc) {
- if (pos != null) {
- System.out.println("Error occurred at row="+pos.row()+" col="+pos.col());
- }
- //vj
- //exc.printStackTrace();
- System.out.println(exc.toString());
- }
- }
-