cdc.perfs.ui.swing.ControlledChartPanel Maven / Gradle / Ivy
package cdc.perfs.ui.swing;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.Timer;
import cdc.perfs.Environment;
import cdc.perfs.EnvironmentKind;
import cdc.perfs.ui.PerfsChartHelper;
import cdc.perfs.ui.RefreshRate;
import cdc.perfs.ui.Rendering;
import cdc.util.lang.Checks;
/**
* Panel dedicated to graphical display of measures and its controls.
*
* @author Damien Carbonne
*
*/
public final class ControlledChartPanel extends JPanel {
private static final long serialVersionUID = 1L;
protected final JButton wMoveToBegin = new JButton("|<");
protected final JButton wLess = new JButton("<");
protected final JButton wMore = new JButton(">");
protected final JButton wMoveToEnd = new JButton(">|");
private final JCheckBox wTimeLocked;
protected final JSlider wScale = new JSlider();
private final JScrollPane wScrollPane = new JScrollPane();
protected final ChartPanel wChart;
private enum TimeChangeMode {
DEC,
INC,
NONE
}
protected TimeChangeMode timeChangeMode = TimeChangeMode.NONE;
protected long timeChangeInit;
private final transient ActionListener timeChangeHandler;
protected final Timer timer;
private final transient PerfsChartHelper.ChangeListener changeHandler = source -> refresh();
public ControlledChartPanel(ContextsTableModel contextsModel) {
super();
final GridBagLayout gbl = new GridBagLayout();
gbl.columnWeights = new double[] { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 };
gbl.rowWeights = new double[] { 1.0, 0.0 };
setLayout(gbl);
// ScrollPane
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 6;
gbc.insets = new Insets(0, 0, 5, 0);
gbc.gridx = 0;
gbc.gridy = 0;
add(wScrollPane, gbc);
}
// Chart
wChart = new ChartPanel(contextsModel);
wScrollPane.setViewportView(wChart);
wChart.addChangeListener(this.changeHandler);
timeChangeHandler = e -> {
final long nanos = System.nanoTime() - timeChangeInit;
switch (timeChangeMode) {
case DEC:
wChart.decrementFocus(nanos);
break;
case INC:
wChart.incrementFocus(nanos);
break;
default:
break;
}
};
this.timer = new Timer(10, this.timeChangeHandler);
this.timer.setInitialDelay(0);
// Move to begin
wMoveToBegin.setEnabled(wChart.isFocusLocked());
wMoveToBegin.addActionListener(event -> wChart.setFocusNanos(0.0));
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 0;
gbc.gridy = 1;
add(wMoveToBegin, gbc);
}
// Less
wLess.setEnabled(wChart.isFocusLocked());
wLess.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
timeChangeMode = TimeChangeMode.DEC;
timeChangeInit = System.nanoTime();
timer.start();
}
@Override
public void mouseReleased(MouseEvent e) {
timer.stop();
timeChangeMode = TimeChangeMode.NONE;
}
});
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 1;
gbc.gridy = 1;
add(wLess, gbc);
}
// Time lock
wTimeLocked = new JCheckBox("lock");
if (getEnvironment().getKind() == EnvironmentKind.SNAPSHOT) {
wTimeLocked.setEnabled(false);
wTimeLocked.setSelected(true);
wChart.setFocusLocked(true);
wMoveToBegin.setEnabled(wChart.isFocusLocked());
wLess.setEnabled(wChart.isFocusLocked());
wMore.setEnabled(wChart.isFocusLocked());
wMoveToEnd.setEnabled(wChart.isFocusLocked());
} else {
wTimeLocked.addActionListener(event -> {
final JCheckBox source = (JCheckBox) event.getSource();
wChart.setFocusLocked(source.isSelected());
wMoveToBegin.setEnabled(wChart.isFocusLocked());
wLess.setEnabled(wChart.isFocusLocked());
wMore.setEnabled(wChart.isFocusLocked());
wMoveToEnd.setEnabled(wChart.isFocusLocked());
});
}
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_END;
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 2;
gbc.gridy = 1;
add(wTimeLocked, gbc);
}
// Scale
wScale.setMajorTickSpacing(PerfsChartHelper.SCALE_MAX / 5);
wScale.setMinorTickSpacing(PerfsChartHelper.SCALE_MAX / 10);
wScale.setPaintTicks(true);
wScale.setMinimum(0);
wScale.setMaximum(PerfsChartHelper.SCALE_MAX);
wScale.setValue(PerfsChartHelper.SCALE_MAX / 2);
final Dimension d = wScale.getPreferredSize();
wScale.setPreferredSize(new Dimension(100, d.height));
wScale.setMinimumSize(wScale.getPreferredSize());
wScale.addChangeListener(event -> {
final JSlider w = (JSlider) event.getSource();
final double scale = PerfsChartHelper.sliderToScale(w.getValue());
wChart.setScale(scale);
});
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 3;
gbc.gridy = 1;
add(wScale, gbc);
}
// More
wMore.setEnabled(wChart.isFocusLocked());
wMore.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
timeChangeMode = TimeChangeMode.INC;
timeChangeInit = System.nanoTime();
timer.start();
}
@Override
public void mouseReleased(MouseEvent e) {
timer.stop();
timeChangeMode = TimeChangeMode.NONE;
}
});
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 4;
gbc.gridy = 1;
add(wMore, gbc);
}
// Move to end
wMoveToEnd.setEnabled(wChart.isFocusLocked());
wMoveToEnd.addActionListener(event -> wChart.setFocusNanos(wChart.getEnvironment().getElapsedNanos()));
{
final GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 0, 5);
gbc.gridx = 5;
gbc.gridy = 1;
add(wMoveToEnd, gbc);
}
setScale(PerfsChartHelper.sliderToScale(wScale.getValue()));
refresh();
}
public Environment getEnvironment() {
return wChart.getEnvironment();
}
public void setRefreshRate(RefreshRate rate) {
Checks.isNotNull(rate, "rate");
wChart.setRefreshRate(rate);
}
public RefreshRate getRefreshRate() {
return wChart.getRefreshRate();
}
public void setRendering(Rendering rendering) {
Checks.isNotNull(rendering, "rendering");
wChart.setRendering(rendering);
}
public Rendering getRendering() {
return wChart.getRendering();
}
public void setDisplayStatsEnabled(boolean enabled) {
wChart.setDisplayStatsEnabled(enabled);
}
public boolean getDisplayStatsEnabled() {
return wChart.getDisplayStatsEnabled();
}
public void setDrawBordersEnabled(boolean enabled) {
wChart.setDrawBordersEnabled(enabled);
}
public boolean getDrawBordersEnabled() {
return wChart.getDrawBordersEnabled();
}
public void setScale(double scale) {
wChart.setScale(scale);
}
public double getScale() {
return wChart.getScale();
}
public void setFocusNanos(double time) {
wChart.setFocusNanos(time);
}
public double getFocusNanos() {
return wChart.getFocusNanos();
}
protected void refresh() {
wScale.setValue((int) PerfsChartHelper.scaleToSlider(wChart.getScale()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy