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 / AgeField.java next >
Encoding:
Java Source  |  1998-03-18  |  5.9 KB  |  192 lines

  1. /*
  2.  * @(#AgeField.java
  3.  *
  4.  * Copyright (c) 1997 Symantec Corporation. All Rights Reserved.
  5.  *
  6.  */
  7.  
  8. package symantec.itools.db.awt;
  9.  
  10. import java.util.*;
  11. import java.sql.*;
  12. import java.lang.*;
  13. import symantec.itools.db.net.*;
  14. import symantec.itools.db.pro.*;
  15.  
  16. /**
  17.  * dbAware AgeField component.
  18.  * <p>
  19.  * This component is a single-line area that displays elapsed time as a number
  20.  * of days, months, or years relative to the current date.
  21.  * <p>
  22.  * It can be bound to a projection in a database.
  23.  */
  24. public class AgeField extends symantec.itools.db.awt.TextField
  25. {
  26.     private java.util.Date          m_currentDate = new java.util.Date();
  27.     private int m_Month             = m_currentDate.getMonth();
  28.     private int m_Day               = m_currentDate.getDate();
  29.     private int m_Year              = m_currentDate.getYear();
  30.     private int m_displayAs         = 0;
  31.     private int[] daysPerMonth      = new int[12];
  32.     private final static int DAYS   = 0;
  33.     private final static int WEEKS  = 1;
  34.     private final static int MONTHS = 2;
  35.     private final static int YEARS  = 3;
  36.  
  37.     /**
  38.      * Constructs a default AgeField. It displays the elapsed time in days
  39.      * and is not editable by default.
  40.      */
  41.     public AgeField()
  42.     {
  43.         super();
  44.         super.setEditable(false);
  45.         daysPerMonth[0] = 31;
  46.         daysPerMonth[1] = 28;
  47.         daysPerMonth[2] = 31;
  48.         daysPerMonth[3] = 30;
  49.         daysPerMonth[4] = 31;
  50.         daysPerMonth[5] = 30;
  51.         daysPerMonth[6] = 31;
  52.         daysPerMonth[7] = 31;
  53.         daysPerMonth[8] = 30;
  54.         daysPerMonth[9] = 31;
  55.         daysPerMonth[10] = 30;
  56.         daysPerMonth[11] = 31;
  57.     }
  58.  
  59.     private String computeDateDiff(java.util.Date tDate)
  60.     {
  61.         int tMonth = tDate.getMonth();
  62.         int tDay = tDate.getDate();
  63.         int tYear = tDate.getYear();
  64.  
  65.         int diff = m_Year - tYear;
  66.  
  67.         if (tDate != null)
  68.         {
  69.             int dayDiff = 0;
  70.             int monthDiff = 0;
  71.             int yearDiff = 0;
  72.  
  73.             if (m_currentDate.after(tDate))
  74.             {
  75.                 switch (m_displayAs)
  76.                 {
  77.                     case YEARS:
  78.                     {
  79.                         if (tMonth > m_Month)
  80.                         {
  81.                             diff--;
  82.                         }
  83.                         else if (tMonth == m_Month)
  84.                         {
  85.                             if (tDay > m_Day)
  86.                             {
  87.                                 diff--;
  88.                             }
  89.                         }
  90.                         break;
  91.                     }
  92.  
  93.                     case MONTHS:
  94.                     {
  95.                         diff *= 12;
  96.                         if (tMonth < m_Month)
  97.                         {
  98.                             diff += (m_Month - tMonth);
  99.                             if (tDay > m_Day) { diff--; }
  100.                         }
  101.                         else if (tMonth == m_Month && tDay > m_Day) { diff--; }
  102.                         else if (tMonth > m_Month)
  103.                         {
  104.                             diff -= (tMonth - m_Month);
  105.                             if (tDay > m_Day) { diff--; }
  106.                         }
  107.                         break;
  108.                     }
  109.  
  110.                     case WEEKS:
  111.                     {
  112.                         break;
  113.                     }
  114.  
  115.                     case DAYS:
  116.                     {
  117.                         if (tMonth < m_Month)
  118.                         {
  119.                             diff *= 365;
  120.                             int d = m_Day;
  121.                             int i = m_Month - 1;
  122.                             while (i > tMonth)
  123.                             {
  124.                                 if (i < 0) { i = 11; }
  125.                                 d += daysPerMonth[i];
  126.                                 i--;
  127.                             }
  128.                             d += (daysPerMonth[tMonth] - tDay);
  129.                             if (diff == 0) { diff = d; } else { diff += d; }
  130.                         }
  131.                         else if (tMonth == m_Month)
  132.                         {
  133.                             diff *= 365;
  134.                             if (tDay > m_Day) { diff -= (tDay - m_Day); }
  135.                             else if (tDay < m_Day) { diff += (m_Day - tDay); }
  136.                         }
  137.                         else if (tMonth > m_Month)
  138.                         {
  139.                             int d = daysPerMonth[tMonth] - tDay;
  140.                             int i = tMonth + 1;
  141.                             while (i != m_Month)
  142.                             {
  143.                                 if (i > 11) { i = 0; }
  144.                                 d += daysPerMonth[i];
  145.                                 i++;
  146.                             }
  147.                             d += m_Day;
  148.                             diff = ( (diff - 1) * 365) + d;
  149.                         }
  150.                         break;
  151.                     }
  152.                 }
  153.             }
  154.             else
  155.             {
  156.  
  157.             }
  158.         }
  159.         return (new Integer(diff).toString());
  160.     }
  161.  
  162.     /**
  163.      * Sets the unit of time displayed.
  164.      *
  165.      * @param value the unit of time to display, one of: DAYS, WEEKS, MONTHS, or YEARS
  166.      */
  167.     public void setDisplayAs(String value)
  168.     {
  169.         if (new String(value).toUpperCase().equals("DAYS"))
  170.         {
  171.             m_displayAs = DAYS;
  172.         }
  173.         else if (new String(value).toUpperCase().equals("WEEKS"))
  174.         {
  175.             m_displayAs = WEEKS;
  176.         }
  177.         else if (new String(value).toUpperCase().equals("MONTHS"))
  178.         {
  179.             m_displayAs = MONTHS;
  180.         }
  181.         else
  182.         {
  183.             m_displayAs = YEARS;
  184.         }
  185.         m_Helper.notifyDataChange();
  186.     }
  187.  
  188.     public void setText(String text) {
  189.         super.setText(computeDateDiff(m_Helper.getBinder().getDate()));
  190.     }
  191.  
  192. }