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 / TextField.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  12.5 KB  |  419 lines

  1. /*
  2.  * @(#TextField.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.awt.*;
  12. import java.util.*;
  13. import java.sql.*;
  14. import java.lang.*;
  15. import symantec.itools.db.net.*;
  16. import symantec.itools.db.pro.*;
  17. import java.awt.event.*;
  18. import java.awt.*;
  19. import symantec.itools.db.beans.binding.Name;
  20.  
  21. /**
  22.  * A dbAWARE TextField component.
  23.  * <p>
  24.  * This component is similar to the basic TextField component, but can
  25.  * be "bound" to a projection within a relation view
  26.  * so that it automatically gets and sets the values in that relation.
  27.  * <p>
  28.  *
  29.  * @see java.awt.TextField
  30.  */
  31. public class TextField extends java.awt.TextField implements ProjectionBean
  32. {
  33.     /**
  34.      * Constant value for the Triggering Event property.
  35.      * Indicates the triggering event is FOCUS_LOST.
  36.      */
  37.     public static final int Focus_Event = ProjectionBeanHelper.Focus_Event;
  38.     /**
  39.      * Constant value for the Triggering Event property.
  40.      * Indicates the triggering event is TEXT_VALUE_CHANGED.
  41.      */
  42.     public static final int Text_Event = ProjectionBeanHelper.Text_Event;
  43.     /**
  44.      * Constant value for the Triggering Event property.
  45.      * Indicates the triggering event is KEY_PRESSED.
  46.      */
  47.     public static final int Key_Event = ProjectionBeanHelper.Key_Event;
  48.     /**
  49.      * Constant value for the Triggering Event property.
  50.      * Indicates the triggering event is ACTION_PERFORMED.
  51.      */
  52.     public static final int Action_Event = ProjectionBeanHelper.Action_Event;
  53.  
  54.     private ProjectionBeanHelper    m_Helper;
  55.     /**
  56.      * The type of event that triggers this component to commit its changes.
  57.      * @see #getTriggeringEvent
  58.      * @see #setTriggeringEvent
  59.      * @see #Focus_Event
  60.      * @see #Key_Event
  61.      * @see #Text_Event
  62.      * @see #Action_Event
  63.      */
  64.     protected int                   m_TriggeringEvent = Focus_Event;
  65.     /**
  66.      * Controls when the component commits its changes.
  67.      * If <code>true</code>, this component sets the triggering event to
  68.      * Text_Event, otherwise the triggering event is set to Focus_Event.
  69.      * @see #getDynamicUpdate
  70.      * @see #setDynamicUpdate
  71.      * @see #Focus_Event
  72.      * @see #Text_Event
  73.      */
  74.     protected boolean               m_DynamicUpdate = false;
  75.  
  76.     /**
  77.      * A constant value indicating how an empty string will be saved in the database.
  78.      */
  79.     public static final String Default = ProjectionBeanHelper.DEFAULT;
  80.     /**
  81.      * A constant value indicating how an empty string will be saved in the database.
  82.      */
  83.     public static final String Blank = ProjectionBeanHelper.BLANK;
  84.     /**
  85.      * A constant value indicating how an empty string will be saved in the database.
  86.      */
  87.     public static final String Null = ProjectionBeanHelper.NULL;
  88.  
  89.     /**
  90.      * Constructs a default TextField.
  91.      * <p>
  92.      * Call setBinding() to bind this component to a projection in a RelationView and
  93.      * auto-set the value.
  94.      *
  95.      * @see #setBinding
  96.      */
  97.     public TextField()
  98.     {
  99.         this("", 0);
  100.     }
  101.  
  102.     /**
  103.      * Constructs a TextField with the specified number of columns.
  104.      * <p>
  105.      * Call setBinding() to bind this component to a projection in a RelationView and
  106.      * auto-set the value.
  107.      *
  108.      * @param cols the number of columns
  109.      *
  110.      * @see #setBinding
  111.      */
  112.     public TextField(int cols)
  113.     {
  114.         this("", cols);
  115.     }
  116.  
  117.     /**
  118.      * Constructs a TextField with the specified initial value.
  119.      * <p>
  120.      * Call setBinding() to bind this component to a projection in a RelationView and
  121.      * auto-set the value.
  122.      *
  123.      * @param text the text to be displayed
  124.      *
  125.      * @see #setBinding
  126.      */
  127.     public TextField(String text)
  128.     {
  129.         this(text, 0);
  130.     }
  131.  
  132.     /**
  133.      * Constructs a TextField with the specified text and number of columns.
  134.      * <p>
  135.      * Call setBinding() to bind this component to a projection in a RelationView and
  136.      * auto-set the value.
  137.      *
  138.      * @param text the text to be displayed
  139.      * @param cols the number of columns
  140.      *
  141.      * @see #setBinding
  142.      */
  143.     public TextField(String text, int cols)
  144.     {
  145.         super(text, cols);
  146.         m_Helper = new ProjectionBeanHelper(this);
  147.         setTriggeringEvent(Text_Event);
  148.     }
  149.  
  150.     /**
  151.      * Sets whether this component determines is user-editablity from the
  152.      * database it's bound with, or whether it will simple not be editable.
  153.      *
  154.      * @param value if true this component is editable if the database its bound
  155.      * with is writable, if false this component is not editable
  156.      */
  157.     public void setEditable(boolean value)
  158.     {
  159.         m_Helper.setBinderDeterminesReadOnly(value);
  160.         super.setEditable(value);
  161.     }
  162.  
  163.     /**
  164.      * Sets whether the data value of this component may be modified.
  165.      * @param value <code>true</code> if the value may not be modified,
  166.      * <code>false</code>if the value may be modified
  167.      */
  168.     public void setReadOnly(boolean value) {
  169.         super.setEditable(!value);
  170.     }
  171.  
  172.     /**
  173.      * Binds this component to a given projection within the specified
  174.      * relation view.
  175.      *
  176.      * @param relView the relation view to bind with
  177.      * @param projection the projection in relView to bind with
  178.      */
  179.     public void setBinding(RelationView relView, String projection)
  180.     {
  181.         m_Helper.setBinding(relView, projection);
  182.     }
  183.  
  184.     /**
  185.      * Returns the projection in the RelationView that this component is bound with.
  186.      * @see #setBinding
  187.      */
  188.     public String getProjection() {
  189.         return m_Helper.getProjection();
  190.     }
  191.  
  192.     /**
  193.      * Binds this component to the given projection within the RelationView
  194.      * the component is currently bound with.
  195.      * @see #setBinding
  196.      * @see #getProjection
  197.      * @see #getRelationView
  198.      */
  199.     public void setProjection(String projection) {
  200.         m_Helper.setProjection(projection);
  201.     }
  202.  
  203.     /**
  204.      * Returns the RelationView that this component is bound with.
  205.      * @see #setBinding
  206.      */
  207.     public RelationView getRelationView() {
  208.         return m_Helper.getRelationView();
  209.     }
  210.  
  211.     /**
  212.      * Sets the RelationView that this component is bound with.
  213.      * @param value the RelationView to bind with
  214.      * @see #getRelationView
  215.      * @see #setBinding
  216.      * @see #setProjection
  217.      */
  218.     public void setRelationView(RelationView value) {
  219.         m_Helper.setRelationView(value);
  220.     }
  221.  
  222.     /**
  223.      * Specifies how an empty string will be set when updating data on
  224.      * the dbANYWHERE server.
  225.      *
  226.      * @param blank one of "DEFAULT", "NULL", or "BLANK"
  227.      *
  228.      * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
  229.      */
  230.     public void setTreatBlankAs(String value) {
  231.         m_Helper.setTreatBlankAsString(value);
  232.     }
  233.  
  234.     /**
  235.      * Indicates when the component commits its changes.
  236.      * If <code>true</code>, this component has set the triggering event to
  237.      * Text_Event, otherwise the triggering event is set to Focus_Event.
  238.      * @see #setDynamicUpdate
  239.      * @see #Focus_Event
  240.      * @see #Text_Event
  241.      */
  242.     public boolean getDynamicUpdate() {
  243.        return m_DynamicUpdate;
  244.     }
  245.  
  246.     /**
  247.      * Sets when the component commits its changes.
  248.      * If <code>true</code>, this component sets the triggering event to
  249.      * Text_Event, otherwise the triggering event is set to Focus_Event.
  250.      * @param value the new dynamic update mode value
  251.      * @see #getDynamicUpdate
  252.      * @see #Focus_Event
  253.      * @see #Text_Event
  254.      */
  255.     public void setDynamicUpdate(boolean value)
  256.     {
  257.         if(value) {
  258.            setTriggeringEvent(Text_Event);
  259.         }
  260.         else {
  261.            setTriggeringEvent(Focus_Event);
  262.         }
  263.         m_DynamicUpdate = value;
  264.     }
  265.  
  266.     /**
  267.      * Sets the value of this component to the given value.
  268.      * @param value the new component value
  269.      * @see #getData
  270.      */
  271.     public void setData(Object value)
  272.     {
  273.         setText(value.toString());
  274.     }
  275.  
  276.     /**
  277.      * Gets the value of this component.
  278.      * @return the current component value
  279.      * @see #setData
  280.      */
  281.     public Object getData() {
  282.         return getText();
  283.     }
  284.  
  285.     /**
  286.      * Gets whether this component saves its value as a text String.
  287.      * @return <code>true</code> if the value is saved as text,
  288.      * <code>false</code> otherwise
  289.      */
  290.     public boolean isTextBased() {
  291.         return true;
  292.     }
  293.  
  294.     /**
  295.      * Gets the number of digits to the right of the decimal point for
  296.      * this component's value.
  297.      * @return the number of digits to the right of the decimal point
  298.      */
  299.     public int getScale() {
  300.         return ProjBinder.DEFAULTSCALE;
  301.     }
  302.  
  303.     /**
  304.      * Registers the standard event listener(s) for this component.
  305.      */
  306.     public void registerListeners() {
  307.         addKeyListener(m_Helper);
  308.     }
  309.     /**
  310.      * Sets the name of the data item to bind this component to.
  311.      * @param name the data item name, like "MyTable@MyColumn"
  312.      * @see #getDataBinding
  313.      */
  314.     public void setDataBinding(String name)
  315.     {
  316.         m_Helper.setDataBinding(new Name(name));
  317.     }
  318.  
  319.     /**
  320.      * Gets the name of the data item this component is bound to.
  321.      * @returns the data item name, like "MyTable@MyColumn"
  322.      * @see #setDataBinding
  323.      */
  324.     public String getDataBinding()
  325.     {
  326.         return  m_Helper.getDataBinding().getFullName();
  327.     }
  328.  
  329.     void removeAllListeners()
  330.     {
  331.         removeFocusListener(m_Helper);
  332.         removeActionListener(m_Helper);
  333.         //this.removeItemListener(m_Helper);
  334.         removeTextListener(m_Helper);
  335.         removeKeyListener(m_Helper);
  336.     }
  337.  
  338.     /**
  339.      * Sets the value of the Triggering Event property.
  340.      * This determines the type of event that triggers the component to commit
  341.      * its changes to the database.
  342.      * @param event one of Focus_Event, Action_Event, Key_Event, or Text_Event
  343.      * @see #getTriggeringEvent
  344.      * @see #Focus_Event
  345.      * @see #Action_Event
  346.      * @see #Key_Event
  347.      * @see #Text_Event
  348.      */
  349.     public void setTriggeringEvent(int event)
  350.     {
  351.         m_TriggeringEvent = event;
  352.         removeAllListeners();
  353.  
  354.         switch(event){
  355.             case Focus_Event:
  356.                 addFocusListener(m_Helper);
  357.                 break;
  358.             case Action_Event:
  359.                 addActionListener(m_Helper);
  360.                 break;
  361.            // case symantec.itools.db.awt.ProjectionBeanHelper.Item_Event:
  362.             //(this).addItemListener(m_Helper);
  363.            // break;
  364.             case Text_Event:
  365.                 addTextListener(m_Helper);
  366.                 break;
  367.             case Key_Event:
  368.                 addKeyListener(m_Helper);
  369.                 break;
  370.             default:
  371.                 addFocusListener(m_Helper);
  372.         }
  373.     }
  374.  
  375.     /**
  376.      * Gets the current value of the Triggering Event property.
  377.      * This determines the type of event that triggers the component to commit
  378.      * its changes to the database.
  379.      * @return one of Focus_Event, Action_Event, Key_Event, or Text_Event
  380.      * @see #setTriggeringEvent
  381.      * @see #Focus_Event
  382.      * @see #Action_Event
  383.      * @see #Key_Event
  384.      * @see #Text_Event
  385.      */
  386.     public int getTriggeringEvent()
  387.     {
  388.         return m_TriggeringEvent;
  389.     }
  390.  
  391.      /**
  392.      * Sets the value of the Empty Means Null property.
  393.      * This determines how an empty string will be saved when updating data on
  394.      * the database server.
  395.      * A value of <code>true</code>indicates an empty string will be saved as a
  396.      * SQL NULL value. Otherwise it will be saved as a blank value.
  397.      * @param propertyValue <code>true</code> for NULL, <code>false</code> for BLANK
  398.      * @see #getEmptyMeansNull
  399.      */
  400.     public void setEmptyMeansNull(boolean propertyValue)
  401.     {
  402.         m_Helper.setEmptyMeansNull(propertyValue);
  403.     }
  404.  
  405.      /**
  406.      * Gets the value of the Empty Means Null property.
  407.      * This determines how an empty string will be saved when updating data on
  408.      * the database server.
  409.      * A value of <code>true</code>indicates an empty string will be saved as a
  410.      * SQL NULL value. Otherwise it will be saved as a blank value.
  411.      * @return <code>true</code> for NULL, <code>false</code> for BLANK
  412.      * @see #setEmptyMeansNull
  413.      */
  414.     public boolean getEmptyMeansNull()
  415.     {
  416.         return m_Helper.getEmptyMeansNull();
  417.     }
  418. }
  419.