Mercurial > hg > thermostat-ng > agent
changeset 2453:fe43ee562095
Add ThermostatChartPanelBuilder.
Reviewed-by: aazores
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/020886.html
author | Severin Gehwolf <sgehwolf@redhat.com> |
---|---|
date | Wed, 14 Sep 2016 15:32:59 +0200 |
parents | bfccdcb426d0 |
children | 69dee5f97497 |
files | client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanel.java client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelBuilder.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelBuilderTest.java client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelTest.java |
diffstat | 4 files changed, 251 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanel.java Thu Sep 15 14:06:36 2016 +0200 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanel.java Wed Sep 14 15:32:59 2016 +0200 @@ -61,6 +61,7 @@ import org.jfree.ui.RectangleEdge; import com.redhat.thermostat.client.swing.components.ValueField; +import com.redhat.thermostat.client.swing.components.experimental.RecentTimeControlPanel.UnitRange; import com.redhat.thermostat.client.ui.SampledDataset; import com.redhat.thermostat.common.Duration; @@ -82,33 +83,28 @@ private static final int MINIMUM_DRAW_SIZE = 100; - private JPanel chartsPanel; - private List<ChartPanel> charts; + private final JPanel chartsPanel; + private final List<ChartPanel> charts; + private final RecentTimeControlPanel recentTimeControlPanel; + private final boolean isFixedXYPlotAutoScale; + private final Duration initialDuration; - private RecentTimeControlPanel recentTimeControlPanel; private JTextComponent label; - - private Duration initialDuration; - private ChartPanel chartPanel; private Crosshair xCrosshair; - - public ThermostatChartPanel(JFreeChart chart, Duration initialDuration) { - this(initialDuration); - - addChart(chart); - } - - public ThermostatChartPanel(Duration initialDuration) { + + // Used by ThermostatChartPanelBuilder + ThermostatChartPanel(JFreeChart chart, Duration initialDuration, boolean isFixedXYPlotAutoScale, UnitRange unitRange) { + this.isFixedXYPlotAutoScale = isFixedXYPlotAutoScale; this.initialDuration = initialDuration; this.chartsPanel = new JPanel(); this.charts = new ArrayList<>(); - + chartsPanel.setLayout(new BoxLayout(chartsPanel, BoxLayout.Y_AXIS)); - + this.setLayout(new BorderLayout()); - - recentTimeControlPanel = new RecentTimeControlPanel(initialDuration); + + recentTimeControlPanel = new RecentTimeControlPanel(initialDuration, unitRange); recentTimeControlPanel.addPropertyChangeListener(RecentTimeControlPanel.PROPERTY_VISIBLE_TIME_RANGE, new PropertyChangeListener() { @Override public void propertyChange(final PropertyChangeEvent evt) { @@ -116,24 +112,36 @@ ThermostatChartPanel.this.firePropertyChange(RecentTimeControlPanel.PROPERTY_VISIBLE_TIME_RANGE, null, d); } }); - + add(chartsPanel, BorderLayout.CENTER); add(recentTimeControlPanel, BorderLayout.SOUTH); + + if (chart != null) { + addChart(chart); + } + revalidate(); + } - revalidate(); + public ThermostatChartPanel(JFreeChart chart, Duration initialDuration) { + this(chart, initialDuration, true, UnitRange.DEFAULT); + } + + public ThermostatChartPanel(Duration initialDuration) { + this(null, initialDuration, true, UnitRange.DEFAULT); } public void addChart(JFreeChart chart) { // jfreechart still generates tooltips when disabled, prevent generation as well if (chart.getPlot() instanceof XYPlot) { chart.getXYPlot().getRenderer().setBaseToolTipGenerator(null); + chart.getXYPlot().getRangeAxis().setAutoRange(true); + + chart.getXYPlot().getDomainAxis().setAutoRange(true); + if (isFixedXYPlotAutoScale) { + chart.getXYPlot().getDomainAxis().setFixedAutoRange(initialDuration.asMilliseconds()); + } } - chart.getXYPlot().getRangeAxis().setAutoRange(true); - - chart.getXYPlot().getDomainAxis().setAutoRange(true); - chart.getXYPlot().getDomainAxis().setFixedAutoRange(initialDuration.asMilliseconds()); - chart.getPlot().setBackgroundPaint(WHITE); chart.getPlot().setBackgroundImageAlpha(TRANSPARENT); chart.getPlot().setOutlinePaint(BLACK); @@ -163,12 +171,16 @@ } public void setTimeRangeToShow(Duration duration) { + // only suitable for XY Plots with fixed auto range + if (!isFixedXYPlotAutoScale) { + return; + } for (ChartPanel cp : charts) { - XYPlot plot = cp.getChart().getXYPlot(); + XYPlot xyPlot = cp.getChart().getXYPlot(); // Don't drop old data; just dont' show it. - plot.getDomainAxis().setAutoRange(true); - plot.getDomainAxis().setFixedAutoRange(duration.asMilliseconds()); + xyPlot.getDomainAxis().setAutoRange(true); + xyPlot.getDomainAxis().setFixedAutoRange(duration.asMilliseconds()); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelBuilder.java Wed Sep 14 15:32:59 2016 +0200 @@ -0,0 +1,121 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat 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 2, or (at your + * option) any later version. + * + * Thermostat 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 Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.components.experimental; + +import java.util.Objects; + +import org.jfree.chart.JFreeChart; + +import com.redhat.thermostat.client.swing.components.experimental.RecentTimeControlPanel.UnitRange; +import com.redhat.thermostat.common.Duration; + +/** + * A builder for {@code ThermostatChartPanel}. + * + */ +public class ThermostatChartPanelBuilder { + + // XY-Plot is fixed auto-range by default + private boolean fixedRange = true; + private Duration duration = ThermostatChartPanel.DEFAULT_DATA_DISPLAY; + private JFreeChart chart; + private UnitRange unitRange = UnitRange.DEFAULT; + + /** + * Sets the initial duration for {@code ThermostatChartPanel}. Default is + * 10 minutes. + * + * @param duration The initial duration to set + * @return this builder. + * + * @throws NullPointerException if {@code duration} was null. + */ + public ThermostatChartPanelBuilder duration(Duration duration) { + this.duration = Objects.requireNonNull(duration); + return this; + } + + /** + * Sets the chart for {@code ThermostatChartPanel}. Defaults to + * {@code null}. + * + * @param chart The chart to set + * @return this builder. + * + * @throws NullPointerException if {@code chart} was null. + */ + public ThermostatChartPanelBuilder chart(JFreeChart chart) { + this.chart = Objects.requireNonNull(chart); + return this; + } + + /** + * Sets whether {@code ThermostatChartPanel} should have fixed auto ranges. + * Only applicable for {@link XYPlot} charts. + * + * @param fixedRange the new value + * @return this builder + * @see #chart + */ + public ThermostatChartPanelBuilder xyPlotFixedAutoRange(boolean fixedRange) { + this.fixedRange = fixedRange; + return this; + } + + /** + * Sets the unit range for the {@code ThermostatChartPanel}. Default is + * coarse. + * + * @param range The unit range to set. + * @return this builder + * + * @throws NullPointerException if {@code range} was null. + */ + public ThermostatChartPanelBuilder unitRange(UnitRange range) { + this.unitRange = Objects.requireNonNull(range); + return this; + } + + /** + * + * @return A configured {@code ThermostatChartPanel}. + */ + public ThermostatChartPanel build() { + return new ThermostatChartPanel(chart, duration, fixedRange, unitRange); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelBuilderTest.java Wed Sep 14 15:32:59 2016 +0200 @@ -0,0 +1,65 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat 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 2, or (at your + * option) any later version. + * + * Thermostat 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 Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.components.experimental; + +import org.junit.Before; +import org.junit.Test; + +public class ThermostatChartPanelBuilderTest { + + private ThermostatChartPanelBuilder builder; + + @Before + public void setup() { + builder = new ThermostatChartPanelBuilder(); + } + + @Test(expected = NullPointerException.class) + public void nullDurationNotAccepted() { + builder.duration(null); + } + + @Test(expected = NullPointerException.class) + public void nullChartNotAccepted() { + builder.chart(null); + } + + @Test(expected = NullPointerException.class) + public void nullUnitRangeNotAccepted() { + builder.unitRange(null); + } +}
--- a/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelTest.java Thu Sep 15 14:06:36 2016 +0200 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/components/experimental/ThermostatChartPanelTest.java Wed Sep 14 15:32:59 2016 +0200 @@ -52,6 +52,9 @@ import org.fest.swing.fixture.FrameFixture; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.category.CategoryDataset; +import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.data.time.TimeSeriesCollection; import org.junit.After; import org.junit.Before; @@ -61,6 +64,7 @@ import org.junit.runner.RunWith; import com.redhat.thermostat.annotations.internal.CacioTest; +import com.redhat.thermostat.client.swing.components.experimental.RecentTimeControlPanel.UnitRange; import com.redhat.thermostat.common.Duration; import net.java.openjdk.cacio.ctc.junit.CacioFESTRunner; @@ -166,6 +170,27 @@ thermostatChartPanel.setTimeRangeToShow(time); assertEquals(time.asMilliseconds(), (long) chart.getXYPlot().getDomainAxis().getRange().getLength()); } + + @GUITest + @Test + public void testTimeRangeToShowNoExceptionsNonXYPlot() { + CategoryDataset defDataSet = new DefaultCategoryDataset(); + final JFreeChart myChart = ChartFactory.createBarChart( + null, + "Time Label", + "Value Label", + defDataSet, + PlotOrientation.VERTICAL, false, false, false); + final ThermostatChartPanel[] panel = new ThermostatChartPanel[1]; + GuiActionRunner.execute(new GuiTask() { + @Override + protected void executeInEDT() throws Throwable { + panel[0] = new ThermostatChartPanel(myChart, ThermostatChartPanel.DEFAULT_DATA_DISPLAY, false, UnitRange.COARSE); + } + }); + Duration time = new Duration(32, TimeUnit.MINUTES); + panel[0].setTimeRangeToShow(time); // must not throw exceptions + } @GUITest @Test