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 / ListPlus.java < prev    next >
Encoding:
Java Source  |  1998-03-19  |  10.9 KB  |  374 lines

  1. /*
  2.  * @(#ListPlus.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.net.*;
  14. import symantec.itools.db.pro.*;
  15. import java.awt.event.*;
  16. import symantec.itools.db.beans.binding.Name;
  17.  
  18. /**
  19.  * A dbAWARE ListPlus 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 ListPlus extends java.awt.List implements ProjectionBean
  26. {
  27.  
  28.     /**
  29.      * Constant value for the Triggering Event property.
  30.      * Indicates the triggering event is FOCUS_LOST.
  31.      */
  32.     public static final int Focus_Event = ProjectionBeanHelper.Focus_Event;
  33.     /**
  34.      * Constant value for the Triggering Event property.
  35.      * Indicates the triggering event is KEY_PRESSED.
  36.      */
  37.     public static final int Key_Event = ProjectionBeanHelper.Key_Event;
  38.     /**
  39.      * Constant value for the Triggering Event property.
  40.      * Indicates the triggering event is ITEM_STATE_CHANGED.
  41.      */
  42.     public static final int Item_Event = ProjectionBeanHelper.Item_Event;
  43.     /**
  44.      * Constant value for the Triggering Event property.
  45.      * Indicates the triggering event is ACTION_PERFORMED.
  46.      */
  47.     public static final int Action_Event = ProjectionBeanHelper.Action_Event;
  48.  
  49.     private boolean m_useStream = false;
  50.  
  51.     private ProjectionBeanHelper m_Helper = null;
  52.     /**
  53.      * The value of the Triggering Event property.
  54.      * The type of event that triggers this component to commit its changes.
  55.      * @see #getTriggeringEvent
  56.      * @see #setTriggeringEvent
  57.      * @see #Focus_Event
  58.      * @see #Key_Event
  59.      * @see #Item_Event
  60.      * @see java.awt.Event#ACTION_EVENT
  61.      */
  62.     protected int TriggeringEvent=Item_Event;
  63.  
  64.     private int blockSize=10;
  65.     private String IName;
  66.     /**
  67.      * Constructs a new ListPlus with no data connectivity, initially.
  68.      *
  69.      * @param rows defines the number of rows of items to display.
  70.      */
  71.     public ListPlus(int rows)
  72.     {
  73.         super(rows, false);
  74.         m_Helper = new ProjectionBeanHelper(this);
  75.         this.setTriggeringEvent(symantec.itools.db.awt.ListPlus.Item_Event);
  76.         setMultipleSelections(false);
  77.     }
  78.  
  79.     /**
  80.      * Constructs a default ListPlus with no data connectivity, initially.
  81.      */
  82.     public ListPlus()
  83.     {
  84.         this(0);
  85.     }
  86.  
  87.     /**
  88.      * Adds the specified item to the end of the list.
  89.      * @param item the item to be added
  90.      */
  91.     public synchronized void addItem(String item) {
  92.         super.addItem(item, -1);
  93.         staticItems.addElement(item);
  94.     }
  95.  
  96.     private Vector staticItems = new Vector();
  97.  
  98.     // ProjectionBean interface methods
  99.     /**
  100.      * Sets whether the data value of this component may be modified.
  101.      * @param value <code>true</code> if the value may not be modified,
  102.      * <code>false</code>if the value may be modified
  103.      */
  104.     public void setReadOnly(boolean value)
  105.     {
  106.     }
  107.  
  108.     /**
  109.      * Sets the value of this component to the given value.
  110.      * @param value the new component value
  111.      * @see #getData
  112.      */
  113.     String oldValue;
  114.     public void setData(Object value) {
  115.         
  116.         boolean bitemFound = false;
  117.         if(value!=null)
  118.         {
  119.             oldValue=value.toString();
  120.         String text = value.toString();
  121.         int numberOfRows=countItems();
  122.         for (int index = 0; index < numberOfRows; index++)
  123.         {
  124.             if (text.equals(getItem(index)))
  125.             {
  126.                 select(index);
  127.                 makeVisible(index);
  128.                 bitemFound = true;
  129.                 break;
  130.             }
  131.         }
  132.         }
  133.             if (!bitemFound)
  134.             {
  135.                 int hasSelection=getSelectedIndex();
  136.                 if (hasSelection != -1)
  137.                 {
  138.                     deselect(hasSelection);
  139.                 }
  140.             }
  141.  
  142.     }
  143.  
  144.     /**
  145.      * Gets the one-relative index of the currently selected list item.
  146.      * @return the one-relative item index, or 0 if no item is selected
  147.      * @see #setIndexFromOne
  148.      */
  149.     public int getIndexFromOne()
  150.     {
  151.         return getSelectedIndex()+1;
  152.     }
  153.     /**
  154.      * Selects a list item using its one-relative index.
  155.      * @param i the one-relative item index
  156.      * @see #getIndexFromOne
  157.      */
  158.     public void setIndexFromOne(int i)
  159.     {
  160.         select(i-1);
  161.     }
  162.  
  163.     /**
  164.      * Gets the value of this component.
  165.      * @return the current component value
  166.      * @see #setData
  167.      */
  168.     public Object getData() {
  169.         return getSelectedItem();
  170.     }
  171.  
  172.     /**
  173.      * Gets whether this component saves its value as a text String.
  174.      * @return <code>true</code> if the value is saved as text,
  175.      * <code>false</code> otherwise
  176.      */
  177.     public boolean isTextBased() {
  178.         return true;
  179.     }
  180.  
  181.     /**
  182.      * Gets the number of digits to the right of the decimal point for
  183.      * this component's value.
  184.      * @return the number of digits to the right of the decimal point
  185.      */
  186.     public int getScale() {
  187.         return ProjBinder.DEFAULTSCALE;
  188.     }
  189.  
  190.     /**
  191.      * Registers the standard event listener(s) for this component.
  192.      */
  193.     public void registerListeners() {
  194.         addItemListener(m_Helper);
  195.     }
  196.  
  197.     // Lookup methods...
  198.     symantec.itools.db.beans.binding.Mediator m_LookupMediator;
  199.     Name lookupName=new Name();
  200.     private String[] m_GetMethods={"getDataAt(row)"};
  201.     private String[] m_SetMethods={"setDataAt(row,Value)"};
  202.     private boolean isLookup=false;
  203.  
  204.     /**
  205.      * Sets the value of the Lookup Name property.
  206.      * This property is the name of a data source table and column.
  207.      * It is used to fill this component with data.
  208.      * @param lame the new lookup name
  209.      * @see #getLookupName
  210.      */
  211.     public void setLookupName(String lname)
  212.     {
  213.         Name name = new Name(lname);
  214.         isLookup=true;
  215.         if(m_LookupMediator==null){
  216.             m_LookupMediator =new symantec.itools.db.beans.binding.Mediator() ;
  217.             m_LookupMediator.setOutput(this);
  218.             m_LookupMediator.setSetMethods(m_SetMethods);
  219.             }
  220.         if(name.getNumberOfRows()==1)name.setNumberOfRows(-1);
  221.         lookupName=name;
  222.         m_LookupMediator.setDataBinding(name.getFullName());
  223.         if(IName!=null)m_Helper.setDataBinding(new Name(IName));
  224.  
  225.     }
  226.     /**
  227.      * Gets the value of the Lookup Name property.
  228.      * This property is the name of a data source table and column.
  229.      * It is used to fill this component with data.
  230.      * @return the property value
  231.      * @see #setLookupName
  232.      */
  233.     public String getLookupName()
  234.     {
  235.         return lookupName.getFullName();
  236.     }
  237.     /**
  238.      * Sets the value stored at a particular row.
  239.      * @param row the zero-relative row index
  240.      * @param value the new value
  241.      * @see #getDataAt
  242.      */
  243.      int currentItems=0;
  244.      String Value;
  245.     public void setDataAt(int row,Object value)
  246.     {
  247.         int totalItems=countItems();
  248.         int newNumberRows=m_LookupMediator.getOutputSize().height;
  249.         if(currentItems>newNumberRows)
  250.         {
  251.             for(int r=newNumberRows;r<currentItems;r++)
  252.             {
  253.                delItem(0);
  254.             }
  255.             currentItems=newNumberRows;
  256.             totalItems=newNumberRows;
  257.         }
  258.         
  259.         if(row >=totalItems)
  260.         {
  261.             for(int i=totalItems;i<=row ;i++)
  262.             {
  263.                 addItem(" ",i);
  264.             }
  265.         } 
  266.         if(value==null)value="";
  267.         Value=value.toString();
  268.         if(!getItem(row).equals(Value))replaceItem(Value,row );
  269.         if(Value.equals("")){
  270.              select(row);
  271.             oldValue=null;
  272.         }
  273.         if(oldValue!=null)
  274.         {
  275.             
  276.             if(oldValue.equals(Value)){
  277.                 select(row);
  278.                 oldValue=null;
  279.                 }
  280.         }
  281.         currentItems=newNumberRows;
  282.           
  283.       
  284.     }
  285.     /**
  286.      * Gets the value stored at a particular row.
  287.      * @param row the zero-relative row index
  288.      * @return the value
  289.      * @see #setDataAt
  290.      */
  291.     public Object getDataAt(int row)
  292.     {
  293.         int totalItems=countItems();
  294.         if(row<totalItems)return (Object)getItem(row);
  295.         return "";
  296.     }
  297.  
  298.     /**
  299.      * Sets the name of the data item to bind this component to.
  300.      * @param name the data item name, like "MyTable@MyColumn"
  301.      * @see #getDataBinding
  302.      */
  303.     public void setDataBinding(String name)
  304.     {
  305.         IName=name;
  306.         m_Helper.setDataBinding(new Name(IName));
  307.     }
  308.  
  309.     /**
  310.      * Gets the name of the data item this component is bound to.
  311.      * @returns the data item name, like "MyTable@MyColumn"
  312.      * @see #setDataBinding
  313.      */
  314.     public String getDataBinding()
  315.     {
  316.         return  m_Helper.getDataBinding().getFullName();
  317.     }
  318.  
  319.     void removeAllListeners()
  320.     {
  321.         this.removeFocusListener(m_Helper);
  322.         this.removeActionListener(m_Helper);
  323.         this.removeItemListener(m_Helper);
  324.         this.removeKeyListener(m_Helper);
  325.     }
  326.     /**
  327.      * Sets the value of the Triggering Event property.
  328.      * This determines the type of event that triggers the component to commit
  329.      * its changes to the database.
  330.      * @param event one of Focus_Event, Action_Event, Key_Event, or Item_Event
  331.      * @see #getTriggeringEvent
  332.      * @see #Focus_Event
  333.      * @see java.awt.Event#ACTION_EVENT
  334.      * @see #Key_Event
  335.      * @see #Item_Event
  336.      */
  337.     public void setTriggeringEvent(int Event)
  338.     {
  339.         TriggeringEvent=Event;
  340.         this.removeAllListeners();
  341.        switch(Event){
  342.             case symantec.itools.db.awt.ProjectionBeanHelper.Focus_Event:
  343.             (this).addFocusListener(m_Helper);
  344.             break;
  345.             case symantec.itools.db.awt.ProjectionBeanHelper.Action_Event:
  346.             (this).addActionListener(m_Helper);
  347.             break;
  348.             case symantec.itools.db.awt.ProjectionBeanHelper.Item_Event:
  349.             (this).addItemListener(m_Helper);
  350.             break;
  351.             case symantec.itools.db.awt.ProjectionBeanHelper.Key_Event:
  352.             (this).addKeyListener(m_Helper);
  353.             break;
  354.             default:
  355.             (this).addActionListener(m_Helper);
  356.        }
  357.     }
  358.     /**
  359.      * Gets the current value of the Triggering Event property.
  360.      * This determines the type of event that triggers the component to commit
  361.      * its changes to the database.
  362.      * @return one of Focus_Event, Action_Event, Key_Event, or Item_Event
  363.      * @see #setTriggeringEvent
  364.      * @see #Focus_Event
  365.      * @see java.awt.Event#ACTION_EVENT
  366.      * @see #Key_Event
  367.      * @see #Item_Event
  368.      */
  369.     public int getTriggeringEvent()
  370.     {
  371.         return TriggeringEvent;
  372.     }
  373. }
  374.