home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 20.3 KB | 617 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.Point;
- import java.awt.Image;
- import symantec.itools.db.awt.genutil.Matrix;
- import symantec.itools.db.awt.event.CellEvent;
-
- /**
- * DataSource that provides a buffer from which to store and retrieve data.
- */
- public class DefaultDataSource implements DataSource {
- Matrix data = new Matrix();
- TableView view;
-
- boolean useDefault = false;
- boolean autoNumber = false;
- int first;
- Data defaultValue;
-
- /**
- * Creates a data source for a TableView that does not use default data for
- * values where no value is set.
- */
- public DefaultDataSource(TableView v) {
- this(v, false, null);
- }
-
- /**
- * Create a data source for a TableView that sets the internal flag for whether
- * to use default Data object for when data is not set in a cell. The default data
- * must be specified in a separate call.
- */
- public DefaultDataSource(TableView v, boolean useDefaults) {
- this(v, useDefaults, null);
- }
-
- /**
- * Create a data source for a TableView that sets the internal flag for whether
- * to use default Data object for when data is not set in a cell.
- */
- public DefaultDataSource(TableView v, boolean useDefaults, Data defaultData) {
- setTableView(v);
- this.useDefault = useDefaults;
- if (useDefaults) {
- defaultValue = (defaultData != null) ?defaultData :new DefaultData(this);
- }
- }
-
- /**
- * Informs the data source whether the TableView will be requesting successive data
- * for read only purposes.
- * @param manual true if the data source should expect read only data requests
- */
- public void fetchMode(boolean b) {}
-
- /**
- * Requests the data source get all of the rows from its source of data.
- */
- public int fetchAllRows() { return rows(); }
-
- /**
- * Informs the data source of the current row of the TableView.
- */
- public void setCurrentRow(int row) {}
-
- /**
- * Gets the number of rows of data stored in the data source
- */
- public int rows() {
- return data.rows();
- }
-
- /**
- * Gets the TableView displaying the data for the DataSource
- */
- public TableView getView() { return view; }
-
- /**
- * Gets the last row valid in requested range
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
- int rows = data.rows();
-
- if (rows <= top) {
- throw new DataNotAvailable("Requested top=" + top + " but only " +
- rows + " available");
- }
-
- return Math.min(rows-1, bottom);
- }
-
- /**
- * Specifies whether data should be based on the row number if requested data
- * is not present in the source.
- */
- public void doAutoNumbering(boolean an) {
- doAutoNumbering(an, first);
- }
-
- /**
- * Specifies whether data should be based on the row number if requested data
- * is not present in the source.
- * @param Whether to enable autonumbering.
- * @param n The value of the first row.
- */
- public void doAutoNumbering(boolean an, int n) {
- autoNumber = an;
- first = n;
- if (!useDefault && an == true) {
- useDefault = true;
- defaultValue = new ImageStringData(this);
- }
- }
-
-
- //BS: added redoautonumbering
- /**
- * Redoes the numbering if the autoNumber property is true
- *
- */
-
- public void redoAutoNumbering() {
- if(autoNumber == true) {
- int myRows = rows();
- Integer anInt;
- for(int i = 0; i < myRows; i++) {
- anInt = new Integer(i + 1);
- setText(i, 0,anInt.toString());
- anInt = null;
- }
- }
- }
-
- /**
- * Commits the current data.
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void commitData() throws TypeNotSupported {}
-
- /**
- * Requests the DataSource set an appropriate default data type
- */
- public void setDefaultData() {
- if (defaultValue == null) {
- setDefaultData(new DefaultData(this));
- }
- }
-
- /**
- * Sets the Data type for cell data when no data value has been set.
- */
- public void setDefaultData(Data d) {
- useDefault = true;
- autoNumber = false;
- defaultValue = d;
- }
-
- /**
- * Gets the default data for the data source.
- */
- public Data getDefaultData() {
- if (defaultValue == null) {
- throw new NullPointerException("Default data value not set");
- }
-
- return defaultValue;
- }
-
- /**
- * Sets the TableView displaying the data.
- */
- public void setTableView(TableView v) {
- view = v;
- }
-
- /**
- * Gets whether the DataSource has the ability to configure a TableView.
- * @return Always returns false.
- */
- public boolean supportsMeta() {
- return false;
- }
-
- /**
- * Called by the TableView to configure the interface if the DataSource supports
- * meta information
- * @param v The TableView to configure
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested
- */
- public void setupTableView(TableView v) throws TypeNotSupported {
- throw new TypeNotSupported("DefaultDataSource does not support meta information");
- }
-
- /**
- * Gets the data for a cell that should only be used for reading. It does
- * not change the cell data being currently edited.
- * @param row the cells row
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public Data readData(int row, int col) throws DataNotAvailable {
- return getData(row, col);
- }
-
- /**
- * Gets the data for a cell. <p>
- * Call this method when going to be doing edits. <p>
- * Call commitData() when done with it (usually when lose focus).
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public Data getData(Coordinate coords) throws DataNotAvailable {
- return getData(coords.row, coords.col);
- }
-
- /**
- * Gets the data for a cell. <p>
- * Call this method when going to be doing edits. <p>
- * Call commitData() when done with it (usually when lose focus).
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public Data getData(int r, int c) throws DataNotAvailable {
- if (data.contains(r, c)) {
- return (Data)data.elementAt(r, c);
- } else {
- if (autoNumber) {
- int state = view.rowState(r+1);
- defaultValue.setText(Integer.toString(r + first));
- if (/*DataState.deleteState(state)*/
- state == DataSource.DELETED_ROW) {
- defaultValue.appendChar('*');
- }//vj:Indicate modified row
- if (state == DataSource.MODIFIED_ROW) {
- defaultValue.appendChar('^');
- }
- return defaultValue;
- } else if (useDefault) {
- if (defaultValue instanceof DefaultData) {
- ((DefaultData)defaultValue).setRowAndCol(r, c);
- }
- return defaultValue;
- } else {
- throw new DataNotAvailable();
- }
- }
- }
-
- /**
- * Sets the data value for a cell.
- * @param coord the cooridates of the cell
- * @param data the data value for the cell
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void setData(Coordinate coords, Data d) throws TypeNotSupported {
- setData(coords.row, coords.col, d);
- }
-
- /**
- * Sets the data value for a cell.
- * @param r the row of the cell
- * @param c the column of the cell
- * @param data the data value for the cell
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void setData(int r, int c, Data d) throws TypeNotSupported {
- data.updateElement(r, c, d);
-
- }
-
- /**
- * Gets the textual representation of a cell's data
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public String getText(Coordinate coords) throws DataNotAvailable {
- return getData(coords.row, coords.col).toString();
- }
-
- /**
- * Gets whether the data source supports a specified type of data
- */
- public boolean supports(Coordinate coords, int type) {
- return type == Data.STRING ||
- type == Data.IMAGE ||
- type == Data.IMAGE_STRING;
- }
-
- /**
- * Gets an image representation of a cell's data
- * @exception DataNotAvailable if the requested data is not set in the data source
- */
- public Image getImage(Coordinate coords) throws DataNotAvailable {
- if (data.contains(coords.row, coords.col)) {
- Data d = (Data)data.elementAt(coords.row, coords.col);
- return d.toImage();
- } else {
- throw new DataNotAvailable();
- }
- }
-
- /**
- * Gives a data source a chance to perform any actions in response to a table
- * level event.
- */
- public void handleTableEvent(symantec.itools.db.awt.event.TableEvent e) {
- }
-
- /**
- * Called in response to circulating an event caused by an invocation of
- * TableView.routeEvent(). The event IDs are specified in the CellEvent class
- */
- public void handleCellEvent(CellEvent e) {
- TableCell cell = e.getCell();
- Data data;
- try {
- data = cell.getData(); //this is kind of silly and circular
- } catch(DataNotAvailable ex) { return; }
-
- switch(e.getID()) {
- case CellEvent.UNDO_CELL_EVENT:
- data.rollback();
- break;
- case CellEvent.LOST_FOCUS:
- try {
- data.commit();
- } catch(TypeNotSupported ex) {
-
- }
- break;
- }
- }
-
- //zero based
- /**
- * Routes any exceptions generated to the TableView for proper handling which may
- * include putting a message on status bar or logging to a file.
- */
- public void handleException(int row, int col, Exception ex) {
- view.handleException(row, col, ex);
- }
-
- /**
- * Requests that any actions performed on a row be undone. The meaning
- * is left open and is to interpreted as appropriate for the type of
- * data source.
- * <p>
- * DefaultDataSource does not support this action.
- * @exception TypeNotSupported since the data source does not support the type of
- * action requested
- */
- public void undoRow(int row) throws TypeNotSupported {
- throw new TypeNotSupported("Undo not supported in DefaultDataSource");
- }
-
- /**
- * Undeletes a row in the data source
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void undeleteRow(int row) throws TypeNotSupported {
- throw new TypeNotSupported("Undelete not supported in DefaultDataSource");
- }
-
- /**
- * Deletes or marks a row for deletion from the data source
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void deleteRow(int row) throws TypeNotSupported {
- data.removeRow(row);
- }
-
- /**
- * Inserts a new row in the data source above the specified row
- * @exception TypeNotSupported If the data source does not support the type of
- * action requested or is not successful
- */
- public void insertRow(int row) throws TypeNotSupported {
- data.addRow(row);
- }
-
- /**
- * Appends a new row at the end of the data source's data.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- public int appendRow() throws TypeNotSupported {
- data.addRow(data.rows());
-
- return data.rows()-1;
- }
-
- //BS: we use a reference to the view in getData when using autonumbering
- //(in autonumbering, no data for the cell is actually stored, it is derived
- //from the row number). The view's datasource is used to obtain the state of the
- //row, thus returning a DataSource state, not a DataState !
- /**
- * Gets the state of the row.
- * @return the state of the row's data as defined by DataState.
- */
- public int rowState(int row) { return /*DataState.NEW*/DataSource.NEW_ROW; }
-
- /**
- * Requests the data source remove all data.
- */
- public void clear() {
- data.removeAllElements();
- }
-
- /**
- * Requests the data source reread it data.
- */
- public void refresh() {}
-
- /**
- * Requests the data source save the current state as appopriate.
- */
- public void save() {}
-
- //returns Data object for row and col. If not found then makes a new one
- //and inserts it.
- Data fetchData(int row, int col) {
- if (data.contains(row, col)) {
- return (Data)data.elementAt(row, col);
- } else {
- //need to create a Data
- Data d = new ImageStringData(this); //need to add column default support here
- data.updateElement(row, col, d);
- return d;
- }
- }
-
- /**
- * Gets whether a cells data is editable or non-editable
- * @return true if the data may be altered by the user.
- */
- public boolean isDataEditable(int row, int col) { return true; }
-
- /**
- * Method used to support Defaultdata class. Gets the type of data supported
- * by the cell.
- */
- public int type(int row, int col) {
- return Data.STRING;
- }
-
- /**
- * Requests the current data element be reset to the previously committed
- * state
- */
- public void rollbackCurrentData() {}
-
- /**
- * Method used to support Defaultdata class. Commits the data for the
- * specified cell
- */
- public void commit(int row, int col) {}
-
- /**
- * Method used to support Defaultdata class. Gets whether the data for the
- * specified cell supports choice selection lists.
- */
- public boolean supportsChoice(int row, int col) {
- return false;
- }
-
- /**
- * Method used to support Defaultdata class. Gets the choices for the cell to
- * display.
- * @exception TypeNotSupported if the data source does not support the type of
- * action requested or is not successful
- */
- public Data[] getChoices(int row, int col) throws TypeNotSupported {
- throw new TypeNotSupported();
- }
-
- /**
- * Method used to support Defaultdata class. Sets the textual value for a cell.
- */
- public void setText(int row, int col, String t) {
- fetchData(row, col).setText(t);
- }
-
- //pos is space where to be inserted (0 = first char)
- /**
- * Method used to support Defaultdata class. Inserts a character at the
- * specified location in the data value for a cell.
- * @param row The cell's row
- * @param col The cell's column
- * @param pos The position to insert character
- * @param c The character
- */
- public void insertChar(int row, int col, int pos, char c) {
- fetchData(row, col).insertChar(pos, c);
- }
-
- /**
- * Method used to support Defaultdata class. Sets the text for a cell to a
- * character
- */
- public void setText(int row, int col, char c) {
- fetchData(row, col).setText(c);
- }
-
- /**
- * Method used to support Defaultdata class. Appends a character to the end
- * of the textual representation of the cell's data.
- */
- public void appendChar(int row, int col, char c) {
- fetchData(row, col).appendChar(c);
- }
-
- /**
- * Method used to support Defaultdata class. Clears the textual value of a cell.
- */
- public void clearText(int row, int col) {
- if (data.contains(row, col)) {
- ((Data)data.elementAt(row, col)).clearText();
- }
- }
-
- /**
- * Method used to support Defaultdata class. Deletes a character from a cell's
- * data.
- * @param row The cell's row
- * @param col The cell's column
- * @param pos The position to delete character
- */
- public void deleteChar(int row, int col, int pos) {
- if (data.contains(row, col)) {
- ((Data)data.elementAt(row, col)).deleteChar(pos);
- }
- }
-
- /**
- * Method used to support Defaultdata class. Gets a substring from a cell's
- * data.
- * @param row The cell's row
- * @param col The cell's column
- * @param spos The starting position of the substring, inclusive
- * @param epos The last position of the substring, exclusive
- */
- public String subString(int row, int col, int spos, int epos) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).subString(spos, epos);
- }
-
- throw new StringIndexOutOfBoundsException();
- }
-
- /**
- * Method used to support Defaultdata class. Sets the image data for a cell
- */
- public void setImage(int row, int col, Image i) {
- fetchData(row, col).setImage(i);
- }
-
- /**
- * Method used to support Defaultdata class. Gets a string representation of
- * a cell's data.
- */
- public String toString(int row, int col) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).toString();
- } else {
- return "";
- }
- }
-
- /**
- * Method used to support Defaultdata class. Gets an image representationof
- * a cell's data.
- */
- public Image toImage(int row, int col) {
- if (data.contains(row, col)) {
- return ((Data)data.elementAt(row, col)).toImage();
- } else {
- return null;
- }
- }
-
- }
-
-