FileDocCategorySizeDatePackage
DateBean.javaAPI DocExample5022Sat Sep 12 03:01:00 BST 1998borland.samples.beans.customizer

DateBean.java

/*
 * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
 * 
 * This SOURCE CODE FILE, which has been provided by Borland as part
 * of a Borland product for use ONLY by licensed users of the product,
 * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
 *
 * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
 * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
 * THE PRODUCT.
 *
 * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
 * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
 * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
 * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
 * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
 * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
 * CODE FILE.
 */
//
// DateBean is a simple bean that displays the current date.  It allows for
// some modification via properties and takes advantage of the new
// enumerationValues concept in BeanInfo.
//

package borland.samples.beans.customizer;
                                  
import java.awt.*;                     
import java.util.Calendar;
import com.sun.java.swing.*;

public class DateBean extends JLabel{
  private Calendar c = Calendar.getInstance();
  private int m, d, y, e;
  private String month, day, year, era, month_str;

  // three of the five properties surfaced by the bean.  Font and Background
  // are the other two and they are inherited from the parent object and don't
  // need local representation
  private boolean useMonthString;
  private Color fontColor;
  private int style;

  // constants used by bean and it's BeanInfo and Customizer
  static final int MONTH_DAY_YEAR     = 1;
  static final int MONTH_DAY_YEAR_ERA = 2;
  static final int YEAR_MONTH_DAY     = 3;
  static final int MONTH_YEAR         = 4;
  static final int DAY_MONTH_YEAR     = 5;
  static final String[] allMonths = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

  public DateBean() {
      m = c.get(Calendar.MONTH) + 1;
      d = c.get(Calendar.DATE);
      y = c.get(Calendar.YEAR);
      e = c.get(Calendar.ERA);

      if(m <= 9)
        month = "0" + String.valueOf(m);
      else
        month = String.valueOf(m);

      if(d <=9)
        day = "0" + String.valueOf(d);
      else
        day = String.valueOf(d);

      year = String.valueOf(y);
      month_str = allMonths[m - 1];

      // AD or BC -- it might matter when Java builds time machines!
      if(e == 1)
        era = "AD";
      else
        era = "BC";

      setStyle(MONTH_DAY_YEAR_ERA);
      useMonthString = false;
  }

  //
  // fontColor makes more descriptive sense then foreground and we'll hide
  // foreground with BeanInfo
  //
  public void setFontColor(Color newFontColor) {
    super.setForeground(newFontColor);
  }

  public Color getFontColor() {
      return super.getForeground();
  }

  public void setFont(Font newFont) {
    super.setFont(newFont);
  }

  public Font getFont() {
      return super.getFont();
  }

  // just overridden from the parent
  public void setBackground(Color newBackground) {
    super.setBackground(newBackground);
  }

  public Color getBackground() {
      return super.getBackground();
  }

  // style is handled in the designer with a new Enumeration Property from the
  // BeanInfo
  public void setStyle(int newStyle) {
    style = newStyle;

    switch(style){
      case MONTH_DAY_YEAR:
        if(useMonthString)
          this.setText(month_str + " " + day + ", " + year);
        else
          this.setText(month + " / " + day + " / " + year);
        break;

      case MONTH_DAY_YEAR_ERA:
        if(useMonthString)
          this.setText(month_str + " " + day + ", " + year + " " + era);
        else
          this.setText(month + " / " + day + " / " + year + " " + era);
        break;

      case YEAR_MONTH_DAY:
        if(useMonthString)
          this.setText(year + " " + month_str + " " + day);
        else
          this.setText(year + " / " + month + " / " + day);
        break;

      case MONTH_YEAR:
        if(useMonthString)
          this.setText(month_str + " " + year);
        else
          this.setText(month + " " + year);
        break;

      case DAY_MONTH_YEAR:
        if(useMonthString)
          this.setText(day + " " + month_str + " " + year);
        else
          this.setText(day + " / " + month + " / " + year);
        break;

      default:
        this.setText("invalid");
    }
  }

  public int getStyle() {
      return style;
  }

  public void setUseMonthString( boolean x ){
    useMonthString = x;
    this.setStyle(this.getStyle());
  }

  public boolean getUseMonthString(){
    return useMonthString;
  }

}