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 / List.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  14.1 KB  |  481 lines

  1. /*
  2.  * @(#List.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8.  
  9. package symantec.itools.db.awt;
  10.  
  11. import java.util.*;
  12. import java.sql.*;
  13. import java.lang.*;
  14. import symantec.itools.db.net.*;
  15. import symantec.itools.db.pro.*;
  16. import java.awt.event.*;
  17.  
  18. /**
  19.  * A dbAWARE List component.
  20.  * <p>
  21.  * This component can be "bound" to a projection within a relation view
  22.  * so that it automatically gets and sets the values in that relation.
  23.  * <p>
  24.  */
  25. public class List extends java.awt.List implements ListLink, ProjectionBean
  26. {
  27.  
  28.     /**
  29.      * The  Binder object which this component can use to get data from
  30.      * the server, or change data at the database server.
  31.      */
  32.     private ListBinder   m_ListBinder;
  33.  
  34.     private boolean m_useStream = false;
  35.  
  36.     private RelationView    m_LookUpRelationView;
  37.     private Vector          m_JoinColumns;
  38.     private Vector          m_JoinColumnNames;
  39.     private ConnectionInfo  m_Conn;
  40.     private String          m_SQL;
  41.  
  42.     private ProjectionBeanHelper m_Helper;
  43.  
  44.     /**
  45.      * A constant value indicating how an empty string will be saved in the database.
  46.      */
  47.     public static final String Default = ProjectionBeanHelper.DEFAULT;
  48.     /**
  49.      * A constant value indicating how an empty string will be saved in the database.
  50.      */
  51.     public static final String Blank = ProjectionBeanHelper.BLANK;
  52.     /**
  53.      * A constant value indicating how an empty string will be saved in the database.
  54.      */
  55.     public static final String Null = ProjectionBeanHelper.NULL;
  56.  
  57.     /**
  58.      * Constructs a new List linked to a database.
  59.      *
  60.      * @param lookupRV the relation view to bind with
  61.      * @param conn the information needed to connect to the database
  62.      */
  63.     public List(RelationView lookupRV, ConnectionInfo conn)
  64.     {
  65.         this();
  66.         m_LookUpRelationView = lookupRV;
  67.         m_Conn = conn;
  68.         m_Helper = new ProjectionBeanHelper(this);
  69.         addFocusListener(m_Helper);
  70.     }
  71.  
  72.     /**
  73.      * Constructs a new List with no data connectivity, initially.
  74.      *
  75.      * @param rows defines the number of rows of items to display.
  76.      */
  77.     public List(int rows)
  78.     {
  79.         super(rows, false);
  80.         m_JoinColumns = new Vector();
  81.         m_JoinColumnNames = new Vector();
  82.         m_SQL = new String();
  83.         m_Helper = new ProjectionBeanHelper(this);
  84.     }
  85.  
  86.     /**
  87.      * Constructs a default List with no data connectivity, initially.
  88.      */
  89.     public List()
  90.     {
  91.         this(0);
  92.     }
  93.  
  94.     /**
  95.      * Called when this component has been bound to a RelationView.
  96.      * This component notes the ListBinder object so that it
  97.      * may notify the dbANYWHERE server of data changes made.
  98.      * <p>
  99.      * This is a method in the ListLink interface.
  100.      *
  101.      * @param lBinder used to notify the dbANYWHERE server of any data changes
  102.      * that are to be persistant.
  103.      *
  104.      * @see symantec.itools.db.pro.ListLink#init
  105.      */
  106.     public void init (ListBinder lBinder)
  107.     {
  108.         m_ListBinder = lBinder;
  109.     }
  110.  
  111.     /**
  112.      * This method is called to update the value of this dbAWARE component.
  113.      * It is called when the dbANYWHERE server has detected a data change made
  114.      * within the RelationView bound to this component.
  115.      * The ListBinder object passed into the notification is used to get the
  116.      * new data value.
  117.      * <p>
  118.      * This is a method in the ListLink interface.
  119.      *
  120.      * @param binder used to get the current value from the dbANYWHERE server.
  121.      *
  122.      * @see symantec.itools.db.pro.ListLink#notifyListChange
  123.      */
  124.     public void notifyListChange(ListBinder lBinder)
  125.     {
  126.         try
  127.         {
  128.             RelationView curRV = lBinder.getRelationView();
  129.             clear();
  130.             while ( curRV.next() == true )
  131.             {
  132.                 if (!(curRV.isNull(1)))
  133.                 {
  134.                     super.addItem( curRV.getString(1));
  135.                 }
  136.             }
  137.             Enumeration e = staticItems.elements();
  138.             while(e.hasMoreElements())
  139.             {
  140.                 super.addItem((String)e.nextElement());
  141.             }
  142.         }
  143.         catch (SQLException Ex)
  144.         {
  145.             m_Helper.raiseException (Ex);
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Adds a column to the list of columns that this component will be bound
  151.      * to. Use this method before calling bindList().
  152.      *
  153.      * @param columnName the name of the column to join
  154.      * @exception SQLException
  155.      * if a SQL database access error occurred
  156.      * @see #bindList
  157.      */
  158.     public void join(String columnName) throws SQLException
  159.     {
  160.         if (!java.beans.Beans.isDesignTime()) {
  161.             int colID = m_LookUpRelationView.findProjByName(columnName);
  162.             if (m_JoinColumns == null)
  163.             {
  164.                 m_JoinColumns = new Vector();
  165.             }
  166.             m_JoinColumns.addElement(new Integer(colID));
  167.         }
  168.         if (m_JoinColumnNames == null)
  169.         {
  170.             m_JoinColumnNames = new Vector();
  171.         }
  172.         m_JoinColumnNames.addElement(new String(columnName));
  173.     }
  174.  
  175.     /**
  176.      * Sets the sql string to use while generating the list for this component
  177.      * to bind to. Use this method before calling bindList().
  178.      *
  179.      * @param sql the sql string
  180.      * @exception SQLException
  181.      * if a SQL database access error occurred
  182.      *
  183.      * @see #bindList
  184.      * @see #getSelectClause
  185.      */
  186.     public void setSQL(String sql) throws SQLException
  187.     {
  188.         m_SQL = sql;
  189.     }
  190.  
  191.     /**
  192.      * Binds this component to a list of columns within a RelationView.
  193.      * The RelationView and connection information to use is specified at
  194.      * construction time. The columns to use are specified using the join()
  195.      * method.
  196.      *
  197.      * @see #join
  198.      * @exception SQLException
  199.      * if a SQL database access error occurred
  200.      */
  201.     public void bindList() throws SQLException
  202.     {
  203.         m_LookUpRelationView.bindList(m_Conn, m_SQL, this, m_JoinColumns);
  204.         m_Helper.notifyDataChange();
  205.     }
  206.  
  207.  
  208.     /**
  209.      * Adds the specified item to the end of the list.
  210.      * @param item the item to be added
  211.      */
  212.     public synchronized void addItem(String item) {
  213.         super.addItem(item, -1);
  214.         staticItems.addElement(item);
  215.     }
  216.  
  217.     private Vector staticItems = new Vector();
  218.  
  219.     /**
  220.      * Binds this component to a given projection within the specified
  221.      * relation view.
  222.      *
  223.      * @param relView the relation view to bind with
  224.      * @param projection the projection in relView to bind with
  225.      */
  226.     public void setBinding(RelationView relView, String projection)
  227.     {
  228.         m_Helper.setBinding(relView, projection);
  229.     }
  230.  
  231.     /**
  232.      * Returns the projection in the RelationView that this component is bound with.
  233.      * @see #setBinding
  234.      * @see #setProjection
  235.      */
  236.     public String getProjection() {
  237.         return m_Helper.getProjection();
  238.     }
  239.  
  240.     /**
  241.      * Binds this component to the given projection within the RelationView
  242.      * the component is currently bound with.
  243.      * @see #setBinding
  244.      * @see #getProjection
  245.      * @see #getRelationView
  246.      */
  247.     public void setProjection(String projection) {
  248.         m_Helper.setProjection(projection);
  249.     }
  250.  
  251.     /**
  252.      * Gets the RelationView that this component is bound with.
  253.      * @return the RelationView currently bound with
  254.      * @see #setRelationView
  255.      * @see #setBinding
  256.      * @see #getProjection
  257.      */
  258.     public RelationView getRelationView() {
  259.         return m_Helper.getRelationView();
  260.     }
  261.  
  262.     /**
  263.      * Sets the RelationView that this component is bound with.
  264.      * @param value the RelationView to bind with
  265.      * @see #getRelationView
  266.      * @see #setBinding
  267.      * @see #setProjection
  268.      */
  269.     public void setRelationView(RelationView value) {
  270.         m_Helper.setRelationView(value);
  271.     }
  272.  
  273.     /**
  274.      * Specifies how an empty string will be set when updating data on
  275.      * the dbANYWHERE server.
  276.      *
  277.      * @param blank one of "DEFAULT", "NULL", or "BLANK"
  278.      *
  279.      * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
  280.      */
  281.     public void setTreatBlankAs(String value) {
  282.         m_Helper.setTreatBlankAsString(value);
  283.     }
  284.  
  285.     /**
  286.      * Indicates when the component commits its changes.
  287.      * @see #setDynamicUpdate
  288.      */
  289.     public boolean getDynamicUpdate() {
  290.         return m_Helper.getDynamicUpdate();
  291.     }
  292.  
  293.     /**
  294.      * Sets when the component commits its changes.
  295.      * @param value the new dynamic update mode value
  296.      * @see #getDynamicUpdate
  297.      */
  298.     public void setDynamicUpdate(boolean value) {
  299.         m_Helper.setDynamicUpdate(value);
  300.         if (value) {
  301.             removeFocusListener(m_Helper);
  302.             addItemListener(m_Helper);
  303.         }
  304.         else {
  305.             removeItemListener(m_Helper);
  306.             addFocusListener(m_Helper);
  307.         }
  308.     }
  309.  
  310.     /**
  311.      * Sets whether the data value of this component may be modified.
  312.      * @param value <code>true</code> if the value may not be modified,
  313.      * <code>false</code>if the value may be modified
  314.      */
  315.     public void setReadOnly(boolean value) {
  316.     }
  317.  
  318.     /**
  319.      * Gets the RelationView this component is bound with.
  320.      * @return the RelationView
  321.      */
  322.     public RelationView getLookUpRelationView() {
  323.         return m_LookUpRelationView;
  324.     }
  325.  
  326.     /**
  327.      * Sets the RelationView this component is bound with.
  328.      * @param value the RelationView
  329.      */
  330.     public void setLookUpRelationView(RelationView value) {
  331.     }
  332.  
  333.     /**
  334.      * Gets the ConnectionInfo this component uses to connect with the database.
  335.      * @return the information needed to connect to the database
  336.      * @see #setConnectionInfo
  337.      */
  338.     public ConnectionInfo getConnectionInfo() {
  339.         return m_Conn;
  340.     }
  341.  
  342.     /**
  343.      * Sets the ConnectionInfo this component uses to connect with the database.
  344.      * @param conn the information needed to connect to the database
  345.      * @see #getConnectionInfo
  346.      */
  347.     public void setConnectionInfo(ConnectionInfo conn) {
  348.         m_Conn = conn;
  349.     }
  350.  
  351.  
  352.     /**
  353.      * Gets the sql string to use while generating the list for this component
  354.      * to bind to.
  355.      * @return the sql statment
  356.      * @see #setSQL
  357.      */
  358.     public String getSelectClause() {
  359.         return m_SQL;
  360.     }
  361.  
  362.     /**
  363.      * Sets the value of this component to the given value.
  364.      * @param value the new component value
  365.      * @see #getData
  366.      */
  367.     public void setData(Object value) {
  368.         String text = (String)value;
  369.         boolean bitemFound = false;
  370.         for (int index = 0; index < countItems(); index++)
  371.         {
  372.             if (text.equals(getItem(index)))
  373.             {
  374.                 select(index);
  375.                 makeVisible(index);
  376.                 bitemFound = true;
  377.                 break;
  378.             }
  379.             if (!bitemFound)
  380.             {
  381.                 if (getSelectedIndex() != -1)
  382.                 {
  383.                     deselect(getSelectedIndex());
  384.                 }
  385.             }
  386.         }
  387.     }
  388.  
  389.     /**
  390.      * Gets the value of this component.
  391.      * @return the current component value
  392.      * @see #setData
  393.      */
  394.     public Object getData() {
  395.         String text = null;
  396.         String[] selectedItems = getSelectedItems();
  397.         if (selectedItems.length == 1)
  398.         {
  399.             text = selectedItems[0];
  400.         }
  401.         return text;
  402.     }
  403.  
  404.     /**
  405.      * Gets whether this component saves its value as a text String.
  406.      * @return <code>true</code> if the value is saved as text,
  407.      * <code>false</code> otherwise
  408.      */
  409.     public boolean isTextBased() {
  410.         return true;
  411.     }
  412.  
  413.     /**
  414.      * Gets the list of columns that this component will be bound to.
  415.      * @return the list of columns
  416.      * @see #join
  417.      */
  418.     public String[] getJoinColumns() {
  419.         String[] joinColumns = new String[m_JoinColumnNames.size()];
  420.         for (int index = 0; index < m_JoinColumnNames.size(); index++) {
  421.             joinColumns[index] = new String((String)m_JoinColumnNames.elementAt(index));
  422.         }
  423.         return joinColumns;
  424.     }
  425.  
  426.     /**
  427.      * Sets the list of columns that this component will be bound to.
  428.      * Use this method before calling bindList().
  429.      * @param value the names of the columns to join
  430.      * @see #join
  431.      * @see #getJoinColumns
  432.      * @see #bindList
  433.      */
  434.     public void setJoinColumns(String[] value) {
  435.     }
  436.  
  437.     /**
  438.      * Gets the number of digits to the right of the decimal point for
  439.      * this component's value.
  440.      * @return the number of digits to the right of the decimal point
  441.      */
  442.     public int getScale() {
  443.         return ProjBinder.DEFAULTSCALE;
  444.     }
  445.  
  446.     /**
  447.      * Registers the standard event listener(s) for this component.
  448.      */
  449.     public void registerListeners() {
  450.         addItemListener(m_Helper);
  451.     }
  452.  
  453.      /**
  454.      * Sets the value of the Empty Means Null property.
  455.      * This determines how an empty string will be saved when updating data on
  456.      * the database server.
  457.      * A value of <code>true</code>indicates an empty string will be saved as a
  458.      * SQL NULL value. Otherwise it will be saved as a blank value.
  459.      * @param propertyValue <code>true</code> for NULL, <code>false</code> for BLANK
  460.      * @see #getEmptyMeansNull
  461.      */
  462.     public void setEmptyMeansNull(boolean propertyValue)
  463.     {
  464.         m_Helper.setEmptyMeansNull(propertyValue);
  465.     }
  466.  
  467.      /**
  468.      * Gets the value of the Empty Means Null property.
  469.      * This determines how an empty string will be saved when updating data on
  470.      * the database server.
  471.      * A value of <code>true</code>indicates an empty string will be saved as a
  472.      * SQL NULL value. Otherwise it will be saved as a blank value.
  473.      * @return <code>true</code> for NULL, <code>false</code> for BLANK
  474.      * @see #setEmptyMeansNull
  475.      */
  476.     public boolean getEmptyMeansNull()
  477.     {
  478.         return m_Helper.getEmptyMeansNull();
  479.     }
  480. }
  481.