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 / ProjectionBeanHelper.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  12.6 KB  |  406 lines

  1. /*
  2.  * @(#ProjectionBeanHelper.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.pro.*;
  11. import java.sql.SQLException;
  12. import java.io.*;
  13. import java.awt.event.*;
  14. import java.awt.*;
  15. import symantec.itools.db.beans.binding.Name;
  16.  
  17. /**
  18.  * Common code required to implement dbAWARE components.
  19.  */
  20. public class ProjectionBeanHelper extends KeyAdapter
  21.     implements ProjLink, ActionListener, FocusListener, ItemListener, TextListener {
  22.  
  23.     private ProjBinder m_ProjBinder; // Object used to interact with server
  24.  
  25.     private int m_ColumnType = 0;
  26.     private boolean m_IsBound = false;
  27.     private boolean m_DynamicUpdate = true;
  28.     private boolean m_BinderDetermines = true;
  29.     private Object  m_BinderData;     // Keep in synch with database
  30.     private Object  m_ScreenData;    // Keep in synch with screen text
  31.     private RelationView    m_RelationView;
  32.     private String          m_ProjectionName = "";
  33.  
  34.     private ProjectionBean  m_ProjectionBean;
  35.     private String[] m_GetMethods={"getData()"};
  36.     private String[] m_SetMethods={"setData(Value)"};
  37.  
  38.     /**
  39.      * Constant value for the Triggering Event property.
  40.      * Indicates the triggering event is ACTION_PERFORMED.
  41.      */
  42.     public static final int Action_Event=0;
  43.     /**
  44.      * Constant value for the Triggering Event property.
  45.      * Indicates the triggering event is FOCUS_LOST.
  46.      */
  47.     public static final int Focus_Event=1;
  48.     /**
  49.      * Constant value for the Triggering Event property.
  50.      * Indicates the triggering event is KEY_PRESSED.
  51.      */
  52.     public static final int Key_Event=2;
  53.     /**
  54.      * Constant value for the Triggering Event property.
  55.      * Indicates the triggering event is ITEM_STATE_CHANGED.
  56.      */
  57.     public static final int Item_Event=5;
  58.     /**
  59.      * Constant value for the Triggering Event property.
  60.      * Indicates the triggering event is TEXT_VALUE_CHANGED.
  61.      */
  62.     public static final int Text_Event=4;
  63.  
  64.     /**
  65.      * A constant value indicating how an empty string will be saved in the database.
  66.      */
  67.     public static final String BLANK="Blank";
  68.     /**
  69.      * A constant value indicating how an empty string will be saved in the database.
  70.      */
  71.     public static final String NULL="Null";
  72.     /**
  73.      * A constant value indicating how an empty string will be saved in the database.
  74.      */
  75.     public static final String DEFAULT="Default";
  76.  
  77.     /**
  78.      * The mediator used to bind this component with the data source.
  79.      */
  80.     public symantec.itools.db.beans.binding.Mediator m_Mediator;
  81.  
  82.     ProjectionBeanHelper(ProjectionBean bean) {
  83.  
  84.         m_ScreenData = new String();
  85.         m_BinderData = new String();
  86.         m_ProjectionBean = bean;
  87.         m_Mediator=new symantec.itools.db.beans.binding.Mediator();
  88.         m_Mediator.setOutput(m_ProjectionBean);
  89.         m_Mediator.setSetMethods(m_SetMethods);
  90.         m_Mediator.setGetMethods(m_GetMethods);
  91.     }
  92.  
  93.     /**
  94.      * Called when this component has been bound to a projection within an
  95.      * RelationView.  This component notes the ProjBinder object so that it
  96.      * may notify the dbANYWHERE server of data changes made.
  97.      * <p>
  98.      * This is a method in the ProjLink interface.
  99.      *
  100.      * @param binder used to notify the dbANYWHERE server of any data changes
  101.      * that are to be persistant.
  102.      * This object is a window into the particular column data to which this
  103.      * component has been bound
  104.      *
  105.      * @see symantec.itools.db.pro.ProjLink#init
  106.      */
  107.     public void init(ProjBinder binder) {
  108.         synchronized (this) {
  109.             m_IsBound = true;
  110.             m_ProjBinder = binder;
  111.             setReadOnly(binder);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * This method is called to ensure the value of this dbAWARE component is
  117.      * not lost.
  118.      * It is called when the dbANYWHERE server has detected a data change made
  119.      * within the current row of the RelationView bound to this component.
  120.      * The ProjBinder object passed into the notification is used to set the
  121.      * new data value.
  122.      * <p>
  123.      * This is a method in the ProjLink interface.
  124.      *
  125.      * @param binder used to notify the dbANYWHERE server of any data changes
  126.      * that are to be persistant.
  127.      * This object is a window into the particular column data to which this
  128.      * component has been bound
  129.      * @return   true if success or false if failure.
  130.      * @exception SQLException
  131.      * if a SQL database access error occurred
  132.      * @see symantec.itools.db.pro.ProjLink#notifySetData
  133.      */
  134.     public boolean notifySetData(ProjBinder binder) throws SQLException
  135.     {
  136.       //   return notifyInputChanged();
  137.       return false;
  138.     }
  139.  
  140.     /**
  141.      * This method is called to update the value of this dbAWARE component.
  142.      * It is called when the dbANYWHERE server has detected a data change made
  143.      * within the scope of the current row of the RelationView bound to this
  144.      * component.
  145.      * The ProjBinder object passed into the notification is used to get the
  146.      * new data value.
  147.      * <p>
  148.      * This is a method in the ProjLink interface.
  149.      *
  150.      * @param binder used to get the current value from the dbANYWHERE server.
  151.      * This object is a window into the particular column data to which this
  152.      * component has been bound
  153.      *
  154.      * @see symantec.itools.db.pro.ProjLink#notifyDataChange
  155.      */
  156.     //  Step 1: set m_ProjBinder equal to binder object                        //
  157.     //  Step 2: synchronize m_BinderData with binder object                    //
  158.     //  Step 3: synchronize m_ScreenData with current screen                   //
  159.     //  Step 4: if the two are different then                                  //
  160.     //       - set m_ScreenData equal to m_BinderData to avoid recursion       //
  161.     //       - call setText to update screen                                   //
  162.     //  Step 5: enable or disable editing based on binder                      //
  163.     //                                                                         //
  164.  
  165.     public void notifyDataChange(ProjBinder binder)
  166.     {
  167.  
  168.     }
  169.  
  170.     /**
  171.      * This method is called by the component when it
  172.      * may need to be re-bound to the data source.
  173.      */
  174.     public void notifyDataChange()
  175.     {
  176.         setDataBinding(getDataBinding());
  177.     }
  178.  
  179.     void raiseException(Exception ex)
  180.     {
  181.         System.out.println("Exception class [" + ex.getClass().getName() + "]");
  182.         System.out.println("Exception message [" + ex.getMessage() + "]");
  183.         Thread.dumpStack();
  184.     }
  185.  
  186.     void setReadOnly(ProjBinder binder)
  187.     {
  188.         //Disable input if binder is not writeable
  189.         boolean isWritable = false;
  190.  
  191.         try
  192.         {
  193.             if (binder != null)
  194.             {
  195.                 isWritable = binder.isWritable();
  196.             }
  197.         }
  198.         catch (Exception Ex)
  199.         {
  200.             raiseException(Ex);
  201.         }
  202.  
  203.         m_ProjectionBean.setReadOnly(!isWritable);
  204.     }
  205.  
  206.  
  207.     boolean notifyInputChanged(Object input)
  208.     {
  209.         m_Mediator.commit();
  210.         return true;
  211.     }
  212.  
  213.     /**
  214.      * Binds this component to a given projection within the specified
  215.      * relation view.
  216.      *
  217.      * @param relView the relation view to bind with
  218.      * @param projection the projection in relView to bind with
  219.      */
  220.     synchronized void setBinding(RelationView relView, String projection)
  221.     {
  222.  
  223.         m_RelationView = relView;
  224.         m_ProjectionName = projection;
  225.  
  226.         if (m_RelationView == null || m_ProjectionName == null) {
  227.             return; // nothing to do
  228.         }
  229.  
  230.         if (m_ProjectionName.equals("")) {
  231.             return; //  nothing to do
  232.         }
  233.  
  234.         try {
  235.             Name name = new Name(m_RelationView.getName(),m_ProjectionName);
  236.             m_Mediator.setDataBinding(name.getFullName());
  237.         }
  238.         catch (java.sql.SQLException e) {
  239.             raiseException(e);
  240.         }
  241.  
  242.     }
  243.  
  244.     synchronized void setDataBinding(Name name)
  245.     {
  246.         m_Mediator.setDataBinding(name.getFullName());
  247.     }
  248.  
  249.     synchronized Name getDataBinding()
  250.     {
  251.         return  new Name(m_Mediator.getDataBinding());
  252.     }
  253.  
  254.     synchronized void setBinderDeterminesReadOnly(boolean value) {
  255.         m_BinderDetermines = value;
  256.     }
  257.  
  258.     synchronized void setTreatBlankAs(int value) {
  259.         m_Mediator.setEmptyMeansNull(value == m_Mediator.Null);
  260.     }
  261.  
  262.     synchronized void setTreatBlankAsString(String value) {
  263.         if(value.equals(BLANK))
  264.         setTreatBlankAs(symantec.itools.db.beans.binding.Mediator.Blank);
  265.         else if(value.equals(NULL))
  266.         setTreatBlankAs(symantec.itools.db.beans.binding.Mediator.Null);
  267.         else setTreatBlankAs(symantec.itools.db.beans.binding.Mediator.Default);
  268.     }
  269.  
  270.     synchronized void setDynamicUpdate(boolean value) {
  271.       /*  if(m_DynamicUpdate!=value && value==true){
  272.             (m_ProjectionBean).setTriggeringEvent(Key_Event);
  273.         }
  274.         if(m_DynamicUpdate!=value && value==false){
  275.             ((Component)m_ProjectionBean).removeKeyListener(this);
  276.         }*/
  277.         m_DynamicUpdate = value;
  278.     }
  279.  
  280.     synchronized boolean getDynamicUpdate() {
  281.         return m_DynamicUpdate;
  282.     }
  283.  
  284.     synchronized void setProjection(String value) {
  285.         setBinding(m_RelationView, value);
  286.     }
  287.  
  288.     synchronized String getProjection() {
  289.         return m_ProjectionName;
  290.     }
  291.  
  292.     synchronized void setRelationView(RelationView value) {
  293.         setBinding(value, m_ProjectionName);
  294.     }
  295.  
  296.     synchronized RelationView getRelationView() {
  297.         return m_RelationView;
  298.     }
  299.  
  300.     synchronized String getRelationViewName() {
  301.         if (m_RelationView != null) {
  302.             try {
  303.                 return m_RelationView.getName();
  304.             }
  305.             catch (Exception ex) {
  306.                 raiseException(ex);
  307.             }
  308.         }
  309.         return "null";
  310.     }
  311.  
  312.     synchronized ProjBinder getBinder() {
  313.         return m_ProjBinder;
  314.     }
  315.  
  316.     /**
  317.      * Handles Focus Gained events.
  318.      * @param e the FocusEvent
  319.      */
  320.     public void focusGained(FocusEvent e) {
  321.     }
  322.  
  323.     /**
  324.      * Handles Focus Lost events.
  325.      * Used to commit data to the data source.
  326.      * @param e the FocusEvent
  327.      */
  328.  
  329.     public void focusLost(FocusEvent e) {
  330.         m_Mediator.commit();
  331.     }
  332.  
  333.     /**
  334.      * Invoked when a key has been released.
  335.      * Used to commit data to the data source.
  336.      * @param e the event
  337.      */
  338.     public void keyReleased(KeyEvent e) {
  339.         m_Mediator.commit();
  340.  
  341.     }
  342.  
  343.     /**
  344.      * Invoked when an action occurs.
  345.      * Used to commit data to the data source.
  346.      * @param e the event
  347.      */
  348.     public void actionPerformed(ActionEvent e) {
  349.         m_Mediator.commit();
  350.     }
  351.  
  352.     /**
  353.      * Invoked when an item's state has been changed.
  354.      * Used to commit data to the data source.
  355.      * @param e the event
  356.      */
  357.     public void itemStateChanged(ItemEvent e) {
  358.         m_Mediator.commit();
  359.     }
  360.  
  361.     /**
  362.      * Invoked when the value of the text has changed.
  363.      * Used to commit data to the data source.
  364.      * @param e the event
  365.      */
  366.     public void textValueChanged(TextEvent e) {
  367.         m_Mediator.commit();
  368.     }
  369.     /**
  370.      * Gets the mediator used to bind the component to a data source.
  371.      * @return the mediator
  372.      */
  373.     public symantec.itools.db.beans.binding.Mediator getMediator()
  374.     {
  375.         return m_Mediator;
  376.     }
  377.  
  378.      /**
  379.      * Sets the value of the Empty Means Null property.
  380.      * This determines how an empty string will be saved when updating data on
  381.      * the database server.
  382.      * A value of <code>true</code>indicates an empty string will be saved as a
  383.      * SQL NULL value. Otherwise it will be saved as a blank value.
  384.      * @param propertyValue <code>true</code> for NULL, <code>false</code> for BLANK
  385.      * @see #getEmptyMeansNull
  386.      */
  387.     public void setEmptyMeansNull(boolean propertyValue)
  388.     {
  389.         m_Mediator.setEmptyMeansNull(propertyValue);
  390.     }
  391.  
  392.      /**
  393.      * Gets the value of the Empty Means Null property.
  394.      * This determines how an empty string will be saved when updating data on
  395.      * the database server.
  396.      * A value of <code>true</code>indicates an empty string will be saved as a
  397.      * SQL NULL value. Otherwise it will be saved as a blank value.
  398.      * @return <code>true</code> for NULL, <code>false</code> for BLANK
  399.      * @see #setEmptyMeansNull
  400.      */
  401.     public boolean getEmptyMeansNull()
  402.     {
  403.         return m_Mediator.getEmptyMeansNull();
  404.     }
  405. }
  406.