home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 23.4 KB | 768 lines |
- /*
- * @(#ComboBox.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
-
- package symantec.itools.db.awt;
-
- import java.util.Vector;
- import java.util.Enumeration;
- import java.lang.*;
- import java.awt.event.*;
- import java.sql.SQLException;
- import symantec.itools.db.pro.*;
- import symantec.itools.db.beans.binding.Name;
- import symantec.itools.db.beans.binding.Mediator;
-
- /**
- * A dbAWARE ComboBox.
- * <p>
- * This component can be "bound" to a projection within a relation view
- * so that it automatically gets and sets the values in that relation.
- * <p>
- */
- public class ComboBox extends symantec.itools.awt.ComboBox implements ListLink, ProjectionBean
- {
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public final static int Default = 0;
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public final static int Null = 1;
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public final static int Blank = 2;
-
- /**
- * Constant value for the Triggering Event property.
- * Indicates the triggering event is ACTION_PERFORMED.
- */
- public static final int Action_Event=0;
- /**
- * Constant value for the Triggering Event property.
- * Indicates the triggering event is FOCUS_LOST.
- */
- public static final int Focus_Event=1;
- /**
- * Constant value for the Triggering Event property.
- * Indicates the triggering event is KEY_PRESSED.
- */
- public static final int Key_Event=2;
- /**
- * The Binder object which this component can use to get data from
- * the server, or change data at the database server.
- */
- /**
- * The value of the Triggering Event property.
- * The type of event that triggers this component to commit its changes.
- * @see #getTriggeringEvent
- * @see #setTriggeringEvent
- * @see #Focus_Event
- * @see #Key_Event
- * @see #Action_Event
- */
- protected int TriggeringEvent=symantec.itools.db.awt.ProjectionBeanHelper.Focus_Event;
- /**
- * Controls when the component commits its changes.
- * If <code>true</code>, this component sets the triggering event to
- * Key_Event, otherwise the triggering event is set to Focus_Event.
- * @see #getDynamicUpdate
- * @see #setDynamicUpdate
- * @see #Focus_Event
- * @see #Key_Event
- */
- protected boolean m_DynamicUpdate=false;
-
- private ListBinder m_ListBinder;
-
- private boolean m_IsBound = false;
- private boolean m_autoExpand = true;
- private boolean m_dynamicPopulate = false;
-
- private RelationView m_LookUpRelationView;
- private Vector m_JoinColumns;
- private ConnectionInfo m_Conn;
- private String m_SQL;
-
- private Vector staticItems = new Vector();
- private Vector allItems = new Vector();
-
- private ProjectionBeanHelper m_Helper;
- private String IName;
-
- /**
- * Constructs a new default ComboBox. The ComboBox is not editable and
- * not searchable by default.
- */
- public ComboBox()
- {
- this(false, false);
- }
-
- /**
- * Constructs a new editable and/or searchable ComboBox.
- * @param editable true = editable, false = non-editable
- * @param searchable true = searchable, false = non-searchable
- */
- public ComboBox(boolean editable, boolean searchable)
- {
- super(editable, searchable);
- m_JoinColumns = new Vector();
- m_SQL = new String();
- m_Helper = new ProjectionBeanHelper(this);
- editBox.addFocusListener(m_Helper);
- }
-
- /**
- * Constructs a new ComboBox linked to a database. The ComboBox is not editable and
- * not searchable by default.
- *
- * @param relView the relation view to bind with
- * @param conn the information needed to connect to the database
- */
- public ComboBox(RelationView lookupRV, ConnectionInfo conn)
- {
- this();
- m_LookUpRelationView = lookupRV;
- m_Conn = conn;
- }
-
- /**
- * Called when this component has been bound to a RelationView.
- * This component notes the ListBinder object so that it
- * may notify the dbANYWHERE server of data changes made.
- * <p>
- * This is a method in the ListLink interface.
- *
- * @param lBinder used to notify the dbANYWHERE server of any data changes
- * that are to be persistant.
- *
- * @see symantec.itools.db.pro.ListLink#init
- */
- public void init (ListBinder lBinder)
- {
- m_ListBinder = lBinder;
- }
-
- /**
- * This method is called to update the value of this dbAWARE component.
- * It is called when the dbANYWHERE server has detected a data change made
- * within the RelationView bound to this component.
- * The ListBinder object passed into the notification is used to get the
- * new data value.
- * <p>
- * This is a method in the ListLink interface.
- *
- * @param binder used to get the current value from the dbANYWHERE server.
- *
- * @see symantec.itools.db.pro.ListLink#notifyListChange
- */
- public void notifyListChange(ListBinder lBinder)
- {
- if (m_dynamicPopulate)
- {
- try
- {
- clear();
- allItems.removeAllElements();
-
- RelationView curRV = lBinder.getRelationView();
- while ( curRV.next() == true )
- {
- super.addItem( curRV.getStringValue(1) );
- allItems.addElement(curRV.getStringValue(1));
- }
- String ne;
- Enumeration e = staticItems.elements();
- while(e.hasMoreElements())
- {
- ne = (String)e.nextElement();
- super.addItem(ne);
- allItems.addElement(ne);
- }
- m_Helper.notifyDataChange();
- }
- catch (Exception Ex) {
- m_Helper.raiseException(Ex);
- }
- }
- }
-
- private boolean searchList(String item)
- {
- boolean found = false;
- String[] e=list.getListItems();
- int size=e.length;
- for(int i=0; i<size;i++)
- {
- if (e[i].equals(item))
- {
- found = true;
- break;
- }
- }
- return found;
-
- }
-
- /**
- * Adds a column to the list of columns that this component will be bound
- * to. Use this method before calling bindList().
- *
- * @param columnName the name of the column to join
- * @exception SQLException
- * if a SQL database access error occurred
- * @see #bindList
- */
- public void join(String columnName) throws SQLException
- {
- int colID = m_LookUpRelationView.findProjByName(columnName);
- if (m_JoinColumns == null)
- {
- m_JoinColumns = new Vector();
- }
- m_JoinColumns.addElement(new Integer(colID));
- }
-
- /**
- * Sets the sql string to use while generating the list for this component
- * to bind to. Use this method before calling bindList().
- *
- * @param sql the sql string
- * @exception SQLException
- * if a SQL database access error occurred
- *
- * @see #bindList
- */
- public void setSQL(String sql) throws SQLException
- {
- m_SQL = sql;
- }
-
- /**
- * Binds this component to a list of columns within a RelationView.
- * The RelationView and connection information to use is specified at
- * construction time. The columns to use are specified using the join()
- * method.
- *
- * @see #join
- * @exception SQLException
- * if a SQL database access error occurred
- */
- public void bindList() throws SQLException
- {
- m_dynamicPopulate = true;
- m_LookUpRelationView.bindList(m_Conn, m_SQL, this, m_JoinColumns);
- }
-
- /**
- * Adds the specified item to the end of drop-down list and enable the item.
- * @param item the item to be added
- * @see #setListItems(java.lang.String[])
- */
- public synchronized void addItem(String item) {
- try {
- super.addItem(item);
- }
- catch (java.beans.PropertyVetoException ex) {
- }
- staticItems.addElement(item);
- if (m_dynamicPopulate) {
- allItems.addElement(item);
- }
- }
-
- /**
- * Sets whether to automatically add items onto the end of this list when
- * they are added to the database.
- *
- * @param ae true to auto-expand, false otherwise
- * @see #getAutoExpand
- */
- public void setAutoExpand(boolean ae)
- {
- m_autoExpand = ae;
- if (m_autoExpand) {
- m_Helper.notifyDataChange(); // In case we're just starting up
- // and we've been bound; but, we
- // weren't autoExpanded so we
- // lost one
- }
- }
-
- /**
- * Gets whether to automatically add items onto the end of this list when
- * they are added to the database.
- *
- * @return true if auto-expands, false otherwise
- * @see #setAutoExpand
- */
- public boolean getAutoExpand() {
- return m_autoExpand;
- }
-
- /**
- * Adds the string array to the list.
- * @param items items to add to the list
- * @see #addItem(java.lang.String)
- * @exception java.beans.PropertyVetoException if the specified property value is unacceptable
- */
- public void setListItems(String[] listItems) throws java.beans.PropertyVetoException
- {
- String[] currentItems = getListItems();
- if (currentItems.length == 0)
- {
- clear();
- }
- for (int i = 0; i < listItems.length; ++i)
- {
- staticItems.addElement(listItems[i]);
- super.addItem(listItems[i]);
- }
- if(IName!=null)m_Helper.setDataBinding(new Name(IName));
-
- }
-
- /**
- * Binds this component to a given projection within the specified
- * relation view.
- *
- * @param relView the relation view to bind with
- * @param projection the projection in relView to bind with
- */
- public void setBinding(RelationView relView, String projection)
- {
- m_Helper.setBinding(relView, projection);
- }
-
- /**
- * Returns the projection in the RelationView that this component is bound with.
- * @see #setProjection
- * @see #setBinding
- */
- public String getProjection() {
- return m_Helper.getProjection();
- }
-
- /**
- * Sets the projection in the RelationView that this component is bound with.
- * @param projection the name of the projection
- * @see #getProjection
- * @see #setBinding
- */
- public void setProjection(String projection) {
- m_Helper.setProjection(projection);
- m_IsBound = true;
- }
-
- /**
- * Gets the RelationView that this component is bound with.
- * @return the RelationView
- * @see #setRelationView
- * @see #setBinding
- */
- public RelationView getRelationView() {
- return m_Helper.getRelationView();
- }
-
- /**
- * Sets the RelationView that this component is bound with.
- * @param rv the new RelationView to bind with
- * @see #getRelationView
- * @see #setBinding
- */
- public void setRelationView(RelationView rv) {
- m_Helper.setRelationView(rv);
- m_IsBound = true;
- }
-
- /**
- * Specifies how an empty string will be set when updating data on
- * the dbANYWHERE server.
- *
- * @param blank one of "DEFAULT", "NULL", or "BLANK"
- *
- * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
- */
-
- public void setTreatBlankAs(int value) {
- m_Helper.setTreatBlankAs(value);
- }
-
- /**
- * Indicates when the component commits its changes.
- * If <code>true</code>, this component has set the triggering event to
- * Key_Event, otherwise the triggering event is set to Focus_Event.
- * @see #setDynamicUpdate
- * @see #Focus_Event
- * @see #Key_Event
- */
- public boolean getDynamicUpdate() {
- return m_DynamicUpdate;
- }
-
- /**
- * Sets when the component commits its changes.
- * If <code>true</code>, this component sets the triggering event to
- * Key_Event, otherwise the triggering event is set to Focus_Event.
- * @param value the new dynamic update mode value
- * @see #getDynamicUpdate
- * @see #Focus_Event
- * @see #Key_Event
- */
- public void setDynamicUpdate(boolean value) {
-
- if(value==true){
- this.setTriggeringEvent(symantec.itools.db.awt.ProjectionBeanHelper.Key_Event);
- }
- if(value==false){
- this.setTriggeringEvent(symantec.itools.db.awt.ProjectionBeanHelper.Focus_Event);
- }
- m_DynamicUpdate = value;
- }
-
- /**
- * Sets whether the data value of this component may be modified.
- * @param value <code>true</code> if the value may not be modified,
- * <code>false</code>if the value may be modified
- */
- public void setReadOnly(boolean value) {
- try {
- setEditable(!value);
- }
- catch (Exception ex) {
- m_Helper.raiseException(ex);
- }
- }
-
- /**
- * Sets the value of this component to the given value.
- * @param value the new component value
- * @see #getData
- */
- String text;
- public void setData(Object value) {
- text = value.toString();
- try {
- list.deselectAll();
- if (((staticItems.size() > 0) || (m_IsBound && allItems.size() > 0)) && searchList(text)) {
- super.select(text);
- }
-
- else if (m_autoExpand && text!="")
- {
- if(!uniqueElements){
- super.addItem(text);
- }
- staticItems.addElement(text);
- }
-
- if(searchList(text)){
- super.select(text);
- }
-
- editBox.setText(text);
- }
-
- catch (java.beans.PropertyVetoException ex) {
- }
- }
-
-
- /**
- * Gets the value of this component.
- * @return the current component value
- * @see #setData
- */
- public Object getData() {
- if(!isSearchable()){
- text = getText();
- return text;
- }
- else {
- text=getSelectedItem();
- editBox.setText(text);
- return(text);
- }
-
- }
-
- /**
- * Gets whether this component saves its value as a text String.
- * @return <code>true</code> if the value is saved as text,
- * <code>false</code> otherwise
- */
- public boolean isTextBased() {
- return true;
- }
-
- /**
- * Gets the number of digits to the right of the decimal point for
- * this component's value.
- * @return the number of digits to the right of the decimal point
- */
- public int getScale() {
- return ProjBinder.DEFAULTSCALE;
- }
-
- /**
- * Registers the standard event listener(s) for this component.
- */
- public void registerListeners() {
- editBox.addKeyListener(m_Helper);
- // editBox.addTextListener(m_Helper);
- addActionListener(m_Helper);
- }
- /**
- * Sets the name of the data item to bind this component to.
- * @param name the data item name, like "MyTable@MyColumn"
- * @see #getDataBinding
- */
- public void setDataBinding(String name)
- {
- IName=name;
- m_Helper.setDataBinding(new Name(IName));
- }
-
- /**
- * Gets the name of the data item this component is bound to.
- * @returns the data item name, like "MyTable@MyColumn"
- * @see #setDataBinding
- */
- public String getDataBinding()
- {
- return m_Helper.getDataBinding().getFullName();
- }
- void removeAllListeners()
- {
- editBox.removeFocusListener(m_Helper);
- editBox.removeActionListener(m_Helper);
- editBox.removeKeyListener(m_Helper);
- }
- /**
- * Sets the value of the Triggering Event property.
- * This determines the type of event that triggers the component to commit
- * its changes to the database.
- * @param event one of Focus_Event, Action_Event, or Key_Event
- * @see #getTriggeringEvent
- * @see #Focus_Event
- * @see #Action_Event
- * @see #Key_Event
- */
- public void setTriggeringEvent(int Event)
- {
- TriggeringEvent=Event;
- this.removeAllListeners();
- switch(Event){
- case symantec.itools.db.awt.ProjectionBeanHelper.Focus_Event:
- editBox.addFocusListener(m_Helper);
- break;
- case symantec.itools.db.awt.ProjectionBeanHelper.Action_Event:
- editBox.addActionListener(m_Helper);
- break;
- case symantec.itools.db.awt.ProjectionBeanHelper.Key_Event:
- editBox.addKeyListener(m_Helper);
- break;
- default:
- editBox.addFocusListener(m_Helper);
- }
- }
- /**
- * Gets the current value of the Triggering Event property.
- * This determines the type of event that triggers the component to commit
- * its changes to the database.
- * @return one of Focus_Event, Action_Event, or Key_Event
- * @see #setTriggeringEvent
- * @see #Focus_Event
- * @see #Action_Event
- * @see #Key_Event
- */
- public int getTriggeringEvent()
- {
- return TriggeringEvent;
- }
- protected void sourceActionEvent(String command)
- {
- super.sourceActionEvent( command);
- m_Helper.m_Mediator.commit();
- }
-
- Mediator m_LookupMediator;
- Name lookupName=new Name();
- private String[] m_GetMethods={"getDataAt(row)"};
- private String[] m_SetMethods={"setDataAtIf(row,Value)"};
- private String[] m_SetMethodsII={"setDataAt(row,Value)"};
- private boolean isLookup=false;
-
- /**
- * Sets the value of the Lookup Name property.
- * This property is the name of a data source table and column.
- * It is used to fill this component with data.
- * @param lame the new lookup name
- * @see #getLookupName
- */
- boolean uniqueElements=false;
- public void setUniqueElements(boolean a)
- {
- uniqueElements=a;
- }
- public boolean getUniqueElements()
- {
- return uniqueElements;
- }
- public void setLookupName(String lname)
- {
- Name name = new Name(lname);
- isLookup=true;
-
- boolean ae=getAutoExpand();
- if(m_LookupMediator==null){
- m_LookupMediator =new symantec.itools.db.beans.binding.Mediator() ;
- m_LookupMediator.setOutput(this);
- if(uniqueElements)m_LookupMediator.setSetMethods(m_SetMethods);
- else
- {
- setAutoExpand(true);
- m_LookupMediator.setSetMethods(m_SetMethodsII);
- }
-
- }
- if(name.getNumberOfRows()==1)name.setNumberOfRows(-1);
- lookupName=name;
- m_LookupMediator.setDataBinding(name.getFullName());
- setAutoExpand(ae);
- arrow.setEnabled(true);
- if(IName!=null)m_Helper.setDataBinding(new Name(IName));
-
-
- }
- /**
- * Gets the value of the Lookup Name property.
- * This property is the name of a data source table and column.
- * It is used to fill this component with data.
- * @return the property value
- * @see #setLookupName
- */
- public String getLookupName()
- {
- return lookupName.getFullName();
- }
-
- /**
- * Sets the value stored at a particular row.
- * @param row the zero-relative row index
- * @param value the new value
- * @see #getDataAt
- */
- int currentItems=0;
- Object oldValue;
- String Value;
- public void setDataAt(int row,Object value)
- {
- int totalItems=countItems();
- int newNumberRows=m_LookupMediator.getOutputSize().height;
- try{
- if(currentItems>newNumberRows)
- {
- for(int r=newNumberRows;r<currentItems;r++)
- {
- list.delItem(0);
- }
- currentItems=newNumberRows;
- totalItems=newNumberRows;
- }
- if(row >=totalItems)
- {
- for(int i=totalItems;i<=row ;i++)
- {
- list.addItem(" ");
- }
- }
- if(value==null)value="";
- Value=value.toString();
- if(!getItem(row).equals(Value))list.setText(row, Value);
- currentItems=newNumberRows;
-
- oldValue=text;
- if(oldValue!=null)
- {
- if( oldValue.equals(Value) && !Value.equals(""))
- {
- super.select(Value);
- // editBox.setText(text);
- }
-
- }
- }catch(java.beans.PropertyVetoException e){}
- }
- /**
- * Sets the value stored at a particular row if it is
- * not equal to the current value.
- * @param row the zero-relative row index
- * @param value the new value
- * @see #getDataAt
- */
-
- public void setDataAtIf(int row,Object value)
- {
- int totalItems=countItems();
-
- if(value!=null && value!="")
- {
- for(int i=0;i<totalItems ;i++)
- {
- if(getItem(i).toString().equals(value.toString()))return;
- }
- addItem(value.toString());
-
- }
- // currentItems=newNumberRows;
- }
- /**
- * Gets the value stored at a particular row.
- * @param row the zero-relative row index
- * @return the value
- * @see #setDataAt
- */
- public Object getDataAt(int row)
- {
- int totalItems=countItems();
- if(row<totalItems)return (Object)list.getItem(row);
- return "";
- }
-
- /**
- * Sets the value of the Empty Means Null property.
- * This determines how an empty string will be saved when updating data on
- * the database server.
- * A value of <code>true</code>indicates an empty string will be saved as a
- * SQL NULL value. Otherwise it will be saved as a blank value.
- * @param propertyValue <code>true</code> for NULL, <code>false</code> for BLANK
- * @see #getEmptyMeansNull
- */
- public void setEmptyMeansNull(boolean propertyValue)
- {
- m_Helper.setEmptyMeansNull(propertyValue);
- }
-
- /**
- * Gets the value of the Empty Means Null property.
- * This determines how an empty string will be saved when updating data on
- * the database server.
- * A value of <code>true</code>indicates an empty string will be saved as a
- * SQL NULL value. Otherwise it will be saved as a blank value.
- * @return <code>true</code> for NULL, <code>false</code> for BLANK
- * @see #setEmptyMeansNull
- */
- public boolean getEmptyMeansNull()
- {
- return m_Helper.getEmptyMeansNull();
- }
- }
-