home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 15
/
BUGCD1998_06.ISO
/
aplic
/
jbuilder
/
jsamples.z
/
InternationalClock.java
< prev
next >
Wrap
Text File
|
1997-07-30
|
4KB
|
163 lines
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) {
}
}
}