Search in sources :

Example 1 with GBC_FULL

use of com.android.tools.adtui.common.AdtUiUtils.GBC_FULL in project android by JetBrains.

the class LineChartVisualTest method populateUi.

@Override
protected void populateUi(@NotNull JPanel panel) {
    JPanel layered = new JPanel(new GridBagLayout());
    JPanel controls = VisualTest.createControlledPane(panel, layered);
    mLineChart.setBorder(BorderFactory.createLineBorder(AdtUiUtils.DEFAULT_BORDER_COLOR));
    layered.add(myOverlayComponent, GBC_FULL);
    layered.add(mySelectionComponent, GBC_FULL);
    layered.add(mLineChart, GBC_FULL);
    final AtomicInteger variance = new AtomicInteger(10);
    final AtomicInteger delay = new AtomicInteger(100);
    Thread updateDataThread = new Thread() {

        @Override
        public void run() {
            super.run();
            try {
                while (true) {
                    int v = variance.get();
                    long nowUs = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
                    for (DefaultDataSeries<Long> series : mData) {
                        ImmutableList<SeriesData<Long>> data = series.getAllData();
                        long last = data.isEmpty() ? 0 : data.get(data.size() - 1).value;
                        float delta = ((float) Math.random() - 0.45f) * v;
                        // Make sure not to add negative numbers.
                        long current = Math.max(last + (long) delta, 0);
                        series.add(nowUs, current);
                    }
                    Thread.sleep(delay.get());
                }
            } catch (InterruptedException e) {
            }
        }
    };
    updateDataThread.start();
    controls.add(VisualTest.createVariableSlider("Delay", 10, 5000, new VisualTests.Value() {

        @Override
        public void set(int v) {
            delay.set(v);
        }

        @Override
        public int get() {
            return delay.get();
        }
    }));
    controls.add(VisualTest.createVariableSlider("Variance", 0, 50, new VisualTests.Value() {

        @Override
        public void set(int v) {
            variance.set(v);
        }

        @Override
        public int get() {
            return variance.get();
        }
    }));
    controls.add(VisualTest.createVariableSlider("Line width", 1, 10, new VisualTests.Value() {

        @Override
        public void set(int v) {
            Stroke stroke = new BasicStroke(v);
            for (int i = 0; i < mRangedData.size(); i += 2) {
                RangedContinuousSeries series = mRangedData.get(i);
                mLineChart.getLineConfig(series).setStroke(stroke);
            }
        }

        @Override
        public int get() {
            // Returns the stroke width of the first line, in case there is one, or a default (1) value
            RangedContinuousSeries firstSeries = mRangedData.get(0);
            Stroke firstLineStroke = mLineChart.getLineConfig(firstSeries).getStroke();
            return firstLineStroke instanceof BasicStroke ? (int) ((BasicStroke) firstLineStroke).getLineWidth() : 1;
        }
    }));
    controls.add(VisualTest.createCheckbox("Shift xRange Min", itemEvent -> mAnimatedTimeRange.setShift(itemEvent.getStateChange() == ItemEvent.SELECTED)));
    controls.add(VisualTest.createCheckbox("Stepped chart", itemEvent -> {
        boolean isStepped = itemEvent.getStateChange() == ItemEvent.SELECTED;
        for (int i = 0; i < mRangedData.size(); i += 2) {
            RangedContinuousSeries series = mRangedData.get(i);
            mLineChart.getLineConfig(series).setStepped(isStepped);
        }
    }));
    controls.add(VisualTest.createCheckbox("Dashed lines", itemEvent -> {
        Stroke stroke = itemEvent.getStateChange() == ItemEvent.SELECTED ? LineConfig.DEFAULT_DASH_STROKE : LineConfig.DEFAULT_LINE_STROKE;
        for (int i = 0; i < mRangedData.size(); i += 2) {
            RangedContinuousSeries series = mRangedData.get(i);
            mLineChart.getLineConfig(series).setStroke(stroke);
        }
    }));
    controls.add(VisualTest.createCheckbox("Filled lines", itemEvent -> {
        boolean isFilled = itemEvent.getStateChange() == ItemEvent.SELECTED;
        for (int i = 0; i < mRangedData.size(); i += 2) {
            RangedContinuousSeries series = mRangedData.get(i);
            mLineChart.getLineConfig(series).setFilled(isFilled);
        }
    }));
    controls.add(VisualTest.createCheckbox("Stacked lines", itemEvent -> {
        boolean isStacked = itemEvent.getStateChange() == ItemEvent.SELECTED;
        for (int i = 0; i < mRangedData.size(); i += 2) {
            RangedContinuousSeries series = mRangedData.get(i);
            mLineChart.getLineConfig(series).setStacked(isStacked);
        }
    }));
    JButton tapButton = VisualTest.createButton("Generate Duration1 (Hold)");
    tapButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            // Starts a new test event and give it a max duration.
            long nowUs = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
            DefaultDurationData newEvent = new DefaultDurationData(UNSPECIFIED_DURATION);
            mDurationData1.add(nowUs, newEvent);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // Wraps up the latest event by assigning it a duration value relative to where it was started.
            long nowUs = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
            ImmutableList<SeriesData<DefaultDurationData>> allEvents = mDurationData1.getAllData();
            SeriesData<DefaultDurationData> lastEvent = allEvents.get(allEvents.size() - 1);
            lastEvent.value.setDuration(nowUs - lastEvent.x);
        }
    });
    controls.add(tapButton);
    tapButton = VisualTest.createButton("Generate Duration2 (Hold)");
    tapButton.addMouseListener(new MouseAdapter() {

        @Override
        public void mousePressed(MouseEvent e) {
            // Starts a new test event and give it a max duration.
            long nowUs = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
            DefaultDurationData newEvent = new DefaultDurationData(UNSPECIFIED_DURATION);
            mDurationData2.add(nowUs, newEvent);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // Wraps up the latest event by assigning it a duration value relative to where it was started.
            long nowUs = TimeUnit.NANOSECONDS.toMicros(System.nanoTime());
            ImmutableList<SeriesData<DefaultDurationData>> allEvents = mDurationData2.getAllData();
            SeriesData<DefaultDurationData> lastEvent = allEvents.get(allEvents.size() - 1);
            lastEvent.value.setDuration(nowUs - lastEvent.x);
        }
    });
    controls.add(tapButton);
    controls.add(mClickDisplayLabel);
    controls.add(new Box.Filler(new Dimension(0, 0), new Dimension(300, Integer.MAX_VALUE), new Dimension(300, Integer.MAX_VALUE)));
}
Also used : ItemEvent(java.awt.event.ItemEvent) LineConfig(com.android.tools.adtui.chart.linechart.LineConfig) LineChart(com.android.tools.adtui.chart.linechart.LineChart) Animatable(com.android.tools.adtui.Animatable) AnimatedTimeRange(com.android.tools.adtui.AnimatedTimeRange) ImmutableList(com.intellij.util.containers.ImmutableList) SelectionComponent(com.android.tools.adtui.SelectionComponent) GBC_FULL(com.android.tools.adtui.common.AdtUiUtils.GBC_FULL) AdtUiUtils(com.android.tools.adtui.common.AdtUiUtils) MouseEvent(java.awt.event.MouseEvent) UNSPECIFIED_DURATION(com.android.tools.adtui.model.DurationData.UNSPECIFIED_DURATION) com.android.tools.adtui.model(com.android.tools.adtui.model) ArrayList(java.util.ArrayList) java.awt(java.awt) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) AnimatedComponent(com.android.tools.adtui.AnimatedComponent) DurationDataRenderer(com.android.tools.adtui.chart.linechart.DurationDataRenderer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MouseAdapter(java.awt.event.MouseAdapter) OverlayComponent(com.android.tools.adtui.chart.linechart.OverlayComponent) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) javax.swing(javax.swing) ImmutableList(com.intellij.util.containers.ImmutableList) MouseEvent(java.awt.event.MouseEvent) MouseAdapter(java.awt.event.MouseAdapter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Aggregations

Animatable (com.android.tools.adtui.Animatable)1 AnimatedComponent (com.android.tools.adtui.AnimatedComponent)1 AnimatedTimeRange (com.android.tools.adtui.AnimatedTimeRange)1 SelectionComponent (com.android.tools.adtui.SelectionComponent)1 DurationDataRenderer (com.android.tools.adtui.chart.linechart.DurationDataRenderer)1 LineChart (com.android.tools.adtui.chart.linechart.LineChart)1 LineConfig (com.android.tools.adtui.chart.linechart.LineConfig)1 OverlayComponent (com.android.tools.adtui.chart.linechart.OverlayComponent)1 AdtUiUtils (com.android.tools.adtui.common.AdtUiUtils)1 GBC_FULL (com.android.tools.adtui.common.AdtUiUtils.GBC_FULL)1 com.android.tools.adtui.model (com.android.tools.adtui.model)1 UNSPECIFIED_DURATION (com.android.tools.adtui.model.DurationData.UNSPECIFIED_DURATION)1 ImmutableList (com.intellij.util.containers.ImmutableList)1 java.awt (java.awt)1 ItemEvent (java.awt.event.ItemEvent)1 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1