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 / Mediator.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  20.5 KB  |  694 lines

  1. /*
  2.  * @(#)Mediator.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7. /**
  8.  * <p>A Mediator is designed to be able to take data on an DataBus and display it
  9.  * in any kind of graphic component. It can display single cell values as well
  10.  * as multi dimensional array of cells.
  11.  *
  12.  * Example of implementation:
  13.  * customer_id= new Mediator();
  14.  * customer_id.setOutput(customer_id_textField);
  15.  * customer_id.setDataName("customer@id%1");
  16.  * customer_id.setSetMeth("setText(Value)");
  17.  * customer_id.setGetMeth("getText()");
  18.  *
  19.  */
  20.  
  21. package symantec.itools.db.beans.binding;
  22.  
  23. import java.awt.Dimension;
  24. import java.util.Vector;
  25. import java.awt.event.FocusListener;
  26. import java.awt.event.FocusEvent;
  27. import java.beans.*;
  28. import symantec.itools.db.beans.binding.databus.*;
  29. import symantec.itools.db.awt.ProjectionBean;
  30.  
  31. public class Mediator  implements DataItemChangedListener,DataBusEventListener,DataBusMember//,FocusListener,
  32. {
  33.      /**
  34.      * A constant indicating how an empty string will be set when updating
  35.      * data on the dbANYWHERE server.
  36.      */
  37.     public final static int Default    =  0;
  38.     /**
  39.      * A constant indicating how an empty string will be set when updating
  40.      * data on the dbANYWHERE server.
  41.      */
  42.     public final static int Null       =  1;
  43.     /**
  44.      * A constant indicating how an empty string will be set when updating
  45.      * data on the dbANYWHERE server.
  46.      */
  47.     public final static int Blank      =  2;
  48.     protected DataItem Input;
  49.     /**
  50.      * name of the DataItem. This Name class contains all the methods for
  51.      * parsing and using the names.
  52.      */
  53.  
  54.     protected String dataBinding;
  55.     protected Object output=this;
  56.     protected String[] setMethods;
  57.     protected String[] getMethods;
  58.     protected OutputComponent Component;
  59.     protected DataBus DBus;
  60.     protected boolean hasFoundSetMethod=false;
  61.     protected boolean hasFoundGetMethod=false;
  62.     private VetoableChangeSupport vetos=new VetoableChangeSupport(this);
  63.     static boolean notifyOnStatic=false;
  64.     protected boolean notifyOn=true;
  65. static boolean notifySizeOnStatic=true;
  66.  
  67.     /**
  68.      * Constructs the default mediator.
  69.      */
  70.     public Mediator()
  71.     {
  72.          killActualInput();
  73.        dataBinding=new String();
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Constructs a Mediator given its output component and the full name of
  79.      * the fields it is supposed to show
  80.      * @param Out.  output component
  81.      * @param name. fullname of the fields
  82.      */
  83.     public Mediator(Object Out, String name)
  84.     {
  85.         setOutput(Out);
  86.         setDataBinding(name);
  87.     }
  88.  
  89.     /**
  90.      * Connects to the DataBus
  91.      * @param b.    the DataBus the mediator connects to
  92.      * @exception PropertyVetoException
  93.      */
  94.     public void setDataBus(DataBus b)throws PropertyVetoException
  95.     {
  96.         vetos.fireVetoableChange("DataBus",DBus,b);
  97.         DBus=b;
  98.         DBus.addDataBusEventListener((DataBusEventListener)this);
  99.     }
  100.  
  101.     /**
  102.      *Returns the current DataBus
  103.      *@return DataBus. the current DataBus
  104.      */
  105.     public DataBus getDataBus()
  106.     {
  107.         return(DBus);
  108.     }
  109.  
  110.     /**
  111.      * adds and removes vetoable change listeners for the data bus.
  112.      */
  113.  
  114.     public void addVetoableChangeListener(VetoableChangeListener listener)
  115.     {
  116.         vetos.addVetoableChangeListener(listener);
  117.     }
  118.  
  119.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  120.     {
  121.          vetos.removeVetoableChangeListener(listener);
  122.     }
  123.  
  124.     /**
  125.      * Handles the DataBus events, in particular the Data available events sent by the QueryNavigators.
  126.      * @param ibe. DataBusevent
  127.      */
  128.     public void handleDataBusEvent(DataBusEvent ibe)
  129.     {
  130.         if(ibe.getEventType()==ibe.DATA_AVAILABLE &&
  131.         ibe.getSource()!=this)
  132.         //not respond to its own events
  133.         {
  134.            if(Input==null)findDI();
  135.         }
  136.         if(ibe.getEventType()==ibe.DATA_REVOKED &&
  137.         ibe.getSource()!=this && dataBinding.substring(0,dataBinding.indexOf('@')).equals(ibe.getDataItemName()))
  138.         {
  139.             suicide();
  140.            // killActualInput();
  141.         }
  142.     }
  143.  
  144.     /**
  145.      * Changes the value of the DataItem when requested by the DataBus
  146.      * @param ibe.  DataBus Event
  147.      */
  148.     public void requestDataItem(DataBusEvent ibe)
  149.     {
  150.        /*if(ibe.getEventType()==ibe.DATA_REQUEST &&
  151.        dataBinding.equals(ibe.getDataItemName()))
  152.        {
  153.             createDataItemFromArray(ibe.getDataItem(),(Object[][])Component.getPrintOut());
  154.        }*/
  155.       
  156.        
  157.     }
  158.  
  159.     /**
  160.      * finds a DataItem given its name
  161.      */
  162.     protected void findDI()
  163.     {
  164.         if (java.beans.Beans.isDesignTime()) {
  165.             return;
  166.         }
  167.  
  168.         killActualInput();
  169.         DataItem DI=DBus.findDataItem(dataBinding,this);
  170.  
  171.         if(DI!=null && DI instanceof DataItem){
  172.             Input=DI;
  173.             setOutputSize(getCollectionSize(DI));
  174.             Input.addDataItemChangedListener(this);
  175.             update(Input);
  176.         }
  177.     }
  178.  
  179.     protected void killActualInput()
  180.     {
  181.         if(Input!=null){
  182.             Input.removeDataItemChangedListener(this);
  183.             Input=null;
  184.         }
  185.     }
  186.  
  187.     /**
  188.      * Responds to the sollicitations of the DataItem.
  189.      * @param e.    dataitemchangedevent
  190.      */
  191.     public void notifyDataItemChanged(DataItemChangedEvent e)
  192.     {
  193.         if((notifyOn || notifyOnStatic) && hasFoundSetMethod &&(DataItem)e.getSource()==Input)
  194.         {
  195.             if(e.getChangeType()==e.DATA_VALUE_CHANGE)
  196.             {
  197.                 update(Input);
  198.             }
  199.  
  200.             if(e.getChangeType()==e.DATA_DELETED)
  201.             {
  202.                 setOutputSize(getCollectionSize(Input));
  203.                 update(Input);
  204.             }
  205.  
  206.             if(e.getChangeType()==e.DATA_SIZE_CHANGE)
  207.             {
  208.                 setOutputSize(getCollectionSize(Input));
  209.                 if(notifySizeOnStatic)
  210.                 {
  211.                     update(Input);
  212.                     
  213.                 }
  214.             }
  215.         }
  216.     }
  217.  
  218.  
  219.     /**
  220.      * Updates the DataItem with the values present in the output component.
  221.      */
  222.     public void commit()
  223.     {
  224.         if(hasFoundGetMethod&&hasFoundSetMethod && Input!=null)
  225.         {
  226.            Object printout=Component.getPrintOut();
  227.            createDataItemFromArray(Input,(Object[][])printout);
  228.  
  229.         }
  230.     }
  231.  
  232.     public void commitUI(TriggerUIEvent trui)
  233.     {
  234.          commit();
  235.     }
  236.     /**
  237.      * returns true if the mediator has found his data Item;
  238.      */
  239.     public boolean hasFoundDI()
  240.     {
  241.         if(Input!=null) return true;
  242.         return false;
  243.     }
  244.  
  245.     /**
  246.      * Sets the output component in witch the Mediator is going to display
  247.      * the data.
  248.      * @param Out.  output component
  249.      */
  250.     public void setOutput(Object Out)
  251.     {
  252.         output=Out;
  253.  
  254.         if(Out instanceof ProjectionBean)Component= new OutputComponentForProj(Out);
  255.         else if(Out instanceof BasicDataSource)Component= new OutputComponentForDS(Out);
  256.         else{
  257.             Component= new OutputComponent(Out);
  258.         }
  259.         if(!dataBinding.equals(""))setDataBinding(dataBinding);
  260.         if(setMethods!=null)setSetMethods(setMethods);
  261.         if(getMethods!=null)setGetMethods(getMethods);
  262.     }
  263.  
  264.     /**
  265.      * Returns the actual output component
  266.      * @return Object. output component
  267.      */
  268.     public Object getOutput()
  269.     {
  270.         return(output);
  271.     }
  272.  
  273.      /**
  274.      * Sets the methods for retrieving a value from the component.
  275.      * therte methods are in an array of strings and will be executed one after the other
  276.      * @param methodnames.   the names of the methods including their parameters
  277.      *  i.e. "setText(Value,Row,Col)"
  278.      */
  279.     public void setSetMethods(String[] methodNames)
  280.     {
  281.         setMethods=methodNames;
  282.         if(Component!=null)
  283.         {
  284.              hasFoundSetMethod=Component.setSetMethods(setMethods);
  285.              if (hasFoundSetMethod && hasFoundDI()) {
  286.                 update(Input);
  287.              }
  288.         }
  289.     }
  290.      /**
  291.      * Sets the methods for displaying a value in the component.
  292.      * @param methodnames.   the names of the methods including their parameters
  293.      *  i.e. {"get Focus(Row,Col)","getText()"}
  294.      */
  295.     public void setGetMethods(String[] methodNames)
  296.     {
  297.         getMethods=methodNames;
  298.         if(Component!=null)
  299.         {
  300.              hasFoundGetMethod=Component.setGetMethods(getMethods);
  301.         }
  302.     }
  303.     /**
  304.      * Returns the full name of the current set method.
  305.      * @return String. the method for setting.
  306.      */
  307.     public String[] getSetMethods()
  308.     {
  309.         return(setMethods);
  310.     }
  311.  
  312.     /**
  313.      * Returns the full name of the current get method.
  314.      * @return String. the method for getting.
  315.      */
  316.     public String[] getGetMethods()
  317.     {
  318.         return(getMethods);
  319.     }
  320.  
  321.     /**
  322.      * Sets the dimension of the output component, meaning how many cells
  323.      * is it going to display.
  324.      * @param size. The desire size
  325.      */
  326.     protected void setOutputSize(Dimension size)
  327.     {
  328.         Component.setOutputSize(size);
  329.     }
  330.  
  331.     /**
  332.      * Returns the actual size of the output component.
  333.      * @return Dimension. the dimension of the output component
  334.      */
  335.     public Dimension getOutputSize()
  336.     {
  337.         return(Component.getOutputSize());
  338.     }
  339.     
  340.     public int getNumberOfRows()
  341.     {
  342.         return getOutputSize().height;
  343.     }
  344.  
  345.     /**
  346.      * Updates the values displayed in the output component if they are
  347.      * different from those in the DataItem.
  348.      */
  349.  
  350.     protected void update(DataItem Input)
  351.     {
  352.         update(Input,0,0);
  353.     }
  354.     protected int currentCursorNumber=0;
  355.     protected void update(DataItem Input,int xcursor,int ycursor)
  356.     {
  357.          currentCursorNumber=0;
  358.          update(Input,null,0,0) ;
  359.          return;
  360.     }
  361.     protected int offset=0;
  362.     protected void update(DataItem Input,int[] columns,int xcursor,int ycursor)
  363.     {
  364.         boolean OutPutSet=false;
  365.  
  366.         if (Input instanceof ImmediateAccess)
  367.         {
  368.            
  369.             //Object ValueAsObject=((ImmediateAccess)Input).getValueAsObject();
  370.             int outputColumn=xcursor;
  371.             if (columns!=null)outputColumn=columns[xcursor];
  372.             Component.setPrintOut(((ImmediateAccess)Input).getValueAsObject(),ycursor-offset,outputColumn);
  373.             return;
  374.                     
  375.         }
  376.         else if (Input instanceof CollectionAccess)
  377.         {
  378.             DataCursor cursor=(DataCursor)((CollectionAccess)Input).getCursor();
  379.             currentCursorNumber++;
  380.             DataItem DI=cursor.getCurrentItem();
  381.             while(DI!=null)
  382.             {
  383.                if(DI instanceof DataItemAddition)
  384.                 {
  385.                     int flag=((DataItemAddition)DI).getStatusFlag();
  386.                     if(flag!=DataItemAddition.EXISTING)
  387.                     {
  388.                        update(DI,columns,xcursor,ycursor);
  389.                        if(flag==DataItemAddition.NEW)
  390.                        {
  391.                       //  ((DataItemAddition)DI).cleanUp();
  392.                    
  393.                        }
  394.                     }
  395.                 }
  396.                 else 
  397.                 update(DI,columns,xcursor,ycursor);
  398.                 if(currentCursorNumber==1)ycursor++;
  399.                 if(currentCursorNumber==2)xcursor++;
  400.                 DI=cursor.getNextItem();
  401.             }
  402.             currentCursorNumber--;
  403.  
  404.         }
  405.                       
  406.     }
  407.  
  408.  
  409.     /**
  410.      * Sets the name for the actual Data Item.And searches the DataBus for it
  411.      * This method is very important in the sense that it is the one that actually connects the
  412.      * mediator to the data bus
  413.      * The format is Tablename@col1,col2%NumberOfRows
  414.      * @param name. the name of the data to be shown
  415.      */
  416.  
  417.     public void setDataBinding(String name)
  418.     {
  419.             dataBinding=name;
  420.             if(Component!=null)
  421.             {
  422.                 if(DBus==null)DataBus.joinDataBus(this);
  423.                 findDI();
  424.             }
  425.     }
  426.  
  427.     /**
  428.      * Returns the full name of the DataItem.
  429.      * @return String.  the full name of the DataItem.
  430.      */
  431.     public String getDataBinding()
  432.     {
  433.         return dataBinding;
  434.     }
  435.  
  436.  
  437.     /**
  438.      * Sets the notify flag.
  439.      */
  440.     protected void notifyEnabled(boolean value)
  441.     {
  442.         notifyOn=value;
  443.     }
  444.  
  445.     public void setEmptyMeansNull(boolean propertyValue)
  446.     {
  447.         Component.setEmptyMeansNull(propertyValue);
  448.     }
  449.  
  450.     public boolean getEmptyMeansNull()
  451.     {
  452.         return Component.getEmptyMeansNull();
  453.     }
  454.     //----------------------------------------------------------------------------
  455.  
  456.     protected Dimension getCollectionSize(DataItem DI)
  457.     {
  458.         if(DI instanceof ImmediateAccess)
  459.         return new Dimension(1,1);
  460.         
  461.         Dimension size=new Dimension(0,0);
  462.         if(DI instanceof CollectionAccess)
  463.         {
  464.             DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
  465.             DataItem DI2=cursor.getCurrentItem();
  466.             if(DI2!=null)
  467.             {
  468.                 size.height++;
  469.             while(cursor.getNextItem()!=null)
  470.                 {
  471.                     size.height++;
  472.                 }
  473.             if (DI2 instanceof CollectionAccess)
  474.             {
  475.                 DataCursor cursor2=(DataCursor)((CollectionAccess)DI2).getCursor();
  476.                  size.width++;
  477.                 while(cursor2.getNextItem()!=null)
  478.                 {
  479.                     size.width++;
  480.                 }
  481.            
  482.             }}
  483.         }
  484.         return size;
  485.     }
  486.  
  487.    /* protected Object[][] createArrayFromDataItem(DataItem DI)
  488.     {
  489.          System.out.println("hello");
  490.         int xcursor=0;
  491.         int ycursor=0;
  492.         Object[][] ValueAsArray;
  493.         if (DI instanceof ImmediateAccess)
  494.         {
  495.             ValueAsArray=new Object[1][1];
  496.             ValueAsArray[0][0]=((ImmediateAccess)DI).getValueAsObject();
  497.             return ValueAsArray;
  498.         }
  499.         else
  500.         {
  501.             Dimension size=getCollectionSize((CollectionAccess)DI);
  502.             System.out.println(size.width+"lllll"+size.height);
  503.             ValueAsArray=new Object[size.width][size.height];
  504.             DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
  505.             DataItem DI2=cursor.getCurrentItem();
  506.             while(DI2!=null)
  507.             {
  508.                 if (DI2 instanceof CollectionAccess)
  509.                 {
  510.                     DataCursor cursor2=(DataCursor)((CollectionAccess)DI2).getCursor();
  511.                     DataItem DI3=cursor2.getCurrentItem();
  512.                     while(DI3!=null)
  513.                     {
  514.                         ValueAsArray[ycursor][xcursor]=(((ImmediateAccess)DI3).getValueAsObject());
  515.                         DI3=cursor2.getNextItem();
  516.                         xcursor++;
  517.                     }
  518.                     cursor2.reset();
  519.                     xcursor=0;
  520.                 }
  521.                 else
  522.                 {
  523.                      ValueAsArray[ycursor][0]=(((ImmediateAccess)DI2).getValueAsObject());
  524.                 }
  525.                 DI2=cursor.getNextItem();
  526.                 ycursor++;
  527.             }
  528.             cursor.reset();
  529.             return ValueAsArray;
  530.         }
  531.     }
  532.     */
  533.     
  534.     protected int recursionLevel=0;
  535.     protected DataItem createDataItemFromArray(DataItem DI,Object[][] obj)
  536.     {
  537.         if(this instanceof MediatorDS)
  538.         {
  539.             ((MediatorDS)this).notifyEnabled(false);
  540.             notifyOnStatic=false;
  541.         }
  542.         boolean New=false;
  543.         int rows=0;
  544.         int cols=0;
  545.         try
  546.         {
  547.             cols=obj[0].length;
  548.             rows=obj.length;
  549.         }
  550.         catch(Exception e)
  551.         {   
  552.            obj=new Object[1][1];
  553.            cols=1;
  554.            rows=1;
  555.            }
  556.         int dif=0;
  557.         if(cols==1 && rows==1 &&(DI==null || DI instanceof MutableImmediateAccess))
  558.         {
  559.             if(DI==null){
  560.                 New=true;
  561.                 DI=new DataInput();
  562.             }
  563.             Object ValueToSet=obj[0][0];
  564.             notifyEnabled(false);
  565.                 ((MutableImmediateAccess)DI).setValue(new NewValue(ValueToSet));
  566.             notifyEnabled(true);
  567.           
  568.             if(New)
  569.             {
  570.                 New=false;
  571.                 if(DI instanceof DataItemAddition)((DataItemAddition)DI).setStatusFlag(DataItemAddition.NEW);
  572.             }
  573.             return DI;
  574.         }
  575.         
  576.         else{
  577.             recursionLevel++;
  578.             if(DI==null){
  579.                 DI =new MutableCollectionItem();
  580.                 ((DataItemAddition)DI).setStatusFlag(DataItemAddition.NEW);
  581.             }
  582.             DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
  583.             DataItem DI2=cursor.getCurrentItem();
  584.         
  585.             int limit=rows;
  586.             if (rows==1 && recursionLevel>1)limit=cols;
  587.             
  588.             if( recursionLevel==1)
  589.             {
  590.                 int oldRows=getCollectionSize(DI).height;
  591.                 dif=oldRows-rows;
  592.                 for(int t=0;t<dif;t++)
  593.                 {
  594.                    DI2=cursor.getNextItem();
  595.                    if(DI2 instanceof DataItemAddition)((DataItemAddition)DI2).setStatusFlag(DataItemAddition.MODIFIED);
  596.                 }
  597.             }
  598.             for(int j=0;j<limit;j++)
  599.             {
  600.                 Object[][] a;
  601.                 if(rows==1 && recursionLevel>1)
  602.                 {
  603.                     a=getSlice(0,obj,j);
  604.                 }
  605.                 else
  606.                 {
  607.                     a=getSlice(1,obj,j);
  608.                 }
  609.                 if(DI2!=null)
  610.                 {
  611.                     DataItem  DI3=DI2;
  612.                     DI2=createDataItemFromArray(DI2,a);
  613.                     if(DI2 instanceof DataItemAddition && dif!=0)((DataItemAddition)DI2).setStatusFlag(DataItemAddition.MODIFIED);
  614.                
  615.                     if(DI2!=DI3)
  616.                     {
  617.                        
  618.                         ((MutableCollectionAccess)DI).insertBeforeCursor(cursor,DI2);
  619.                         ((MutableCollectionAccess)DI).removeAtCursor(cursor);
  620.                       
  621.                         if(DI2 instanceof DataItemAddition)
  622.                         {
  623.                               
  624.                             ((DataItemAddition)DI).setStatusFlag(((DataItemAddition)DI2).getStatusFlag());
  625.                         }
  626.                     }
  627.                   
  628.                     DI2=cursor.getNextItem();
  629.                 }
  630.                 else
  631.                 {
  632.                     DI2=createDataItemFromArray(null,a);
  633.                     DI2.addDataItemChangedListener((DataItemChangedListener)DI);
  634.                     ((MutableCollectionAccess)DI).addDataItem(DI2);
  635.                     DI2=null;
  636.                 }
  637.  
  638.             }
  639.             while(DI2!=null)
  640.             {
  641.                 ((MutableCollectionAccess)DI).removeAtCursor(cursor);
  642.                 DI2.removeDataItemChangedListener((DataItemChangedListener)DI);
  643.                     
  644.                 DI2=cursor.getNextItem();
  645.             }
  646.             if( recursionLevel==1)
  647.             {
  648.                cursor.reset();
  649.                for(int t=0;t<dif;t++)
  650.               
  651.                {
  652.                   if(t!=dif-1)notifySizeOnStatic=false;
  653.                   else notifySizeOnStatic=true;
  654.                   
  655.                   ((MutableCollectionAccess)DI).removeAtCursor(cursor);
  656.                   notifySizeOnStatic=true;
  657.                }
  658.               
  659.              
  660.             }
  661.             recursionLevel--;
  662.             cursor.reset();
  663.           
  664.             return DI;
  665.         }
  666.  
  667.     }
  668.     protected Object[][] getSlice(int type,Object[][] obj,int j)
  669.     {
  670.         if(type==0)
  671.         {
  672.              Object[][] a={{obj[0][j]}};
  673.              return a;
  674.         }
  675.         else
  676.         {
  677.             Object[][] a={obj[j]};
  678.             return a;
  679.         }
  680.     }
  681.     protected void finalize()throws Throwable
  682.     {
  683.         super.finalize();
  684.     }
  685.     protected void suicide()
  686.     {
  687.         DBus.removeDataBusEventListener((DataBusEventListener)this);
  688.         DBus.leaveDataBus((DataBusMember)this);
  689.         try{
  690.         finalize();
  691.         }
  692.         catch(Throwable e){System.out.println("could'nt do it");}
  693.     }  
  694. }