home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 11.4 KB | 348 lines |
- /*
- * @(#)MediatorDS.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
- /**
- * <p>The MediatorDS is a special kind of mediator specialy designed for
- * working with data sources. The data sources must implement the
- * BasicDataSource interface.
- *
- */
-
- package symantec.itools.db.beans.binding;
-
- import java.awt.Dimension;
- import symantec.itools.db.beans.binding.databus.*;
- import java.util.Vector;
- import java.beans.PropertyVetoException;
- import java.beans.VetoableChangeListener;
-
-
- public class MediatorDS extends Mediator implements TriggerUIListener
- {
- /**
- * Contains all the DataItem requested by the components.
- */
- protected Name dataBinding;
- private Vector DataInputVector =new Vector();
- private Vector DataInputCols =new Vector();
- protected BasicDataSource output;
- protected String[] setMethods={"setValueTrust(Value,Row,Col)"};
- protected String[] getMethods={"getValue(Row,Col)"};
- protected boolean notifyOnDS=true;
- /**
- * Constructs the default mediatorDS.
- */
- public MediatorDS()
- {
- super();
- DataInputVector =new Vector();
- DataInputCols =new Vector();
- notifyOnDS=true;
- }
-
- /**
- * Constructs a MediatorDS 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 MediatorDS(Object Out,String Name)
- {
- super(Out,Name);
- }
-
- /**
- * Connects to the DataBus
- * @param b=the DataBus the mediator connects to
- * @exception PropertyVetoException
- */
- public void setDataBus(DataBus b)throws PropertyVetoException{
- DBus=b;
- DBus.addDataBusEventListener((DataBusEventListener)this);
- }
-
- /**
- * Sets the QueryNavigator for the MediatorDS.
- * @param Out=data source
- */
- public void setOutput(Object Out)
- {
-
- super.setOutput(Out);
- output=(BasicDataSource)Out;
- try{
- Component.InputClass=Class.forName("java.lang.Object");
- }
- catch(Exception e){}
- super.setEmptyMeansNull(false);
- ((BasicDataSource)Out).addTriggerUIListener(this);
- }
- public void handleDataBusEvent(DataBusEvent ibe)
- {
- }
-
- /**
- * Updates all the DataItems with the values present in the QueryNavigator.
- */
- int currentRow=0;
- public void commit()
- {
- if(hasFoundSetMethod && hasFoundGetMethod)
- {
- this.notifyEnabled(false);
- int row=output.getCurrentRowNumber();
-
- for(int i=0;i<DataInputVector.size();i++)
- {
- DataItem elem=(DataItem)DataInputVector.elementAt(i);
- commit(elem,row,(int[])DataInputCols.elementAt(i));
- }
- for(int i=0;i<DataInputVector.size();i++)
- {
- ((DataItemAddition)DataInputVector.elementAt(i)).cleanUp();
- }
-
-
- currentRow=row;
- this.notifyEnabled(true);
- }
- }
-
-
-
-
- public void commit(DataItem DI,int row,int[] cols)
- {
- String dataItemName=getDataItemName(DI);
- if(!(Name.getNumberOfRows(dataItemName)==Name.AllRows))
- {
- Component.setOutputSize(getCollectionSize(DI));
- createDataItemFromArray(DI,(Object[][])Component.getPrintOut(cols));
- }
- else
- {
- //if(currentRow==row)
- {
- Component.setOutputSize(new Dimension(getCollectionSize(DI).width,output.getTotalNumberOfRows()));
- createDataItemFromArray(DI,(Object[][])Component.getPrintOut(cols,row));
- }
- }
- }
-
- /**
- * Responds to the sollicitations of the DataItems.
- * @param e
- */
- public void notifyDataItemChanged(DataItemChangedEvent e)
- {
- if(e.getChangeType()==e.DATA_VALUE_CHANGE)
- {
- if(notifyOnDS || notifyOnStatic)
- update((DataItem)e.getSource());
-
- }
- if(e.getChangeType()==e.DATA_DELETED)
- {
- }
-
- if(e.getChangeType()==e.DATA_SIZE_CHANGE)
- {
- if(notifyOnDS || notifyOnStatic){
- //setOutputSize(getCollectionSize((DataItem)e.getSource()));
- //update((DataItem)e.getSource());
- }
- }
-
- }
- /**
- * Updates the values contained in the QueryNavigator if they are
- * different from those in the DataItem.
- * @param Input=the current dataItem
- */
- protected void update(DataItem Input)
- {
- if(hasFoundSetMethod && hasFoundGetMethod)
- {
- String nameString=getDataItemName((DataItem)Input);
- if((Name.getNumberOfRows(nameString)==Name.AllRows))
- {
- offset=output.getCurrentRowNumber();
- }
- else offset=0;
- setOutputSize(getCollectionSize((DataItem)Input));
- int[] cols=dataBinding.getColumnsFromName(nameString);
- currentCursorNumber=0;
- update(Input,cols,0,0) ;
- return;
- }
- }
-
-
- /**
- * Changes the value of the DataItem when requested by the DataBus.
- * Also creates new dataItems when it does not exist
- * @param ibe=DataBus Event
- */
- public void requestDataItem(DataBusEvent ibe)
- {
- String dataItemName=ibe.getDataItemName();
-
- if(ibe.getEventType()==ibe.DATA_REQUEST&&
- dataBinding.isMyField(dataItemName))
- {
-
- int numRows=Name.getNumberOfRows(dataItemName);
- int[] actualNameCols=dataBinding.getColumnsFromName(dataItemName);
- for(int i=0;i<DataInputVector.size();i++)
- {
- String ActualItemName=getDataItemName((DataItem)DataInputVector.elementAt(i));
- int[] storedNameCols=dataBinding.getColumnsFromName(ActualItemName);
- if(storedNameCols.length==actualNameCols.length &&
- numRows==Name.getNumberOfRows(ActualItemName))
- {
- boolean isSame=true;
- for(int j=0;j<storedNameCols.length;j++)
- {
- if(!(storedNameCols[j]==actualNameCols[j]))
- {
- isSame=false;
- break;
- }
- }
- if(isSame){
- ibe.setDataItem((DataItem)DataInputVector.elementAt(i));
- //return;
- }
- }
- }
- boolean hasAllRows=false;
- if (numRows==Name.AllRows){
- numRows=((BasicDataSource)Component.getOutput()).getTotalNumberOfRows();
- hasAllRows=true;
- }
-
- Dimension D=new Dimension(Name.getNumberOfCols(dataItemName),numRows);
-
- if(ibe.getDataItem()==null)
- {
-
- notifyEnabled(false);
- if(Name.getFieldName(dataItemName).equals(Name.AllColumnsString)){
- String stringToRemove=","+QueryNavigator.RowNumber+","+QueryNavigator.RowState+","+QueryNavigator.NumberOfRows+","+QueryNavigator.CurrentDataSource;
- String fullName=dataBinding.getFullName();
- String newName;
- if(!hasAllRows)newName=fullName.substring(0,fullName.lastIndexOf(stringToRemove))+fullName.substring(fullName.lastIndexOf('%'),fullName.length());
- else newName=fullName.substring(0,fullName.lastIndexOf(stringToRemove))+"%"+Name.AllRows;
- dataItemName=newName;
- }
-
- DataItem DI;
- Component.setOutputSize(D);
- int cols[]=dataBinding.getColumnsFromName(Name.getFieldName(dataItemName));
-
- if(!hasAllRows)
- {
- DI=createDataItemFromArray(null,(Object[][])Component.getPrintOut(cols));
- }
- else
- {
- DI=createDataItemFromArray(new MutableCollectionItem() ,(Object[][])Component.getPrintOut(cols,((BasicDataSource)Component.getOutput()).getCurrentRowNumber()));
- }
- setDataItemName(DI,dataItemName);
- DI.addDataItemChangedListener(this);
- DataInputVector.addElement(DI);
- DataInputCols.addElement(dataBinding.getColumnsFromName(dataItemName));
- ibe.setDataItem(DI);
- //System.out.println("created"+dataItemName);
- notifyEnabled(true);
- }
- }
-
-
- }
- public void setDataBinding(String name)
- {
- if(Component!=null)
- {
-
- dataBinding=new Name(name);
- if(DBus==null)
- {
- DataBus.joinDataBus(this);
- }
- try{
- for(int i=0;i<DataInputVector.size();i++)
- {
- DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_REVOKED,getDataItemName((DataItem)DataInputVector.elementAt(i)),this));
- ((DataItem)DataInputVector.elementAt(i)).removeDataItemChangedListener(this);
- }
- DataInputVector.removeAllElements();
- DataInputCols.removeAllElements();
- DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_AVAILABLE,dataBinding.getFullName(),this));
- commit();
- }
- catch(InvalidType e){}
- }
-
- }
- public String getDataBinding()
- {
- return dataBinding.getFullName();
- }
- protected void notifyEnabled(boolean value)
- {
- notifyOnDS=value;
- }
- public void commitUI(TriggerUIEvent trui)
- {
- if(trui.m_Type==TriggerUIEvent.FORCEREFRESH)
- {
- Mediator.notifyOnStatic=true;
- Mediator.notifySizeOnStatic=true;
- commit();
- Mediator.notifyOnStatic=false;
- Mediator.notifySizeOnStatic=false;
- }
- else commit();
-
- }
- protected void findDI()
- {
- if (java.beans.Beans.isDesignTime()) {
- return;
- }
-
- killActualInput();
- DataItem DI=DBus.findDataItem(dataBinding.getFullName(),this);
-
- if(DI!=null && DI instanceof DataItem){
- Input=DI;
- Input.addDataItemChangedListener(this);
- }
- }
-
- protected String getDataItemName(DataItem DI)
- {
- if(DI instanceof DataItemAddition)return ((DataItemAddition)DI).getName();
- else return null;
- }
- protected void setDataItemName(DataItem DI,String name)
- {
- if(DI instanceof DataItemAddition)((DataItemAddition)DI).setName(name);
- }
-
- public void killAll()
- {
-
- try{
- DBus.announceDataItem(new DataBusEvent(DataBusEvent.DATA_REVOKED,dataBinding.getTableName(),this));
- }catch(Exception e){System.out.println("could'nt do kill all");}
- DataInputVector.removeAllElements();
- suicide();
-
- }
-
- }