home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-18 | 5.9 KB | 192 lines |
- /*
- * @(#AgeField.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.*;
-
- /**
- * dbAware AgeField component.
- * <p>
- * This component is a single-line area that displays elapsed time as a number
- * of days, months, or years relative to the current date.
- * <p>
- * It can be bound to a projection in a database.
- */
- public class AgeField extends symantec.itools.db.awt.TextField
- {
- private java.util.Date m_currentDate = new java.util.Date();
- private int m_Month = m_currentDate.getMonth();
- private int m_Day = m_currentDate.getDate();
- private int m_Year = m_currentDate.getYear();
- private int m_displayAs = 0;
- private int[] daysPerMonth = new int[12];
- private final static int DAYS = 0;
- private final static int WEEKS = 1;
- private final static int MONTHS = 2;
- private final static int YEARS = 3;
-
- /**
- * Constructs a default AgeField. It displays the elapsed time in days
- * and is not editable by default.
- */
- public AgeField()
- {
- super();
- super.setEditable(false);
- daysPerMonth[0] = 31;
- daysPerMonth[1] = 28;
- daysPerMonth[2] = 31;
- daysPerMonth[3] = 30;
- daysPerMonth[4] = 31;
- daysPerMonth[5] = 30;
- daysPerMonth[6] = 31;
- daysPerMonth[7] = 31;
- daysPerMonth[8] = 30;
- daysPerMonth[9] = 31;
- daysPerMonth[10] = 30;
- daysPerMonth[11] = 31;
- }
-
- private String computeDateDiff(java.util.Date tDate)
- {
- int tMonth = tDate.getMonth();
- int tDay = tDate.getDate();
- int tYear = tDate.getYear();
-
- int diff = m_Year - tYear;
-
- if (tDate != null)
- {
- int dayDiff = 0;
- int monthDiff = 0;
- int yearDiff = 0;
-
- if (m_currentDate.after(tDate))
- {
- switch (m_displayAs)
- {
- case YEARS:
- {
- if (tMonth > m_Month)
- {
- diff--;
- }
- else if (tMonth == m_Month)
- {
- if (tDay > m_Day)
- {
- diff--;
- }
- }
- break;
- }
-
- case MONTHS:
- {
- diff *= 12;
- if (tMonth < m_Month)
- {
- diff += (m_Month - tMonth);
- if (tDay > m_Day) { diff--; }
- }
- else if (tMonth == m_Month && tDay > m_Day) { diff--; }
- else if (tMonth > m_Month)
- {
- diff -= (tMonth - m_Month);
- if (tDay > m_Day) { diff--; }
- }
- break;
- }
-
- case WEEKS:
- {
- break;
- }
-
- case DAYS:
- {
- if (tMonth < m_Month)
- {
- diff *= 365;
- int d = m_Day;
- int i = m_Month - 1;
- while (i > tMonth)
- {
- if (i < 0) { i = 11; }
- d += daysPerMonth[i];
- i--;
- }
- d += (daysPerMonth[tMonth] - tDay);
- if (diff == 0) { diff = d; } else { diff += d; }
- }
- else if (tMonth == m_Month)
- {
- diff *= 365;
- if (tDay > m_Day) { diff -= (tDay - m_Day); }
- else if (tDay < m_Day) { diff += (m_Day - tDay); }
- }
- else if (tMonth > m_Month)
- {
- int d = daysPerMonth[tMonth] - tDay;
- int i = tMonth + 1;
- while (i != m_Month)
- {
- if (i > 11) { i = 0; }
- d += daysPerMonth[i];
- i++;
- }
- d += m_Day;
- diff = ( (diff - 1) * 365) + d;
- }
- break;
- }
- }
- }
- else
- {
-
- }
- }
- return (new Integer(diff).toString());
- }
-
- /**
- * Sets the unit of time displayed.
- *
- * @param value the unit of time to display, one of: DAYS, WEEKS, MONTHS, or YEARS
- */
- public void setDisplayAs(String value)
- {
- if (new String(value).toUpperCase().equals("DAYS"))
- {
- m_displayAs = DAYS;
- }
- else if (new String(value).toUpperCase().equals("WEEKS"))
- {
- m_displayAs = WEEKS;
- }
- else if (new String(value).toUpperCase().equals("MONTHS"))
- {
- m_displayAs = MONTHS;
- }
- else
- {
- m_displayAs = YEARS;
- }
- m_Helper.notifyDataChange();
- }
-
- public void setText(String text) {
- super.setText(computeDateDiff(m_Helper.getBinder().getDate()));
- }
-
- }