home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 27.3 KB | 754 lines |
- /*
- * @(#DBTstamp.java
- *
- * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
- *
- */
-
- package symantec.itools.db.awt;
-
- import java.sql.*;
- import symantec.itools.db.pro.*;
- import symantec.itools.awt.*;
- import java.awt.*;
- import java.awt.event.*;
- import symantec.itools.db.beans.binding.Name;
- import symantec.itools.db.beans.binding.Mediator;
- /**
- * A box in which a date or timestamp can be read or entered using any common
- * date or time format.
- * This component is available in Visual CafΘ Database Developer Edition only.
- * Use DBTstamp to:
- * <UL>
- * <LI>Edit a date or a timestamp.
- * <LI>Display a date or timestamp in a pre-defined display format.
- * </UL>
- * <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.
- */
- public class DBTstamp extends symantec.itools.db.awt.Tstamp implements ProjLink,FocusListener,ActionListener
- {
-
- private ProjBinder m_ProjBinder = null;
- private String m_ScreenData = "";
- private String m_BinderData = null;
- private boolean m_BinderDetermines = false;
- private String m_ProjectionName="";
- private RelationView m_RelationView = null;
- private boolean isSet=false;
-
- /**
- * 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;
-
- /**
- * A constant indicating that the value should be displayed/entered as a
- * time.
- */
- public static String Time="Time";
- /**
- * A constant indicating that the value should be displayed/entered as a
- * timestamp.
- */
- public static String Timestamp="Timestamp";
- /**
- * A constant indicating that the value should be displayed/entered as a
- * date.
- */
- public static String Date="Date";
-
-
- private String DefaultDateDisplayFormat= "D* M* D%TH, Y*";
- private String DefauldDateEntryFormat= "MDY";
- private String DefaultDateDBDisplayFormat= "Y*-MM-DD";
- private String DefaultDateDBEntryFormat= "YMD";
-
- private String DefaultTimeStampDisplayFormat= "D* M* D%TH, Y* H%:m%:s%,n3 AM";
- private String DefauldTimeStampEntryFormat= "MDYhmsn";
- private String DefaultTimeStampDBDisplayFormat= "Y*-MM-DD-hh-mm-ss-n9";
- private String DefaultTimeStampDBEntryFormat= "YMDhmsn";
-
- private String DefaultTimeDisplayFormat= "H%:m%:s% AM";
- private String DefauldTimeEntryFormat= "hms";
- private String DefaultTimeDBDisplayFormat= "hh-mm-ss";
- private String DefaultTimeDBEntryFormat= "hmsn";
-
- private String DataBaseDisplayFormat=DefaultTimeStampDBDisplayFormat;
- private String DataBaseEntryFormat=DefaultTimeStampDBEntryFormat;
- private String TYPE="";
-
- /**
- * The mediator that binds this component to a data source.
- */
- public Mediator m_Mediator;
- private String[] m_GetMethods={"getData()"};
- private String[] m_SetMethods={"setData(Value)"};
-
- /**
- * Constructs a default DBTstamp.
- */
- public DBTstamp()
- {
-
- super();
-
- m_Mediator=new Mediator();
- m_Mediator.setOutput(this);
- m_Mediator.setSetMethods(m_SetMethods);
- m_Mediator.setGetMethods(m_GetMethods);
- this.addFocusListener(this);
- this.addActionListener(this);
- m_BinderData = new String();
- m_ScreenData = new String();
- }
- /**
- * Sets the format for displaying database timestamp values.
- * @param form the display format
- * @see #getDataBaseDisplayFormat
- * @see symantec.itools.db.awt.Tstamp#getDisplayFormat
- */
- public void setDataBaseDisplayFormat(String format)
- {
- DataBaseDisplayFormat=format;
- }
- /**
- * Gets the current format for displaying database timestamp values.
- * @return the display format
- * @see #setDataBaseDisplayFormat
- * @see symantec.itools.db.awt.Tstamp#getDisplayFormat
- */
- public String getDataBaseDisplayFormat()
- {
- return(DataBaseDisplayFormat);
- }
- /**
- * Sets the format for entering database timestamp values.
- * @param form the entry format
- * @see #getDataBaseEntryFormat
- * @see symantec.itools.db.awt.Tstamp#getEntryFormat
- */
- public void setDataBaseEntryFormat(String format)
- {
- DataBaseEntryFormat=format;
- }
- /**
- * Gets the current format for entering database timestamp values.
- * @return the entry format
- * @see #setDataBaseEntryFormat
- * @see symantec.itools.db.awt.Tstamp#getEntryFormat
- */
- public String getDataBaseEntryFormat()
- {
- return(DataBaseEntryFormat);
- }
- /**
- * Gets the current datatype being displayed and/or entered.
- * @return the datatype. One of: Time, Timestamp, or Date
- * @see #Time
- * @see #Timestamp
- * @see #Date
- */
- public String getType()
- {
- return(TYPE);
- }
-
- /**
- * Sets the datatype being displayed and/or entered.
- * This changes the entry and display formats appropriately.
- * @param type the datatype. One of: Time, Timestamp, or Date
- * @see #Time
- * @see #Timestamp
- * @see #Date
- */
-
- public void setType(String type)
- {
-
- TYPE=type;
- if(type.equals(Date)){
-
- if(m_DisplayFormat.equals(""))this.setDisplayFormat(DefaultDateDisplayFormat);
- if(m_EntryFormat.equals(""))this.setEntryFormat(DefauldDateEntryFormat);
- setDataBaseDisplayFormat(DefaultDateDBDisplayFormat);
- setDataBaseEntryFormat(DefaultDateDBEntryFormat);
- }
-
- else if(type.equals(Time)){
- if(m_DisplayFormat.equals(""))this.setDisplayFormat(DefaultTimeDisplayFormat);
- if(m_EntryFormat.equals(""))this.setEntryFormat(DefauldTimeEntryFormat);
- setDataBaseDisplayFormat(DefaultTimeDBDisplayFormat);
- setDataBaseEntryFormat(DefaultTimeDBEntryFormat);
- }
-
- else if(type.equals(Timestamp)){
- if(m_DisplayFormat.equals(""))this.setDisplayFormat(DefaultTimeStampDisplayFormat);
- if(m_EntryFormat.equals(""))this.setEntryFormat(DefauldTimeStampEntryFormat);
- setDataBaseDisplayFormat(DefaultTimeStampDBDisplayFormat);
- setDataBaseEntryFormat(DefaultTimeStampDBEntryFormat);
- }
- }
-
- /**
- * Called when this component has been bound to a projection within an
- * RelationView. This component notes the ProjBinder object so that it
- * may notify the dbANYWHERE server of data changes made.
- * <p>
- * This is a method in the ProjLink interface.
- *
- * @param binder used to notify the dbANYWHERE server of any data changes
- * that are to be persistant.
- * This object is a window into the particular column data to which this
- * component has been bound
- *
- * @see symantec.itools.db.pro.ProjLink#init
- */
- public void init(ProjBinder binder)
- {
- m_ProjBinder = binder;
- setEditable(m_ProjBinder);
- }
-
- /**
- * 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
- * @exception SQLException
- * if a SQL database access error occurred
- */
- public synchronized void setBinding(RelationView relView, String projection)
- {
-
- m_RelationView = relView;
- m_ProjectionName = projection;
- String dataBindingName = "";
- try {
- dataBindingName = relView.getName();
- }
- catch (java.sql.SQLException e) {
- raiseException(e);
- }
- dataBindingName += "@" + projection;
- setDataBinding(dataBindingName);
- if (java.beans.Beans.isDesignTime()) {
- return;
- }
-
- if (m_RelationView == null || m_ProjectionName == null) {
- return; // nothing to do
- }
-
- if (m_ProjectionName.equals("")) {
- return; // nothing to do
- }
-
- try {
- Name name = new Name(m_RelationView.getName(),m_ProjectionName);
- m_Mediator.setDataBinding(name.getFullName());
- }
- catch (java.sql.SQLException e) {
- raiseException(e);
- }
-
- }
-
-
-
- synchronized String getRelationViewName() {
- if (m_RelationView != null) {
- try {
- return m_RelationView.getName();
- }
- catch (Exception ex) {
- raiseException(ex);
- }
- }
- return "null";
- }
- /**
- * Specifies how an empty string will be set when updating data on
- * the dbANYWHERE server.
- *
- * @param blank one of Default, Null, or Blank
- *
- * @see #Default
- * @see #Null
- * @see #Blank
- * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
- */
-
-
- //*************************************************************************//
- // //
- // FUNCTION: notifyDataChange //
- // //
- // PURPOSE: //
- // Implement the ProjLink interface function, notifyDataChange(). //
- // This function is called when the projection's data changes at //
- // the database. //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to get or change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // Step 1: set m_ProjBinder equal to binder object //
- // Step 2: synchronize m_BinderData with binder object //
- // Step 3: synchronize m_ScreenData with current screen //
- // Step 4: if the two are different then //
- // - set m_ScreenData equal to m_BinderData to avoid recursion //
- // - call setText to update screen //
- // Step 5: enable or disable editing based on binder //
- // //
- //*************************************************************************//
-
- /**
- * 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 scope of the current row of the RelationView bound to this
- * component.
- * The ProjBinder object passed into the notification is used to get the
- * new data value.
- * <p>
- * This is a method in the ProjLink interface.
- *
- * @param binder used to get the current value from the dbANYWHERE server.
- * This object is a window into the particular column data to which this
- * component has been bound
- *
- * @see symantec.itools.db.pro.ProjLink#notifyDataChange
- */
- public void notifyDataChange(ProjBinder binder)
- {
-
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: notifyInputChange //
- // //
- // PURPOSE: Handle any event that causes us to send data to binder object //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // //
- // Step 1: synchronize m_ScreenData with current screen //
- // Step 2: if m_ScreenData differs from m_BinderData then... //
- // - set m_BinderData equal to m_ScreenData to avoid recursion //
- // - call m_Binder.setString to update binder //
- // //
- //*************************************************************************//
-
- boolean notifyInputChanged(String input)
- {
-
- return true;
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: notifySetData //
- // //
- // PURPOSE: Send data to binder //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- // //
- //*************************************************************************//
-
- /**
- * This method is called to ensure the value of this dbAWARE component is
- * not lost.
- * It is called when the dbANYWHERE server has detected a data change made
- * within the current row of the RelationView bound to this component.
- * The ProjBinder object passed into the notification is used to set the
- * new data value.
- * <p>
- * This is a method in the ProjLink interface.
- *
- * @param binder used to notify the dbANYWHERE server of any data changes
- * that are to be persistant.
- * This object is a window into the particular column data to which this
- * component has been bound
- * @return true if success or false if failure.
- * @exception SQLException
- * if a SQL database access error occurred
- * @see symantec.itools.db.pro.ProjLink#notifySetData
- */
- public boolean notifySetData(ProjBinder binder) throws SQLException
- {
- return ( notifyInputChanged(getText()) );
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setEditable //
- // //
- // PURPOSE: Enable or disable input //
- // //
- // PARAMETERS: //
- // @param binder The ProjBinder binder object which this component can use//
- // to change data at the database server. //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- void setEditable(ProjBinder binder)
- {
- //Disable input if binder is not writeable
- boolean isWritable = false;
-
- try
- {
- if (binder != null)
- {
- RelationView rv = binder.getRelationView();
- if (rv != null && (rv.getCurrentRecordState()
- != Record.RECSTATE_INVALID))
- {
- isWritable = binder.isWritable();
- }
- }
- }
- catch (SQLException Ex)
- {
- raiseException(Ex);
- }
- super.setEditable(isWritable);
- }
-
- //*************************************************************************//
- // //
- // FUNCTION: setEditable //
- // //
- // PURPOSE: Set editable attribute //
- // //
- // PARAMETERS: //
- // //
- // RETURNS: //
- // //
- // COMMENTS: //
- // //
- //*************************************************************************//
-
- /**
- * Conditionally sets the contents of this component to user-editable.
- */
- public void setEditable(boolean value)
- {
- m_BinderDetermines = value;
- super.setEditable(value);
- }
-
- /**
- * Sets the display and input format.
- * @param dispfmt the new display and entry format
- * @see symantec.itools.db.awt.Tstamp#getDisplayFormat
- */
- public void setDisplayFormat(String dispFmt)
- {
- super.setDisplayFormat(dispFmt);
- notifyDataChange(m_ProjBinder);
- }
-
- /**
- * Processes events for this component.
- * This is a standard Java AWT method which gets called by the AWT
- * to handle this component's events. The default handler for
- * components dispatches to one of the following methods as needed:
- * action(), gotFocus(), lostFocus(), keyDown(), keyUp(), mouseEnter(),
- * mouseExit(), mouseMove(), mouseDrag(), mouseDown(), or mouseUp().
- *
- * @param event the event to handle
- * @return true if the event was handled and no further action is needed,
- * false to pass the event to this component's parent
- * @see java.awt.Component#action
- * @see java.awt.Component#gotFocus
- * @see java.awt.Component#lostFocus
- * @see java.awt.Component#keyDown
- * @see java.awt.Component#keyUp
- * @see java.awt.Component#mouseEnter
- * @see java.awt.Component#mouseExit
- * @see java.awt.Component#mouseMove
- * @see java.awt.Component#mouseDrag
- * @see java.awt.Component#mouseDown
- * @see java.awt.Component#mouseUp
- */
-
- public void actionPerformed(ActionEvent e)
- {
- if(getText().trim().length()<3)
- {
- if(m_BinderData.length()==0)
- setTstamp(new java.util.Date());
- }
- else
- {
- setTstamp(getText(),getEntryFormat());
- }
- printTstamp();
- isSet=true;
- }
-
- /**
- * This method commits the component's data to its data source.
- * <p>
- * A method of the FocusListener interface.
- * @param e the event
- */
- public void focusLost(FocusEvent e)
- {
- if(getText().trim().length()>=3)
- {
- setTstamp(getText(),getEntryFormat());
- printTstamp();
- isSet=true;
- }
- else if (getText().trim().length()<3)
- {
- isSet=false;
- }
- m_Mediator.commit();
-
- }
- /**
- * Takes no action.
- * <p>
- * A method of the FocusListener interface.
- * @param e the event
- */
- public void focusGained(FocusEvent e)
- {}
-
- /**
- * Returns the projection in the RelationView that this component is bound with.
- * @see #setProjection
- */
- public String getProjection() {
- return m_ProjectionName;
- }
-
- /**
- * Binds this component to the given projection within the RelationView
- * the component is currently bound with.
- * @see #getProjection
- * @see #getRelationView
- */
- public void setProjection(String projection) {
- try {
- setBinding(m_RelationView, projection);
- }
- catch (Exception ex) {
- raiseException(ex);
- }
- }
-
- /**
- * Gets the RelationView that this component is bound with.
- * @return the RelationView currently bound with
- * @see #setRelationView
- * @see #getProjection
- */
- public RelationView getRelationView() {
- return m_RelationView;
- }
-
- /**
- * Sets the RelationView that this component is bound with.
- * @param value the RelationView to bind with
- * @see #getRelationView
- * @see #setProjection
- */
- public void setRelationView(RelationView rv) {
- try {
- setBinding(rv, m_ProjectionName);
- }
- catch (Exception ex) {
- raiseException(ex);
- }
- }
-
- /**
- * The font used for editable text.
- */
- public Font font;
- /**
- * Sets the font used for editable text.
- * @param f the font
- * @see #getEditFont
- */
- public void setEditFont(Font f)
- {
- font=f;
- }
- /**
- * Gets the font used for editable text.
- * @return the font
- * @see #setEditFont
- */
- public Font getEditFont()
- {
- return font;
- }
- /**
- * Sets when the component commits its changes.
- * @param value the new dynamic update mode value
- * @see #getDynamicUpdate
- */
- public void setDynamicUpdate(boolean b)
- {
- }
- /**
- * Indicates when the component commits its changes.
- * @see #setDynamicUpdate
- */
- public boolean getDynamicUpdate()
- {
- return false;
- }
- /**
- * Sets the value of this component to the given value.
- * @param value the new component value
- * @see #getData
- */
- public void setData(Object value)
- {
-
- if(value==null || value.equals("")){
- // System.out.println("null");
- setText("");
- isSet=false;
- return;
- }
- isSet=true;
- if(TYPE.equals("")){
- if (value instanceof java.sql.Date) setType(Date);
- else if (value instanceof java.sql.Time) setType(Time);
- else if (value instanceof java.sql.Timestamp) setType(Timestamp);
- }
- readDate(value.toString(),DataBaseEntryFormat);
- setText(exTstamp(getDisplayFormat()));
-
- }
- /**
- * Gets the value of this component.
- * @return the current component value
- * @see #setData
- */
- public Object getData()
- {
-
- if(!isSet) return null;
-
- else if(TYPE==Date){
- java.sql.Date o=new java.sql.Date(year-1900,month,day);
- return (Object)o;
- }
- else if(TYPE==Time){
- java.sql.Time o=new java.sql.Time(hour,minute,second);
- return (Object)o;
- }
- else if(TYPE==Timestamp){
- java.sql.Timestamp o=new java.sql.Timestamp(year-1900,month,day,hour,minute,second,nanosec);
- return (Object)o;
- }
-
- return null;
- }
-
- /**
- * 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)
- {
- m_Mediator.setDataBinding(name);
- }
-
- /**
- * 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_Mediator.getDataBinding();
- }
-
- /**
- * Specifies how an empty string will be set when updating data on
- * the dbANYWHERE server.
- *
- * @param blank one of Default, Null, or Blank
- *
- * @see #Default
- * @see #Null
- * @see #Blank
- * @see symantec.itools.db.pro.ProjBinder#setValueFromString(java.lang.String, int, int)
- */
- public void setTreatBlankAs(int treatAs)
- {
- setEmptyMeansNull(treatAs == m_Mediator.Null);
- }
-
- /**
- * 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_Mediator.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_Mediator.getEmptyMeansNull();
- }
- }
-