Example usage for com.google.gwt.canvas.dom.client Context2d fillText

List of usage examples for com.google.gwt.canvas.dom.client Context2d fillText

Introduction

In this page you can find the example usage for com.google.gwt.canvas.dom.client Context2d fillText.

Prototype

public final native void fillText(String text, double x, double y, double maxWidth) ;

Source Link

Document

Draws filled text squeezed into the given max width.

Usage

From source file:gov.nist.spectrumbrowser.client.SensorDataStream.java

License:Open Source License

@Override
public void onMessage(String msg) {
    // TODO : Make this configurable.
    int nSpectrums = 200; // # of spectrums to show in the spectrogram window.
    double yScale = 0;
    try {//from  www .ja  va2 s  .  co  m
        if (state == STATUS_MESSAGE_NOT_SEEN) {
            JSONValue statusMessage = JSONParser.parseLenient(msg);
            JSONObject jsonObj = statusMessage.isObject();
            if (jsonObj.get("status").isString().stringValue().equals("NO_DATA")) {

                Window.alert("NO Data Available");
                websocket.close();
                spectrumBrowserShowDatasets.draw();
            } else if (jsonObj.get("status").isString().stringValue().equals("OK")) {
                state = STATUS_MESSAGE_SEEN;
            }
        } else if (state == STATUS_MESSAGE_SEEN) {

            dataMessage = JSONParser.parseLenient(msg);
            logger.finer("msg = " + msg);
            JSONObject mpar = dataMessage.isObject().get("mPar").isObject();
            nFrequencyBins = (int) mpar.get("n").isNumber().doubleValue();
            // The default cutoff value (add 2 to the noise floor).
            cutoff = round(dataMessage.isObject().get("cutoff").isNumber().doubleValue());

            if (cutoff < 0)
                cutoffTextBox.setText(Integer.toString((int) (cutoff - 0.5)));
            else
                cutoffTextBox.setText(Integer.toString((int) (cutoff + 0.5)));

            logger.finer("n = " + nFrequencyBins);
            minFreqHz = (long) mpar.get("fStart").isNumber().doubleValue();
            maxFreqHz = (long) mpar.get("fStop").isNumber().doubleValue();
            minFreq = (mpar.get("fStart").isNumber().doubleValue() / 1E6);
            maxFreq = mpar.get("fStop").isNumber().doubleValue() / 1E6;
            sys2detect = dataMessage.isObject().get("Sys2Detect").isString().stringValue();

            // For computing the occupancy, determine the cutoff.

            logger.finer("fStart / fStop = " + Double.toString(minFreq) + " " + Double.toString(maxFreq));
            Context2d ctx = frequencyValuesCanvas.getContext2d();
            ctx.setTextAlign(TextAlign.LEFT);
            ctx.fillText(Double.toString(maxFreq), 0, 10, 100);
            ctx.fillText("Freq (MHz)", 0, SpectrumBrowser.SPEC_HEIGHT / 2 - 4, 100);
            ctx.fillText(Double.toString(minFreq), 0, SpectrumBrowser.SPEC_HEIGHT - 4, 100);
            spectrogramFragment = Canvas.createIfSupported();
            spectrogramFragment.setWidth(SpectrumBrowser.SPEC_WIDTH + "px");
            spectrogramFragment.setHeight(SpectrumBrowser.SPEC_HEIGHT + "px");
            spectrogramFragment.setCoordinateSpaceWidth(SpectrumBrowser.SPEC_WIDTH);
            spectrogramFragment.setCoordinateSpaceHeight(SpectrumBrowser.SPEC_HEIGHT);
            spectrogramFragment.getCanvasElement().setWidth(SpectrumBrowser.SPEC_WIDTH);
            spectrogramFragment.getCanvasElement().setHeight(SpectrumBrowser.SPEC_HEIGHT);
            state = DATA_MESSAGE_SEEN;
            context2d.setFillStyle(CssColor.make("black"));
            context2d.fillRect(0, 0, SpectrumBrowser.SPEC_WIDTH, SpectrumBrowser.SPEC_HEIGHT);
            spectrogramFragment.setVisible(false);
            double timePerMeasurement = (float) mpar.get("tm").isNumber().doubleValue();
            timeResolution = (float) (dataMessage.isObject().get("_spectrumsPerFrame").isNumber().doubleValue()
                    * timePerMeasurement);
            HTML html = new HTML("<h2>Sensor Data Stream for " + sensorId + "</h2>");
            titlePanel.add(html);
            HTML help = new HTML("<p>Click on spectrogram to freeze/unfreze. "
                    + "Click on occupancy point to show spectrum</p>");
            titlePanel.add(help);
            String filter = dataMessage.isObject().get(Defines.STREAMING_FILTER).isString().stringValue();
            float freqResolution = round((float) (maxFreq - minFreq) / nFrequencyBins * 1000);
            html = new HTML("<h3>Resolution Bandwidth = " + freqResolution + " kHz.; Detected System = "
                    + sys2detect + "; Time resoultion = " + AbstractSpectrumBrowserScreen.round2(timeResolution)
                    + " s. Aggregation Filter = " + filter + " </h3>");
            titlePanel.add(html);
        } else if (state == DATA_MESSAGE_SEEN) {
            String[] values = msg.split(",");
            int powerValues[] = new int[values.length];

            int occupancyCount = 0;
            for (int i = 0; i < values.length; i++) {
                powerValues[i] = Integer.parseInt(values[i].trim());
                if (powerValues[i] > cutoff) {
                    occupancyCount++;
                }
            }

            float occupancy = round(((double) occupancyCount / (double) values.length) * 100);
            double xScale = ((double) SpectrumBrowser.SPEC_WIDTH / (double) nSpectrums);

            if (chartApiLoaded && occupancyDataTable == null) {
                occupancyDataTable = DataTable.create();
                spectrumDataTable = DataTable.create();
                occupancyPlotOptions = ScatterChartOptions.create();
                occupancyPlotOptions.setBackgroundColor("#f0f0f0");
                occupancyPlotOptions.setPointSize(5);
                occupancyPlotOptions.setHAxis(HAxis.create("Time (sec)"));
                VAxis vaxis = VAxis.create("Occupancy %");
                vaxis.setMaxValue(100.0);
                vaxis.setMinValue(0);
                occupancyPlotOptions.setVAxis(vaxis);
                Legend legend = Legend.create();
                legend.setPosition(LegendPosition.NONE);
                occupancyPlotOptions.setLegend(legend);
                spectrumPlotOptions = ScatterChartOptions.create();
                spectrumPlotOptions.setBackgroundColor("#f0f0f0");
                spectrumPlotOptions.setPointSize(5);
                spectrumPlotOptions.setHAxis(HAxis.create("Frequency (MHz)"));
                spectrumPlotOptions.setVAxis(VAxis.create("Power (dBm)"));

                legend = Legend.create();
                legend.setPosition(LegendPosition.NONE);
                spectrumPlotOptions.setLegend(legend);
                occupancyPlot = new ScatterChart();

                spectrumPlot = new ScatterChart();
                occupancyPlot.addSelectHandler(new SelectHandler() {

                    @Override
                    public void onSelect(SelectEvent event) {

                        if (!isFrozen) {
                            logger.finer("Please Freeze canvas before clicking");
                            return;
                        } else {
                            double minPower = 10000;
                            double maxPower = -10000;
                            for (int i = 0; i < powerValuesList.size(); i++) {
                                int[] data = powerValuesList.get(i);
                                for (int j = 0; j < data.length; j++) {
                                    if (data[j] < minPower) {
                                        minPower = data[j];
                                    }
                                    if (data[j] > maxPower) {
                                        maxPower = data[j];

                                    }
                                }
                            }
                            JsArray<Selection> selection = occupancyPlot.getSelection();

                            int row = selection.get(0).getRow();
                            logger.finer("Selected row" + row);
                            int[] spectrumData = powerValuesList.get(row);
                            double mhzPerDivision = (maxFreq - minFreq) / spectrumData.length;
                            for (int i = 0; i < spectrumData.length; i++) {
                                double freq = minFreq + mhzPerDivision * i;
                                spectrumDataTable.setCell(i, 0, freq, freq + " Mhz");
                                spectrumDataTable.setCell(i, 1, spectrumData[i], spectrumData[i] + " dBm");
                            }

                            HAxis haxis = HAxis.create("Freq. MHz.");
                            haxis.setMinValue(minFreq);
                            haxis.setMaxValue(maxFreq);

                            VAxis vaxis = VAxis.create("Power (dBm)");
                            vaxis.setMinValue(minPower);
                            vaxis.setMaxValue(maxPower);

                            spectrumPlotOptions.setVAxis(vaxis);
                            spectrumPlotOptions.setHAxis(haxis);

                            spectrumPlot.draw(spectrumDataTable, spectrumPlotOptions);
                        }

                    }
                });

                occupancyPlot.setPixelSize(SpectrumBrowser.SPEC_WIDTH + 260, SpectrumBrowser.SPEC_HEIGHT);
                occupancyPlot.setTitle("Occupancy");
                spectrumPlot.setPixelSize(SpectrumBrowser.SPEC_WIDTH + 260, SpectrumBrowser.SPEC_HEIGHT);
                occupancyPanel.add(occupancyPlot);
                spectrumPanel.add(spectrumPlot);
                occupancyDataTable.addColumn(ColumnType.NUMBER, "Time (sec)");
                occupancyDataTable.addColumn(ColumnType.NUMBER, "Occupancy %");
                spectrumDataTable.addColumn(ColumnType.NUMBER, "Frequency (MHz)");
                spectrumDataTable.addColumn(ColumnType.NUMBER, "Power (milliwatts)");
                spectrumDataTable.setColumnLabel(0, "Frequency (MHz)");
                spectrumDataTable.setColumnLabel(1, "Power (mw)");
                occupancyDataTable.addRows(nSpectrums);
                spectrumDataTable.addRows(powerValues.length);

                DataView dataView = DataView.create(occupancyDataTable);

                for (int i = 0; i < nSpectrums; i++) {
                    occupancyDataTable.setCell(i, 0, round3(i * timeResolution),
                            round3(i * timeResolution) + " sec");
                    occupancyDataTable.setCell(i, 1, 0, "0 % occupancy");
                    occupancyPlot.draw(dataView, occupancyPlotOptions);

                }
                // Initialize the spectrum list
                for (int i = 0; i < nSpectrums; i++) {
                    int[] dummyValues = new int[values.length];
                    for (int j = 0; j < dummyValues.length; j++) {
                        dummyValues[j] = 0;
                    }
                    powerValuesList.add(dummyValues);
                }
                counter = nSpectrums - 1;
            }

            if (!isFrozen) {

                if (occupancyDataTable != null) {
                    occupancyDataTable.removeRow(0);
                    occupancyDataTable.addRow();
                    int rowCount = occupancyDataTable.getNumberOfRows();
                    counter++;
                    for (int i = 0; i < nSpectrums; i++) {
                        occupancyDataTable.setCell(i, 0, round3(i * timeResolution),
                                round3(i * timeResolution) + " sec");
                    }
                    occupancyDataTable.setCell(rowCount - 1, 1, occupancy, occupancy + " % occupancy");
                    occupancyPlot.redraw();
                    powerValuesList.remove(0);
                    powerValuesList.add(powerValues);
                    // occupancyPlot.draw(dataTable);
                }

                context2d.save();
                Context2d tempContext = spectrogramFragment.getContext2d();
                tempContext.drawImage(spectrogramCanvas.getCanvasElement(), 0, 0,
                        (double) SpectrumBrowser.SPEC_WIDTH, (double) SpectrumBrowser.SPEC_HEIGHT);
                RootPanel.get().add(spectrogramFragment);

                // nSpectrums = powerValues.length / nFrequencyBins;
                yScale = (double) SpectrumBrowser.SPEC_HEIGHT / (double) nFrequencyBins;
                for (int i = 0; i < powerValues.length; i++) {
                    CssColor color = colorMap.getColor(powerValues[i]);
                    int row = (int) ((i % nFrequencyBins) * yScale);
                    int col = (int) ((i / nFrequencyBins) * xScale);

                    context2d.setFillStyle(color);
                    double x = SpectrumBrowser.SPEC_WIDTH - col - xScale;
                    double y = SpectrumBrowser.SPEC_HEIGHT - row - yScale;
                    double w = xScale;
                    double h = yScale;
                    context2d.fillRect(x, y, w, h);

                }

                context2d.translate(-xScale, 0);
                context2d.drawImage(spectrogramFragment.getCanvasElement(), 0, 0,
                        spectrogramFragment.getCanvasElement().getWidth(),
                        spectrogramFragment.getCanvasElement().getHeight(), 0, 0, SpectrumBrowser.SPEC_WIDTH,
                        SpectrumBrowser.SPEC_HEIGHT);
                // reset the transformation matrix
                context2d.setTransform(1, 0, 0, 1, 0, 0);
                RootPanel.get().remove(spectrogramFragment);
            }
        }
    } catch (Throwable ex) {
        logger.log(Level.SEVERE, "ERROR parsing data ", ex);
    }

}