home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 October A / Pcwk10a98.iso / Inprise / TRIAL / JBUILDER / JSAMPLES.Z / InternationalClock.java < prev    next >
Text File  |  1998-05-08  |  5KB  |  182 lines

  1. /*
  2.  * Copyright (c) 1997-1998 Borland International, Inc. All Rights Reserved.
  3.  * 
  4.  * This SOURCE CODE FILE, which has been provided by Borland as part
  5.  * of a Borland product for use ONLY by licensed users of the product,
  6.  * includes CONFIDENTIAL and PROPRIETARY information of Borland.  
  7.  *
  8.  * USE OF THIS SOFTWARE IS GOVERNED BY THE TERMS AND CONDITIONS 
  9.  * OF THE LICENSE STATEMENT AND LIMITED WARRANTY FURNISHED WITH
  10.  * THE PRODUCT.
  11.  *
  12.  * IN PARTICULAR, YOU WILL INDEMNIFY AND HOLD BORLAND, ITS RELATED
  13.  * COMPANIES AND ITS SUPPLIERS, HARMLESS FROM AND AGAINST ANY CLAIMS
  14.  * OR LIABILITIES ARISING OUT OF THE USE, REPRODUCTION, OR DISTRIBUTION
  15.  * OF YOUR PROGRAMS, INCLUDING ANY CLAIMS OR LIABILITIES ARISING OUT OF
  16.  * OR RESULTING FROM THE USE, MODIFICATION, OR DISTRIBUTION OF PROGRAMS
  17.  * OR FILES CREATED FROM, BASED ON, AND/OR DERIVED FROM THIS SOURCE
  18.  * CODE FILE.
  19.  */
  20. package borland.samples.intl.beans;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.text.*;
  25. import java.util.*;
  26. import borland.jbcl.layout.*;
  27. import borland.jbcl.control.*;
  28.  
  29. import borland.samples.intl.beans.event.*;
  30.  
  31. /**
  32.  * A clock bean which displays time and date in locale-sensitive format.
  33.  */
  34. public class InternationalClock extends BevelPanel implements LocaleChangeListener, Runnable {
  35.   private BorderLayout borderLayout1 = new BorderLayout();
  36.   protected LabelControl labelControl1 = new LabelControl();
  37.   protected DateFormat clockFormat = DateFormat.getDateTimeInstance();
  38.   private int dateStyle = DateFormat.DEFAULT;
  39.   private int timeStyle = DateFormat.DEFAULT;
  40.   private boolean displayTime = true;
  41.   private boolean displayDate = true;
  42.   private Thread internalClock;
  43.   private boolean clockEnabled = false;
  44.   private int clockTickRate = 900;
  45.  
  46.   public InternationalClock() {
  47.     try {
  48.       jbInit();
  49.     }
  50.     catch (Exception e) {
  51.       e.printStackTrace();
  52.     }
  53.   }
  54.  
  55.   public void jbInit() throws Exception{
  56.     labelControl1.setText(clockFormat.format(new Date()));
  57.     this.setLayout(borderLayout1);
  58.     this.add(labelControl1, BorderLayout.CENTER);
  59.     setClockEnabled(true);
  60.   }
  61.  
  62.   public void updateTime() { 
  63.     if (clockFormat == null) {
  64.       labelControl1.setText("");
  65.     } else {
  66.       labelControl1.setText(clockFormat.format(new Date()));
  67.     }
  68.   }
  69.  
  70.   public void setDisplayDate(boolean displayDate) {
  71.     this.displayDate = displayDate;
  72.     updateDisplayFormat();
  73.   }
  74.  
  75.   public boolean isDisplayDate() {
  76.     return displayDate;
  77.   }
  78.  
  79.   public void setDateStyle(int dateStyle) {
  80.     this.dateStyle = dateStyle;
  81.     updateDisplayFormat();
  82.   }
  83.  
  84.   public int getDateStyle() {
  85.     return dateStyle;
  86.   }
  87.  
  88.   public void setDisplayTime(boolean displayTime) {
  89.     this.displayTime = displayTime;
  90.     updateDisplayFormat();
  91.   }
  92.  
  93.   public boolean isDisplayTime() {
  94.     return displayTime;
  95.   }
  96.  
  97.   public void setTimeStyle(int timeStyle) {
  98.     this.timeStyle = timeStyle;
  99.     updateDisplayFormat();
  100.   }
  101.  
  102.   public int getTimeStyle() {
  103.     return timeStyle;
  104.   }
  105.  
  106.   public void setLocale(Locale locale) {
  107.     super.setLocale(locale);
  108.     updateDisplayFormat();
  109.   }
  110.  
  111.   public Locale getLocale() {
  112.     try {
  113.       return super.getLocale();
  114.     } catch (IllegalComponentStateException e) {
  115.       return Locale.getDefault();
  116.     }
  117.   }
  118.  
  119.   public void setAlignment(int alignment) {
  120.     labelControl1.setAlignment(alignment);
  121.   }
  122.  
  123.   public int getAlignment() {
  124.     return labelControl1.getAlignment();
  125.   }
  126.  
  127.   public void localeChanged(LocaleChangeEvent e) {
  128.     updateDisplayFormat();
  129.   }
  130.  
  131.   private void updateDisplayFormat() {
  132.  
  133.     if (displayTime) {
  134.       if (displayDate) {
  135.         clockFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, getLocale());
  136.       } else {
  137.         clockFormat = DateFormat.getTimeInstance(timeStyle, getLocale());
  138.       }
  139.     } else {
  140.       if (displayDate) {
  141.         clockFormat = DateFormat.getDateInstance(dateStyle, getLocale());
  142.       } else {
  143.         clockFormat = null;
  144.       }
  145.     }
  146.  
  147.     updateTime();
  148.   }
  149.  
  150.   public synchronized void setClockEnabled(boolean clockEnabled) {
  151.     this.clockEnabled = clockEnabled;
  152.     if (clockEnabled && internalClock == null) {
  153.       internalClock = new Thread(this);
  154.       internalClock.start();
  155.     }
  156.     if (clockEnabled) {
  157.       notify();
  158.     }
  159.   }
  160.  
  161.   public boolean getClockEnabled() {
  162.     return clockEnabled;
  163.   }
  164.  
  165.   // implementation of Runnable interface
  166.   public void run() {
  167.     try {
  168.       while (true) {
  169.         synchronized (this) {
  170.           while (!clockEnabled) {
  171.             wait();
  172.           }
  173.         }
  174.         updateTime();
  175.         Thread.sleep(clockTickRate);
  176.       }
  177.     } catch (InterruptedException e) {
  178.     }
  179.   }
  180.  
  181. }
  182.