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 / JDBCDataSourceDataStore.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  31.6 KB  |  1,003 lines

  1. /*
  2.  * @(#JDBCDataSourceDataStore.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.awt;
  9.  
  10. import symantec.itools.db.beans.binding.*;
  11. import symantec.itools.db.pro.*;
  12. import java.sql.SQLException;
  13. import java.util.BitSet;
  14. import java.util.StringTokenizer;
  15. import java.sql.Types;
  16.  
  17.  
  18. /**
  19.  * This implements data store for the TableView component that uses a JDBCDataSource
  20.  * for its data.
  21.  * <p>
  22.  * All rows coming from DbaDataSource are zero-relative.<br>
  23.  * All columns coming from DbaDataSource are one-relative.
  24.  *
  25.  * @see symantec.itools.db.awt.DbDataSource
  26.  * @see symantec.itools.db.awt.DbDataUpdater
  27.  * @see symantec.itools.db.awt.MetaTable
  28.  */
  29. public class JDBCDataSourceDataStore  implements DbDataStore, MetaTable, DbDataUpdater,
  30.                                                  TriggerUIListener/*, RowChangedListener*/{
  31.  
  32.     symantec.itools.db.beans.binding.QueryNavigator jdbcDataSource = null;
  33.     DataStoreMetaTable meta = null;
  34.  
  35.     int             coltoshow=0;
  36.     boolean fetchModeFlag = false;
  37.     DbDataSource    source = null;
  38.     boolean wantPrintOuts=false;
  39.     boolean ignoreEvent = false;
  40.  
  41.     /**
  42.      * Obsolete.
  43.      */
  44.     public static final int MAX_DISPLAY_SIZE = 60;
  45.  
  46.     /**
  47.      * Constructs a DbaDataStore that uses the given JDBCDataSource for data.
  48.      *
  49.      * @param rv the RelatioView to used for data access
  50.      * @param masterView obsolete
  51.      * @exception SQLException
  52.      * if a SQL database access error occurred
  53.      */
  54.     public JDBCDataSourceDataStore(symantec.itools.db.beans.binding.QueryNavigator jdbcDataSource){
  55.         if (jdbcDataSource != null)
  56.         {
  57.             try{
  58.                 this.jdbcDataSource = jdbcDataSource;
  59.                 Class   m_POFactory=null;
  60.                 java.util.StringTokenizer st = new java.util.StringTokenizer(jdbcDataSource.getClassName());
  61.                 String className = null;
  62.                 //the returned class name can be something like 'class JDBCRow'
  63.                 //only want 'JDBCRow'
  64.                 while(st.hasMoreTokens()){
  65.                     className = st.nextToken();
  66.                 }
  67.                 m_POFactory = Class.forName(className);
  68.                 PersistentObject newPO = (PersistentObject)m_POFactory.newInstance();
  69.  
  70.                 meta = new DataStoreMetaTable(newPO/*jdbcDataSource.getTable()*/);
  71.  
  72.                 jdbcDataSource.addTriggerUIListener(this);
  73.  
  74.             }catch(Exception e){}
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Clears all row data from this jdbcDataSource.
  80.      * <p>
  81.      * Note that setDbDataSource() must be called before using this method.
  82.      *
  83.      * @exception SQLException
  84.      * if a SQL database access error occurred
  85.      *
  86.      * @see #setDbDataSource
  87.      */
  88.     void resetRows() throws SQLException
  89.     {
  90.         if(wantPrintOuts)System.out.println("in resetrows");
  91.         ignoreEvent = true;
  92.         initRowMapping();
  93.         source.view.clear();
  94.         // source.view.redrawAsync();   // clear calls redrawAsync
  95.     }
  96.  
  97.     /**
  98.      * Sets the fetch mode for getting data.  When fetch mode is enabled,
  99.      * a grid expects to rapidly read lots of data for read only purposes.
  100.      * Knowing this can allow some data stores to more efficiently get the
  101.      * data.  The TableView provides a guarantee that the current data row will
  102.      * not be changed
  103.      * @param manual true if fetch mode is to be entered
  104.      */
  105.     public void fetchMode(boolean manual)
  106.     {
  107.        // if(wantPrintOuts)System.out.println("in fetchmode");
  108.  
  109.         setManRowChangeFlag(manual);
  110.     }
  111.  
  112.     /**
  113.      * Returns the current send notification flag.
  114.      * This is used to determine if the RelationView sends notifications
  115.      * when it's modified.
  116.      * When fetch mode is enabled this returns a value of true. == no notification
  117.      *
  118.      * @return the current send notification flag
  119.      * @see #fetchMode
  120.      */
  121.     public boolean manualRowChangeFlag()
  122.     {
  123.         if(wantPrintOuts)System.out.println("in manualRowChangeFlag");
  124.  
  125.         return fetchModeFlag;
  126.     }
  127.  
  128.     private void setManRowChangeFlag(boolean val)
  129.     {
  130.        // if(wantPrintOuts)System.out.println("in setManrowChangeFlag");
  131.  
  132.         fetchModeFlag = val;
  133.     }
  134.  
  135.     /**
  136.      * Sets the current row in the data store.
  137.      * <p>
  138.      * This is a standard DbDataStore interface method.
  139.      *
  140.      * @param row the zero-relative row index
  141.      * @exception TypeNotSupported
  142.      * if the data store does not support the type of action requested
  143.      * or is not successful
  144.      */
  145.     public void setCurrentRow(int row) throws TypeNotSupported {
  146.  
  147.  
  148.         if(wantPrintOuts)System.out.println("in set current row" + row);
  149.  
  150.         row++;//goto is 1 based
  151.  
  152.         ignoreEvent = true;
  153.  
  154.         try
  155.         {
  156.             {
  157.                 jdbcDataSource.go_to(row);
  158.             }
  159.         } catch(Exception ex)
  160.         {
  161.             throw new TypeNotSupported(ex.getMessage());
  162.         }
  163.  
  164.     }
  165.  
  166.     /**
  167.      * Determines if the given row is the current row in the data store.
  168.      *
  169.      * @param row the zero-relative index of the row to check
  170.      * @return true if it is the current row, false otherwise
  171.      * @see #setCurrentRow
  172.      */
  173.     public boolean isCurrentRow(int row) {
  174.  
  175.         if(wantPrintOuts)System.out.println("in isCurrentRow");
  176.         try
  177.         {
  178.             {
  179.                 return row == jdbcDataSource.getCurrentRowNumber();
  180.             }
  181.         }
  182.         catch(Exception ex)
  183.         {
  184.             return false;
  185.         }
  186.     }
  187.  
  188.     /**
  189.      * Sets the database data source that requests data and possibly
  190.      * caches the SQL data.
  191.      * <p>
  192.      * This is a standard DbDataStore interface method.
  193.      *
  194.      * @param ds the data source
  195.      */
  196.     public void setDbDataSource(DbDataSource ds) {
  197.  
  198.         if(wantPrintOuts)System.out.println("in setDbDataSource");
  199.  
  200.         if (source != null)
  201.         {
  202.             return;
  203.         }
  204.         source = ds;
  205.         initRowMapping();
  206.     }
  207.  
  208.     /**
  209.      * Called by a DbDataSource to determine if the store provides its own
  210.      * caching services.
  211.      * <p>
  212.      * This is a standard DbDataStore interface method.
  213.      *
  214.      * @return true if the store provides scrollable caching
  215.      */
  216.     public boolean supportsCaching() {
  217.         if(wantPrintOuts)System.out.println("in supportCaching");
  218.  
  219.         return true;
  220.     }
  221.  
  222.     /**
  223.      * Gets the last row that exists within the specified range.
  224.      * <p>
  225.      * This is a standard DbDataStore interface method.
  226.      *
  227.      * @param top the zero-relative index of the first row in the range
  228.      * @param bottom the zero-relative index of the last row in the range
  229.      * @return the last row that exists in the given range
  230.      * @exception DataNotAvailable
  231.      * if the requested data cannot be accessed in the data store
  232.      */
  233.     public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  234.         //iterate from top to bottom and find the last valid number in range
  235.  
  236.         if(wantPrintOuts)System.out.println("in ValidDataRowRange");
  237.  
  238.  
  239.         bottom += 3;  //a few extra for scrollbars
  240.  
  241.         int lastInRange = Math.max(0, top); //always want to ensure at least try
  242.                                             //first row
  243.  
  244.         try {
  245.             setManRowChangeFlag(true);
  246.  
  247.             while(lastInRange<=bottom) {
  248.                 jdbcDataSource.getElementAt(lastInRange);
  249.                 lastInRange++;
  250.             }
  251.         } catch(PositionOutOfRangeException e) {
  252.         } finally{
  253.             setManRowChangeFlag(false);
  254.         }
  255.  
  256.         if (lastInRange == top) {
  257.             throw new DataNotAvailable("top is greater than last row in database");
  258.         }
  259.  
  260.         lastIndex_ = (lastIndex_ < lastInRange) ? lastInRange : lastIndex_;
  261.  
  262.         return lastInRange;
  263.  
  264.  
  265.         //return bottom;
  266.     }
  267.  
  268.     /**
  269.      * Gets the data element for the specified position.
  270.      * <p>
  271.      * This is a standard DbDataStore interface method.
  272.      *
  273.      * @param row the zero-relative index of the element's row
  274.      * @param col the one-relative index of the element's column
  275.      * @return the element's data
  276.      * @exception DataNotAvailable
  277.      * if the requested data cannot be accessed in the data store
  278.      */
  279.     public Data getData(int row, int col) throws DataNotAvailable {
  280.  
  281.         //if(wantPrintOuts)System.out.println("in getdata");
  282.  
  283.         Data d = null;
  284.  
  285.        try {
  286.             setManRowChangeFlag(true);
  287.             String str = null;
  288.             Object ob = null;
  289.             ob = jdbcDataSource.getValueFromFirst(row, col-1);
  290.             if(ob != null){
  291.                 str = ob.toString();
  292.             }else{
  293.                 str = "";
  294.             }
  295.  
  296.  
  297.             d = new ImageStringData(source, str);
  298.  
  299.         } catch(Exception ex) {
  300.             throw new DataNotAvailable(ex.getMessage());
  301.         } finally {
  302.             setManRowChangeFlag(false);
  303.         }
  304.  
  305.         lastIndex_ = (lastIndex_ < row) ? row : lastIndex_;
  306.  
  307.         return d;
  308.     }
  309.  
  310.  
  311.     /**
  312.      * Sets a new value in the specified position.  The method is only
  313.      * called when the store supports caching.
  314.      * <p>
  315.      * This is a standard DbDataStore interface method.
  316.      *
  317.      * @param row the zero-relative index of the row to update
  318.      * @param col the one-relative index of the column to update
  319.      * @param data the data element that holds the new value
  320.      * @exception TypeNotSupported
  321.      * if the data store does not support the type of action requested
  322.      * or is not successful
  323.      */
  324.     public void update(int row, int col, Data data) throws TypeNotSupported {
  325.  
  326.         if(wantPrintOuts)System.out.println("in update");
  327.  
  328.         try {
  329.             setManRowChangeFlag(true);
  330.  
  331.             //ignoreEvent = true;
  332.  
  333.             jdbcDataSource.setValueFromFirst(data.toString(),row,col-1);
  334.         } catch(Exception ex) {
  335.             throw new TypeNotSupported(ex.getMessage());
  336.         } finally {
  337.             setManRowChangeFlag(false);
  338.         }
  339.  
  340.     }
  341.  
  342.     //BS: added NONEXISTS_ROW support
  343.  
  344.     /**
  345.      * Gets the current state of the specified row.
  346.      *
  347.      * @param row the zero-relative row index
  348.      * @return the state of the row's data. One of NEW_ROW, CLEAN_ROW, DELETED_ROW,
  349.      *          or MODIFIED_ROW or NONEXISTS_ROW
  350.      * @see symantec.itools.db.awt.DataSource#NEW_ROW
  351.      * @see symantec.itools.db.awt.DataSource#CLEAN_ROW
  352.      * @see symantec.itools.db.awt.DataSource#DELETED_ROW
  353.      * @see symantec.itools.db.awt.DataSource#MODIFIED_ROW
  354.      * @see symantec.itools.db.awt.DataSource#NONEXISTS_ROW
  355.      */
  356.     public int rowState(int row)
  357.     {
  358.  
  359.         if(wantPrintOuts)System.out.println("into Rowstate");
  360.  
  361.         int rowState = DataSource.NEW_ROW;
  362.         try
  363.         {
  364.             setManRowChangeFlag(true);
  365.  
  366.             boolean success = true;
  367.  
  368.             //we want the state of the row Row but without setting it as current
  369.             PersistentObject po = null;
  370.             try{
  371.                 po = (PersistentObject)jdbcDataSource.getElementAt(row);
  372.             }catch (PositionOutOfRangeException e) {
  373.                 success = false;
  374.             }
  375.  
  376.             if (!success)
  377.                 rowState = DataSource.NONEXISTS_ROW;
  378.  
  379.             if(rowState != DataSource.NONEXISTS_ROW){
  380.                 if(po != null){
  381.                     if(po.getMarkedAsDeleted()){
  382.                         rowState = DataSource.DELETED_ROW;
  383.                     }else
  384.                         if(po.getMarkedAsNew()){
  385.                             rowState = DataSource.NEW_ROW;
  386.                     }else
  387.                         if(po.getMarkedAsModified()){
  388.                             rowState = DataSource.MODIFIED_ROW;
  389.                     }else
  390.                         rowState = DataSource.CLEAN_ROW;
  391.  
  392.                 }    //now what?!?
  393.  
  394.             }
  395.         }
  396.         catch(Exception ex)
  397.         {
  398.             //if fails assume new
  399.             ex.printStackTrace();
  400.         } finally {
  401.             setManRowChangeFlag(false);
  402.         }
  403.  
  404.  
  405.         return rowState;
  406.     }
  407.  
  408.     /**
  409.      * Requests the data store clear all cached data.
  410.      * <p>
  411.      * This is a standard DbDataStore interface method.
  412.      *
  413.      */
  414.     public void clear() {
  415.  
  416.         if(wantPrintOuts)System.out.println("in clear");
  417.  
  418.         initRowMapping();
  419.         //time to start all over
  420.     }
  421.  
  422.     /**
  423.      * Requests the data store get the values from the database again.
  424.      * <p>
  425.      * This is a standard DbDataStore interface method.
  426.      *
  427.      */
  428.     public void refresh() {
  429.         if(wantPrintOuts)System.out.println("in refresh");
  430.        // try {
  431.             ignoreEvent = true;
  432.             //vj: Invoke the datasource restart method
  433.             //    We need to do this because of master/detail
  434.             //    changes.
  435.  
  436.             //jdbcDataSource.startQueryByExample();
  437.             //ignoreEvent = true;
  438.             //jdbcDataSource.executeQueryByExample();
  439.             jdbcDataSource.restart();
  440.             lastIndex_ = 0;
  441.  
  442.        // } catch(SQLException ex) {
  443.        // }
  444.     }
  445.  
  446.     /**
  447.      * Requests that any actions performed on a row be undone. The meaning
  448.      * is left open and is interpreted as appropriate for the type of
  449.      * data store.
  450.      * <p>
  451.      * This is a standard DbDataStore interface method.
  452.      *
  453.      * @param row the zero-relative row index
  454.      * @exception TypeNotSupported
  455.      * if the data store does not support the type of action requested
  456.      * or is not successful
  457.      */
  458.     public void undoRow(int row) throws TypeNotSupported {
  459.  
  460.         if(wantPrintOuts)System.out.println("in undorow");
  461.  
  462.         row++;
  463.         //vj: Set the row as the current row. Only this would enable multiple-undo's
  464.         ignoreEvent = true;
  465.         jdbcDataSource.go_to(row);
  466.         ignoreEvent = true;
  467.  
  468.       //  try {
  469.                 jdbcDataSource.undoChanges();
  470.       //  } catch(SQLException ex) {
  471.       //      throw new TypeNotSupported(ex.getMessage());
  472.       //  }
  473.     }
  474.  
  475.     /**
  476.      * Returns the number of rows actually retrieved from the database.
  477.      * <p>
  478.      * This is a standard DbDataStore interface method.
  479.      *
  480.      */
  481.     public int rowsRetrieved() {
  482.         //if(wantPrintOuts)System.out.println("in rowsretrieved");
  483.  
  484.         return lastIndex_;
  485.     }
  486.  
  487.     /**
  488.      * Requests the store to get all of the elements in the result set.
  489.      * <p>
  490.      * This is a standard DbDataStore interface method.
  491.      *
  492.      * @return the number of rows gotten
  493.      */
  494.     public int fetchAllRows() {
  495.         if(wantPrintOuts)System.out.println("in fetchallrows");
  496.  
  497.         int count=1;
  498.  
  499.         try {
  500.             setManRowChangeFlag(true);
  501.  
  502.             while(true) {
  503.                 jdbcDataSource.getElementAt(count);
  504.                 ignoreEvent = true;
  505.                 jdbcDataSource.go_to(count);
  506.                 count++;
  507.             }
  508.         }catch(PositionOutOfRangeException ex){
  509.         }finally {
  510.             setManRowChangeFlag(false);
  511.         }
  512.  
  513.         lastIndex_ = count-1;
  514.  
  515.         return count-1;
  516.     }
  517.  
  518.  
  519.     //-------------------------------------------------------------------------
  520.     //              DbDataUpdater Methods
  521.     //-------------------------------------------------------------------------
  522.  
  523.     /**
  524.      * Undeletes a row.
  525.      * <p>
  526.      * This is a standard DbDataUpdater interface method.
  527.      *
  528.      * @param row the zero-relative row index
  529.      * @exception TypeNotSupported
  530.      * if the data source does not support the type of action requested
  531.      * or is not successful
  532.      */
  533.     public void undeleteRow(int row) throws TypeNotSupported {
  534.         if(wantPrintOuts)System.out.println("in undeleteRow");
  535.             undoRow(row);
  536.      //   throw new TypeNotSupported("undelete row not supported"/*ex.getMessage()*/);
  537.     }
  538.  
  539.     /**
  540.      * Deletes a row or marks a row for deletion.
  541.      * <p>
  542.      * This is a standard DbDataUpdater interface method.
  543.      *
  544.      * @param row the zero-relative row index
  545.      * @exception TypeNotSupported
  546.      * if the data source does not support the type of action requested
  547.      * or is not successful
  548.      */
  549.     public void deleteRow(int row) throws TypeNotSupported {
  550.         if(wantPrintOuts)System.out.println("in deleteRow");
  551.  
  552.             row++;
  553.  
  554.             //BS: current record in relation view should be set to 'row'
  555.             //otherwise the current record is always the first and gets
  556.             //deleted
  557.             ignoreEvent = true;
  558.             jdbcDataSource.go_to(row);
  559.  
  560.             ignoreEvent = true;
  561.             jdbcDataSource.delete();
  562.  
  563.     }
  564.  
  565.     /**
  566.      * Saves the current state as appopriate.
  567.      * If this object caches, it iterates through the cache and updates
  568.      * all non-clean rows.
  569.      * <p>
  570.      * This is a standard DbDataUpdater interface method.
  571.      *
  572.      * @exception TypeNotSupported
  573.      * if the data source does not support the type of action requested
  574.      * or is not successful
  575.      */
  576.     public void save() throws TypeNotSupported {
  577.         if(wantPrintOuts)System.out.println("in save");
  578.         ignoreEvent = true;
  579.  
  580.        // try {
  581.             jdbcDataSource.saveAll();
  582.             if(wantPrintOuts)System.out.println("JDBCDataSourceDataStore: in save");
  583.        // } catch(SQLException ex) {
  584.        //     throw new TypeNotSupported(ex.getMessage());
  585.        // }
  586.     }
  587.  
  588.     /**
  589.      * Gets the next new Record within the RelationView's Record buffers.
  590.      * After calling this method, the current Record of the RelationView will
  591.      * be positioned at the new Record.
  592.      *
  593.      * @exception TypeNotSupported
  594.      * if the data source does not support the type of action requested
  595.      * or is not successful
  596.      */
  597.     public void getNewRecord() throws TypeNotSupported
  598.     {
  599.         if(wantPrintOuts)System.out.println("in getNewRecord");
  600.         ignoreEvent = true;
  601.  
  602.         showRecords(0,10);
  603.       //  try {
  604.             jdbcDataSource.insert();
  605.         showRecords(0,10);
  606.             //source.view.setFocusToRow(/*jdbcDataSource.getCurrentRowNumber() -*/1);
  607.             if(wantPrintOuts)System.out.println(jdbcDataSource.getCurrentRowNumber());
  608.       //  } catch(SQLException ex) {
  609.       //      throw new TypeNotSupported(ex.getMessage());
  610.       //  }
  611.     }
  612.  
  613.     /**
  614.      * Appends a new row.
  615.      * @param row ignored
  616.      * @exception TypeNotSupported
  617.      * if the data source does not support the type of action requested
  618.      * or is not successful
  619.      */
  620.     public void insertRow(int row) throws TypeNotSupported {
  621.         if(wantPrintOuts)System.out.println("in insertRow");
  622.  
  623.         appendRow();
  624.     }
  625.  
  626.     /**
  627.      * Appends a new row.
  628.      * <p>
  629.      * This is a standard DbDataUpdater interface method.
  630.      *
  631.      * @return the zero-relative index of the new row
  632.      * @exception TypeNotSupported
  633.      * if the data source does not support the type of action requested
  634.      * or is not successful
  635.      */
  636.     public int appendRow() throws TypeNotSupported {
  637.         if(wantPrintOuts)System.out.println("in appendRow");
  638.  
  639.         getNewRecord();
  640.         return 0;
  641.     }
  642.  
  643.     String m_InputColumnNames = null;
  644.  
  645.     public void setColumnsNamesToShow(String name){
  646.         m_InputColumnNames = name;
  647.     }
  648.  
  649.     //hides or shows columns depending of the DataBinding that was given to the Grid
  650.     public void setColumnsToShow() throws java.sql.SQLException {
  651.  
  652.         //suppress the part after '%'
  653.         StringTokenizer stf = new StringTokenizer(m_InputColumnNames,"%");
  654.         StringTokenizer st = null;
  655.  
  656.         if(stf.hasMoreTokens())
  657.             st = new StringTokenizer(stf.nextToken(),"@,");
  658.         String n = null;
  659.         if(st.hasMoreTokens())
  660.             n = st.nextToken();
  661.  
  662.         if(st.hasMoreTokens()){
  663.             //check if the 1st parameter is 'All', in that case, don't do anything
  664.             n = st.nextToken();
  665.             if(!n.equalsIgnoreCase("All")){
  666.                 int count = meta.getColumnCount();
  667.                 try{
  668.                     //first hide all columns (easier)
  669.                     for(int i = 0; i < count; i++)
  670.                         source.view.showColumn(i+1, false);
  671.                     boolean goon = true;
  672.                     do{
  673.                         //is this a valid column name ?
  674.                         try{
  675.                             source.view.showColumn(meta.columnNumberfromName(n)+1, true);//meta index is 0 based
  676.                         }catch(SQLException e){
  677.                             //this maybe a column index
  678.                             source.view.showColumn(Integer.parseInt(n), true);
  679.                         }
  680.                         if(st.hasMoreTokens())
  681.                             n = st.nextToken();
  682.                         else
  683.                             goon = false;
  684.                     }while(goon);
  685.                 }catch(NumberFormatException ex){
  686.                     //we got an error there, a parameter is neither a valid
  687.                     //column index, nor a valid column name. Show all columns
  688.                     for(int i = 0; i < count; i++)
  689.                         source.view.showColumn(i+1, true);
  690.                 }
  691.             }
  692.         }
  693.  
  694.     }
  695.     //-------------------------------------------------------------------------
  696.     //              MetaTable Methods
  697.     //-------------------------------------------------------------------------
  698.     public void setColtoShow(int val){
  699.         if(wantPrintOuts)System.out.println("in setColtoShow");
  700.  
  701.         coltoshow=val;
  702.     }
  703.  
  704.     /**
  705.      * Configures the given TableView the desired way.
  706.      * TableView setup often depends on the number and type of data fields
  707.      * that will be displayed.
  708.      * <p>
  709.      * This is a standard MetaTable interface method.
  710.      *
  711.      * @param view the grid to configure
  712.      */
  713.  
  714.  
  715.      //BS: setupTableView changed to setupTableView(TableView v);
  716.      //changed name of meth. and param.
  717.      //TableView.AUTONUMBER changed to TableView.AUTONUMBER
  718.  
  719.     public void setupTableView(TableView v) {
  720.         if(wantPrintOuts)System.out.println("in setupTableView");
  721.  
  722.         try {
  723.  
  724.             //create the proper number of columns
  725.             //make sure the BasicCell is set as default
  726.             v.setAutoRedraw(false);
  727.             int cols = meta.getColumnCount();
  728.             //int cols = coltoshow;
  729.             if(coltoshow==0||meta.getColumnCount()<=coltoshow)
  730.                 cols=meta.getColumnCount();
  731.             v.createColumns(cols);
  732.  
  733.             //setup row headings
  734.             v.setRowLabelHeadingStyle(TableView.AUTONUMBER);
  735.  
  736.             //set the column headings and widths
  737.             for (int col=1; col<=cols; col++) {
  738.                 int size = meta.getColumnDisplaySize(col-1);//JDBCRow has 0-based col indexes
  739.                 size = Math.min(0, size);
  740.                 size = Math.max(0, MAX_DISPLAY_SIZE);
  741.  
  742.                 v.setHeading(meta.getColumnLabel(col-1),
  743.                                 col,
  744.                                 java.lang.Math.min(size, 10));
  745.                 setupColumn(v, col);
  746.             }
  747.  
  748.             setColumnsToShow();
  749.         } catch(Exception ex) {
  750.             //Not much to do here but set one heading called error
  751.             //and set the message in the first cell
  752.         }finally{
  753.             v.setAutoRedraw(true);
  754.         }
  755.     }
  756.  
  757.     /**
  758.      * Obsolete.
  759.      */
  760.     public void setDataSource(DataSource ds) {
  761.         if(wantPrintOuts)System.out.println("in setDatasource");
  762.     }
  763.  
  764.     /**
  765.      * Determines if the data of the cell at the given location is
  766.      * user-editable.
  767.      * <p>
  768.      * This is a standard MetaTable interface method.
  769.      *
  770.  
  771.      * @param row the zero-relative row index
  772.      * @param col the one-relative column index
  773.      * @return true if it is user-editable, false otherwise
  774.      * @exception DataNotAvailable
  775.      * if the requested data cannot be accessed in the data store
  776.      */
  777.     public boolean isDataEditable(int row, int col) throws DataNotAvailable {
  778.         if(wantPrintOuts)System.out.println("in isDataEditable");
  779.  
  780.         //only tests on the column
  781.         //return true;
  782.         return meta.isWritable(col-1);
  783.     }
  784.  
  785.     //BS: added arrangeForViewing, this function newly defined in DataStore interface
  786.  
  787.     /**
  788.      * Adjusts the given data as needed before it is viewed in the TableView.
  789.      * <p>
  790.      * This is a standard MetaTable interface method.
  791.      *
  792.      * @param the data
  793.      * @return the arranged data
  794.      */
  795.     public Data[] arrangeForViewing(Data data[]) {
  796.         if(wantPrintOuts)System.out.println("in arrangeForViewing");
  797.         return data;
  798.     }
  799.  
  800.     /**
  801.      * Sets up the specified column in the given view as needed.
  802.      * Column setup often varies depending on the type of data that will
  803.      * be shown in the column.
  804.      * <p>
  805.      * This is a standard MetaTable interface method.
  806.      *
  807.      * @param view the TableView that is being set up
  808.      * @param col the one-relative position of the column to set up
  809.      */
  810.     public void setupColumn(TableView view, int col) throws java.sql.SQLException, DataNotAvailable {
  811.         if(wantPrintOuts)System.out.println("in setupColumn");
  812.  
  813.         //assume all cols aligned left
  814.        // try {
  815.             int type = meta.getColumnType(col-1);
  816.             switch(type) {
  817.                 case Types.BIGINT:
  818.                 case Types.INTEGER:
  819.                 case Types.DOUBLE:
  820.                 case Types.FLOAT:
  821.                 case Types.NUMERIC:
  822.                 case Types.SMALLINT:
  823.                 case Types.TINYINT:
  824.                 case Types.REAL:
  825.                     view.setColumnAlignment(col,TableView.RIGHT);
  826.                     break;
  827.                 case Types.CHAR:
  828.                 case Types.DATE:
  829.                 case Types.VARCHAR:
  830.                 case Types.LONGVARCHAR:
  831.                 case Types.NULL:
  832.                 case Types.OTHER:
  833.                 case Types.BINARY:
  834.                 case Types.VARBINARY:
  835.                 case Types.LONGVARBINARY:
  836.                 case Types.TIME:
  837.                 case Types.TIMESTAMP:
  838.                 case Types.BIT:
  839.                 default:
  840.                     view.setColumnAlignment(col,TableView.LEFT);
  841.             }
  842.  
  843.             if (meta.isCurrency(col-1)) {
  844.                 view.setColumnAlignment(col,TableView.RIGHT);
  845.             }
  846.  
  847.             if (!meta.isWritable(col-1)) {
  848.                 view.setColEditable(col, false);
  849.             }
  850.        // } catch(SQLException ex) {
  851.             //why not try a default
  852.        //     view.setColumnAlignment(col,TableView.LEFT);
  853.        // }
  854.     }
  855.  
  856.     int     lastIndex_ = 0;         //pointer to last element in map,
  857.  
  858.     private void initRowMapping() {
  859.         if(wantPrintOuts)System.out.println("in initRowMapping");
  860.  
  861.         lastIndex_ = 0;
  862.     }
  863.  
  864.  
  865.  
  866.     /**
  867.      * Processes a TriggerUIEvent
  868.      * <p>
  869.      * This is a standard TriggerUIListener interface method.
  870.      *
  871.      * @param e the TriggerUIEvent to process
  872.      */
  873.     public void commitUI(TriggerUIEvent e){
  874.  
  875.         if(wantPrintOuts)System.out.println("JDBCDataSourceDatastore: in commitUI; ignoreEvent:" +
  876.                             ignoreEvent);
  877.  
  878.         if(!ignoreEvent){
  879.             if(wantPrintOuts)System.out.println("type: " + e.m_Type);
  880.             switch(e.m_Type){
  881.                 case TriggerUIEvent.RECORDSETCHANGED:
  882.                     source.view.refreshView();
  883.                     break;
  884.                 case TriggerUIEvent.RECORDNAVIGATION:
  885.                 //vj: row is already zero-based
  886.                     //source.view.setFocusToRow(e.m_Row-1);
  887.                     source.view.setFocusToRow(e.m_Row + 1);
  888.                     break;
  889.                 case TriggerUIEvent.RECORDDELETED:
  890.                     source.view.redrawRow(e.m_Row -1);
  891.                     break;
  892.                 case TriggerUIEvent.RECORDINSERTED:
  893.                     source.view.refreshView();
  894.                 //vj: row is already zero-based
  895.                     //source.view.setFocusToRow(e.m_Row-1);
  896.                     source.view.setFocusToRow(e.m_Row + 1);
  897.                     break;
  898.                 case TriggerUIEvent.RECORDSAVEDALL:
  899.                     source.view.refreshView();
  900.                     break;
  901.                 case TriggerUIEvent.RECORDCHANGED:
  902.                     source.view.redrawRow(e.m_Row -1);
  903.                     if(wantPrintOuts)System.out.println("refreshing row: " + (e.m_Row -1));
  904.                     break;
  905.                 default:
  906.                     source.view.refreshView();
  907.                     ignoreEvent=true;
  908.                     break;
  909.             }
  910.         }else{
  911.             ignoreEvent = false;
  912.         }
  913.  
  914.     }
  915.  
  916.     //debug function
  917.     void showRecords(int start, int stop){
  918.  
  919.         if(wantPrintOuts)System.out.println("Current row: " + jdbcDataSource.getCurrentRowNumber());
  920.         for(int i = start; i < stop; i++){
  921.             try{
  922.                 if(wantPrintOuts)System.out.println("rowState: " + rowState(i));
  923.                 if(getData(i,1)==null)
  924.                     if(wantPrintOuts)System.out.println("getData(" + i + ",1) est null");
  925.                 if(wantPrintOuts)System.out.println("getData: " + i + " " + getData(i,1) + " " +
  926.                                     getData(i,2));
  927.             }catch(DataNotAvailable e){
  928.                 if(wantPrintOuts)System.out.println("showRecords caught an exception: " + e.getMessage());
  929.                 if(wantPrintOuts)System.out.println(e.getMessage());
  930.             }
  931.         }
  932.     }
  933.  
  934.     //we don't need to support the dataBinding method, the Grid supports it
  935.     public void setDataBinding(String dataNamme){}
  936.  
  937.  
  938.     public class DataStoreMetaTable {
  939.  
  940.         //The metaData has column indexes starting at 0, we use indexes starting
  941.         // at 1 !!!!
  942.  
  943.         symantec.itools.db.beans.binding.PersistentObject t = null;
  944.  
  945.         public DataStoreMetaTable(symantec.itools.db.beans.binding.PersistentObject table){
  946.             t = table;
  947.         }
  948.  
  949.         public int getColumnCount() throws java.sql.SQLException
  950.         {
  951.             return t.getDataModel().getMemberCount();
  952.         }
  953.  
  954.         public String getColumnLabel(int col) throws java.sql.SQLException
  955.         {
  956.             return t.getDataModel().getMemberModel(col).getName();
  957.         }
  958.  
  959.  
  960.         /* returns a int corresponding to a type as defined in java.sql.Types
  961.         */
  962.         public int getColumnType(int col) throws java.sql.SQLException
  963.         {
  964.             return t.getDataModel().getMemberModel(col).getType();
  965.         }
  966.  
  967.         public boolean isWritable(int col) throws DataNotAvailable
  968.         {
  969.             return t.getDataModel().getMemberModel(col).isWritable();
  970.         }
  971.  
  972.         /* not supported by columnMetaData
  973.         */
  974.         public int getColumnDisplaySize(int col) throws java.sql.SQLException
  975.         {
  976.             return (getColumnLabel(col)).length();
  977.         }
  978.  
  979.         /* not supported by columnMetaData
  980.         */
  981.         public boolean isCurrency(int col)
  982.         {
  983.             return false;
  984.         }
  985.  
  986.         public int columnNumberfromName(String n) throws SQLException{
  987.             int col = 0;
  988.             int count = getColumnCount();
  989.             while(col < count){
  990.                 if(n.equals(getColumnLabel(col))){
  991.                     return col;
  992.                 }
  993.                 col++;
  994.             }
  995.             throw new SQLException("column name: " + n + "not found!");
  996.         }
  997.     }
  998.  
  999. }
  1000.  
  1001.  
  1002.  
  1003.