Skip to content

Commit

Permalink
gwt: Add server time monitoring widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
calin-iorgulescu committed Mar 10, 2015
1 parent b0de73e commit 693cac4
Show file tree
Hide file tree
Showing 14 changed files with 380 additions and 3 deletions.
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;
}
}
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);
}
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();
}
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
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
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ă
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ro.pub.cs.vmchecker.client.ui.AssignmentBoardWidget;
import ro.pub.cs.vmchecker.client.ui.NumberedMenu;
import ro.pub.cs.vmchecker.client.ui.StatisticsWidget;
import ro.pub.cs.vmchecker.client.ui.ServerTimeWidget;
import ro.pub.cs.vmchecker.client.model.User;

import com.google.gwt.core.client.GWT;
Expand All @@ -33,12 +34,14 @@ public class AssignmentPresenter implements Presenter {
private MenuPresenter menuPresenter = null;
private AssignmentBoardPresenter boardPresenter = null;
private StatisticsPresenter statsPresenter = null;
private ServerTimePresenter serverTimePresenter = null;
private static VmcheckerConstants constants = GWT
.create(VmcheckerConstants.class);

public interface AssignmentWidget {
HasWidgets getMenuPanel();
HasWidgets getBoardPanel();
HasWidgets getSidePanel();
HasClickHandlers getViewStatsButton();
}

Expand Down Expand Up @@ -101,15 +104,18 @@ public void onSuccess(Assignment[] result) {
menuPresenter = new MenuPresenter(eventBus, new NumberedMenu(titles));
boardPresenter = new AssignmentBoardPresenter(eventBus, service, courseId, new AssignmentBoardWidget());
statsPresenter = new StatisticsPresenter(eventBus, service, courseId, user, result, new StatisticsWidget());
serverTimePresenter = new ServerTimePresenter(eventBus, new ServerTimeWidget());

bindWidget(widget);

widget.getMenuPanel().clear();
menuPresenter.go(widget.getMenuPanel());
menuPresenter.getWidget().setSelectedIndex(-1);
/* init */

statsPresenter.go(widget.getBoardPanel());
//fireAssignmentSelected(0);
/* boardPresenter.assignmentSelected(assignments[0]); */

serverTimePresenter.go(widget.getSidePanel());

container.add((Widget)widget);
eventBus.fireEvent(new StatusChangedEvent(StatusChangedEvent.StatusType.RESET, null));
}
Expand All @@ -126,6 +132,12 @@ public void clearEventHandlers() {
if (boardPresenter != null) {
boardPresenter.clearEventHandlers();
}
if (statsPresenter != null) {
statsPresenter.clearEventHandlers();
}
if (serverTimePresenter != null) {
serverTimePresenter.clearEventHandlers();
}
}

}
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();
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import java.util.HashMap;
import java.util.Iterator;
import java.util.Date;

import com.google.gwt.i18n.shared.DateTimeFormat;

import ro.pub.cs.vmchecker.client.event.AuthenticationEvent;
import ro.pub.cs.vmchecker.client.event.ServerTimeUpdateEvent;
import ro.pub.cs.vmchecker.client.event.ErrorDisplayEvent;
import ro.pub.cs.vmchecker.client.model.ErrorResponse;
import ro.pub.cs.vmchecker.client.service.ServiceError;
Expand All @@ -17,6 +21,7 @@
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.http.client.Header;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.LocaleInfo;
Expand Down Expand Up @@ -54,6 +59,17 @@ public Delegate(EventBus eventBus, String url, boolean isGet, boolean attachLoca

}

private Date parseDateFromResponse(Response response) {
Header[] headers = response.getHeaders();
for (Header header : headers) {
if (header.getName() == "Date") {
DateTimeFormat dtf = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.RFC_2822);
return dtf.parse(header.getValue());
}
}
return new Date(0);
}

private String packParameters(HashMap<String, String> params) {
StringBuffer result = new StringBuffer();
for (Iterator<String> it = params.keySet().iterator(); it.hasNext(); ) {
Expand Down Expand Up @@ -97,6 +113,7 @@ public void onResponseReceived(Request request, Response response) {
} else {
T result = decoder.getResult();
callback.onSuccess(result);
eventBus.fireEvent(new ServerTimeUpdateEvent(parseDateFromResponse(response)));
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import ro.pub.cs.vmchecker.client.presenter.AssignmentPresenter;

Expand All @@ -31,6 +32,9 @@ interface AssignmentWidgetUiBinder extends
@UiField
SimplePanel boardPanel;

@UiField
VerticalPanel sidePanel;

@UiField
Anchor viewStatsButton;

Expand All @@ -49,6 +53,11 @@ public HasWidgets getMenuPanel() {
return menuPanel;
}

@Override
public HasWidgets getSidePanel() {
return sidePanel;
}

@Override
public HasClickHandlers getViewStatsButton() {
return viewStatsButton;
Expand Down
Loading

0 comments on commit 693cac4

Please sign in to comment.