Java tutorial
/******************************************************************************* * Copyright (c) 2014, 2015 Scott Clarke (scott@dawg6.com). * * This file is part of Dawg6's Demon Hunter DPS Calculator. * * Dawg6's Demon Hunter DPS Calculator is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Dawg6's Demon Hunter DPS Calculator is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ package com.dawg6.gwt.client.service; import com.dawg6.gwt.client.ApplicationPanel; import com.dawg6.gwt.client.ApplicationPanel.DialogBoxResultHandler; import com.dawg6.gwt.common.util.AsyncTask; import com.dawg6.gwt.common.util.AsyncTaskHandler; import com.dawg6.gwt.common.util.VersionBuilder; import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.http.client.Request; import com.google.gwt.http.client.RequestBuilder; import com.google.gwt.http.client.RequestCallback; import com.google.gwt.http.client.Response; import com.google.gwt.user.client.Command; import com.google.gwt.user.client.ui.DialogBox; public abstract class ServiceUtil { private final VersionBuilder CLIENT_VERSION; private final String versionServletUri; protected ServiceUtil(String version, String versionDate) { this(version, versionDate, "version?v2"); } protected ServiceUtil(String version, String versionDate, String versionServletUri) { this.CLIENT_VERSION = VersionBuilder.getVersion(version, versionDate); this.versionServletUri = versionServletUri; } protected void execute(final AsyncTask task) { execute(true, task); } private AsyncTaskHandler waitDialog = null; protected AsyncTaskHandler createWaitDialog(String title, String message) { return ApplicationPanel.showWaitDialogBox(title, message); } protected void execute(boolean showDialog, final AsyncTask task) { boolean createDlg = false; if ((waitDialog == null) && showDialog) { waitDialog = createWaitDialog("Please wait...", null); createDlg = true; } final AsyncTaskHandler handler = createDlg ? new AsyncTaskHandler() { @Override public void taskCompleted() { waitDialog.taskCompleted(); waitDialog = null; } } : AsyncTaskHandler.Default; checkVersion(new Handler<VersionCheck>() { @Override public void handleResult(VersionCheck result) { if (result.success) { task.run(handler); } else { handler.taskCompleted(); } } }); } public void checkVersion(final Handler<VersionCheck> handler) { versionCheck(new Handler<VersionCheck>() { @Override public void handleResult(VersionCheck result) { if (!result.success) { versionCheckFailed(result); } if (handler != null) handler.handleResult(result); } }); } private DialogBox reloadDialogBox = null; private void versionCheckFailed(VersionCheck result) { if (reloadDialogBox == null) { final DialogBoxResultHandler dialogHandler = new DialogBoxResultHandler() { @Override public void dialogBoxResult(int result) { reloadDialogBox = null; if (result == ApplicationPanel.OK) forceReload(); } }; String message; String title; if (result.serverVersion != null) { title = "Server Version Mismatch"; message = "New Version (" + result.serverVersion + ") available on Server."; } else if (result.exception != null) { title = "Error"; message = result.exception; } else { title = "Error"; message = "Unknown error communicating with server."; } message += "\nPress OK to force browser refresh."; reloadDialogBox = ApplicationPanel.showDialogBox(title, message, ApplicationPanel.OK | ApplicationPanel.CANCEL, dialogHandler); } else { reloadDialogBox.center(); reloadDialogBox.hide(); reloadDialogBox.show(); } } private static class VersionCheck { public boolean success; public String serverVersion; public String exception; } private interface Handler<T> { void handleResult(T result); } public void versionCheck(final Handler<VersionCheck> handler) { Scheduler.get().scheduleDeferred(new Command() { @Override public void execute() { RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, GWT.getHostPageBaseURL() + versionServletUri); // GWT.log("Version URL = " + builder.getUrl()); final VersionCheck result = new VersionCheck(); try { builder.sendRequest(null, new RequestCallback() { @Override public void onResponseReceived(Request request, Response response) { String text = response.getText(); if (text != null) { if (text.startsWith(VersionBuilder.PREFIX)) { result.serverVersion = text.substring(VersionBuilder.PREFIX.length()); if (text.startsWith(CLIENT_VERSION.getPrefixString())) { result.success = true; } } } if ((!result.success) && (result.serverVersion == null)) { result.exception = "Unable to obtain Server version."; } if (handler != null) handler.handleResult(result); } @Override public void onError(Request request, Throwable exception) { result.success = false; result.exception = "Error communicating with server."; if (handler != null) handler.handleResult(result); } }); } catch (Exception e) { result.success = false; result.exception = "Error communicating with server."; if (handler != null) handler.handleResult(result); } } }); } public static native void forceReload() /*-{ $wnd.location.reload(true); }-*/; }