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 / JFCBinderModel.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  19.1 KB  |  629 lines

  1. package symantec.itools.db.beans.binding;
  2. import symantec.itools.db.beans.binding.databus.*;
  3. import java.util.Vector;
  4. import java.beans.PropertyVetoException;
  5. import java.beans.VetoableChangeSupport;
  6. import java.beans.VetoableChangeListener;
  7. import com.sun.java.swing.event.ListDataListener;
  8. import com.sun.java.swing.event.ListDataEvent;
  9. import com.sun.java.swing.event.TableModelListener;
  10. import com.sun.java.swing.event.TableModelEvent;
  11. import com.sun.java.swing.DefaultListSelectionModel;
  12. import com.sun.java.swing.event.DocumentListener;
  13. import com.sun.java.swing.event.DocumentEvent;
  14. import com.sun.java.swing.ButtonModel; 
  15. import com.sun.java.swing.DefaultButtonModel; 
  16. import com.sun.java.swing.ListSelectionModel;
  17. import com.sun.java.swing.event.ListSelectionListener;
  18. import com.sun.java.swing.text.PlainDocument;
  19. import com.sun.java.swing.text.Document;
  20. import com.sun.java.swing.text.BadLocationException;
  21. import com.sun.java.swing.text.SimpleAttributeSet;
  22. public class JFCBinderModel extends PlainDocument implements DataBusEventListener,DataBusMember,java.awt.event.ActionListener,DataItemChangedListener,com.sun.java.swing.ComboBoxModel,com.sun.java.swing.table.TableModel,DocumentListener,ListSelectionModel,ButtonModel,Document
  23. {
  24.     public JFCBinderModel()
  25.     {
  26.     }
  27.     protected DataBus dBus;
  28.     protected DataItem input;
  29.     protected DataItem lookupInput;
  30.     protected VetoableChangeSupport vetos=new VetoableChangeSupport(this);
  31.     protected Vector ListDataListeners =new Vector();
  32.     protected Vector TableModelListeners =new Vector();
  33.     protected Vector DocumentListeners=new Vector();
  34.     
  35.     protected String dataBinding;
  36.     protected String lookupBinding;
  37.     protected String[] ColumnNames;
  38.     protected int selected=-1;
  39.  
  40.     protected DefaultListSelectionModel DLSModel;
  41.     protected DefaultButtonModel DBModel;
  42.     protected String BooleanTrueString="true";
  43.     protected String BooleanFalseString="false";
  44.     protected boolean UseAString=false;
  45.     public void handleDataBusEvent(DataBusEvent e){}
  46.     public  void requestDataItem(DataBusEvent e){}
  47.     public void setDataBus(DataBus b)throws PropertyVetoException
  48.     {
  49.         vetos.fireVetoableChange("DataBus",dBus,b);
  50.         dBus=b;
  51.         dBus.addDataBusEventListener((DataBusEventListener)this);
  52.     }
  53.  
  54.     /**
  55.      *Returns the current DataBus
  56.      *@return DataBus. the current DataBus
  57.      */
  58.     public DataBus getDataBus()
  59.     {
  60.         return dBus;
  61.     }
  62.  
  63.      //These methods constitute the design pattern for a property named "DataBus". This property is "pushed"
  64.     // onto a new bus member when that new member is added to the bus. (See DataBus.joinDataBus()
  65.     // above.)
  66.  
  67.     public void addVetoableChangeListener(VetoableChangeListener listener)
  68.     {
  69.         vetos.addVetoableChangeListener(listener);
  70.     }
  71.  
  72.     public void removeVetoableChangeListener(VetoableChangeListener listener)
  73.     {
  74.          vetos.removeVetoableChangeListener(listener);
  75.     }
  76.     public void setDataBinding(String name)
  77.     {
  78.             dataBinding=name;
  79.             if(dBus==null)DataBus.joinDataBus(this);
  80.             input=findDI(name);
  81.             input.addDataItemChangedListener(this);
  82.            // this.addDocumentListener((DocumentListener)this);
  83.             dataBinding= ((DataItemAddition)input).getName();
  84.             setUpDocument();
  85.     }
  86.     public String getDataBinding()
  87.     {
  88.         return dataBinding;
  89.     }
  90.     public void setLookupBinding(String name)
  91.     {
  92.         if(DLSModel==null)DLSModel=new DefaultListSelectionModel() ;
  93.         lookupBinding=name;
  94.         if(dBus==null)DataBus.joinDataBus(this);
  95.         lookupInput=findDI(name);
  96.         lookupInput.addDataItemChangedListener(this);
  97.         lookupBinding= ((DataItemAddition)lookupInput).getName();
  98.        
  99.         
  100.     }
  101.     public String getLookupBinding()
  102.     {
  103.         return lookupBinding;
  104.     }
  105.     protected DataItem findDI(String binding)
  106.     {
  107.         if (java.beans.Beans.isDesignTime()) {
  108.             return null;
  109.         }
  110.         DataItem DI=dBus.findDataItem(binding,this);
  111.         if(DI!=null && DI instanceof DataItem){
  112.             return DI;
  113.         }
  114.         return null;
  115.     }
  116.     public void actionPerformed(java.awt.event.ActionEvent e)
  117.     {
  118.     }
  119.     
  120.     // LIST MODEL
  121.     
  122.    
  123.     public void addListDataListener(ListDataListener LDL){
  124.         if(input!=null)setUpDocument();
  125.         ListDataListeners.addElement(LDL);
  126.         }
  127.     public void removeListDataListener(ListDataListener LDL){
  128.         ListDataListeners.removeElement(LDL);
  129.         }
  130.  
  131.     public int getSize()
  132.     {
  133.         if(lookupInput instanceof DataInput)return 1;
  134.         return ((MutableCollectionItem)lookupInput).dataItems.size();
  135.     }
  136.  
  137.     public Object getElementAt(int i)
  138.     {
  139.         DataItem CDI=(DataItem)((MutableCollectionItem)lookupInput).dataItems.elementAt(i);
  140.         if(CDI instanceof DataInput)
  141.         {
  142.             return ((DataInput)CDI).getValueAsObject();
  143.         }
  144.         else return null;
  145.     }
  146.     
  147.     //COMBOBOX MODEL
  148.     Object def= null;
  149.     public Object getSelectedItem()
  150.     {
  151.         //System.out.println("getCalled");
  152.         if(selected==-1)return def;
  153.         return getElementAt(selected);
  154.     }
  155.     public void setSelectedItem(Object o)
  156.     {
  157.         //System.out.println("SDetting shit");
  158.         def=o;
  159.         selected=findSelectedItem(o);
  160.         if(input!=null && selected!=-1)
  161.         {
  162.              
  163.             notify=false;
  164.             ((MutableImmediateAccess)input).setValue(new NewValue(o));  
  165.             notify=true;
  166.         }
  167.             
  168.     }
  169.     protected int findSelectedItem(Object o)
  170.     {
  171.         if(o!=null)
  172.         {
  173.             for(int i=0;i<((MutableCollectionItem)lookupInput).dataItems.size();i++)
  174.             {
  175.                 if(o.toString().equals(getElementAt(i).toString()))
  176.                 {
  177.                     DLSModel.setSelectionInterval(i,i);
  178.                     //System.out.println("returning" +i+" "+o);
  179.                     return i;
  180.                 }
  181.             }
  182.         }
  183.        DLSModel.clearSelection();
  184.        return -1;
  185.     }
  186.     //LISTSELECTIONMODEL
  187.     
  188.  
  189.   public void addListSelectionListener(ListSelectionListener lsl){
  190.     if(DLSModel==null)DLSModel=new DefaultListSelectionModel() ;
  191.     if(input!=null)setUpDocument();
  192.     DLSModel.addListSelectionListener(lsl);
  193.   }
  194.   public void addSelectionInterval(int a, int b){
  195.     DLSModel.addSelectionInterval(a,b);
  196.   }
  197.   public void clearSelection(){
  198.     DLSModel.clearSelection();
  199.   }
  200.   public int getAnchorSelectionIndex(){
  201.     return DLSModel.getAnchorSelectionIndex();
  202.   }
  203.   public void  setAnchorSelectionIndex(int a){
  204.     DLSModel.setAnchorSelectionIndex(a);
  205.   }
  206.   public int getLeadSelectionIndex(){
  207.     return DLSModel.getLeadSelectionIndex();
  208.   }
  209.   public void setLeadSelectionIndex(int a){
  210.     DLSModel.setLeadSelectionIndex(a);
  211.   }
  212.   public int getMaxSelectionIndex(){
  213.     return DLSModel.getMaxSelectionIndex();
  214.   }
  215.   public int getMinSelectionIndex(){
  216.     return DLSModel.getMinSelectionIndex();
  217.   }
  218.   public boolean getValueIsAdjusting(){
  219.     return DLSModel.getValueIsAdjusting();
  220.   }
  221.   public void insertIndexInterval(int a, int b, boolean c){
  222.     DLSModel.insertIndexInterval(a,b,c);
  223.   }
  224.   public boolean isSelectedIndex(int index){
  225.     return DLSModel.isSelectedIndex(index);
  226.   }
  227.   public boolean isSelectionEmpty(){
  228.     return DLSModel.isSelectionEmpty();
  229.   }
  230.   public void removeIndexInterval(int a, int b){
  231.     DLSModel.removeIndexInterval(a,b);
  232.   }
  233.   public void removeListSelectionListener(ListSelectionListener lsl){
  234.     DLSModel.removeListSelectionListener(lsl);
  235.   }
  236.   public void removeSelectionInterval(int a, int b){
  237.     DLSModel.removeSelectionInterval(a,b);
  238.   }
  239.   public void setSelectionInterval(int a, int b){
  240.    
  241.     if(input!=null)
  242.     {
  243.         notify=false;
  244.         ((MutableImmediateAccess)input).setValue(new NewValue(getElementAt(a)));  
  245.         notify=true;
  246.     }
  247.     //setSelectedItem(getElementAt(a));
  248.     DLSModel.setSelectionInterval(a,a);
  249.   }
  250.   public void setValueIsAdjusting(boolean a){
  251.     DLSModel.setValueIsAdjusting(a);
  252.   }
  253.   public void setSelectionMode(int mode){
  254.     DLSModel.setSelectionMode(mode);
  255.   } 
  256.   public int getSelectionMode(){
  257.     return DLSModel.getSelectionMode();
  258.   }
  259.  
  260.   //BUTTON MODEL
  261.   
  262.   public void addActionListener(java.awt.event.ActionListener al){
  263.     DBModel.addActionListener(al);
  264.   }
  265.   public void addChangeListener(com.sun.java.swing.event.ChangeListener cl){
  266.     if(DBModel==null)DBModel=new DefaultButtonModel() ;
  267.     DBModel.addChangeListener(cl);
  268.   }
  269.   public void addItemListener(java.awt.event.ItemListener il){
  270.     DBModel.addItemListener(il);
  271.   }
  272.   public String getActionCommand(){
  273.     return DBModel.getActionCommand();
  274.   }
  275.  /* public int getKeyAccelerator(){
  276.     return DBModel.getKeyAccelerator();
  277.   }*/
  278.   public boolean isArmed(){
  279.     return DBModel.isArmed();
  280.   }
  281.   public boolean isEnabled(){
  282.     return DBModel.isEnabled();
  283.   }
  284.   public boolean isPressed(){
  285.     return DBModel.isPressed();
  286.   }
  287.   public boolean isRollover(){
  288.     return DBModel.isRollover();
  289.   }
  290.   public boolean isSelected(){
  291.     return DBModel.isSelected();
  292.   }
  293.   public void removeActionListener(java.awt.event.ActionListener al){
  294.     DBModel.removeActionListener(al);
  295.   }
  296.   public void removeChangeListener(com.sun.java.swing.event.ChangeListener cl){
  297.     DBModel.removeChangeListener(cl);
  298.   }
  299.   public void removeItemListener(java.awt.event.ItemListener il){
  300.     DBModel.removeItemListener(il);
  301.   }
  302.   public void setActionCommand(String a){
  303.     DBModel.setActionCommand(a);
  304.   }
  305.   public void setArmed(boolean a){
  306.     DBModel.setArmed(a);
  307.   }
  308.   public void setEnabled(boolean a){
  309.     DBModel.setEnabled(a);
  310.   }
  311.   public void setGroup(com.sun.java.swing.ButtonGroup a){
  312.     DBModel.setGroup(a);
  313.   }
  314.  /* public void setKeyAccelerator(int a){
  315.     DBModel.setKeyAccelerator(a);
  316.   }*/
  317.   public void setPressed(boolean a){
  318.     DBModel.setPressed(a);
  319.    if(a) setSelected(!DBModel.isSelected());
  320.   }
  321.   public void setRollover(boolean a){
  322.     DBModel.setRollover(a);
  323.   }
  324.   public void setSelected(boolean a){
  325.      DBModel.setSelected(a);
  326.     notify=false;
  327.     if(UseAString)
  328.     {
  329.          if(a) ((MutableImmediateAccess)input).setValue(new NewValue(BooleanTrueString));
  330.          else if(!a) ((MutableImmediateAccess)input).setValue(new NewValue(BooleanFalseString));
  331.     }
  332.     else if(!UseAString) 
  333.     {
  334.         ((MutableImmediateAccess)input).setValue(new NewValue(new Boolean(a)));  
  335.     }
  336.     notify=true;
  337.   }
  338.   public Object[] getSelectedObjects(){
  339.     return DBModel.getSelectedObjects();
  340.   }
  341.   public void setMnemonic(int a){
  342.     DBModel.setMnemonic(a);
  343.   }
  344.   public int getMnemonic(){
  345.     return DBModel.getMnemonic();
  346.   }
  347.   
  348.   public void setBooleanTrueString(String s){
  349.     UseAString=true;
  350.     BooleanTrueString=s;
  351.   }
  352.   public String getBooleanTrueString(){
  353.     return BooleanTrueString;
  354.   }
  355.   public String getBooleanFalseString(){
  356.     return BooleanFalseString;
  357.   }
  358.    public void setBooleanFalseString(String s){
  359.     UseAString=true;
  360.     BooleanFalseString=s;
  361.   }
  362.   
  363.   
  364.     //TABLE MODEL
  365.     public void addTableModelListener(TableModelListener TML){
  366.         //System.out.println("added");
  367.         TableModelListeners.addElement(TML);
  368.         setUp();
  369.     }
  370.     public void removeTableModelListener(TableModelListener TML){
  371.         TableModelListeners.removeElement(TML);
  372.     }
  373.     public int getColumnCount()
  374.     {
  375.             return symantec.itools.db.beans.binding.Name.getNumberOfCols(lookupBinding);
  376.     }
  377.     public String getColumnName(int i)
  378.     {
  379.         return ColumnNames[i];
  380.     }
  381.     public int getRowCount()
  382.     {
  383.         return ((MutableCollectionItem)lookupInput).dataItems.size();
  384.     }
  385.     public boolean  isCellEditable(int i, int j)
  386.     {
  387.         return true;
  388.     }
  389.     public Class getColumnClass(int columnIndex)
  390.     {
  391.         try
  392.         {
  393.             return java.lang.Class.forName("java.lang.Object");
  394.         }
  395.         catch(Exception e){System.out.println("error");}
  396.         return null;
  397.     }
  398.     public Object getValueAt(int i, int j)
  399.     {
  400.         if(i<((MutableCollectionItem)lookupInput).dataItems.size())
  401.         {
  402.             DataItem CDI=(DataItem)((MutableCollectionItem)lookupInput).dataItems.elementAt(i);
  403.             if(CDI instanceof DataInput && j==0)
  404.             {
  405.                 return ((DataInput)CDI).getValueAsObject();
  406.             }
  407.             else if(CDI instanceof MutableCollectionAccess)
  408.             {
  409.                  CDI=(DataItem)(((MutableCollectionItem)lookupInput).dataItems).elementAt(i);
  410.                  DataItem CCDI=(DataItem)((MutableCollectionItem)CDI).dataItems.elementAt(j);
  411.                  return ((ImmediateAccess)CCDI).getValueAsObject();
  412.             }
  413.         }
  414.  
  415.         return null;
  416.     }
  417.  
  418.     public void setValueAt(Object o,int i, int j)
  419.     {
  420.         if(i<((MutableCollectionItem)lookupInput).dataItems.size())
  421.         {
  422.             DataItem CDI=(DataItem)((MutableCollectionItem)lookupInput).dataItems.elementAt(i);
  423.             if(CDI instanceof DataInput && j==0)
  424.             {
  425.                  ((MutableImmediateAccess)CDI).setValue(new NewValue(o));
  426.             }
  427.             else if(CDI instanceof MutableCollectionAccess)
  428.             {
  429.                  CDI=(DataItem)(((MutableCollectionItem)lookupInput).dataItems).elementAt(i);
  430.                  DataItem CCDI=(DataItem)((MutableCollectionItem)CDI).dataItems.elementAt(j);
  431.                  ((MutableImmediateAccess)CCDI).setValue(new NewValue(o));
  432.             }
  433.         }
  434.     }
  435.     public Object getColumnIdentifier(int i)
  436.     {
  437.         return ColumnNames[i];
  438.     }
  439.     public int getColumnIndex(Object o)
  440.     {
  441.          for(int i=0;i<ColumnNames.length;i++)
  442.          {
  443.             if(o.equals(ColumnNames[i])){
  444.                 return i;
  445.             }
  446.          }
  447.         return 0;
  448.     }
  449.     public void setUp()
  450.     {
  451.          
  452.         int cols=getColumnCount();
  453.         ColumnNames=new String[cols];
  454.         String Fields=symantec.itools.db.beans.binding.Name.getFieldName(lookupBinding)+",";
  455.  
  456.         for(int i=0;i<cols;i++)
  457.         {
  458.             ColumnNames[i]=Fields.substring(0,Fields.indexOf(','));
  459.             Fields=Fields.substring(Fields.indexOf(',')+1,Fields.length());
  460.         }
  461.       
  462.     }
  463.     //DOCUMENT
  464.     protected boolean notify=true;
  465.     protected boolean documentSet=false;
  466.     public String getText(int a,int b)throws BadLocationException
  467.     {
  468.         if(!documentSet)
  469.         {
  470.             setUpDocument();
  471.             this.addDocumentListener(this);
  472.             documentSet=true;
  473.         }
  474.         return super.getText(a,b);
  475.     }
  476.    
  477.     protected void setUpDocument()
  478.     { 
  479.         if(notify)
  480.         {
  481.             Object value=((ImmediateAccess)input).getValueAsObject();
  482.            
  483.             if(value instanceof Boolean)
  484.             {
  485.                 DBModel.setSelected(((Boolean)value).booleanValue());
  486.             }
  487.             else
  488.             {
  489.                 if(DBModel!=null)
  490.                 {
  491.                 UseAString=true;
  492.                 DBModel.setSelected(value.equals(BooleanTrueString));
  493.                 }
  494.                 try
  495.                 {
  496.                     notify=false;
  497.                     remove(0,getLength());
  498.                     insertString(0,value.toString(),new SimpleAttributeSet());
  499.                     if(ListDataListeners.size()!=0)
  500.                     {
  501.                         selected=findSelectedItem(value);
  502.                     }
  503.                     notify=true;
  504.                     
  505.                 }
  506.                 catch(BadLocationException e){}
  507.             }
  508.           
  509.         }
  510.     }
  511.     
  512.     public  void insertUpdate(DocumentEvent e)
  513.     {
  514.         if(notify)notifyInput();
  515.     }
  516.     public void removeUpdate(DocumentEvent e)
  517.     {
  518.         if(notify)notifyInput();
  519.     }
  520.     public  void changedUpdate(DocumentEvent e){}
  521.     
  522.     protected void notifyInput()
  523.     {
  524.         try{
  525.             notify=false; 
  526.             ((MutableImmediateAccess)input).setValue(new NewValue(getText(0,getLength())));
  527.             notify=true;
  528.         }
  529.         catch(BadLocationException ex){} 
  530.     }
  531.     public void addDocumentListener(DocumentListener dl)
  532.     {
  533.         DocumentListeners.addElement(dl);
  534.         super.addDocumentListener(dl);
  535.     }
  536.     public void removeDocumentListener(DocumentListener dl)
  537.     {
  538.         DocumentListeners.removeElement(dl);
  539.         super.removeDocumentListener(dl);
  540.     }
  541.     //EVENT HANDELING FOR DATAITEMCHANGED
  542.     public void notifyDataItemChanged(DataItemChangedEvent event)
  543.     {
  544.     Vector l;
  545.  
  546.     if(event.getSource()==input)
  547.     {
  548.         if(event.getChangeType()==event.DATA_VALUE_CHANGE)
  549.         {
  550.                  setUpDocument();
  551.         }
  552.         if(lookupInput!=null)
  553.         {
  554.             if(ListDataListeners.size()!=0)
  555.         {
  556.             try
  557.             {
  558.                 ListDataEvent lde=new ListDataEvent(this,ListDataEvent.CONTENTS_CHANGED,0,0);
  559.                 synchronized(this)
  560.                 {
  561.                     l=(Vector)ListDataListeners.clone();
  562.                 }
  563.                 for(int i=0;i<l.size();i++)
  564.                 {
  565.                     ((ListDataListener)l.elementAt(i)). contentsChanged(lde);
  566.                 }
  567.             }
  568.             catch(Exception e){}
  569.         }
  570.         }
  571.     }
  572.     else
  573.     {
  574.         if(ListDataListeners.size()!=0)
  575.         {
  576.             try
  577.             {
  578.                 ListDataEvent lde=new ListDataEvent(this,ListDataEvent.CONTENTS_CHANGED,0,0);
  579.                 synchronized(this)
  580.                 {
  581.                     l=(Vector)ListDataListeners.clone();
  582.                 }
  583.                 for(int i=0;i<l.size();i++)
  584.                 {
  585.                     ((ListDataListener)l.elementAt(i)). contentsChanged(lde);
  586.                 }
  587.             }
  588.             catch(Exception e){}
  589.         }
  590.         if(TableModelListeners.size()!=0)
  591.         {
  592.             try
  593.             {
  594.                 TableModelEvent tme=new TableModelEvent(this);//,1,getRowCount() );
  595.                 synchronized(this)
  596.                 {
  597.                     l=(Vector)TableModelListeners.clone();
  598.                 }
  599.                 for(int i=0;i<l.size();i++)
  600.                 {
  601.                     ((TableModelListener)l.elementAt(i)).tableChanged(tme); 
  602.                 }
  603.             }
  604.             catch(Exception e){}
  605.         }
  606.         if(DocumentListeners.size()!=0)
  607.         {
  608.             try
  609.             {
  610.                 
  611.                 DefaultDocumentEvent tme=new  DefaultDocumentEvent(1, getLength(),DocumentEvent. EventType.CHANGE);
  612.                 synchronized(this)
  613.                 {
  614.                     l=(Vector)DocumentListeners.clone();
  615.                 }
  616.                 for(int i=0;i<l.size();i++)
  617.                 {
  618.                     ((DocumentListener)l.elementAt(i)).changedUpdate(tme); 
  619.                 }
  620.             }
  621.             catch(Exception e){
  622.             System.out.println("doc error");}
  623.         }
  624.     }
  625.     }
  626.     
  627.     
  628. }
  629.