home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 14.1 KB | 481 lines |
- /*
- * @(#List.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
-
- package symantec.itools.db.awt;
-
- import java.util.*;
- import java.sql.*;
- import java.lang.*;
- import symantec.itools.db.net.*;
- import symantec.itools.db.pro.*;
- import java.awt.event.*;
-
- /**
- * A dbAWARE List component.
- * <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 List extends java.awt.List implements ListLink, ProjectionBean
- {
-
- /**
- * The Binder object which this component can use to get data from
- * the server, or change data at the database server.
- */
- private ListBinder m_ListBinder;
-
- private boolean m_useStream = false;
-
- private RelationView m_LookUpRelationView;
- private Vector m_JoinColumns;
- private Vector m_JoinColumnNames;
- private ConnectionInfo m_Conn;
- private String m_SQL;
-
- private ProjectionBeanHelper m_Helper;
-
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public static final String Default = ProjectionBeanHelper.DEFAULT;
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public static final String Blank = ProjectionBeanHelper.BLANK;
- /**
- * A constant value indicating how an empty string will be saved in the database.
- */
- public static final String Null = ProjectionBeanHelper.NULL;
-
- /**
- * Constructs a new List linked to a database.
- *
- * @param lookupRV the relation view to bind with
- * @param conn the information needed to connect to the database
- */
- public List(RelationView lookupRV, ConnectionInfo conn)
- {
- this();
- m_LookUpRelationView = lookupRV;
- m_Conn = conn;
- m_Helper = new ProjectionBeanHelper(this);
- addFocusListener(m_Helper);
- }
-
- /**
- * Constructs a new List with no data connectivity, initially.
- *
- * @param rows defines the number of rows of items to display.
- */
- public List(int rows)
- {
- super(rows, false);
- m_JoinColumns = new Vector();
- m_JoinColumnNames = new Vector();
- m_SQL = new String();
- m_Helper = new ProjectionBeanHelper(this);
- }
-
- /**
- * Constructs a default List with no data connectivity, initially.
- */
- public List()
- {
- this(0);
- }
-
- /**
- * 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)
- {
- try
- {
- RelationView curRV = lBinder.getRelationView();
- clear();
- while ( curRV.next() == true )
- {
- if (!(curRV.isNull(1)))
- {
- super.addItem( curRV.getString(1));
- }
- }
- Enumeration e = staticItems.elements();
- while(e.hasMoreElements())
- {
- super.addItem((String)e.nextElement());
- }
- }
- catch (SQLException Ex)
- {
- m_Helper.raiseException (Ex);
- }
- }
-
- /**
- * 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
- {
- if (!java.beans.Beans.isDesignTime()) {
- int colID = m_LookUpRelationView.findProjByName(columnName);
- if (m_JoinColumns == null)
- {
- m_JoinColumns = new Vector();
- }
- m_JoinColumns.addElement(new Integer(colID));
- }
- if (m_JoinColumnNames == null)
- {
- m_JoinColumnNames = new Vector();
- }
- m_JoinColumnNames.addElement(new String(columnName));
- }
-
- /**
- * 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
- * @see #getSelectClause
- */
- 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_LookUpRelationView.bindList(m_Conn, m_SQL, this, m_JoinColumns);
- m_Helper.notifyDataChange();
- }
-
-
- /**
- * Adds the specified item to the end of the list.
- * @param item the item to be added
- */
- public synchronized void addItem(String item) {
- super.addItem(item, -1);
- staticItems.addElement(item);
- }
-
- private Vector staticItems = new Vector();
-
- /**
- * 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 #setBinding
- * @see #setProjection
- */
- public String getProjection() {
- return m_Helper.getProjection();
- }
-
- /**
- * Binds this component to the given projection within the RelationView
- * the component is currently bound with.
- * @see #setBinding
- * @see #getProjection
- * @see #getRelationView
- */
- public void setProjection(String projection) {
- m_Helper.setProjection(projection);
- }
-
- /**
- * Gets the RelationView that this component is bound with.
- * @return the RelationView currently bound with
- * @see #setRelationView
- * @see #setBinding
- * @see #getProjection
- */
- public RelationView getRelationView() {
- return m_Helper.getRelationView();
- }
-
- /**
- * Sets the RelationView that this component is bound with.
- * @param value the RelationView to bind with
- * @see #getRelationView
- * @see #setBinding
- * @see #setProjection
- */
- public void setRelationView(RelationView value) {
- m_Helper.setRelationView(value);
- }
-
- /**
- * 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(String value) {
- m_Helper.setTreatBlankAsString(value);
- }
-
- /**
- * Indicates when the component commits its changes.
- * @see #setDynamicUpdate
- */
- public boolean getDynamicUpdate() {
- return m_Helper.getDynamicUpdate();
- }
-
- /**
- * Sets when the component commits its changes.
- * @param value the new dynamic update mode value
- * @see #getDynamicUpdate
- */
- public void setDynamicUpdate(boolean value) {
- m_Helper.setDynamicUpdate(value);
- if (value) {
- removeFocusListener(m_Helper);
- addItemListener(m_Helper);
- }
- else {
- removeItemListener(m_Helper);
- addFocusListener(m_Helper);
- }
- }
-
- /**
- * 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) {
- }
-
- /**
- * Gets the RelationView this component is bound with.
- * @return the RelationView
- */
- public RelationView getLookUpRelationView() {
- return m_LookUpRelationView;
- }
-
- /**
- * Sets the RelationView this component is bound with.
- * @param value the RelationView
- */
- public void setLookUpRelationView(RelationView value) {
- }
-
- /**
- * Gets the ConnectionInfo this component uses to connect with the database.
- * @return the information needed to connect to the database
- * @see #setConnectionInfo
- */
- public ConnectionInfo getConnectionInfo() {
- return m_Conn;
- }
-
- /**
- * Sets the ConnectionInfo this component uses to connect with the database.
- * @param conn the information needed to connect to the database
- * @see #getConnectionInfo
- */
- public void setConnectionInfo(ConnectionInfo conn) {
- m_Conn = conn;
- }
-
-
- /**
- * Gets the sql string to use while generating the list for this component
- * to bind to.
- * @return the sql statment
- * @see #setSQL
- */
- public String getSelectClause() {
- return m_SQL;
- }
-
- /**
- * Sets the value of this component to the given value.
- * @param value the new component value
- * @see #getData
- */
- public void setData(Object value) {
- String text = (String)value;
- boolean bitemFound = false;
- for (int index = 0; index < countItems(); index++)
- {
- if (text.equals(getItem(index)))
- {
- select(index);
- makeVisible(index);
- bitemFound = true;
- break;
- }
- if (!bitemFound)
- {
- if (getSelectedIndex() != -1)
- {
- deselect(getSelectedIndex());
- }
- }
- }
- }
-
- /**
- * Gets the value of this component.
- * @return the current component value
- * @see #setData
- */
- public Object getData() {
- String text = null;
- String[] selectedItems = getSelectedItems();
- if (selectedItems.length == 1)
- {
- text = selectedItems[0];
- }
- 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 list of columns that this component will be bound to.
- * @return the list of columns
- * @see #join
- */
- public String[] getJoinColumns() {
- String[] joinColumns = new String[m_JoinColumnNames.size()];
- for (int index = 0; index < m_JoinColumnNames.size(); index++) {
- joinColumns[index] = new String((String)m_JoinColumnNames.elementAt(index));
- }
- return joinColumns;
- }
-
- /**
- * Sets the list of columns that this component will be bound to.
- * Use this method before calling bindList().
- * @param value the names of the columns to join
- * @see #join
- * @see #getJoinColumns
- * @see #bindList
- */
- public void setJoinColumns(String[] value) {
- }
-
- /**
- * 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() {
- addItemListener(m_Helper);
- }
-
- /**
- * 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();
- }
- }
-