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 / Checkbox.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  15.3 KB  |  493 lines

  1. /*
  2.  * @(#Checkbox.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.awt;
  9.  
  10. import java.applet.*;
  11. import java.awt.event.*;
  12. import symantec.itools.db.pro.*;
  13. import symantec.itools.db.beans.binding.Name;
  14. import symantec.itools.db.beans.binding.Mediator;
  15.  
  16. /**
  17.  * A dbAWARE Checkbox.
  18.  * <p>
  19.  * This component can be "bound" to a projection within a relation view
  20.  * so that it automatically gets and sets the values in that relation.
  21.  *
  22.  */
  23. public class Checkbox extends java.awt.Checkbox implements ProjectionBean
  24. {
  25.     private String  m_trueValue = new String("");
  26.     private String  m_falseValue = new String("");
  27.     private ProjectionBeanHelper m_Helper;
  28.     /**
  29.      * The value of the Triggering Event property.
  30.      * The type of event that triggers this component to commit its changes.
  31.      * @see #getTriggeringEvent
  32.      * @see #setTriggeringEvent
  33.      * @see #Focus_Event
  34.      * @see #Key_Event
  35.      * @see #Item_Event
  36.      */
  37.     protected int m_TriggeringEvent = Focus_Event;
  38.     /**
  39.      * Controls when the component commits its changes.
  40.      * If <code>true</code>, this component sets the triggering event to
  41.      * Item_Event, otherwise the triggering event is set to Focus_Event.
  42.      * @see #getDynamicUpdate
  43.      * @see #setDynamicUpdate
  44.      * @see #Focus_Event
  45.      * @see #Item_Event
  46.      */
  47.     protected boolean m_DynamicUpdate=false;
  48.  
  49.     /**
  50.      * Constant value for the Triggering Event property.
  51.      * Indicates the triggering event is FOCUS_LOST.
  52.      */
  53.     public static final int Focus_Event = ProjectionBeanHelper.Focus_Event;
  54.     /**
  55.      * Constant value for the Triggering Event property.
  56.      * Indicates the triggering event is KEY_PRESSED.
  57.      */
  58.     public static final int Key_Event = ProjectionBeanHelper.Key_Event;
  59.     /**
  60.      * Constant value for the Triggering Event property.
  61.      * Indicates the triggering event is ITEM_STATE_CHANGED.
  62.      */
  63.     public static final int Item_Event = ProjectionBeanHelper.Item_Event;
  64.  
  65.     /**
  66.      * A constant value indicating how an empty string will be saved in the database.
  67.      */
  68.     public static final String Default = ProjectionBeanHelper.DEFAULT;
  69.     /**
  70.      * A constant value indicating how an empty string will be saved in the database.
  71.      */
  72.     public static final String Blank = ProjectionBeanHelper.BLANK;
  73.     /**
  74.      * A constant value indicating how an empty string will be saved in the database.
  75.      */
  76.     public static final String Null = ProjectionBeanHelper.NULL;
  77.  
  78.     /**
  79.      * The value of the Foreground color property.
  80.      * @see java.awt.Component#getForeground
  81.      * @see #setForeground
  82.      */
  83.     public java.awt.Color foreg=java.awt.Color.black;
  84.     /**
  85.      * Indicates whether the value of this component is of type Boolean.
  86.      * @see #getData
  87.      * @see #setData
  88.      *
  89.     public boolean hasReceivedABoolean=false;*/
  90.     /**
  91.      * <code>True</code> if the Checkbox value is neither true nor false.
  92.      * This happens when <code>setData</code> is called with a value other
  93.      * than those chosen to represent true or false.
  94.      * @see #setData
  95.      * @see #getData
  96.      * @see #getTrueValue
  97.      * @see #setTrueValue
  98.      * @see #getFalseValue
  99.      * @see #setFalseValue
  100.      */
  101.     protected boolean isNotDefined=false;
  102.     /**
  103.      * The current value of this component as a String.
  104.      * @see #getData
  105.      * @see #setData
  106.      */
  107.     public Object Text;
  108.     /**
  109.      * Constructs a Checkbox with the specified label.
  110.      * It will have no Checkbox group, and be initialized to a false state.
  111.      * @param label the label on the Checkbox
  112.      */
  113.     public Checkbox(String label) {
  114.         super(label);
  115.         m_Helper = new ProjectionBeanHelper(this);
  116.         addFocusListener(m_Helper);
  117.         setTriggeringEvent(Item_Event);
  118.     }
  119.  
  120.     /**
  121.      * Constructs a default Checkbox.
  122.      * It will have no label, no Checkbox group, and be initialized to a false state.
  123.      */
  124.     public Checkbox() {
  125.         this("");
  126.     }
  127.  
  128.     /**
  129.      * Sets the string sent to the database server to indicate that this
  130.      * checkbox is checked.
  131.      * @param trueValue the string used to indicate checked
  132.      * @see #getTrueValue
  133.      */
  134.     public void setTrueValue(String trueValue) {
  135.         m_trueValue = new String(trueValue);
  136.         m_Helper.notifyDataChange();
  137.         //m_Helper.m_Mediator.setBooleanTrueValue(m_trueValue);
  138.     }
  139.  
  140.     /**
  141.      * Gets the string sent to the database server to indicate that this
  142.      * checkbox is checked.
  143.      * @return the string used to indicate checked
  144.      * @see #setTrueValue
  145.      */
  146.     public String getTrueValue() {
  147.         return m_trueValue;
  148.     }
  149.  
  150.     /**
  151.      * Sets the string sent to the database server to indicate that this
  152.      * checkbox is unchecked.
  153.      * @param falseValue the string used to indicate unchecked
  154.      * @see #getFalseValue
  155.      */
  156.     public void setFalseValue(String falseValue) {
  157.         m_falseValue = new String(falseValue);
  158.         m_Helper.notifyDataChange();
  159.       //  m_Helper.m_Mediator.setBooleanFalseValue(m_falseValue);
  160.     }
  161.  
  162.     /**
  163.      * Gets the string sent to the database server to indicate that this
  164.      * checkbox is unchecked.
  165.      * @return the string used to indicate unchecked
  166.      * @see #setFalseValue
  167.      */
  168.     public String getFalseValue() {
  169.         return m_falseValue;
  170.     }
  171.  
  172.     /**
  173.      * Binds this component to a given projection within the specified
  174.      * RelationView.
  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.      * @see #setProjection
  188.      */
  189.     public String getProjection() {
  190.         return m_Helper.getProjection();
  191.     }
  192.  
  193.     /**
  194.      * Binds this component to the given projection within the RelationView
  195.      * the component is currently bound with.
  196.      * @see #setBinding
  197.      * @see #getProjection
  198.      * @see #getRelationView
  199.      */
  200.     public void setProjection(String projection) {
  201.         m_Helper.setProjection(projection);
  202.     }
  203.  
  204.     /**
  205.      * Gets the RelationView that this component is bound with.
  206.      * @return the RelationView currently bound with
  207.      * @see #setRelationView
  208.      * @see #setBinding
  209.      * @see #getProjection
  210.      */
  211.     public RelationView getRelationView() {
  212.         return m_Helper.getRelationView();
  213.     }
  214.  
  215.     /**
  216.      * Sets the RelationView that this component is bound with.
  217.      * @param value the RelationView to bind with
  218.      * @see #getRelationView
  219.      * @see #setBinding
  220.      * @see #setProjection
  221.      */
  222.     public void setRelationView(RelationView value) {
  223.         m_Helper.setRelationView(value);
  224.     }
  225.  
  226.     /**
  227.      * Specifies how an empty string will be set when updating data on
  228.      * the database server.
  229.      *
  230.      * @param value one of "DEFAULT", "NULL", or "BLANK"
  231.      *
  232.      * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
  233.      */
  234.     public void setTreatBlankAs(String value) {
  235.         m_Helper.setTreatBlankAsString(value);
  236.     }
  237.  
  238.     /**
  239.      * Indicates when the component commits its changes.
  240.      * If <code>true</code>, this component has set the triggering event to
  241.      * Item_Event, otherwise the triggering event is set to Focus_Event.
  242.      * @see #setDynamicUpdate
  243.      * @see #Focus_Event
  244.      * @see #Item_Event
  245.      */
  246.     public boolean getDynamicUpdate() {
  247.        return m_DynamicUpdate;
  248.     }
  249.  
  250.     /**
  251.      * Sets when the component commits its changes.
  252.      * If <code>true</code>, this component sets the triggering event to
  253.      * Item_Event, otherwise the triggering event is set to Focus_Event.
  254.      * @param value the new dynamic update mode value
  255.      * @see #getDynamicUpdate
  256.      * @see #Focus_Event
  257.      * @see #Item_Event
  258.      */
  259.     public void setDynamicUpdate(boolean value)
  260.     {
  261.         if(value){
  262.            setTriggeringEvent(Item_Event);
  263.         }
  264.         else {
  265.            setTriggeringEvent(Focus_Event);
  266.         }
  267.         m_DynamicUpdate = value;
  268.     }
  269.     /**
  270.      * Sets the value of the Foreground color property.
  271.      * @see java.awt.Component#getForeground
  272.      */
  273.     public void setForeground(java.awt.Color c)
  274.     {
  275.         foreg=c;
  276.         if(!isNotDefined)super.setForeground(c);
  277.     }
  278.  
  279.     /**
  280.      * Sets whether the data value of this component may be modified.
  281.      * @param value <code>true</code> if the value may not be modified,
  282.      * <code>false</code>if the value may be modified
  283.      */
  284.     public void setReadOnly(boolean value) {
  285.     }
  286.  
  287.     /**
  288.      * Sets the value of this component to the given state.
  289.      * @param b <code>true</code> if the Checkbox is checked
  290.      * @see #setData
  291.      */
  292.     public void setState(boolean b)
  293.     {
  294.          if(b)Text=m_trueValue;
  295.          else  Text=m_falseValue;
  296.          super.setState(b);
  297.     }
  298.  
  299.     /**
  300.      * Sets the value of this component to the given value.
  301.      * If the value parameter is of class Boolean, its value is used to
  302.      * set this component's value directly. Otherwise the parameter's
  303.      * <code>toString</code> method is called and this component's value
  304.      * is set according to whether the string matches that of the
  305.      * True Value, or False Value property.
  306.      *
  307.      * @param value the new component value
  308.      * @see #getData
  309.      * @see #setState
  310.      * @see #setTrueValue
  311.      * @see #setFalseValue
  312.      */
  313.     public void setData(Object value) {
  314.             if(value != null && value instanceof Boolean)
  315.             {
  316.                 setState(((Boolean)value).booleanValue());
  317.                 return;
  318.             }
  319.             else if(value != null)
  320.             {
  321.                 if (value != null) {
  322.                     Text = value.toString();
  323.                 }
  324.                 else {
  325.                     Text = null;
  326.                     setState(false);
  327.                     super.setForeground(java.awt.Color.gray);
  328.                     return;
  329.                 }
  330.  
  331.                 if(Text.equals(m_trueValue)||(!Text.equals(m_falseValue)&&m_trueValue.equals(""))){
  332.                     Text=m_trueValue;
  333.                      super.setForeground(foreg);
  334.                     setState(true);}
  335.  
  336.                 else if(Text.equals(m_falseValue)||m_falseValue.equals("")){
  337.                     Text=m_falseValue;
  338.                     super.setForeground(foreg);
  339.                     setState(false);
  340.                 }
  341.                 else {
  342.                     setState(false);
  343.                     isNotDefined=true;
  344.                     super.setForeground(java.awt.Color.gray);
  345.                    }
  346.             }
  347.     }
  348.  
  349.     /**
  350.      * Gets the value of this component.
  351.      * If this component's value was set with a Boolean, this method
  352.      * returns a Boolean. Otherwise it returns the current value
  353.      * as a String.
  354.      * @return the current component value
  355.      * @see #setData
  356.      * @see #getTrueValue
  357.      * @see #getFalseValue
  358.      */
  359.     public Object getData() {
  360.         if(m_trueValue.equals("") && m_falseValue.equals(""))
  361.         {
  362.             return new Boolean(getState());
  363.         }
  364.         super.setForeground(foreg);
  365.         return Text;
  366.     }
  367.  
  368.     /**
  369.      * Gets whether this component saves its value as a text String.
  370.      * @return <code>true</code> if the value is saved as text,
  371.      * <code>false</code> otherwise
  372.      */
  373.     public boolean isTextBased() {
  374.         return true;
  375.     }
  376.  
  377.     /**
  378.      * Gets the number of digits to the right of the decimal point for
  379.      * this component's value.
  380.      * @return the number of digits to the right of the decimal point
  381.      */
  382.     public int getScale() {
  383.         return ProjBinder.DEFAULTSCALE;
  384.     }
  385.  
  386.     /**
  387.      * Registers the standard event listener(s) for this component.
  388.      */
  389.     public void registerListeners() {
  390.         addItemListener(m_Helper);
  391.     }
  392.  
  393.     /**
  394.      * Sets the name of the data item to bind this component to.
  395.      * @param name the data item name, like "MyTable@MyColumn"
  396.      * @see #getDataBinding
  397.      */
  398.     public void setDataBinding(String name)
  399.     {
  400.         m_Helper.setDataBinding(new Name(name));
  401.     }
  402.  
  403.     /**
  404.      * Gets the name of the data item this component is bound to.
  405.      * @returns the data item name, like "MyTable@MyColumn"
  406.      * @see #setDataBinding
  407.      */
  408.     public String getDataBinding()
  409.     {
  410.         return  m_Helper.getDataBinding().getFullName();
  411.     }
  412.  
  413.     void removeAllListeners()
  414.     {
  415.         removeFocusListener(m_Helper);
  416.         removeKeyListener(m_Helper);
  417.         removeItemListener(m_Helper);
  418.     }
  419.  
  420.     /**
  421.      * Sets the value of the Triggering Event property.
  422.      * This determines the type of event that triggers the component to commit
  423.      * its changes to the database.
  424.      * @param event one of Focus_Event, Key_Event, or Item_Event
  425.      * @see #getTriggeringEvent
  426.      * @see #Focus_Event
  427.      * @see #Key_Event
  428.      * @see #Item_Event
  429.      */
  430.     public void setTriggeringEvent(int event)
  431.     {
  432.         m_TriggeringEvent = event;
  433.  
  434.         removeAllListeners();
  435.         switch(event) {
  436.             case Focus_Event:
  437.                 addFocusListener(m_Helper);
  438.                 break;
  439.             case Item_Event:
  440.                 addItemListener(m_Helper);
  441.                 break;
  442.             case Key_Event:
  443.                 addKeyListener(m_Helper);
  444.                 break;
  445.             default:
  446.                 addFocusListener(m_Helper);
  447.         }
  448.     }
  449.  
  450.     /**
  451.      * Gets the current value of the Triggering Event property.
  452.      * This determines the type of event that triggers the component to commit
  453.      * its changes to the database.
  454.      * @return one of Focus_Event, Key_Event, or Item_Event
  455.      * @see #setTriggeringEvent
  456.      * @see #Focus_Event
  457.      * @see #Key_Event
  458.      * @see #Item_Event
  459.      */
  460.     public int getTriggeringEvent()
  461.     {
  462.         return m_TriggeringEvent;
  463.     }
  464.  
  465.      /**
  466.      * Sets the value of the Empty Means Null property.
  467.      * This determines how an empty string will be saved when updating data on
  468.      * the database server.
  469.      * A value of <code>true</code>indicates an empty string will be saved as a
  470.      * SQL NULL value. Otherwise it will be saved as a blank value.
  471.      * @param propertyValue <code>true</code> for NULL, <code>false</code> for BLANK
  472.      * @see #getEmptyMeansNull
  473.      */
  474.    public void setEmptyMeansNull(boolean propertyValue)
  475.     {
  476.         m_Helper.setEmptyMeansNull(propertyValue);
  477.     }
  478.  
  479.      /**
  480.      * Gets the value of the Empty Means Null property.
  481.      * This determines how an empty string will be saved when updating data on
  482.      * the database server.
  483.      * A value of <code>true</code>indicates an empty string will be saved as a
  484.      * SQL NULL value. Otherwise it will be saved as a blank value.
  485.      * @return <code>true</code> for NULL, <code>false</code> for BLANK
  486.      * @see #setEmptyMeansNull
  487.      */
  488.     public boolean getEmptyMeansNull()
  489.     {
  490.         return m_Helper.getEmptyMeansNull();
  491.     }
  492. }
  493.