Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package cdc.perfs.ui.swing;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.List;
import java.util.function.Function;
import javax.swing.JComponent;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JToolTip;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import cdc.perfs.api.Source;
import cdc.perfs.core.Context;
import cdc.perfs.core.Environment;
import cdc.perfs.core.EnvironmentKind;
import cdc.perfs.core.Measure;
import cdc.perfs.core.MeasureStatus;
import cdc.perfs.core.Position;
import cdc.perfs.core.SpanPosition;
import cdc.perfs.ui.PerfsChartHelper;
import cdc.perfs.ui.PerfsChartHelper.ChangeListener;
import cdc.perfs.ui.PerfsChartHelper.PickedData;
import cdc.perfs.ui.RefreshRate;
import cdc.perfs.ui.Rendering;
import cdc.perfs.ui.SourceDisplay;
import cdc.util.time.RefTime;
import cdc.util.time.TimeMeasureMode;
import cdc.util.time.TimeUnit;
/**
* Panel dedicated to graphical display of measures.
*
* @author Damien Carbonne
*
*/
final class ChartPanel extends JPanel {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LogManager.getLogger(ChartPanel.class);
final transient DragParams dragParams = new DragParams();
final Timer timer;
private RefreshRate refreshRate = null;
private Rendering rendering = null;
private boolean displayStatsEnabled = false;
boolean drawBordersEnabled = false;
boolean mouseOver = false;
private JToolTip wToolTip = null;
/**
* Action that is triggered on timer events for refresh.
*/
private final transient ActionListener timerHandler;
final ControlledChartPanel wControl;
final ContextsTableModel contextsModel;
final SourcesTableModel sourcesModel;
final transient Helper helper;
public ChartPanel(ControlledChartPanel control,
ContextsTableModel contextsModel,
SourcesTableModel sourcesModel) {
super();
this.helper = new Helper(contextsModel.getEnvironment().getKind(),
sourcesModel::getDisplay);
this.timerHandler = e -> {
helper.computeParameters();
repaint();
if (mouseOver) {
refreshToolTip(ChartPanel.this, e.getWhen());
}
};
this.wControl = control;
this.contextsModel = contextsModel;
this.sourcesModel = sourcesModel;
// Do this to receive key events
setFocusable(true);
if (contextsModel.getEnvironment().getKind() == EnvironmentKind.RUNTIME) {
this.timer = new Timer(20, this.timerHandler);
} else {
this.timer = null;
this.contextsModel.addTableModelListener(event -> repaintIfSnapshot());
this.sourcesModel.addTableModelListener(event -> repaintIfSnapshot());
}
addMouseWheelListener(e -> {
if (e.getWheelRotation() < 0) {
wControl.zoomIn();
} else {
wControl.zoomOut();
}
});
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
dragParams.enabled = helper.isFocusLocked() && (e.getButton() == MouseEvent.BUTTON1);
dragParams.x0 = e.getX();
dragParams.px = dragParams.x0;
if (e.isPopupTrigger()) {
showPopup(e.getComponent(),
e.getX(),
e.getY());
}
}
@Override
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
showPopup(e.getComponent(),
e.getX(),
e.getY());
}
}
@Override
public void mouseEntered(MouseEvent e) {
mouseOver = true;
// Do this to receive key events
requestFocusInWindow();
}
@Override
public void mouseExited(MouseEvent e) {
mouseOver = false;
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
if (dragParams.enabled) {
final int dx = e.getX() - dragParams.px;
dragParams.px = e.getX();
setFocusNanos(helper.getFocusNanos() + helper.deltaXToDeltaTime(-dx));
}
}
});
addHierarchyListener(e -> {
if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) {
if (isShowing()) {
repaintIfSnapshot();
if (timer != null) {
timer.restart();
}
} else {
if (timer != null) {
timer.stop();
}
}
}
});
addAncestorListener(new AncestorListener() {
@Override
public void ancestorRemoved(AncestorEvent event) {
// Ignore
}
@Override
public void ancestorMoved(AncestorEvent event) {
// Called when chart is scrolled
repaintIfSnapshot();
}
@Override
public void ancestorAdded(AncestorEvent event) {
// Ignore
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == ' ' && contextsModel.getEnvironment().getKind() == EnvironmentKind.RUNTIME) {
wControl.setFocusLocked(!isFocusLocked());
}
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
wControl.less();
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
wControl.more();
} else if (e.getKeyCode() == KeyEvent.VK_PLUS || e.getKeyCode() == KeyEvent.VK_ADD || e.getKeyChar() == '+') {
wControl.zoomIn();
} else if (e.getKeyCode() == KeyEvent.VK_LESS || e.getKeyCode() == KeyEvent.VK_SUBTRACT || e.getKeyChar() == '-') {
wControl.zoomOut();
}
}
@Override
public void keyReleased(KeyEvent e) {
wControl.stop();
}
});
setRefreshRate(RefreshRate.LOW);
setRendering(Rendering.FASTEST);
setPreferredSize(new Dimension(100, 1000));
if (timer != null) {
timer.start();
} else {
repaintIfSnapshot();
}
setToolTipText("TODO");
}
@Override
public JToolTip createToolTip() {
wToolTip = super.createToolTip();
return wToolTip;
}
@Override
public String getToolTipText(MouseEvent event) {
final PickedData data = helper.pick(event.getX(), event.getY());
return PerfsChartHelper.getToolTipTextHtml(data);
}
/**
* Blends a color with a fixed color (black).
*
* For a Runtime environment, result equals input color.
* For Snapshot environment, result is different.
*
* @param kind The environment kind.
* @param c The color to blend.
* @return The blended color.
*/
static Color blend(EnvironmentKind kind,
Color c) {
if (kind == EnvironmentKind.RUNTIME) {
return c;
} else {
final double w0 = 0.9;
final double w1 = 1.0 - w0;
final double r = w0 * c.getRed() + w1 * 0.0;
final double g = w0 * c.getGreen() + w1 * 0.0;
final double b = w0 * c.getBlue() + w1 * 0.0;
return new Color((int) r, (int) g, (int) b, 255);
}
}
private class Helper extends PerfsChartHelper {
private final Function