Example usage for com.google.gwt.json.client JSONValue isNumber

List of usage examples for com.google.gwt.json.client JSONValue isNumber

Introduction

In this page you can find the example usage for com.google.gwt.json.client JSONValue isNumber.

Prototype

public JSONNumber isNumber() 

Source Link

Document

Returns a non-null reference if this JSONValue is really a JSONNumber.

Usage

From source file:tsd.client.QueryUi.java

License:Open Source License

private void refreshGraph() {
    final Date start = start_datebox.getValue();
    if (start == null) {
        graphstatus.setText("Please specify a start time.");
        return;/*  w ww.java  2 s .  c  o  m*/
    }
    final Date end = end_datebox.getValue();
    if (end != null && !autoreload.getValue()) {
        if (end.getTime() <= start.getTime()) {
            end_datebox.addStyleName("dateBoxFormatError");
            graphstatus.setText("End time must be after start time!");
            return;
        }
    }
    final StringBuilder url = new StringBuilder();
    url.append("/q?start=");
    final String start_text = start_datebox.getTextBox().getText();
    if (start_text.endsWith(" ago") || start_text.endsWith("-ago")) {
        url.append(start_text);
    } else {
        url.append(FULLDATE.format(start));
    }
    if (end != null && !autoreload.getValue()) {
        url.append("&end=");
        final String end_text = end_datebox.getTextBox().getText();
        if (end_text.endsWith(" ago") || end_text.endsWith("-ago")) {
            url.append(end_text);
        } else {
            url.append(FULLDATE.format(end));
        }
    } else {
        // If there's no end-time, the graph may change while the URL remains
        // the same.  No browser seems to re-fetch an image once it's been
        // fetched, even if we destroy the DOM object and re-created it with the
        // same src attribute.  This has nothing to do with caching headers sent
        // by the server.  The browsers simply won't retrieve the same URL again
        // through JavaScript manipulations, period.  So as a workaround, we add
        // a special parameter that the server will delete from the query.
        url.append("&ignore=" + nrequests++);
    }
    if (!addAllMetrics(url)) {
        return;
    }
    addLabels(url);
    addFormats(url);
    addYRanges(url);
    addLogscales(url);
    if (nokey.getValue()) {
        url.append("&nokey");
    } else if (!keypos.isEmpty() || horizontalkey.getValue()) {
        url.append("&key=");
        if (!keypos.isEmpty()) {
            url.append(keypos);
        }
        if (horizontalkey.getValue()) {
            url.append(" horiz");
        }
        if (keybox.getValue()) {
            url.append(" box");
        }
    }
    url.append("&wxh=").append(wxh.getText());
    if (smooth.getValue()) {
        url.append("&smooth=csplines");
    }
    final String unencodedUri = url.toString();
    final String uri = URL.encode(unencodedUri);
    if (uri.equals(lastgraphuri)) {
        return; // Don't re-request the same graph.
    } else if (pending_requests++ > 0) {
        return;
    }
    lastgraphuri = uri;
    graphstatus.setText("Loading graph...");
    asyncGetJson(uri + "&json", new GotJsonCallback() {
        public void got(final JSONValue json) {
            if (autoreoload_timer != null) {
                autoreoload_timer.cancel();
                autoreoload_timer = null;
            }
            final JSONObject result = json.isObject();
            final JSONValue err = result.get("err");
            String msg = "";
            if (err != null) {
                displayError("An error occurred while generating the graph: " + err.isString().stringValue());
                graphstatus.setText("Please correct the error above.");
            } else {
                clearError();

                String history = unencodedUri.substring(3) // Remove "/q?".
                        .replaceFirst("ignore=[^&]*&", ""); // Unnecessary cruft.
                if (autoreload.getValue()) {
                    history += "&autoreload=" + autoreoload_interval.getText();
                }
                if (!history.equals(URL.decode(History.getToken()))) {
                    History.newItem(history, false);
                }

                final JSONValue nplotted = result.get("plotted");
                final JSONValue cachehit = result.get("cachehit");
                if (cachehit != null) {
                    msg += "Cache hit (" + cachehit.isString().stringValue() + "). ";
                }
                if (nplotted != null && nplotted.isNumber().doubleValue() > 0) {
                    graph.setUrl(uri + "&png");
                    graph.setVisible(true);

                    msg += result.get("points").isNumber() + " points retrieved, " + nplotted
                            + " points plotted";
                } else {
                    graph.setVisible(false);
                    msg += "Your query didn't return anything";
                }
                final JSONValue timing = result.get("timing");
                if (timing != null) {
                    msg += " in " + timing + "ms.";
                } else {
                    msg += '.';
                }
            }
            final JSONValue info = result.get("info");
            if (info != null) {
                if (!msg.isEmpty()) {
                    msg += ' ';
                }
                msg += info.isString().stringValue();
            }
            graphstatus.setText(msg);
            if (result.get("etags") != null) {
                final JSONArray etags = result.get("etags").isArray();
                final int netags = etags.size();
                for (int i = 0; i < netags; i++) {
                    if (i >= metrics.getWidgetCount()) {
                        break;
                    }
                    final Widget widget = metrics.getWidget(i);
                    if (!(widget instanceof MetricForm)) {
                        break;
                    }
                    final MetricForm metric = (MetricForm) widget;
                    final JSONArray tags = etags.get(i).isArray();
                    // Skip if no tags were associated with the query.
                    if (null != tags) {
                        for (int j = 0; j < tags.size(); j++) {
                            metric.autoSuggestTag(tags.get(j).isString().stringValue());
                        }
                    }
                }
            }
            if (autoreload.getValue()) {
                final int reload_in = Integer.parseInt(autoreoload_interval.getValue());
                if (reload_in >= 5) {
                    autoreoload_timer = new Timer() {
                        public void run() {
                            // Verify that we still want auto reload and that the graph
                            // hasn't been updated in the mean time.
                            if (autoreload.getValue() && lastgraphuri == uri) {
                                // Force refreshGraph to believe that we want a new graph.
                                lastgraphuri = "";
                                refreshGraph();
                            }
                        }
                    };
                    autoreoload_timer.schedule(reload_in * 1000);
                }
            }
            if (--pending_requests > 0) {
                pending_requests = 0;
                refreshGraph();
            }
        }
    });
}