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 / DefaultDataSource.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  20.3 KB  |  617 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. package symantec.itools.db.awt;
  33.  
  34. import java.awt.Point;
  35. import java.awt.Image;
  36. import symantec.itools.db.awt.genutil.Matrix;
  37. import symantec.itools.db.awt.event.CellEvent;
  38.  
  39. /**
  40.  * DataSource that provides a buffer from which to store and retrieve data.
  41.  */
  42. public class DefaultDataSource implements DataSource {
  43.     Matrix      data = new Matrix();
  44.     TableView        view;
  45.  
  46.     boolean     useDefault = false;
  47.     boolean     autoNumber = false;
  48.     int         first;
  49.     Data        defaultValue;
  50.  
  51.     /**
  52.      * Creates a data source for a TableView that does not use default data for
  53.      * values where no value is set.
  54.      */
  55.     public DefaultDataSource(TableView v) {
  56.         this(v, false, null);
  57.     }
  58.  
  59.     /**
  60.      * Create a data source for a TableView that sets the internal flag for whether
  61.      * to use default Data object for when data is not set in a cell. The default data
  62.      * must be specified in a separate call.
  63.      */
  64.     public DefaultDataSource(TableView v, boolean useDefaults) {
  65.         this(v, useDefaults, null);
  66.     }
  67.  
  68.     /**
  69.      * Create a data source for a TableView that sets the internal flag for whether
  70.      * to use default Data object for when data is not set in a cell.
  71.      */
  72.     public DefaultDataSource(TableView v, boolean useDefaults, Data defaultData) {
  73.         setTableView(v);
  74.         this.useDefault = useDefaults;
  75.         if (useDefaults) {
  76.             defaultValue = (defaultData != null) ?defaultData  :new DefaultData(this);
  77.         }
  78.     }
  79.  
  80.     /**
  81.      * Informs the data source whether the TableView will be requesting successive data
  82.      * for read only purposes.
  83.      * @param manual true if the data source should expect read only data requests
  84.      */
  85.     public void fetchMode(boolean b) {}
  86.  
  87.     /**
  88.      * Requests the data source get all of the rows from its source of data.
  89.      */
  90.     public int fetchAllRows() { return rows(); }
  91.  
  92.     /**
  93.      * Informs the data source of the current row of the TableView.
  94.      */
  95.     public void setCurrentRow(int row) {}
  96.  
  97.     /**
  98.      * Gets the number of rows of data stored in the data source
  99.      */
  100.     public int rows() {
  101.         return data.rows();
  102.     }
  103.  
  104.     /**
  105.      * Gets the TableView displaying the data for the DataSource
  106.      */
  107.     public TableView getView() { return view; }
  108.  
  109.     /**
  110.      * Gets the last row valid in requested range
  111.      * @exception DataNotAvailable if the requested data is not set in the data source
  112.      */
  113.     public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  114.         int rows = data.rows();
  115.  
  116.         if (rows <= top) {
  117.             throw new DataNotAvailable("Requested top=" + top + " but only " +
  118.                                        rows + " available");
  119.         }
  120.  
  121.         return Math.min(rows-1, bottom);
  122.     }
  123.  
  124.     /**
  125.      * Specifies whether data should be based on the row number if requested data
  126.      * is not present in the source.
  127.      */
  128.     public void doAutoNumbering(boolean an) {
  129.         doAutoNumbering(an, first);
  130.     }
  131.  
  132.     /**
  133.      * Specifies whether data should be based on the row number if requested data
  134.      * is not present in the source.
  135.      * @param Whether to enable autonumbering.
  136.      * @param n The value of the first row.
  137.      */
  138.     public void doAutoNumbering(boolean an, int n) {
  139.         autoNumber = an;
  140.         first = n;
  141.         if (!useDefault && an == true) {
  142.             useDefault = true;
  143.             defaultValue = new ImageStringData(this);
  144.         }
  145.     }
  146.  
  147.  
  148.     //BS: added redoautonumbering
  149.     /**
  150.      * Redoes the numbering if the autoNumber property is true
  151.      *
  152.      */
  153.  
  154.      public void redoAutoNumbering() {
  155.         if(autoNumber == true) {
  156.             int myRows = rows();
  157.             Integer anInt;
  158.             for(int i = 0; i < myRows; i++) {
  159.                 anInt = new Integer(i + 1);
  160.                 setText(i, 0,anInt.toString());
  161.                 anInt = null;
  162.             }
  163.         }
  164.      }
  165.  
  166.     /**
  167.      * Commits the current data.
  168.      * @exception   TypeNotSupported If the data source does not support the type of
  169.      *          action requested or is not successful
  170.      */
  171.     public void commitData() throws TypeNotSupported {}
  172.  
  173.     /**
  174.      * Requests the DataSource set an appropriate default data type
  175.      */
  176.     public void setDefaultData() {
  177.         if (defaultValue == null) {
  178.             setDefaultData(new DefaultData(this));
  179.         }
  180.     }
  181.  
  182.     /**
  183.      * Sets the Data type for cell data when no data value has been set.
  184.      */
  185.     public void setDefaultData(Data d) {
  186.         useDefault = true;
  187.         autoNumber = false;
  188.         defaultValue = d;
  189.     }
  190.  
  191.     /**
  192.      * Gets the default data for the data source.
  193.      */
  194.     public Data getDefaultData() {
  195.         if (defaultValue == null) {
  196.             throw new NullPointerException("Default data value not set");
  197.         }
  198.  
  199.         return defaultValue;
  200.     }
  201.  
  202.     /**
  203.      * Sets the TableView displaying the data.
  204.      */
  205.     public void setTableView(TableView v) {
  206.         view = v;
  207.     }
  208.  
  209.     /**
  210.      * Gets whether the DataSource has the ability to configure a TableView.
  211.      * @return Always returns false.
  212.      */
  213.     public boolean supportsMeta() {
  214.         return false;
  215.     }
  216.  
  217.     /**
  218.      * Called by the TableView to configure the interface if the DataSource supports
  219.      * meta information
  220.      * @param v The TableView to configure
  221.      * @exception   TypeNotSupported If the data source does not support the type of
  222.      *          action requested
  223.      */
  224.     public void setupTableView(TableView v) throws TypeNotSupported {
  225.         throw new TypeNotSupported("DefaultDataSource does not support meta information");
  226.     }
  227.  
  228.     /**
  229.      * Gets the data for a cell that should only be used for reading. It does
  230.      * not change the cell data being currently edited.
  231.      * @param row the cells row
  232.      * @exception DataNotAvailable if the requested data is not set in the data source
  233.      */
  234.     public Data readData(int row, int col) throws DataNotAvailable {
  235.         return getData(row, col);
  236.     }
  237.  
  238.     /**
  239.      * Gets the data for a cell.   <p>
  240.      * Call this method when going to be doing edits. <p>
  241.      * Call commitData() when done with it (usually when lose focus).
  242.      * @exception DataNotAvailable if the requested data is not set in the data source
  243.      */
  244.     public Data getData(Coordinate coords) throws DataNotAvailable {
  245.         return getData(coords.row, coords.col);
  246.     }
  247.  
  248.     /**
  249.      * Gets the data for a cell.   <p>
  250.      * Call this method when going to be doing edits. <p>
  251.      * Call commitData() when done with it (usually when lose focus).
  252.      * @exception DataNotAvailable if the requested data is not set in the data source
  253.      */
  254.     public Data getData(int r, int c) throws DataNotAvailable {
  255.         if (data.contains(r, c)) {
  256.             return (Data)data.elementAt(r, c);
  257.         } else {
  258.             if (autoNumber) {
  259.                 int state = view.rowState(r+1);
  260.                 defaultValue.setText(Integer.toString(r + first));
  261.                 if (/*DataState.deleteState(state)*/
  262.                     state == DataSource.DELETED_ROW) {
  263.                     defaultValue.appendChar('*');
  264.                 }//vj:Indicate modified row
  265.                 if (state == DataSource.MODIFIED_ROW) {
  266.                     defaultValue.appendChar('^');
  267.                 }
  268.                 return defaultValue;
  269.             } else if (useDefault) {
  270.                 if (defaultValue instanceof DefaultData) {
  271.                     ((DefaultData)defaultValue).setRowAndCol(r, c);
  272.                 }
  273.                 return defaultValue;
  274.             } else {
  275.                 throw new DataNotAvailable();
  276.             }
  277.         }
  278.     }
  279.  
  280.     /**
  281.      * Sets the data value for a cell.
  282.      * @param coord the cooridates of the cell
  283.      * @param data the data value for the cell
  284.      * @exception   TypeNotSupported If the data source does not support the type of
  285.      *          action requested or is not successful
  286.      */
  287.     public void setData(Coordinate coords, Data d) throws TypeNotSupported {
  288.         setData(coords.row, coords.col, d);
  289.     }
  290.  
  291.     /**
  292.      * Sets the data value for a cell.
  293.      * @param r the row of the cell
  294.      * @param c the column of the cell
  295.      * @param data the data value for the cell
  296.      * @exception   TypeNotSupported If the data source does not support the type of
  297.      *          action requested or is not successful
  298.      */
  299.     public void setData(int r, int c, Data d) throws TypeNotSupported {
  300.         data.updateElement(r, c, d);
  301.  
  302.     }
  303.  
  304.     /**
  305.      * Gets the textual representation of a cell's data
  306.      * @exception DataNotAvailable if the requested data is not set in the data source
  307.      */
  308.     public String getText(Coordinate coords) throws DataNotAvailable {
  309.         return getData(coords.row, coords.col).toString();
  310.     }
  311.  
  312.     /**
  313.      * Gets whether the data source supports a specified type of data
  314.      */
  315.     public boolean supports(Coordinate coords, int type) {
  316.         return type == Data.STRING ||
  317.                type == Data.IMAGE ||
  318.                type == Data.IMAGE_STRING;
  319.     }
  320.  
  321.     /**
  322.      * Gets an image representation of a cell's data
  323.      * @exception DataNotAvailable if the requested data is not set in the data source
  324.      */
  325.     public Image getImage(Coordinate coords) throws DataNotAvailable {
  326.         if (data.contains(coords.row, coords.col)) {
  327.             Data d = (Data)data.elementAt(coords.row, coords.col);
  328.             return d.toImage();
  329.         } else {
  330.             throw new DataNotAvailable();
  331.         }
  332.     }
  333.  
  334.     /**
  335.      * Gives a data source a chance to perform any actions in response to a table
  336.      * level event.
  337.      */
  338.     public void handleTableEvent(symantec.itools.db.awt.event.TableEvent e) {
  339.     }
  340.  
  341.     /**
  342.      * Called in response to circulating an event caused by an invocation of
  343.      * TableView.routeEvent().  The event IDs are specified in the CellEvent class
  344.      */
  345.     public void handleCellEvent(CellEvent e) {
  346.             TableCell cell = e.getCell();
  347.             Data data;
  348.             try {
  349.                 data = cell.getData();  //this is kind of silly and circular
  350.             } catch(DataNotAvailable ex) { return; }
  351.  
  352.             switch(e.getID()) {
  353.                 case CellEvent.UNDO_CELL_EVENT:
  354.                     data.rollback();
  355.                     break;
  356.                 case CellEvent.LOST_FOCUS:
  357.                     try {
  358.                         data.commit();
  359.                     } catch(TypeNotSupported ex) {
  360.  
  361.                     }
  362.                     break;
  363.             }
  364.     }
  365.  
  366.     //zero based
  367.     /**
  368.      * Routes any exceptions generated to the TableView for proper handling which may
  369.      * include putting a message on status bar or logging to a file.
  370.      */
  371.     public void handleException(int row, int col, Exception ex) {
  372.         view.handleException(row, col, ex);
  373.     }
  374.  
  375.     /**
  376.      * Requests that any actions performed on a row be undone. The meaning
  377.      * is left open and is to interpreted as appropriate for the type of
  378.      * data source.
  379.      * <p>
  380.      * DefaultDataSource does not support this action.
  381.      * @exception TypeNotSupported since the data source does not support the type of
  382.      *          action requested
  383.      */
  384.     public void undoRow(int row) throws TypeNotSupported {
  385.         throw new TypeNotSupported("Undo not supported in DefaultDataSource");
  386.     }
  387.  
  388.     /**
  389.      * Undeletes a row in the data source
  390.      * @exception   TypeNotSupported If the data source does not support the type of
  391.      *          action requested or is not successful
  392.      */
  393.     public void undeleteRow(int row) throws TypeNotSupported {
  394.         throw new TypeNotSupported("Undelete not supported in DefaultDataSource");
  395.     }
  396.  
  397.     /**
  398.      * Deletes or marks a row for deletion from the data source
  399.      * @exception   TypeNotSupported If the data source does not support the type of
  400.      *          action requested or is not successful
  401.      */
  402.     public void deleteRow(int row) throws TypeNotSupported {
  403.         data.removeRow(row);
  404.     }
  405.  
  406.     /**
  407.      * Inserts a new row in the data source above the specified row
  408.      * @exception   TypeNotSupported If the data source does not support the type of
  409.      *          action requested or is not successful
  410.      */
  411.     public void insertRow(int row) throws TypeNotSupported {
  412.         data.addRow(row);
  413.     }
  414.  
  415.     /**
  416.      * Appends a new row at the end of the data source's data.
  417.      * @exception TypeNotSupported if the data source does not support the type of
  418.      *          action requested or is not successful
  419.      */
  420.     public int appendRow() throws TypeNotSupported {
  421.         data.addRow(data.rows());
  422.  
  423.         return data.rows()-1;
  424.     }
  425.  
  426. //BS: we use a reference to the view in getData when using autonumbering
  427. //(in autonumbering, no data for the cell is actually stored, it is derived
  428. //from the row number). The view's datasource is used to obtain the state of the
  429. //row, thus returning a DataSource state, not a DataState !
  430.     /**
  431.      * Gets the state of the row.
  432.      * @return the state of the row's data as defined by DataState.
  433.      */
  434.     public int rowState(int row) { return /*DataState.NEW*/DataSource.NEW_ROW; }
  435.  
  436.     /**
  437.      * Requests the data source remove all data.
  438.      */
  439.     public void clear() {
  440.         data.removeAllElements();
  441.     }
  442.  
  443.     /**
  444.      * Requests the data source reread it data.
  445.      */
  446.     public void refresh() {}
  447.  
  448.     /**
  449.      * Requests the data source save the current state as appopriate.
  450.      */
  451.     public void save() {}
  452.  
  453.     //returns Data object for row and col.  If not found then makes a new one
  454.     //and inserts it.
  455.     Data fetchData(int row, int col) {
  456.         if (data.contains(row, col)) {
  457.             return (Data)data.elementAt(row, col);
  458.         } else {
  459.             //need to create a Data
  460.             Data d = new ImageStringData(this);    //need to add column default support here
  461.             data.updateElement(row, col, d);
  462.             return d;
  463.         }
  464.     }
  465.  
  466.     /**
  467.      * Gets whether a cells data is editable or non-editable
  468.      * @return true if the data may be altered by the user.
  469.      */
  470.     public boolean isDataEditable(int row, int col) { return true; }
  471.  
  472.     /**
  473.      * Method used to support Defaultdata class. Gets the type of data supported
  474.      * by the cell.
  475.      */
  476.     public int type(int row, int col) {
  477.         return Data.STRING;
  478.     }
  479.  
  480.     /**
  481.      * Requests the current data element be reset to the previously committed
  482.      * state
  483.      */
  484.     public void rollbackCurrentData() {}
  485.  
  486.     /**
  487.      * Method used to support Defaultdata class. Commits the data for the
  488.      * specified cell
  489.      */
  490.     public void commit(int row, int col) {}
  491.  
  492.     /**
  493.      * Method used to support Defaultdata class. Gets whether the data for the
  494.      * specified cell supports choice selection lists.
  495.      */
  496.     public boolean supportsChoice(int row, int col) {
  497.         return false;
  498.     }
  499.  
  500.     /**
  501.      * Method used to support Defaultdata class. Gets the choices for the cell to
  502.      * display.
  503.      * @exception TypeNotSupported if the data source does not support the type of
  504.      *          action requested or is not successful
  505.      */
  506.     public Data[] getChoices(int row, int col) throws TypeNotSupported {
  507.         throw new TypeNotSupported();
  508.     }
  509.  
  510.     /**
  511.      * Method used to support Defaultdata class. Sets the textual value for a cell.
  512.      */
  513.     public void setText(int row, int col, String t) {
  514.         fetchData(row, col).setText(t);
  515.     }
  516.  
  517.     //pos is space where to be inserted (0 = first char)
  518.     /**
  519.      * Method used to support Defaultdata class. Inserts a character at the
  520.      * specified location in the data value for a cell.
  521.      * @param row The cell's row
  522.      * @param col The cell's column
  523.      * @param pos The position to insert character
  524.      * @param c The character
  525.      */
  526.     public void insertChar(int row, int col, int pos, char c) {
  527.         fetchData(row, col).insertChar(pos, c);
  528.     }
  529.  
  530.     /**
  531.      * Method used to support Defaultdata class. Sets the text for a cell to a
  532.      * character
  533.      */
  534.     public void setText(int row, int col, char c) {
  535.         fetchData(row, col).setText(c);
  536.     }
  537.  
  538.     /**
  539.      * Method used to support Defaultdata class.  Appends a character to the end
  540.      * of the textual representation of the cell's data.
  541.      */
  542.     public void appendChar(int row, int col, char c) {
  543.         fetchData(row, col).appendChar(c);
  544.     }
  545.  
  546.     /**
  547.      * Method used to support Defaultdata class. Clears the textual value of a cell.
  548.      */
  549.     public void clearText(int row, int col) {
  550.         if (data.contains(row, col)) {
  551.             ((Data)data.elementAt(row, col)).clearText();
  552.         }
  553.     }
  554.  
  555.     /**
  556.      * Method used to support Defaultdata class. Deletes a character from a cell's
  557.      * data.
  558.      * @param row The cell's row
  559.      * @param col The cell's column
  560.      * @param pos The position to delete character
  561.      */
  562.     public void deleteChar(int row, int col, int pos) {
  563.         if (data.contains(row, col)) {
  564.             ((Data)data.elementAt(row, col)).deleteChar(pos);
  565.         }
  566.     }
  567.  
  568.     /**
  569.      * Method used to support Defaultdata class. Gets a substring from a cell's
  570.      * data.
  571.      * @param row The cell's row
  572.      * @param col The cell's column
  573.      * @param spos The starting position of the substring, inclusive
  574.      * @param epos The last position of the substring, exclusive
  575.      */
  576.     public String subString(int row, int col, int spos, int epos) {
  577.         if (data.contains(row, col)) {
  578.             return ((Data)data.elementAt(row, col)).subString(spos, epos);
  579.         }
  580.  
  581.         throw new StringIndexOutOfBoundsException();
  582.     }
  583.  
  584.     /**
  585.      * Method used to support Defaultdata class. Sets the image data for a cell
  586.      */
  587.     public void setImage(int row, int col, Image i) {
  588.         fetchData(row, col).setImage(i);
  589.     }
  590.  
  591.     /**
  592.      * Method used to support Defaultdata class. Gets a string representation of
  593.      * a cell's data.
  594.      */
  595.     public String toString(int row, int col) {
  596.         if (data.contains(row, col)) {
  597.             return ((Data)data.elementAt(row, col)).toString();
  598.         } else {
  599.             return "";
  600.         }
  601.     }
  602.  
  603.     /**
  604.      * Method used to support Defaultdata class. Gets an image representationof
  605.      * a cell's data.
  606.      */
  607.     public Image toImage(int row, int col) {
  608.         if (data.contains(row, col)) {
  609.             return ((Data)data.elementAt(row, col)).toImage();
  610.         } else {
  611.             return null;
  612.         }
  613.     }
  614.  
  615. }
  616.  
  617.