Example usage for com.google.gwt.user.client Timer schedule

List of usage examples for com.google.gwt.user.client Timer schedule

Introduction

In this page you can find the example usage for com.google.gwt.user.client Timer schedule.

Prototype

public synchronized void schedule(int delayMs) 

Source Link

Document

Schedules a timer to elapse in the future.

Usage

From source file:com.google.appinventor.client.explorer.commands.WaitForBuildWebResultCommand.java

License:Open Source License

@Override
public void execute(final ProjectNode node) {
    final Ode ode = Ode.getInstance();
    messagesOutput.clear();/*from   w ww  .j av  a2 s.c  o  m*/
    messagesOutput.addMessages(MESSAGES.buildRequestedMessage(node.getName(), buildRequestTime));

    OdeAsyncCallback<RpcResult> callback = new OdeAsyncCallback<RpcResult>(
            // failure message
            MESSAGES.buildError()) {
        @Override
        public void onSuccess(RpcResult result) {
            messagesOutput.addMessages("Waiting for " + getElapsedMillis() / 1000 + " seconds.");
            messagesOutput.addMessages(result.getOutput());
            messagesOutput.addMessages(result.getError());
            Tracking.trackEvent(Tracking.PROJECT_EVENT, Tracking.PROJECT_SUBACTION_BUILD_YA, node.getName(),
                    getElapsedMillis());
            if (result.succeeded()) {
                ode.getTopToolbar().updateKeystoreFileMenuButtons();
                executeNextCommand(node);
            } else if (result.getResult() == 1) {
                // General build error
                String errorMsg = result.getError();
                // This is not an internal App Inventor bug. The error should be
                // reported in a yellow info box.
                ErrorReporter
                        .reportInfo(MESSAGES.buildFailedError() + (errorMsg.isEmpty() ? "" : " " + errorMsg));
                executionFailedOrCanceled();
            } else if (result.getResult() == 2) {
                // Yail generation error
                String formName = extractFormName(result);
                ErrorReporter.reportError(MESSAGES.errorGeneratingYail(formName));
                String blocksFileName = formName + YoungAndroidSourceAnalyzer.CODEBLOCKS_SOURCE_EXTENSION;
                YoungAndroidBlocksNode blocksNode = findBlocksNode((YoungAndroidProjectNode) node,
                        blocksFileName);
                if (blocksNode != null) {
                    showProblemBlocks(blocksNode);
                }
                executionFailedOrCanceled();
            } else {
                // Build isn't done yet
                Timer timer = new Timer() {
                    @Override
                    public void run() {
                        execute(node);
                    }
                };
                // TODO(user): Maybe do an exponential backoff here.
                timer.schedule(WAIT_INTERVAL_MILLIS);
            }
        }

        @Override
        public void onFailure(Throwable caught) {
            super.onFailure(caught);
            executionFailedOrCanceled();
        }
    };

    ode.getProjectService().getBuildWebResult(node.getProjectId(), target, callback);
}

From source file:com.google.appinventor.client.explorer.commands.WaitForEclipseProjectBuildResultCommand.java

License:Open Source License

@Override
public void execute(final ProjectNode node) {
    final Ode ode = Ode.getInstance();
    messagesOutput.clear();// w  w  w  .  jav  a  2 s. c o m
    messagesOutput.addMessages(MESSAGES.buildRequestedMessage(node.getName(), buildRequestTime));

    OdeAsyncCallback<RpcResult> callback = new OdeAsyncCallback<RpcResult>(
            // failure message
            MESSAGES.buildError()) {
        @Override
        public void onSuccess(RpcResult result) {
            messagesOutput.addMessages("Waiting for " + getElapsedMillis() / 1000 + " seconds.");
            messagesOutput.addMessages(result.getOutput());
            messagesOutput.addMessages(result.getError());
            Tracking.trackEvent(Tracking.PROJECT_EVENT, Tracking.PROJECT_SUBACTION_BUILD_YA, node.getName(),
                    getElapsedMillis());
            if (result.succeeded()) {
                executeNextCommand(node);
            } else if (result.getResult() == 3) {
                // General build error
                String errorMsg = result.getError();
                // This is not an internal App Inventor bug. The error should be
                // reported in a yellow info box.
                ErrorReporter
                        .reportInfo(MESSAGES.buildFailedError() + (errorMsg.isEmpty() ? "" : " " + errorMsg));
                executionFailedOrCanceled();
            } else {
                // Build isn't done yet
                Timer timer = new Timer() {
                    @Override
                    public void run() {
                        execute(node);
                    }
                };
                // TODO(user): Maybe do an exponential backoff here.
                timer.schedule(WAIT_INTERVAL_MILLIS);
            }
        }

        @Override
        public void onFailure(Throwable caught) {
            super.onFailure(caught);
            executionFailedOrCanceled();
        }
    };

    ode.getProjectService().getEclipseProjectBuildResult(node.getProjectId(), target, buildOption, callback);
}

From source file:com.google.appinventor.client.jsonp.JsonpConnection.java

License:Open Source License

/**
 * Sends a JSONP request by embedding a SCRIPT tag into the document and
 * then polls for the result by resending the request with the parameter
 * {@link JsonpConstants#POLLING}. We continue polling until we receive a
 * response that is not {@link JsonpConstants#NOT_FINISHED_YET}.
 *
 * @param request the request to be made
 * @param p the parameters for the request
 * @param function a function that transforms the response into the type
 *        that the callback needs// ww  w .  j a  v  a 2  s.  c  o m
 * @param callback the callback that should be called with the transformed
 *        response
 */
public <T> void sendJsonpRequestAndPoll(final String request, Map<String, Object> p,
        final Function<String, ? extends T> function, final AsyncCallback<T> callback) {
    // Make a new parameters Map so we can add the POLLING parameter below.
    final Map<String, Object> parameters = new HashMap<String, Object>();
    if (p != null) {
        parameters.putAll(p);
    }
    final String initialRequestId = getNextRequestId();

    AsyncCallback<String> pollingCallback = new AsyncCallback<String>() {
        @Override
        public void onSuccess(String response) {
            if (JsonpConstants.NOT_FINISHED_YET.equals(response)) {
                // No response is available yet, create a timer to try again in a second.
                parameters.put(JsonpConstants.POLLING, initialRequestId);

                final AsyncCallback<String> pollingCallback = this;
                Timer timer = new Timer() {
                    @Override
                    public void run() {
                        sendJsonpRequest(request, parameters, stringResponseDecoder, pollingCallback);
                    }
                };
                timer.schedule(1000); // schedule the timer
            } else {
                // The response is available, convert it to the correct type T.
                T result;
                try {
                    result = function.apply(response);
                } catch (RuntimeException e) {
                    callback.onFailure(e);
                    return;
                }
                callback.onSuccess(result);
            }
        }

        @Override
        public void onFailure(Throwable caught) {
            callback.onFailure(caught);
        }
    };

    sendJsonpRequest(initialRequestId, request, parameters, stringResponseDecoder, pollingCallback);
}

From source file:com.google.appinventor.client.youngandroid.CodeblocksManager.java

License:Open Source License

private void downloadJnlpAndMakeConnection() {
    // fire up codeblocks via JavaWebStart. The download command goes from
    // the client to the ODE Server, which responds with the JNLP file
    // that causes the browser to start codeblocks via JavaWebStart.
    // We append a hash of the userid to the file name because we need the
    // jnlp file to be user-specific, while still be cacheable.
    String userId = Ode.getInstance().getUser().getUserId();
    Downloader.getInstance().download(ServerLayout.WEBSTART_JNLP_SERVLET_BASE
            + ServerLayout.WEBSTART_JNLP_PURPOSE_CODEBLOCKS + "/" + userId.hashCode());

    final long[] timeToStopTryingToConnect = { System.currentTimeMillis() + MAX_TIME_TO_CONNECT_MILLIS };

    // Get the connection info for the codeblocks HTTP server.
    Timer timer = new Timer() {
        @Override//from  www  .  ja  va2  s .c o m
        public void run() {
            Ode.getInstance().getLaunchService().retrieveJsonpConnectionInfo(
                    YaHttpServerConstants.CODEBLOCKS_INFO_FILE_PREFIX,
                    new AsyncCallback<JsonpConnectionInfo>() {
                        @Override
                        public void onSuccess(JsonpConnectionInfo connInfo) {
                            if (connInfo == null) {
                                // The connection info has not been sent to the ODE server yet.
                                boolean continueConnecting = true;
                                if (System.currentTimeMillis() > timeToStopTryingToConnect[0]) {
                                    continueConnecting = askIfUserWantsToContinueTryingToConnect();
                                    if (continueConnecting) {
                                        timeToStopTryingToConnect[0] = System.currentTimeMillis()
                                                + MAX_TIME_TO_CONNECT_MILLIS;
                                    }
                                }
                                // Try again, unless the user has canceled.
                                if (startCanceled || !continueConnecting) {
                                    startingCodeblocks = false;
                                    updateCodeblocksButton();
                                } else {
                                    schedule(CODEBLOCKS_CONNECTION_RETRY_DELAY_MILLIS); // schedule the timer
                                }
                            } else {
                                conn = new CodeblocksConnection(connInfo, new ClientJsonParser(),
                                        Urls.getEscapeQueryParameterFunction());
                                conn.addConnectivityListener(CodeblocksManager.this);

                                startingCodeblocks = false;
                                updateCodeblocksButton();

                                // The user may have switched projects/forms while codeblocks was starting.
                                // We want the current project/form now.
                                YaFormEditor formEditor = Ode.getInstance().getCurrentYoungAndroidFormEditor();
                                if (formEditor != null) {
                                    YoungAndroidFormNode formNode = formEditor.getFormNode();
                                    loadPropertiesAndBlocks(formNode, null);
                                }
                            }
                        }

                        @Override
                        public void onFailure(Throwable caught) {
                            ErrorReporter.reportError(MESSAGES.startingCodeblocksFailed());
                            startingCodeblocks = false;
                            updateCodeblocksButton();
                        }
                    });
        }
    };

    // Schedule the timer for 1 second to give some time for the download to occur.
    timer.schedule(CODEBLOCKS_CONNECTION_RETRY_DELAY_MILLIS);
}

From source file:com.google.code.p.gwtchismes.client.GWTCPopupBox.java

License:Apache License

/**
 * Shows the alert dialog, and hides it after a period
 *  /*from  w  w w .  j  a v a2 s .c  om*/
 * @param timeout
 *            timeout in seconds for autohide           
 */
public void show(int timeout) {
    if (timeout > 0) {
        Timer t = new Timer() {
            public void run() {
                hide();
            }
        };
        t.schedule(timeout * 1000);
    }
    center();
}

From source file:com.google.code.p.gwtchismes.client.GWTCPrint.java

License:Apache License

/**
 * Sends the page to the printer/*  w ww  .  java 2 s . co m*/
 */
public void print() {
    setVisible(false);
    Window.print();
    Timer t = new Timer() {
        public void run() {
            setVisible(true);
        }
    };
    t.schedule(5 * 1000);
}

From source file:com.google.code.p.gwtchismes.client.GWTCWait.java

License:Apache License

/**
 *  Show the wait dialog for timeout seconds
 *  /*  w ww. j a v a2s .c o m*/
 * @param timeout seconds to wait before hide the dialog (timeout=0 wait for ever)
 */
public void show(int timeout) {
    if (timeout > 0) {
        Timer t = new Timer() {
            public void run() {
                hide();
            }
        };
        t.schedule(timeout * 1000);
    }
    setVisible(true);
    center();
}

From source file:com.google.gwt.examples.TimerExample.java

License:Apache License

public void onClick(ClickEvent event) {
    // Create a new timer that calls Window.alert().
    Timer t = new Timer() {
        @Override//  w  w  w  . j a va 2 s .  c  o  m
        public void run() {
            Window.alert("Nifty, eh?");
        }
    };

    // Schedule the timer to run once in 5 seconds.
    t.schedule(5000);
}

From source file:com.google.gwt.maps.sample.hellomaps.client.ControlsDemo.java

License:Apache License

public ControlsDemo() {
    map = new MapWidget(LatLng.newInstance(37.4419, -122.1419), 13);
    map.setSize("500px", "300px");
    initWidget(map);/*  w  w  w.ja  va  2s. c om*/
    map.addControl(new SmallMapControl());
    map.addControl(new MapTypeControl());

    Timer timer = new Timer() {

        @Override
        public void run() {
            // Exercise the minimum & maximum resolution entry points.
            MapType types[] = map.getMapTypes();
            for (MapType t : types) {
                int minResolution = t.getMinimumResolution();
                int maxResolution = t.getMaximumResolution();
                GWT.log("Map Type: " + t.getName(true) + " Min resolution: " + minResolution
                        + " Max Resolution: " + maxResolution, null);

                minResolution = t.getMinimumResolution();
                maxResolution = t.getMaximumResolution();
                GWT.log("@ point: " + map.getCenter().toString() + " Map Type: " + t.getName(true)
                        + " Min resolution: " + minResolution + " Max Resolution: " + maxResolution, null);
            }
        }
    };
    timer.schedule(1000);
}

From source file:com.google.gwt.maps.testing.client.maps.StreetViewSideBySideMapWidget.java

License:Apache License

/**
 * render the components in the widget//ww w .j a  va 2 s  .  co  m
 */
private void draw() {
    pWidget.clear();

    pWidget.add(new HTML("<br>Street View Demo - click on the map"));

    hp = new HorizontalPanel();
    pWidget.add(hp);

    drawMap();

    drawStreeView();

    // allow for things to setup, otherwise getting pano gets null
    Timer t = new Timer() {
        public void run() {
            setupStartingMarker();
        }
    };
    t.schedule(1500); // b/c this widget is first and I have so many loading on
                      // page.
}