package com.example.dwr.reverseajax;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.directwebremoting.Browser;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.ui.dwr.Util;
public class Clock implements Runnable {
protected transient boolean active = false;
int i = 0;
public Clock() {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
1);
executor.scheduleAtFixedRate(this, 1, 1, TimeUnit.SECONDS);
}
public void run() {
if (active) {
setClockDisplay((new java.text.SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒")).format(new Date()));
}
}
/**
* Called from the client to turn the clock on/off
*/
public synchronized void toggle() {
i++;
active = !active;
System.out.println("toggle clicked!" + i);
System.out.println("active:" + active);
if (active) {
setClockDisplay("Started");
} else {
setClockDisplay("Stopped");
}
}
public void setClockDisplay(final String output) {
String page = ServerContextFactory.get().getContextPath() + "/test.jsp";
Browser.withPage(page, new Runnable() {
public void run() {
Util.setValue("clockDisplay", output);
}
});
}
}