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 / OutputComponent.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  15.4 KB  |  514 lines

  1. /*
  2.  * @(#)OutputComponent.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  /**
  8.  * <p> This class takes care of all the component related issues. for instance
  9.  * finding the methods, getting and setting the values in the component.
  10.  *
  11.  */
  12. package symantec.itools.db.beans.binding;
  13. import java.awt.Dimension;
  14. import java.lang.reflect.*;
  15.  
  16.  
  17. public class OutputComponent
  18. {
  19.  
  20.     final static String ColumnNotFoundString="Not Found";
  21.     protected Method[] setMethods;
  22.     protected String[][] setMethodsParam;
  23.     protected int numberOfInvocationsForSet;
  24.     protected Method[] getMethods;
  25.     protected String[][] getMethodsParam;
  26.     protected int numberOfInvocationsForGet;
  27.     protected String[] setMethFullName;
  28.     protected String[] getMethFullName;
  29.     protected String[] getMethParam={};
  30.     protected String[] setMethParam={};
  31.     protected Dimension OutputSize;
  32.     protected Object Component;
  33.  
  34.     public Class InputClass=null;
  35.  
  36.    // protected String booleanTrueValue="true";
  37.    // protected String booleanFalseValue="false";
  38.    // protected boolean isConvertingBooleansIntoStrings=false;
  39.  
  40.     protected boolean m_EmptyMeansNull = true;
  41.  
  42.   
  43.     public OutputComponent()
  44.     {
  45.         
  46.     }
  47.     /**
  48.      * Creates a OutputComponent object given the output component.
  49.      * @param component
  50.      */
  51.     public OutputComponent(Object component)
  52.     {
  53.         Component=component;
  54.     }
  55.  
  56.     /**
  57.      * Creates a OutputComponent object given the output component,and the access
  58.      * methods name.
  59.      * @param component.    the output component
  60.      * @param setMethName.  method for setting
  61.      * @param getmethName.  method for getting
  62.      */
  63.   /*  public OutputComponent(Object component,String[] setMethName,String[] getMethName)
  64.     {
  65.  
  66.         setOutput(component);
  67.         setSetMethods(setMethName);
  68.         setGetMethods(getMethName);
  69.  
  70.     }
  71.  
  72.     /**
  73.      * Sets the output component.
  74.      * @param component.    the output component.
  75.      */
  76.     public void setOutput(Object component)
  77.     {
  78.         Component=component;
  79.  
  80.     }
  81.  
  82.     /**
  83.      * Returns the output component.
  84.      * @return Object. the output component.
  85.      */
  86.     public Object getOutput(){
  87.         return(Component);
  88.     }
  89.  
  90.     /**
  91.      * Searches the output component for a specified method and returns it.
  92.      * @param methodName=actual name of the method
  93.      * @param parameters=array containing the type of parameters.
  94.      *  Can be: "Value","Row" and "Col"
  95.      * @return Method. specified method
  96.      */
  97.     public Method findMethod(String methodName,String[] parameters)
  98.     {
  99.         boolean isRightMethod=true;
  100.         int numberOfParameters=0;
  101.         if(parameters!=null)numberOfParameters=parameters.length;
  102.         Method[] Met=(Component.getClass().getMethods());
  103.         for(int i=0;i<((Method[])Met).length;i++)
  104.         {
  105.             if(Met[i].getName().equals(methodName))
  106.             {
  107.                 isRightMethod=true;
  108.                 Class[] MethParam=Met[i].getParameterTypes();
  109.                 if(parameters!=null){
  110.                 for(int j=0;j<numberOfParameters;j++)
  111.                 {
  112.                     if((parameters[j].toLowerCase().equals("row")||parameters[j].toLowerCase().equals("col"))
  113.                     &&(!(MethParam[j].toString().equals("int"))))
  114.                     {
  115.                         isRightMethod=false;
  116.                     }
  117.                     if(!parameters[j].toLowerCase().equals("value")&&
  118.                     !parameters[j].toLowerCase().equals("col")&&
  119.                     !parameters[j].toLowerCase().equals("row"))
  120.                     {
  121.                         isRightMethod=false;
  122.                     }
  123.  
  124.                 }}
  125.                 if(isRightMethod&&(MethParam.length==numberOfParameters))
  126.                 {
  127.                     return(Met[i]);
  128.                 }
  129.             }
  130.         }
  131.         return(null);
  132.     }
  133.  
  134.     /**
  135.      * Parses the name of the setting method given by the user and calls
  136.      * findmethod. Sets the setting method for the component.
  137.      * @param method=full method name ie:"setText(Value)"
  138.      * @return boolean. is the method found and set?
  139.      */
  140.  
  141.  
  142.     public boolean setSetMethods(String[] methods)
  143.     {
  144.         setMethFullName=methods;
  145.         numberOfInvocationsForSet=methods.length;
  146.         setMethodsParam=new String[numberOfInvocationsForSet][];
  147.         setMethods=new Method[numberOfInvocationsForSet];
  148.         for(int i=0;i<numberOfInvocationsForSet;i++)
  149.         {
  150.             setMethodsParam[i]=getMethodParameters(methods[i]);
  151.             setMethods[i]=findMethod(methods[i].substring(0,methods[i].indexOf("(")),setMethodsParam[i]);
  152.             if(setMethods[i]==null)
  153.             {
  154.                 System.out.println("Method "+methods[i]+" not found");
  155.                 return false;
  156.             }
  157.             
  158.         }
  159.         return true;
  160.     }
  161.     /**
  162.      * Parses the name of the getting method given by the user and calls
  163.      * findmethod. Sets the getting method for the component.
  164.      * @param method=full method name ie:"getText(Row,Col)"
  165.      * @return boolean. is the method found and set?
  166.      */
  167.  
  168.     public boolean setGetMethods(String[] methods)
  169.     {
  170.         getMethFullName=methods;
  171.         numberOfInvocationsForGet=methods.length;
  172.         getMethodsParam=new String[numberOfInvocationsForGet][];
  173.         getMethods=new Method[numberOfInvocationsForGet];
  174.         for(int i=0;i<numberOfInvocationsForGet;i++)
  175.         {
  176.             getMethodsParam[i]=getMethodParameters(methods[i]);
  177.             getMethods[i]=findMethod(methods[i].substring(0,methods[i].indexOf("(")),getMethodsParam[i]);
  178.             if(getMethods[i]==null)
  179.             {
  180.                 System.out.println("Method "+methods[i]+" not found");
  181.                 return false;
  182.             }
  183.             else
  184.             {
  185.                 if(Component!=null && InputClass==null)
  186.                     {
  187.                     InputClass=getMethods[numberOfInvocationsForGet-1].getReturnType();
  188.                     }
  189.             }
  190.         }
  191.         return true;
  192.     }
  193.  
  194.     protected String[] getMethodParameters(String method)
  195.     {
  196.         int numberOfParam=0;
  197.         String methodName=method.substring(0,method.indexOf("("));
  198.         method=method.substring(method.indexOf("(")+1,method.length()-1)+",";
  199.         for(int i=0;i<method.length();i++)
  200.         {
  201.             if(method.charAt(i)==','&& method.length()>1)
  202.             {
  203.                 numberOfParam++;
  204.             }
  205.         }
  206.         if(numberOfParam!=0)
  207.         {
  208.         String[] param=new String[numberOfParam];
  209.         for(int i=0;i<numberOfParam;i++)
  210.         {
  211.             param[i]=method.substring(0,method.indexOf(","));
  212.             method=method.substring(method.indexOf(",")+1,method.length());
  213.         }
  214.         return param;
  215.         }
  216.         else return null;
  217.     }
  218.  
  219.  
  220.  
  221.     /**
  222.      * Returns the full name of the setting method
  223.      * @return String. the full name of the setting method
  224.      */
  225.     public String[] getSetMethods()
  226.     {
  227.         return(setMethFullName);
  228.     }
  229.  
  230.     /**
  231.      * Returns the full name of the setting method
  232.      * @return String. the full name of the getting method
  233.      */
  234.     public String[] getGetMethods()
  235.     {
  236.         return(getMethFullName);
  237.     }
  238.  
  239.     /**
  240.      * Sets the size for the output component.
  241.      * @param size
  242.      */
  243.     public void setOutputSize(Dimension size)
  244.     {
  245.         OutputSize=size;
  246.     }
  247.  
  248.     /**
  249.      * Returns the size for the output component.
  250.      * @return Dimension.   the size for the output component.
  251.      */
  252.     public Dimension getOutputSize()
  253.     {
  254.         return(OutputSize);
  255.     }
  256.     /**
  257.      * sets the true value for passing booleans
  258.      * @param the value as a String
  259.      */
  260.     /*public void setBooleanTrueValue(String Value)
  261.     {
  262.         booleanTrueValue=Value;
  263.     }
  264.     /**
  265.      * returns  the true value for passing booleans
  266.      */
  267.     /*public String getBooleanTrueValue()
  268.     {
  269.         return booleanTrueValue;
  270.     }
  271.  
  272.     /**
  273.      * sets the false value for passing booleans
  274.      * @param the value as a String
  275.      */
  276.     /*public void setBooleanFalseValue(String Value)
  277.     {
  278.         booleanFalseValue=Value;
  279.     }
  280.     /**
  281.      * returns  the false value for passing booleans
  282.      */
  283.     /*public String getBooleanFalseValue()
  284.     {
  285.         return booleanFalseValue;
  286.     }
  287.  
  288.  
  289.     /**
  290.      * When a get or set method is invoked on a output component, this method
  291.      * prepares the parameters for invoke().
  292.      * @param MethParam=list of parameters
  293.      * @param Value
  294.      * @param row
  295.      * @param col
  296.      * @return Object[]. set of parameters for invoking a method
  297.      */
  298.     protected Object[] setParameters (String[] MethParam,Object Value,int row,int col)
  299.     {
  300.         if(MethParam!=null)
  301.         {
  302.         Object Parameters[]=new Object[MethParam.length];
  303.         for(int i=0;i<Parameters.length;i++)
  304.         {
  305.             if(MethParam[i].toLowerCase().equals("value"))
  306.             {
  307.                /* if(InputClass!= null && InputClass.toString().equals("boolean") && (Value instanceof String))
  308.                 {
  309.                     isConvertingBooleansIntoStrings=true;
  310.                     Parameters[i]=setBooleanParameter(Value);
  311.                 }*/
  312.                 if(InputClass!= null && InputClass.toString().equals("class java.lang.String"))
  313.                 {
  314.                     if(Value!=null) Parameters[i]=((Object)Value.toString());
  315.                     else Parameters[i]=(Object)"";
  316.                 }
  317.                 else Parameters[i]=((Object)Value);
  318.             }
  319.             else if(MethParam[i].toLowerCase().equals("col")) Parameters[i]=(Object)(new Integer(col));
  320.             else if(MethParam[i].toLowerCase().equals("row")) Parameters[i]=(Object)(new Integer(row));
  321.         }
  322.         return(Parameters);
  323.         }
  324.         else return null;
  325.     }
  326.  
  327.     /*protected Boolean setBooleanParameter(Object Value)
  328.     {
  329.         if(Value.equals(booleanTrueValue))
  330.         {
  331.             return(new Boolean(true));
  332.         }
  333.         else if (Value.equals(booleanFalseValue))
  334.         {
  335.             return(new Boolean(false));
  336.         }
  337.         else
  338.         {
  339.             return(new Boolean(false));
  340.         }
  341.     }*/
  342.     /**
  343.      * Updates every cell of the output component.
  344.      * @param Value
  345.      */
  346.     public void setPrintOut(Object Value)
  347.     {
  348.         for(int i=0;i<OutputSize.height;i++)
  349.         {
  350.             for(int j=0;j<OutputSize.width;j++)
  351.             {
  352.                 setPrintOut(Value,i,j);
  353.             }
  354.         }
  355.     }
  356.  
  357.     /**
  358.      * Updates one cell of the output component.
  359.      * @param Value
  360.      * @param Row
  361.      * @param Col
  362.      */
  363.     synchronized public void setPrintOut(Object Value,int Row,int Col)
  364.     {
  365.         if(InputClass!= null && InputClass.toString().equals("class java.lang.String"))
  366.         {
  367.             if(Value!=null) Value=((Object)Value.toString());
  368.             else Value="";
  369.         }
  370.         for(int i=0;i<numberOfInvocationsForSet;i++)
  371.         {
  372.             Object Values[]=setParameters(setMethodsParam[i],Value,Row,Col);
  373.             try
  374.             {
  375.                 if(setMethods[i]!=null && Col>=0)
  376.                 {
  377.                  
  378.                     setMethods[i].invoke(Component,Values);
  379.                    
  380.                 }
  381.             }
  382.             catch(IllegalAccessException e)
  383.             {
  384.                 System.out.println("Exception IllegalAcces");
  385.             }
  386.             catch(IllegalArgumentException e)
  387.             {
  388.                 System.out.println("Exception IllegalArgument on set "+setMethods[i].getName());
  389.             }
  390.             catch(InvocationTargetException e)
  391.             {
  392.                 System.out.println("Exception InvocationTarget "+setMethods[i].getName());
  393.             }
  394.         }
  395.     }
  396.     /**
  397.      * Returns all the data in the output component.
  398.      * @return Object. all the data in the output component.
  399.      */
  400.     public Object getPrintOut()
  401.     {
  402.         Object Out[][]=new Object[OutputSize.height][OutputSize.width];
  403.         for(int i=0;i<OutputSize.height;i++)
  404.         {
  405.             for(int j=0;j<OutputSize.width;j++)
  406.             {
  407.                 Out[i][j]=getPrintOut(i,j);
  408.             }
  409.         }
  410.         return(Out);
  411.     }
  412.  
  413.     /**
  414.      * Returns the data from certain columns of the output component.
  415.      * @param cols=list of the columns concerned
  416.      */
  417.      public Object getPrintOut(int[] cols)
  418.     {
  419.         return getPrintOut(cols,0);
  420.     }
  421.     public Object getPrintOut(int[] cols,int negOffset)
  422.     {
  423.         int x=OutputSize.height;
  424.         int y=cols.length;
  425.         Object Out[][]=new Object[x][y];
  426.         for(int i=0;i<y;i++)
  427.         {
  428.             for(int j=0;j<x;j++)
  429.             {
  430.                Out[j][i]=getPrintOut(j-negOffset,cols[i]);
  431.               
  432.             }
  433.         }
  434.         return(Out);
  435.     }
  436.  
  437.     /**
  438.      * Returns data from one cell.
  439.      * @param Row
  440.      * @param Col
  441.      */
  442.     public Object getPrintOut(int Row,int Col)
  443.     {
  444.         Object Out=new Object();
  445.         for(int i=0;i<numberOfInvocationsForGet;i++)
  446.         {
  447.             Object Values[]=setParameters(getMethodsParam[i],"",Row,Col);
  448.             if(getMethods[i]!=null )
  449.             {
  450.                 if(Col==Name.ColumnNotFoundField)
  451.                 {
  452.                     return(ColumnNotFoundString);
  453.                 }
  454.                 try
  455.                 {
  456.                     if(i==numberOfInvocationsForGet-1)
  457.                  {
  458.                     Out=(Object)getMethods[i].invoke(Component,Values);
  459.                 }
  460.                     else
  461.                         getMethods[i].invoke(Component,Values);
  462.                 }
  463.                 catch(IllegalAccessException e)
  464.                 {
  465.                     System.out.println("Exception IllegalAcces");
  466.                 }
  467.                 catch(IllegalArgumentException e)
  468.                 {
  469.                     System.out.println("Exception IllegalArgument is here "+getMethods[i].getName());
  470.                 }
  471.                 catch(InvocationTargetException e)
  472.                 {
  473.                     System.out.println("Exception InvocationTarget "+getMethods[i].getName());
  474.                 }
  475.                 if(Out!=null && InputClass==null)
  476.                     {
  477.                     InputClass=Out.getClass();
  478.                     }
  479.                 /*if(InputClass!=null && InputClass.toString().equals("boolean") && isConvertingBooleansIntoStrings)
  480.                 {
  481.                     if(((Boolean)Out).booleanValue())
  482.                     {
  483.                         return(booleanTrueValue);
  484.                     }
  485.                     else
  486.                     {
  487.                         return(booleanFalseValue);
  488.                     }
  489.                 }*/
  490.             }
  491.             if(Out==null || Out.equals(""))
  492.             {
  493.                 if(getEmptyMeansNull()){
  494.                     Out=null;
  495.                 }
  496.                 else{
  497.                     Out=new String();
  498.                 }
  499.             }
  500.             return Out;
  501.         }
  502.        return null;
  503.     }
  504.  
  505.     protected void setEmptyMeansNull(boolean emptyMeansNull)
  506.     {
  507.         m_EmptyMeansNull = emptyMeansNull;
  508.     }
  509.     protected boolean getEmptyMeansNull()
  510.     {
  511.         return m_EmptyMeansNull;
  512.     }
  513. }
  514.