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 / FormattedTextField.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  13.0 KB  |  446 lines

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