home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 15.4 KB | 514 lines |
- /*
- * @(#)OutputComponent.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
- /**
- * <p> This class takes care of all the component related issues. for instance
- * finding the methods, getting and setting the values in the component.
- *
- */
- package symantec.itools.db.beans.binding;
- import java.awt.Dimension;
- import java.lang.reflect.*;
-
-
- public class OutputComponent
- {
-
- final static String ColumnNotFoundString="Not Found";
- protected Method[] setMethods;
- protected String[][] setMethodsParam;
- protected int numberOfInvocationsForSet;
- protected Method[] getMethods;
- protected String[][] getMethodsParam;
- protected int numberOfInvocationsForGet;
- protected String[] setMethFullName;
- protected String[] getMethFullName;
- protected String[] getMethParam={};
- protected String[] setMethParam={};
- protected Dimension OutputSize;
- protected Object Component;
-
- public Class InputClass=null;
-
- // protected String booleanTrueValue="true";
- // protected String booleanFalseValue="false";
- // protected boolean isConvertingBooleansIntoStrings=false;
-
- protected boolean m_EmptyMeansNull = true;
-
-
- public OutputComponent()
- {
-
- }
- /**
- * Creates a OutputComponent object given the output component.
- * @param component
- */
- public OutputComponent(Object component)
- {
- Component=component;
- }
-
- /**
- * Creates a OutputComponent object given the output component,and the access
- * methods name.
- * @param component. the output component
- * @param setMethName. method for setting
- * @param getmethName. method for getting
- */
- /* public OutputComponent(Object component,String[] setMethName,String[] getMethName)
- {
-
- setOutput(component);
- setSetMethods(setMethName);
- setGetMethods(getMethName);
-
- }
-
- /**
- * Sets the output component.
- * @param component. the output component.
- */
- public void setOutput(Object component)
- {
- Component=component;
-
- }
-
- /**
- * Returns the output component.
- * @return Object. the output component.
- */
- public Object getOutput(){
- return(Component);
- }
-
- /**
- * Searches the output component for a specified method and returns it.
- * @param methodName=actual name of the method
- * @param parameters=array containing the type of parameters.
- * Can be: "Value","Row" and "Col"
- * @return Method. specified method
- */
- public Method findMethod(String methodName,String[] parameters)
- {
- boolean isRightMethod=true;
- int numberOfParameters=0;
- if(parameters!=null)numberOfParameters=parameters.length;
- Method[] Met=(Component.getClass().getMethods());
- for(int i=0;i<((Method[])Met).length;i++)
- {
- if(Met[i].getName().equals(methodName))
- {
- isRightMethod=true;
- Class[] MethParam=Met[i].getParameterTypes();
- if(parameters!=null){
- for(int j=0;j<numberOfParameters;j++)
- {
- if((parameters[j].toLowerCase().equals("row")||parameters[j].toLowerCase().equals("col"))
- &&(!(MethParam[j].toString().equals("int"))))
- {
- isRightMethod=false;
- }
- if(!parameters[j].toLowerCase().equals("value")&&
- !parameters[j].toLowerCase().equals("col")&&
- !parameters[j].toLowerCase().equals("row"))
- {
- isRightMethod=false;
- }
-
- }}
- if(isRightMethod&&(MethParam.length==numberOfParameters))
- {
- return(Met[i]);
- }
- }
- }
- return(null);
- }
-
- /**
- * Parses the name of the setting method given by the user and calls
- * findmethod. Sets the setting method for the component.
- * @param method=full method name ie:"setText(Value)"
- * @return boolean. is the method found and set?
- */
-
-
- public boolean setSetMethods(String[] methods)
- {
- setMethFullName=methods;
- numberOfInvocationsForSet=methods.length;
- setMethodsParam=new String[numberOfInvocationsForSet][];
- setMethods=new Method[numberOfInvocationsForSet];
- for(int i=0;i<numberOfInvocationsForSet;i++)
- {
- setMethodsParam[i]=getMethodParameters(methods[i]);
- setMethods[i]=findMethod(methods[i].substring(0,methods[i].indexOf("(")),setMethodsParam[i]);
- if(setMethods[i]==null)
- {
- System.out.println("Method "+methods[i]+" not found");
- return false;
- }
-
- }
- return true;
- }
- /**
- * Parses the name of the getting method given by the user and calls
- * findmethod. Sets the getting method for the component.
- * @param method=full method name ie:"getText(Row,Col)"
- * @return boolean. is the method found and set?
- */
-
- public boolean setGetMethods(String[] methods)
- {
- getMethFullName=methods;
- numberOfInvocationsForGet=methods.length;
- getMethodsParam=new String[numberOfInvocationsForGet][];
- getMethods=new Method[numberOfInvocationsForGet];
- for(int i=0;i<numberOfInvocationsForGet;i++)
- {
- getMethodsParam[i]=getMethodParameters(methods[i]);
- getMethods[i]=findMethod(methods[i].substring(0,methods[i].indexOf("(")),getMethodsParam[i]);
- if(getMethods[i]==null)
- {
- System.out.println("Method "+methods[i]+" not found");
- return false;
- }
- else
- {
- if(Component!=null && InputClass==null)
- {
- InputClass=getMethods[numberOfInvocationsForGet-1].getReturnType();
- }
- }
- }
- return true;
- }
-
- protected String[] getMethodParameters(String method)
- {
- int numberOfParam=0;
- String methodName=method.substring(0,method.indexOf("("));
- method=method.substring(method.indexOf("(")+1,method.length()-1)+",";
- for(int i=0;i<method.length();i++)
- {
- if(method.charAt(i)==','&& method.length()>1)
- {
- numberOfParam++;
- }
- }
- if(numberOfParam!=0)
- {
- String[] param=new String[numberOfParam];
- for(int i=0;i<numberOfParam;i++)
- {
- param[i]=method.substring(0,method.indexOf(","));
- method=method.substring(method.indexOf(",")+1,method.length());
- }
- return param;
- }
- else return null;
- }
-
-
-
- /**
- * Returns the full name of the setting method
- * @return String. the full name of the setting method
- */
- public String[] getSetMethods()
- {
- return(setMethFullName);
- }
-
- /**
- * Returns the full name of the setting method
- * @return String. the full name of the getting method
- */
- public String[] getGetMethods()
- {
- return(getMethFullName);
- }
-
- /**
- * Sets the size for the output component.
- * @param size
- */
- public void setOutputSize(Dimension size)
- {
- OutputSize=size;
- }
-
- /**
- * Returns the size for the output component.
- * @return Dimension. the size for the output component.
- */
- public Dimension getOutputSize()
- {
- return(OutputSize);
- }
- /**
- * sets the true value for passing booleans
- * @param the value as a String
- */
- /*public void setBooleanTrueValue(String Value)
- {
- booleanTrueValue=Value;
- }
- /**
- * returns the true value for passing booleans
- */
- /*public String getBooleanTrueValue()
- {
- return booleanTrueValue;
- }
-
- /**
- * sets the false value for passing booleans
- * @param the value as a String
- */
- /*public void setBooleanFalseValue(String Value)
- {
- booleanFalseValue=Value;
- }
- /**
- * returns the false value for passing booleans
- */
- /*public String getBooleanFalseValue()
- {
- return booleanFalseValue;
- }
-
-
- /**
- * When a get or set method is invoked on a output component, this method
- * prepares the parameters for invoke().
- * @param MethParam=list of parameters
- * @param Value
- * @param row
- * @param col
- * @return Object[]. set of parameters for invoking a method
- */
- protected Object[] setParameters (String[] MethParam,Object Value,int row,int col)
- {
- if(MethParam!=null)
- {
- Object Parameters[]=new Object[MethParam.length];
- for(int i=0;i<Parameters.length;i++)
- {
- if(MethParam[i].toLowerCase().equals("value"))
- {
- /* if(InputClass!= null && InputClass.toString().equals("boolean") && (Value instanceof String))
- {
- isConvertingBooleansIntoStrings=true;
- Parameters[i]=setBooleanParameter(Value);
- }*/
- if(InputClass!= null && InputClass.toString().equals("class java.lang.String"))
- {
- if(Value!=null) Parameters[i]=((Object)Value.toString());
- else Parameters[i]=(Object)"";
- }
- else Parameters[i]=((Object)Value);
- }
- else if(MethParam[i].toLowerCase().equals("col")) Parameters[i]=(Object)(new Integer(col));
- else if(MethParam[i].toLowerCase().equals("row")) Parameters[i]=(Object)(new Integer(row));
- }
- return(Parameters);
- }
- else return null;
- }
-
- /*protected Boolean setBooleanParameter(Object Value)
- {
- if(Value.equals(booleanTrueValue))
- {
- return(new Boolean(true));
- }
- else if (Value.equals(booleanFalseValue))
- {
- return(new Boolean(false));
- }
- else
- {
- return(new Boolean(false));
- }
- }*/
- /**
- * Updates every cell of the output component.
- * @param Value
- */
- public void setPrintOut(Object Value)
- {
- for(int i=0;i<OutputSize.height;i++)
- {
- for(int j=0;j<OutputSize.width;j++)
- {
- setPrintOut(Value,i,j);
- }
- }
- }
-
- /**
- * Updates one cell of the output component.
- * @param Value
- * @param Row
- * @param Col
- */
- synchronized public void setPrintOut(Object Value,int Row,int Col)
- {
- if(InputClass!= null && InputClass.toString().equals("class java.lang.String"))
- {
- if(Value!=null) Value=((Object)Value.toString());
- else Value="";
- }
- for(int i=0;i<numberOfInvocationsForSet;i++)
- {
- Object Values[]=setParameters(setMethodsParam[i],Value,Row,Col);
- try
- {
- if(setMethods[i]!=null && Col>=0)
- {
-
- setMethods[i].invoke(Component,Values);
-
- }
- }
- catch(IllegalAccessException e)
- {
- System.out.println("Exception IllegalAcces");
- }
- catch(IllegalArgumentException e)
- {
- System.out.println("Exception IllegalArgument on set "+setMethods[i].getName());
- }
- catch(InvocationTargetException e)
- {
- System.out.println("Exception InvocationTarget "+setMethods[i].getName());
- }
- }
- }
- /**
- * Returns all the data in the output component.
- * @return Object. all the data in the output component.
- */
- public Object getPrintOut()
- {
- Object Out[][]=new Object[OutputSize.height][OutputSize.width];
- for(int i=0;i<OutputSize.height;i++)
- {
- for(int j=0;j<OutputSize.width;j++)
- {
- Out[i][j]=getPrintOut(i,j);
- }
- }
- return(Out);
- }
-
- /**
- * Returns the data from certain columns of the output component.
- * @param cols=list of the columns concerned
- */
- public Object getPrintOut(int[] cols)
- {
- return getPrintOut(cols,0);
- }
- public Object getPrintOut(int[] cols,int negOffset)
- {
- int x=OutputSize.height;
- int y=cols.length;
- Object Out[][]=new Object[x][y];
- for(int i=0;i<y;i++)
- {
- for(int j=0;j<x;j++)
- {
- Out[j][i]=getPrintOut(j-negOffset,cols[i]);
-
- }
- }
- return(Out);
- }
-
- /**
- * Returns data from one cell.
- * @param Row
- * @param Col
- */
- public Object getPrintOut(int Row,int Col)
- {
- Object Out=new Object();
- for(int i=0;i<numberOfInvocationsForGet;i++)
- {
- Object Values[]=setParameters(getMethodsParam[i],"",Row,Col);
- if(getMethods[i]!=null )
- {
- if(Col==Name.ColumnNotFoundField)
- {
- return(ColumnNotFoundString);
- }
- try
- {
- if(i==numberOfInvocationsForGet-1)
- {
- Out=(Object)getMethods[i].invoke(Component,Values);
- }
- else
- getMethods[i].invoke(Component,Values);
- }
- catch(IllegalAccessException e)
- {
- System.out.println("Exception IllegalAcces");
- }
- catch(IllegalArgumentException e)
- {
- System.out.println("Exception IllegalArgument is here "+getMethods[i].getName());
- }
- catch(InvocationTargetException e)
- {
- System.out.println("Exception InvocationTarget "+getMethods[i].getName());
- }
- if(Out!=null && InputClass==null)
- {
- InputClass=Out.getClass();
- }
- /*if(InputClass!=null && InputClass.toString().equals("boolean") && isConvertingBooleansIntoStrings)
- {
- if(((Boolean)Out).booleanValue())
- {
- return(booleanTrueValue);
- }
- else
- {
- return(booleanFalseValue);
- }
- }*/
- }
- if(Out==null || Out.equals(""))
- {
- if(getEmptyMeansNull()){
- Out=null;
- }
- else{
- Out=new String();
- }
- }
- return Out;
- }
- return null;
- }
-
- protected void setEmptyMeansNull(boolean emptyMeansNull)
- {
- m_EmptyMeansNull = emptyMeansNull;
- }
- protected boolean getEmptyMeansNull()
- {
- return m_EmptyMeansNull;
- }
- }
-