-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gwt: Add server time monitoring widget.
- Loading branch information
1 parent
b0de73e
commit 693cac4
Showing
14 changed files
with
380 additions
and
3 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/event/ServerTimeUpdateEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ro.pub.cs.vmchecker.client.event; | ||
|
||
import java.util.Date; | ||
import com.google.gwt.event.shared.GwtEvent; | ||
|
||
public class ServerTimeUpdateEvent extends GwtEvent<ServerTimeUpdateEventHandler> { | ||
public static final GwtEvent.Type<ServerTimeUpdateEventHandler> TYPE = new GwtEvent.Type<ServerTimeUpdateEventHandler>(); | ||
|
||
private Date date; | ||
public ServerTimeUpdateEvent(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public Date getDate() { | ||
return this.date; | ||
} | ||
|
||
@Override | ||
protected void dispatch(ServerTimeUpdateEventHandler handler) { | ||
handler.onServerTimeUpdate(this); | ||
} | ||
|
||
@Override | ||
public com.google.gwt.event.shared.GwtEvent.Type<ServerTimeUpdateEventHandler> getAssociatedType() { | ||
return TYPE; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/event/ServerTimeUpdateEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ro.pub.cs.vmchecker.client.event; | ||
|
||
import com.google.gwt.event.shared.EventHandler; | ||
|
||
public interface ServerTimeUpdateEventHandler extends EventHandler { | ||
void onServerTimeUpdate(ServerTimeUpdateEvent event); | ||
} |
20 changes: 20 additions & 0 deletions
20
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/i18n/ServerTimeConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package ro.pub.cs.vmchecker.client.i18n; | ||
|
||
import com.google.gwt.core.client.GWT; | ||
import com.google.gwt.i18n.client.Constants; | ||
|
||
public interface ServerTimeConstants extends Constants { | ||
|
||
String serverTimeMsg(); | ||
String browserTimeAheadMsg(); | ||
String browserTimeBehindMsg(); | ||
|
||
String hours(); | ||
String hour(); | ||
String minutes(); | ||
String minute(); | ||
String seconds(); | ||
String second(); | ||
String and(); | ||
String lessThanASecond(); | ||
} |
1 change: 1 addition & 0 deletions
1
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/i18n/ServerTimeConstants.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ServerTimeConstants_en.properties |
12 changes: 12 additions & 0 deletions
12
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/i18n/ServerTimeConstants_en.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
serverTimeMsg = The current server time is: | ||
browserTimeAheadMsg = Your browser is ahead by: | ||
browserTimeBehindMsg = Your browser is behind by: | ||
|
||
hours = hours | ||
hour = hour | ||
minutes = minutes | ||
minute = minute | ||
seconds = seconds | ||
second = second | ||
and = and | ||
lessThanASecond = less than a second |
12 changes: 12 additions & 0 deletions
12
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/i18n/ServerTimeConstants_fr.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
serverTimeMsg = Le temps du serveur est: | ||
browserTimeAheadMsg = Votre navigateur est en avance de: | ||
browserTimeBehindMsg = Votre navigateur est en retard de: | ||
|
||
hours = heures | ||
hour = heure | ||
minutes = minutes | ||
minute = minute | ||
seconds = secondes | ||
second = second | ||
and = et | ||
lessThanASecond = moins d'une seconde |
12 changes: 12 additions & 0 deletions
12
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/i18n/ServerTimeConstants_ro.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
serverTimeMsg = Ora actuală a serverului este: | ||
browserTimeAheadMsg = Browserul este înainte cu: | ||
browserTimeBehindMsg = Browserul este în urmă cu: | ||
|
||
hours = ore | ||
hour = oră | ||
minutes = minute | ||
minute = minut | ||
seconds = secunde | ||
second = secundă | ||
and = și | ||
lessThanASecond = mai puțin de o secundă |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
gwt/vmchecker-gui/src/ro/pub/cs/vmchecker/client/presenter/ServerTimePresenter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package ro.pub.cs.vmchecker.client.presenter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Date; | ||
|
||
import ro.pub.cs.vmchecker.client.event.ServerTimeUpdateEvent; | ||
import ro.pub.cs.vmchecker.client.event.ServerTimeUpdateEventHandler; | ||
import ro.pub.cs.vmchecker.client.i18n.ServerTimeConstants; | ||
|
||
import com.google.gwt.event.dom.client.ChangeEvent; | ||
import com.google.gwt.event.dom.client.ChangeHandler; | ||
import com.google.gwt.event.dom.client.ClickEvent; | ||
import com.google.gwt.event.dom.client.ClickHandler; | ||
import com.google.gwt.event.dom.client.HasChangeHandlers; | ||
import com.google.gwt.event.dom.client.HasClickHandlers; | ||
import com.google.gwt.event.shared.EventBus; | ||
import com.google.gwt.user.client.Window; | ||
import com.google.gwt.user.client.Timer; | ||
import com.google.gwt.user.client.rpc.AsyncCallback; | ||
import com.google.gwt.user.client.ui.HasText; | ||
import com.google.gwt.user.client.ui.HasWidgets; | ||
import com.google.gwt.user.client.ui.Widget; | ||
import com.google.gwt.i18n.shared.DateTimeFormat; | ||
|
||
import com.google.gwt.core.client.GWT; | ||
public class ServerTimePresenter implements Presenter { | ||
|
||
private EventBus eventBus; | ||
private ServerTimePresenter.Widget widget; | ||
private Date serverTime; | ||
private DateTimeFormat dtf = DateTimeFormat.getFormat("HH:mm:ss, dd MMMM yyyy"); | ||
private Timer clockTimer; | ||
|
||
private static ServerTimeConstants constants = GWT | ||
.create(ServerTimeConstants.class); | ||
|
||
public interface Widget { | ||
HasText getServerTime(); | ||
HasText getBrowserTimeOffsetMsg(); | ||
HasText getBrowserTimeOffset(); | ||
} | ||
|
||
public ServerTimePresenter(EventBus eventBus, ServerTimePresenter.Widget widget) { | ||
this.eventBus = eventBus; | ||
this.widget = widget; | ||
this.serverTime = new Date(0); | ||
listenServerTimeUpdate(); | ||
launchClockUpdateTimer(); | ||
} | ||
|
||
private void updateDisplayedTime(Date newTime) { | ||
widget.getServerTime().setText(dtf.format(newTime)); | ||
} | ||
|
||
private void updateDisplayedOffset(Date serverTime) { | ||
Date browserTime = new Date(); | ||
if (serverTime.compareTo(browserTime) < 0) { | ||
widget.getBrowserTimeOffsetMsg().setText(constants.browserTimeAheadMsg()); | ||
widget.getBrowserTimeOffset().setText(formatDuration(browserTime.getTime(), serverTime.getTime())); | ||
} else { | ||
widget.getBrowserTimeOffsetMsg().setText(constants.browserTimeBehindMsg()); | ||
widget.getBrowserTimeOffset().setText(formatDuration(serverTime.getTime(), browserTime.getTime())); | ||
} | ||
} | ||
|
||
private String formatDuration(long time1, long time2) { | ||
long diff = time1 - time2; | ||
long diffSecs = (diff / 1000) % 60; | ||
long diffMins = ((diff / 1000) / 60) % 60; | ||
long diffHours = (((diff / 1000) / 60) / 60); | ||
if (diffHours == 0 && diffMins == 0 && diffSecs == 0) { | ||
return constants.lessThanASecond(); | ||
} | ||
StringBuilder sb = new StringBuilder(); | ||
if (diffHours > 0) { | ||
sb.append(diffHours); | ||
sb.append(" "); | ||
if (diffHours == 1) { | ||
sb.append(constants.hour()); | ||
} else { | ||
sb.append(constants.hours()); | ||
} | ||
} | ||
if (sb.length() > 0 && diffMins > 0 && diffSecs > 0) { | ||
sb.append(", "); | ||
} else if (sb.length() > 0 && (diffMins > 0 || diffSecs > 0)) { | ||
sb.append(" "); | ||
sb.append(constants.and()); | ||
sb.append(" "); | ||
|
||
} | ||
if (diffMins > 0) { | ||
sb.append(diffMins); | ||
sb.append(" "); | ||
if (diffMins == 1) { | ||
sb.append(constants.minute()); | ||
} else { | ||
sb.append(constants.minutes()); | ||
} | ||
} | ||
if (sb.length() > 0 && diffSecs > 0) { | ||
sb.append(" "); | ||
sb.append(constants.and()); | ||
sb.append(" "); | ||
|
||
} | ||
if (diffSecs > 0) { | ||
sb.append(diffSecs); | ||
sb.append(" "); | ||
if (diffSecs == 1) { | ||
sb.append(constants.second()); | ||
} else { | ||
sb.append(constants.seconds()); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
private void listenServerTimeUpdate() { | ||
this.eventBus.addHandler(ServerTimeUpdateEvent.TYPE, new ServerTimeUpdateEventHandler() { | ||
public void onServerTimeUpdate(ServerTimeUpdateEvent event) { | ||
ServerTimePresenter.this.serverTime = event.getDate(); | ||
updateDisplayedTime(ServerTimePresenter.this.serverTime); | ||
updateDisplayedOffset(ServerTimePresenter.this.serverTime); | ||
} | ||
}); | ||
} | ||
|
||
private void launchClockUpdateTimer() { | ||
clockTimer = new Timer() { | ||
@Override | ||
public void run() { | ||
serverTime.setTime(serverTime.getTime() + 1000); | ||
updateDisplayedTime(serverTime); | ||
} | ||
}; | ||
|
||
clockTimer.scheduleRepeating(1000); | ||
} | ||
|
||
public Widget getWidget() { | ||
return (Widget) widget; | ||
} | ||
|
||
@Override | ||
public void go(HasWidgets container) { | ||
container.add((com.google.gwt.user.client.ui.Widget)widget); | ||
} | ||
|
||
@Override | ||
public void clearEventHandlers() { | ||
if (clockTimer != null) { | ||
clockTimer.cancel(); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.