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 / MediatorDS.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  11.4 KB  |  348 lines

  1. /*
  2.  * @(#)MediatorDS.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  /**
  8.  * <p>The MediatorDS is a special kind of mediator specialy designed for
  9.  * working with data sources. The data sources must implement the
  10.  * BasicDataSource interface.
  11.  *
  12.  */
  13.  
  14. package symantec.itools.db.beans.binding;
  15.  
  16. import java.awt.Dimension;
  17. import symantec.itools.db.beans.binding.databus.*;
  18. import java.util.Vector;
  19. import java.beans.PropertyVetoException;
  20. import java.beans.VetoableChangeListener;
  21.  
  22.  
  23. public class MediatorDS extends Mediator implements TriggerUIListener
  24. {
  25.     /**
  26.      * Contains all the DataItem requested by the components.
  27.      */
  28.     protected Name dataBinding;
  29.     private Vector DataInputVector =new Vector();
  30.     private Vector DataInputCols =new Vector();
  31.     protected BasicDataSource output;
  32.     protected String[] setMethods={"setValueTrust(Value,Row,Col)"};
  33.     protected String[] getMethods={"getValue(Row,Col)"};
  34.     protected boolean notifyOnDS=true;
  35.     /**
  36.      * Constructs the default mediatorDS.
  37.      */
  38.     public MediatorDS()
  39.     {
  40.        super();
  41.        DataInputVector =new Vector();
  42.        DataInputCols =new Vector();
  43.        notifyOnDS=true;
  44.     }
  45.  
  46.     /**
  47.      * Constructs a MediatorDS given its output component and the full name of
  48.      * the fields it is supposed to show
  49.      * @param Out=output component
  50.      * @param name=fullname of the fields
  51.      */
  52.     public MediatorDS(Object Out,String Name)
  53.     {
  54.         super(Out,Name);
  55.     }
  56.  
  57.     /**
  58.      * Connects to the DataBus
  59.      * @param b=the DataBus the mediator connects to
  60.      * @exception PropertyVetoException
  61.      */
  62.     public void setDataBus(DataBus b)throws PropertyVetoException{
  63.         DBus=b;
  64.         DBus.addDataBusEventListener((DataBusEventListener)this);
  65.     }
  66.  
  67.     /**
  68.      * Sets the QueryNavigator for the MediatorDS.
  69.      * @param Out=data source
  70.      */
  71.     public void setOutput(Object Out)
  72.     {
  73.  
  74.         super.setOutput(Out);
  75.         output=(BasicDataSource)Out;
  76.         try{
  77.         Component.InputClass=Class.forName("java.lang.Object");
  78.         }
  79.         catch(Exception e){}
  80.         super.setEmptyMeansNull(false);
  81.         ((BasicDataSource)Out).addTriggerUIListener(this);
  82.     }
  83.     public void handleDataBusEvent(DataBusEvent ibe)
  84.     {
  85.     }
  86.  
  87.     /**
  88.      * Updates all the DataItems with the values present in the QueryNavigator.
  89.      */
  90.     int currentRow=0;
  91.     public void commit()
  92.     {
  93.         if(hasFoundSetMethod && hasFoundGetMethod)
  94.         {
  95.             this.notifyEnabled(false);
  96.            int row=output.getCurrentRowNumber();
  97.  
  98.             for(int i=0;i<DataInputVector.size();i++)
  99.             {
  100.                 DataItem elem=(DataItem)DataInputVector.elementAt(i);
  101.                 commit(elem,row,(int[])DataInputCols.elementAt(i));
  102.             }
  103.             for(int i=0;i<DataInputVector.size();i++)
  104.             {
  105.                 ((DataItemAddition)DataInputVector.elementAt(i)).cleanUp();
  106.             }
  107.  
  108.  
  109.             currentRow=row;
  110.              this.notifyEnabled(true);
  111.         }
  112.     }
  113.  
  114.  
  115.  
  116.  
  117.     public void commit(DataItem DI,int row,int[] cols)
  118.     {
  119.         String dataItemName=getDataItemName(DI);
  120.                 if(!(Name.getNumberOfRows(dataItemName)==Name.AllRows))
  121.                 {
  122.                     Component.setOutputSize(getCollectionSize(DI));
  123.                     createDataItemFromArray(DI,(Object[][])Component.getPrintOut(cols));
  124.                 }
  125.                 else
  126.                 {
  127.                     //if(currentRow==row)
  128.                     {
  129.                     Component.setOutputSize(new Dimension(getCollectionSize(DI).width,output.getTotalNumberOfRows()));
  130.                     createDataItemFromArray(DI,(Object[][])Component.getPrintOut(cols,row));
  131.                     }
  132.                 }
  133.      }
  134.  
  135.     /**
  136.      * Responds to the sollicitations of the DataItems.
  137.      * @param e
  138.      */
  139.     public void notifyDataItemChanged(DataItemChangedEvent e)
  140.     {
  141.         if(e.getChangeType()==e.DATA_VALUE_CHANGE)
  142.         {
  143.             if(notifyOnDS || notifyOnStatic)
  144.             update((DataItem)e.getSource());
  145.  
  146.         }
  147.         if(e.getChangeType()==e.DATA_DELETED)
  148.         {
  149.         }
  150.  
  151.         if(e.getChangeType()==e.DATA_SIZE_CHANGE)
  152.         {
  153.             if(notifyOnDS || notifyOnStatic){
  154.             //setOutputSize(getCollectionSize((DataItem)e.getSource()));
  155.             //update((DataItem)e.getSource());
  156.             }
  157.         }
  158.  
  159.     }
  160.     /**
  161.      * Updates the values contained in the QueryNavigator if they are
  162.      * different from those in the DataItem.
  163.      * @param Input=the current dataItem
  164.      */
  165.     protected void update(DataItem Input)
  166.     {
  167.          if(hasFoundSetMethod && hasFoundGetMethod)
  168.         {
  169.             String nameString=getDataItemName((DataItem)Input);
  170.             if((Name.getNumberOfRows(nameString)==Name.AllRows))
  171.             {
  172.                 offset=output.getCurrentRowNumber();
  173.             }
  174.             else offset=0;
  175.             setOutputSize(getCollectionSize((DataItem)Input));
  176.              int[] cols=dataBinding.getColumnsFromName(nameString);
  177.              currentCursorNumber=0;
  178.              update(Input,cols,0,0) ;
  179.              return;
  180.         }
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Changes the value of the DataItem when requested by the DataBus.
  186.      * Also creates new dataItems when it does not exist
  187.      * @param ibe=DataBus Event
  188.      */
  189.     public void requestDataItem(DataBusEvent ibe)
  190.     {
  191.        String dataItemName=ibe.getDataItemName();
  192.  
  193.         if(ibe.getEventType()==ibe.DATA_REQUEST&&
  194.         dataBinding.isMyField(dataItemName))
  195.         {
  196.  
  197.             int numRows=Name.getNumberOfRows(dataItemName);
  198.             int[] actualNameCols=dataBinding.getColumnsFromName(dataItemName);
  199.             for(int i=0;i<DataInputVector.size();i++)
  200.                 {
  201.                     String ActualItemName=getDataItemName((DataItem)DataInputVector.elementAt(i));
  202.                     int[] storedNameCols=dataBinding.getColumnsFromName(ActualItemName);
  203.                     if(storedNameCols.length==actualNameCols.length &&
  204.                        numRows==Name.getNumberOfRows(ActualItemName))
  205.                         {
  206.                         boolean isSame=true;
  207.                         for(int j=0;j<storedNameCols.length;j++)
  208.                         {
  209.                             if(!(storedNameCols[j]==actualNameCols[j]))
  210.                             {
  211.                                 isSame=false;
  212.                                 break;
  213.                             }
  214.                         }
  215.                         if(isSame){
  216.                         ibe.setDataItem((DataItem)DataInputVector.elementAt(i));
  217.                         //return;
  218.                         }
  219.                     }
  220.                 }
  221.             boolean hasAllRows=false;
  222.             if (numRows==Name.AllRows){
  223.                 numRows=((BasicDataSource)Component.getOutput()).getTotalNumberOfRows();
  224.                 hasAllRows=true;
  225.             }
  226.  
  227.             Dimension D=new Dimension(Name.getNumberOfCols(dataItemName),numRows);
  228.  
  229.             if(ibe.getDataItem()==null)
  230.             {
  231.  
  232.                 notifyEnabled(false);
  233.                 if(Name.getFieldName(dataItemName).equals(Name.AllColumnsString)){
  234.                     String stringToRemove=","+QueryNavigator.RowNumber+","+QueryNavigator.RowState+","+QueryNavigator.NumberOfRows+","+QueryNavigator.CurrentDataSource;
  235.                     String fullName=dataBinding.getFullName();
  236.                     String newName;
  237.                     if(!hasAllRows)newName=fullName.substring(0,fullName.lastIndexOf(stringToRemove))+fullName.substring(fullName.lastIndexOf('%'),fullName.length());
  238.                     else newName=fullName.substring(0,fullName.lastIndexOf(stringToRemove))+"%"+Name.AllRows;
  239.                     dataItemName=newName;
  240.                     }
  241.  
  242.                 DataItem DI;
  243.                 Component.setOutputSize(D);
  244.                 int cols[]=dataBinding.getColumnsFromName(Name.getFieldName(dataItemName));
  245.  
  246.                 if(!hasAllRows)
  247.                 {
  248.                     DI=createDataItemFromArray(null,(Object[][])Component.getPrintOut(cols));
  249.                 }
  250.                 else
  251.                 {
  252.                     DI=createDataItemFromArray(new MutableCollectionItem() ,(Object[][])Component.getPrintOut(cols,((BasicDataSource)Component.getOutput()).getCurrentRowNumber()));
  253.                 }
  254.                 setDataItemName(DI,dataItemName);
  255.                 DI.addDataItemChangedListener(this);
  256.                 DataInputVector.addElement(DI);
  257.                 DataInputCols.addElement(dataBinding.getColumnsFromName(dataItemName));
  258.                 ibe.setDataItem(DI);
  259.                 //System.out.println("created"+dataItemName);
  260.                 notifyEnabled(true);
  261.             }
  262.         }
  263.  
  264.  
  265.     }
  266.     public void setDataBinding(String name)
  267.     {
  268.             if(Component!=null)
  269.             {
  270.  
  271.                 dataBinding=new Name(name);
  272.                 if(DBus==null)
  273.                     {
  274.                     DataBus.joinDataBus(this);
  275.                     }
  276.                 try{
  277.                     for(int i=0;i<DataInputVector.size();i++)
  278.                     {
  279.                         DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_REVOKED,getDataItemName((DataItem)DataInputVector.elementAt(i)),this));
  280.                         ((DataItem)DataInputVector.elementAt(i)).removeDataItemChangedListener(this);
  281.                     }
  282.                     DataInputVector.removeAllElements();
  283.                     DataInputCols.removeAllElements();
  284.                     DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_AVAILABLE,dataBinding.getFullName(),this));
  285.                     commit();
  286.                 }
  287.                 catch(InvalidType e){}
  288.             }
  289.  
  290.     }
  291.      public String getDataBinding()
  292.     {
  293.         return dataBinding.getFullName();
  294.     }
  295.     protected void notifyEnabled(boolean value)
  296.     {
  297.         notifyOnDS=value;
  298.     }
  299.     public void commitUI(TriggerUIEvent trui)
  300.     {
  301.         if(trui.m_Type==TriggerUIEvent.FORCEREFRESH)
  302.         {
  303.             Mediator.notifyOnStatic=true;
  304.             Mediator.notifySizeOnStatic=true;
  305.             commit();
  306.             Mediator.notifyOnStatic=false;
  307.             Mediator.notifySizeOnStatic=false;
  308.         }
  309.         else commit();
  310.  
  311.     }
  312.     protected void findDI()
  313.     {
  314.         if (java.beans.Beans.isDesignTime()) {
  315.             return;
  316.         }
  317.  
  318.         killActualInput();
  319.         DataItem DI=DBus.findDataItem(dataBinding.getFullName(),this);
  320.  
  321.         if(DI!=null && DI instanceof DataItem){
  322.             Input=DI;
  323.             Input.addDataItemChangedListener(this);
  324.         }
  325.     }
  326.  
  327.     protected String getDataItemName(DataItem DI)
  328.     {
  329.         if(DI instanceof DataItemAddition)return ((DataItemAddition)DI).getName();
  330.         else return null;
  331.     }
  332.     protected void setDataItemName(DataItem DI,String name)
  333.     {
  334.         if(DI instanceof DataItemAddition)((DataItemAddition)DI).setName(name);
  335.     }
  336.  
  337.     public void killAll()
  338.     {
  339.  
  340.         try{
  341.         DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_REVOKED,dataBinding.getTableName(),this));
  342.         }catch(Exception e){System.out.println("could'nt do kill all");}
  343.         DataInputVector.removeAllElements();
  344.         suicide();
  345.  
  346.     }
  347.  
  348. }