home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-05-08 | 4.7 KB | 182 lines |
- /*
- * 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.
- */
- package borland.samples.intl.beans;
-
- import java.awt.*;
- import java.awt.event.*;
- import java.text.*;
- import java.util.*;
- import borland.jbcl.layout.*;
- import borland.jbcl.control.*;
-
- import borland.samples.intl.beans.event.*;
-
- /**
- * A clock bean which displays time and date in locale-sensitive format.
- */
- public class InternationalClock extends BevelPanel implements LocaleChangeListener, Runnable {
- private BorderLayout borderLayout1 = new BorderLayout();
- protected LabelControl labelControl1 = new LabelControl();
- protected DateFormat clockFormat = DateFormat.getDateTimeInstance();
- private int dateStyle = DateFormat.DEFAULT;
- private int timeStyle = DateFormat.DEFAULT;
- private boolean displayTime = true;
- private boolean displayDate = true;
- private Thread internalClock;
- private boolean clockEnabled = false;
- private int clockTickRate = 900;
-
- public InternationalClock() {
- try {
- jbInit();
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public void jbInit() throws Exception{
- labelControl1.setText(clockFormat.format(new Date()));
- this.setLayout(borderLayout1);
- this.add(labelControl1, BorderLayout.CENTER);
- setClockEnabled(true);
- }
-
- public void updateTime() {
- if (clockFormat == null) {
- labelControl1.setText("");
- } else {
- labelControl1.setText(clockFormat.format(new Date()));
- }
- }
-
- public void setDisplayDate(boolean displayDate) {
- this.displayDate = displayDate;
- updateDisplayFormat();
- }
-
- public boolean isDisplayDate() {
- return displayDate;
- }
-
- public void setDateStyle(int dateStyle) {
- this.dateStyle = dateStyle;
- updateDisplayFormat();
- }
-
- public int getDateStyle() {
- return dateStyle;
- }
-
- public void setDisplayTime(boolean displayTime) {
- this.displayTime = displayTime;
- updateDisplayFormat();
- }
-
- public boolean isDisplayTime() {
- return displayTime;
- }
-
- public void setTimeStyle(int timeStyle) {
- this.timeStyle = timeStyle;
- updateDisplayFormat();
- }
-
- public int getTimeStyle() {
- return timeStyle;
- }
-
- public void setLocale(Locale locale) {
- super.setLocale(locale);
- updateDisplayFormat();
- }
-
- public Locale getLocale() {
- try {
- return super.getLocale();
- } catch (IllegalComponentStateException e) {
- return Locale.getDefault();
- }
- }
-
- public void setAlignment(int alignment) {
- labelControl1.setAlignment(alignment);
- }
-
- public int getAlignment() {
- return labelControl1.getAlignment();
- }
-
- public void localeChanged(LocaleChangeEvent e) {
- updateDisplayFormat();
- }
-
- private void updateDisplayFormat() {
-
- if (displayTime) {
- if (displayDate) {
- clockFormat = DateFormat.getDateTimeInstance(dateStyle, timeStyle, getLocale());
- } else {
- clockFormat = DateFormat.getTimeInstance(timeStyle, getLocale());
- }
- } else {
- if (displayDate) {
- clockFormat = DateFormat.getDateInstance(dateStyle, getLocale());
- } else {
- clockFormat = null;
- }
- }
-
- updateTime();
- }
-
- public synchronized void setClockEnabled(boolean clockEnabled) {
- this.clockEnabled = clockEnabled;
- if (clockEnabled && internalClock == null) {
- internalClock = new Thread(this);
- internalClock.start();
- }
- if (clockEnabled) {
- notify();
- }
- }
-
- public boolean getClockEnabled() {
- return clockEnabled;
- }
-
- // implementation of Runnable interface
- public void run() {
- try {
- while (true) {
- synchronized (this) {
- while (!clockEnabled) {
- wait();
- }
- }
- updateTime();
- Thread.sleep(clockTickRate);
- }
- } catch (InterruptedException e) {
- }
- }
-
- }
-