home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 20.5 KB | 694 lines |
- /*
- * @(#)Mediator.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
- /**
- * <p>A Mediator is designed to be able to take data on an DataBus and display it
- * in any kind of graphic component. It can display single cell values as well
- * as multi dimensional array of cells.
- *
- * Example of implementation:
- * customer_id= new Mediator();
- * customer_id.setOutput(customer_id_textField);
- * customer_id.setDataName("customer@id%1");
- * customer_id.setSetMeth("setText(Value)");
- * customer_id.setGetMeth("getText()");
- *
- */
-
- package symantec.itools.db.beans.binding;
-
- import java.awt.Dimension;
- import java.util.Vector;
- import java.awt.event.FocusListener;
- import java.awt.event.FocusEvent;
- import java.beans.*;
- import symantec.itools.db.beans.binding.databus.*;
- import symantec.itools.db.awt.ProjectionBean;
-
- public class Mediator implements DataItemChangedListener,DataBusEventListener,DataBusMember//,FocusListener,
- {
- /**
- * A constant indicating how an empty string will be set when updating
- * data on the dbANYWHERE server.
- */
- public final static int Default = 0;
- /**
- * A constant indicating how an empty string will be set when updating
- * data on the dbANYWHERE server.
- */
- public final static int Null = 1;
- /**
- * A constant indicating how an empty string will be set when updating
- * data on the dbANYWHERE server.
- */
- public final static int Blank = 2;
- protected DataItem Input;
- /**
- * name of the DataItem. This Name class contains all the methods for
- * parsing and using the names.
- */
-
- protected String dataBinding;
- protected Object output=this;
- protected String[] setMethods;
- protected String[] getMethods;
- protected OutputComponent Component;
- protected DataBus DBus;
- protected boolean hasFoundSetMethod=false;
- protected boolean hasFoundGetMethod=false;
- private VetoableChangeSupport vetos=new VetoableChangeSupport(this);
- static boolean notifyOnStatic=false;
- protected boolean notifyOn=true;
- static boolean notifySizeOnStatic=true;
-
- /**
- * Constructs the default mediator.
- */
- public Mediator()
- {
- killActualInput();
- dataBinding=new String();
- }
-
-
- /**
- * Constructs a Mediator given its output component and the full name of
- * the fields it is supposed to show
- * @param Out. output component
- * @param name. fullname of the fields
- */
- public Mediator(Object Out, String name)
- {
- setOutput(Out);
- setDataBinding(name);
- }
-
- /**
- * Connects to the DataBus
- * @param b. the DataBus the mediator connects to
- * @exception PropertyVetoException
- */
- public void setDataBus(DataBus b)throws PropertyVetoException
- {
- vetos.fireVetoableChange("DataBus",DBus,b);
- DBus=b;
- DBus.addDataBusEventListener((DataBusEventListener)this);
- }
-
- /**
- *Returns the current DataBus
- *@return DataBus. the current DataBus
- */
- public DataBus getDataBus()
- {
- return(DBus);
- }
-
- /**
- * adds and removes vetoable change listeners for the data bus.
- */
-
- public void addVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.addVetoableChangeListener(listener);
- }
-
- public void removeVetoableChangeListener(VetoableChangeListener listener)
- {
- vetos.removeVetoableChangeListener(listener);
- }
-
- /**
- * Handles the DataBus events, in particular the Data available events sent by the QueryNavigators.
- * @param ibe. DataBusevent
- */
- public void handleDataBusEvent(DataBusEvent ibe)
- {
- if(ibe.getEventType()==ibe.DATA_AVAILABLE &&
- ibe.getSource()!=this)
- //not respond to its own events
- {
- if(Input==null)findDI();
- }
- if(ibe.getEventType()==ibe.DATA_REVOKED &&
- ibe.getSource()!=this && dataBinding.substring(0,dataBinding.indexOf('@')).equals(ibe.getDataItemName()))
- {
- suicide();
- // killActualInput();
- }
- }
-
- /**
- * Changes the value of the DataItem when requested by the DataBus
- * @param ibe. DataBus Event
- */
- public void requestDataItem(DataBusEvent ibe)
- {
- /*if(ibe.getEventType()==ibe.DATA_REQUEST &&
- dataBinding.equals(ibe.getDataItemName()))
- {
- createDataItemFromArray(ibe.getDataItem(),(Object[][])Component.getPrintOut());
- }*/
-
-
- }
-
- /**
- * finds a DataItem given its name
- */
- protected void findDI()
- {
- if (java.beans.Beans.isDesignTime()) {
- return;
- }
-
- killActualInput();
- DataItem DI=DBus.findDataItem(dataBinding,this);
-
- if(DI!=null && DI instanceof DataItem){
- Input=DI;
- setOutputSize(getCollectionSize(DI));
- Input.addDataItemChangedListener(this);
- update(Input);
- }
- }
-
- protected void killActualInput()
- {
- if(Input!=null){
- Input.removeDataItemChangedListener(this);
- Input=null;
- }
- }
-
- /**
- * Responds to the sollicitations of the DataItem.
- * @param e. dataitemchangedevent
- */
- public void notifyDataItemChanged(DataItemChangedEvent e)
- {
- if((notifyOn || notifyOnStatic) && hasFoundSetMethod &&(DataItem)e.getSource()==Input)
- {
- if(e.getChangeType()==e.DATA_VALUE_CHANGE)
- {
- update(Input);
- }
-
- if(e.getChangeType()==e.DATA_DELETED)
- {
- setOutputSize(getCollectionSize(Input));
- update(Input);
- }
-
- if(e.getChangeType()==e.DATA_SIZE_CHANGE)
- {
- setOutputSize(getCollectionSize(Input));
- if(notifySizeOnStatic)
- {
- update(Input);
-
- }
- }
- }
- }
-
-
- /**
- * Updates the DataItem with the values present in the output component.
- */
- public void commit()
- {
- if(hasFoundGetMethod&&hasFoundSetMethod && Input!=null)
- {
- Object printout=Component.getPrintOut();
- createDataItemFromArray(Input,(Object[][])printout);
-
- }
- }
-
- public void commitUI(TriggerUIEvent trui)
- {
- commit();
- }
- /**
- * returns true if the mediator has found his data Item;
- */
- public boolean hasFoundDI()
- {
- if(Input!=null) return true;
- return false;
- }
-
- /**
- * Sets the output component in witch the Mediator is going to display
- * the data.
- * @param Out. output component
- */
- public void setOutput(Object Out)
- {
- output=Out;
-
- if(Out instanceof ProjectionBean)Component= new OutputComponentForProj(Out);
- else if(Out instanceof BasicDataSource)Component= new OutputComponentForDS(Out);
- else{
- Component= new OutputComponent(Out);
- }
- if(!dataBinding.equals(""))setDataBinding(dataBinding);
- if(setMethods!=null)setSetMethods(setMethods);
- if(getMethods!=null)setGetMethods(getMethods);
- }
-
- /**
- * Returns the actual output component
- * @return Object. output component
- */
- public Object getOutput()
- {
- return(output);
- }
-
- /**
- * Sets the methods for retrieving a value from the component.
- * therte methods are in an array of strings and will be executed one after the other
- * @param methodnames. the names of the methods including their parameters
- * i.e. "setText(Value,Row,Col)"
- */
- public void setSetMethods(String[] methodNames)
- {
- setMethods=methodNames;
- if(Component!=null)
- {
- hasFoundSetMethod=Component.setSetMethods(setMethods);
- if (hasFoundSetMethod && hasFoundDI()) {
- update(Input);
- }
- }
- }
- /**
- * Sets the methods for displaying a value in the component.
- * @param methodnames. the names of the methods including their parameters
- * i.e. {"get Focus(Row,Col)","getText()"}
- */
- public void setGetMethods(String[] methodNames)
- {
- getMethods=methodNames;
- if(Component!=null)
- {
- hasFoundGetMethod=Component.setGetMethods(getMethods);
- }
- }
- /**
- * Returns the full name of the current set method.
- * @return String. the method for setting.
- */
- public String[] getSetMethods()
- {
- return(setMethods);
- }
-
- /**
- * Returns the full name of the current get method.
- * @return String. the method for getting.
- */
- public String[] getGetMethods()
- {
- return(getMethods);
- }
-
- /**
- * Sets the dimension of the output component, meaning how many cells
- * is it going to display.
- * @param size. The desire size
- */
- protected void setOutputSize(Dimension size)
- {
- Component.setOutputSize(size);
- }
-
- /**
- * Returns the actual size of the output component.
- * @return Dimension. the dimension of the output component
- */
- public Dimension getOutputSize()
- {
- return(Component.getOutputSize());
- }
-
- public int getNumberOfRows()
- {
- return getOutputSize().height;
- }
-
- /**
- * Updates the values displayed in the output component if they are
- * different from those in the DataItem.
- */
-
- protected void update(DataItem Input)
- {
- update(Input,0,0);
- }
- protected int currentCursorNumber=0;
- protected void update(DataItem Input,int xcursor,int ycursor)
- {
- currentCursorNumber=0;
- update(Input,null,0,0) ;
- return;
- }
- protected int offset=0;
- protected void update(DataItem Input,int[] columns,int xcursor,int ycursor)
- {
- boolean OutPutSet=false;
-
- if (Input instanceof ImmediateAccess)
- {
-
- //Object ValueAsObject=((ImmediateAccess)Input).getValueAsObject();
- int outputColumn=xcursor;
- if (columns!=null)outputColumn=columns[xcursor];
- Component.setPrintOut(((ImmediateAccess)Input).getValueAsObject(),ycursor-offset,outputColumn);
- return;
-
- }
- else if (Input instanceof CollectionAccess)
- {
- DataCursor cursor=(DataCursor)((CollectionAccess)Input).getCursor();
- currentCursorNumber++;
- DataItem DI=cursor.getCurrentItem();
- while(DI!=null)
- {
- if(DI instanceof DataItemAddition)
- {
- int flag=((DataItemAddition)DI).getStatusFlag();
- if(flag!=DataItemAddition.EXISTING)
- {
- update(DI,columns,xcursor,ycursor);
- if(flag==DataItemAddition.NEW)
- {
- // ((DataItemAddition)DI).cleanUp();
-
- }
- }
- }
- else
- update(DI,columns,xcursor,ycursor);
- if(currentCursorNumber==1)ycursor++;
- if(currentCursorNumber==2)xcursor++;
- DI=cursor.getNextItem();
- }
- currentCursorNumber--;
-
- }
-
- }
-
-
- /**
- * Sets the name for the actual Data Item.And searches the DataBus for it
- * This method is very important in the sense that it is the one that actually connects the
- * mediator to the data bus
- * The format is Tablename@col1,col2%NumberOfRows
- * @param name. the name of the data to be shown
- */
-
- public void setDataBinding(String name)
- {
- dataBinding=name;
- if(Component!=null)
- {
- if(DBus==null)DataBus.joinDataBus(this);
- findDI();
- }
- }
-
- /**
- * Returns the full name of the DataItem.
- * @return String. the full name of the DataItem.
- */
- public String getDataBinding()
- {
- return dataBinding;
- }
-
-
- /**
- * Sets the notify flag.
- */
- protected void notifyEnabled(boolean value)
- {
- notifyOn=value;
- }
-
- public void setEmptyMeansNull(boolean propertyValue)
- {
- Component.setEmptyMeansNull(propertyValue);
- }
-
- public boolean getEmptyMeansNull()
- {
- return Component.getEmptyMeansNull();
- }
- //----------------------------------------------------------------------------
-
- protected Dimension getCollectionSize(DataItem DI)
- {
- if(DI instanceof ImmediateAccess)
- return new Dimension(1,1);
-
- Dimension size=new Dimension(0,0);
- if(DI instanceof CollectionAccess)
- {
- DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
- DataItem DI2=cursor.getCurrentItem();
- if(DI2!=null)
- {
- size.height++;
- while(cursor.getNextItem()!=null)
- {
- size.height++;
- }
- if (DI2 instanceof CollectionAccess)
- {
- DataCursor cursor2=(DataCursor)((CollectionAccess)DI2).getCursor();
- size.width++;
- while(cursor2.getNextItem()!=null)
- {
- size.width++;
- }
-
- }}
- }
- return size;
- }
-
- /* protected Object[][] createArrayFromDataItem(DataItem DI)
- {
- System.out.println("hello");
- int xcursor=0;
- int ycursor=0;
- Object[][] ValueAsArray;
- if (DI instanceof ImmediateAccess)
- {
- ValueAsArray=new Object[1][1];
- ValueAsArray[0][0]=((ImmediateAccess)DI).getValueAsObject();
- return ValueAsArray;
- }
- else
- {
- Dimension size=getCollectionSize((CollectionAccess)DI);
- System.out.println(size.width+"lllll"+size.height);
- ValueAsArray=new Object[size.width][size.height];
- DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
- DataItem DI2=cursor.getCurrentItem();
- while(DI2!=null)
- {
- if (DI2 instanceof CollectionAccess)
- {
- DataCursor cursor2=(DataCursor)((CollectionAccess)DI2).getCursor();
- DataItem DI3=cursor2.getCurrentItem();
- while(DI3!=null)
- {
- ValueAsArray[ycursor][xcursor]=(((ImmediateAccess)DI3).getValueAsObject());
- DI3=cursor2.getNextItem();
- xcursor++;
- }
- cursor2.reset();
- xcursor=0;
- }
- else
- {
- ValueAsArray[ycursor][0]=(((ImmediateAccess)DI2).getValueAsObject());
- }
- DI2=cursor.getNextItem();
- ycursor++;
- }
- cursor.reset();
- return ValueAsArray;
- }
- }
- */
-
- protected int recursionLevel=0;
- protected DataItem createDataItemFromArray(DataItem DI,Object[][] obj)
- {
- if(this instanceof MediatorDS)
- {
- ((MediatorDS)this).notifyEnabled(false);
- notifyOnStatic=false;
- }
- boolean New=false;
- int rows=0;
- int cols=0;
- try
- {
- cols=obj[0].length;
- rows=obj.length;
- }
- catch(Exception e)
- {
- obj=new Object[1][1];
- cols=1;
- rows=1;
- }
- int dif=0;
- if(cols==1 && rows==1 &&(DI==null || DI instanceof MutableImmediateAccess))
- {
- if(DI==null){
- New=true;
- DI=new DataInput();
- }
- Object ValueToSet=obj[0][0];
- notifyEnabled(false);
- ((MutableImmediateAccess)DI).setValue(new NewValue(ValueToSet));
- notifyEnabled(true);
-
- if(New)
- {
- New=false;
- if(DI instanceof DataItemAddition)((DataItemAddition)DI).setStatusFlag(DataItemAddition.NEW);
- }
- return DI;
- }
-
- else{
- recursionLevel++;
- if(DI==null){
- DI =new MutableCollectionItem();
- ((DataItemAddition)DI).setStatusFlag(DataItemAddition.NEW);
- }
- DataCursor cursor=(DataCursor)((CollectionAccess)DI).getCursor();
- DataItem DI2=cursor.getCurrentItem();
-
- int limit=rows;
- if (rows==1 && recursionLevel>1)limit=cols;
-
- if( recursionLevel==1)
- {
- int oldRows=getCollectionSize(DI).height;
- dif=oldRows-rows;
- for(int t=0;t<dif;t++)
- {
- DI2=cursor.getNextItem();
- if(DI2 instanceof DataItemAddition)((DataItemAddition)DI2).setStatusFlag(DataItemAddition.MODIFIED);
- }
- }
- for(int j=0;j<limit;j++)
- {
- Object[][] a;
- if(rows==1 && recursionLevel>1)
- {
- a=getSlice(0,obj,j);
- }
- else
- {
- a=getSlice(1,obj,j);
- }
- if(DI2!=null)
- {
- DataItem DI3=DI2;
- DI2=createDataItemFromArray(DI2,a);
- if(DI2 instanceof DataItemAddition && dif!=0)((DataItemAddition)DI2).setStatusFlag(DataItemAddition.MODIFIED);
-
- if(DI2!=DI3)
- {
-
- ((MutableCollectionAccess)DI).insertBeforeCursor(cursor,DI2);
- ((MutableCollectionAccess)DI).removeAtCursor(cursor);
-
- if(DI2 instanceof DataItemAddition)
- {
-
- ((DataItemAddition)DI).setStatusFlag(((DataItemAddition)DI2).getStatusFlag());
- }
- }
-
- DI2=cursor.getNextItem();
- }
- else
- {
- DI2=createDataItemFromArray(null,a);
- DI2.addDataItemChangedListener((DataItemChangedListener)DI);
- ((MutableCollectionAccess)DI).addDataItem(DI2);
- DI2=null;
- }
-
- }
- while(DI2!=null)
- {
- ((MutableCollectionAccess)DI).removeAtCursor(cursor);
- DI2.removeDataItemChangedListener((DataItemChangedListener)DI);
-
- DI2=cursor.getNextItem();
- }
- if( recursionLevel==1)
- {
- cursor.reset();
- for(int t=0;t<dif;t++)
-
- {
- if(t!=dif-1)notifySizeOnStatic=false;
- else notifySizeOnStatic=true;
-
- ((MutableCollectionAccess)DI).removeAtCursor(cursor);
- notifySizeOnStatic=true;
- }
-
-
- }
- recursionLevel--;
- cursor.reset();
-
- return DI;
- }
-
- }
- protected Object[][] getSlice(int type,Object[][] obj,int j)
- {
- if(type==0)
- {
- Object[][] a={{obj[0][j]}};
- return a;
- }
- else
- {
- Object[][] a={obj[j]};
- return a;
- }
- }
- protected void finalize()throws Throwable
- {
- super.finalize();
- }
- protected void suicide()
- {
- DBus.removeDataBusEventListener((DataBusEventListener)this);
- DBus.leaveDataBus((DataBusMember)this);
- try{
- finalize();
- }
- catch(Throwable e){System.out.println("could'nt do it");}
- }
- }