Commit 50a76407 authored by Clemens Krug's avatar Clemens Krug
Browse files

Enhancements for the Plotting View

Rework the plotting view to lay out the charts nicer.
Instead of scaling the charts down till they are not readable anymore, a minimum height is maintained and scrollbars are added instead.
parent 5e0e5f62
......@@ -20,44 +20,41 @@
package de.tud.kom.p2psim.impl.topology.views.visualization.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import com.google.common.collect.Maps;
/**
* JFrame which holds all the live plots.
*
* @author Unknown
*
* 21.03.2017 Clemens Krug:
* Changed this class up quite a bit because when one would
* add more and more metrics to the visualisation, they would get unreadable small. Reworked this
* class so the charts will allways fill the view if theres is space, but if you add do many plots
* they will stay add a minimum height of 250 px and a scrollbar will be added instead. Also added a little
* JavaDoc.
*/
public class PlottingView extends JFrame {
private static final int VIEW_WIDTH = 900;
private static final int VIEW_HEIGHT = 800;
private static final int PLOT_HEIGHT = 120;
private static final int PLOT_HEIGHT_MIN = 250;
private static final Color PLOT_BACKGROUND_COLOR = Color.WHITE;
private String name;
private Map<String, XYChart> nameToPlotMap = Maps.newLinkedHashMap();
private JPanel content;
private JPanel selectorPanel;
private Box plotBox;
private GridBagConstraints layoutConstraints;
private JPanel plotBox;
private JScrollPane spane;
public PlottingView(String name) {
this.name = name;
setTitle(name);
setVisible(true);
......@@ -65,26 +62,16 @@ public class PlottingView extends JFrame {
}
private void buildUI() {
content = new JPanel();
content.setLayout(new BorderLayout());
selectorPanel = new JPanel();
selectorPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
layoutConstraints = new GridBagConstraints();
layoutConstraints.anchor = GridBagConstraints.NORTH;
layoutConstraints.weighty = 1;
plotBox = Box.createVerticalBox();
plotBox = new PlotBox();
plotBox.setLayout(new BoxLayout(plotBox, BoxLayout.Y_AXIS));
plotBox.setBackground(PLOT_BACKGROUND_COLOR);
plotBox.setBorder(BorderFactory.createEtchedBorder());
content.add(selectorPanel, BorderLayout.NORTH);
content.add(plotBox, BorderLayout.CENTER);
add(content, BorderLayout.CENTER);
spane = new JScrollPane(plotBox);
add(spane);
setSize(VIEW_WIDTH, VIEW_HEIGHT);
setPreferredSize(new Dimension(VIEW_WIDTH, VIEW_HEIGHT));
}
public XYChart createPlot(String name, String seriesName) {
......@@ -95,7 +82,8 @@ public class PlottingView extends JFrame {
chartPanel = wrapInCollabsable(chartPanel, name);
plotBox.add(chartPanel);
plotBox.validate();
plotBox.repaint();
spane.validate();
nameToPlotMap.put(name, plot);
......@@ -140,9 +128,12 @@ public class PlottingView extends JFrame {
if (collapseButton.getText().equals("-")) {
collapseButton.setText("+");
panel.remove(innerPanel);
panel.setMinimumSize(header.getPreferredSize());
panel.repaint();
} else {
collapseButton.setText("-");
panel.add(innerPanel, BorderLayout.CENTER);
panel.setMinimumSize(new Dimension(850,PLOT_HEIGHT_MIN));
}
}
});
......@@ -158,6 +149,7 @@ public class PlottingView extends JFrame {
panel.add(header, BorderLayout.NORTH);
panel.add(innerPanel, BorderLayout.CENTER);
panel.setMinimumSize(new Dimension(850,PLOT_HEIGHT_MIN));
return panel;
}
......@@ -165,7 +157,65 @@ public class PlottingView extends JFrame {
public void removeAllPlots() {
nameToPlotMap.clear();
plotBox.removeAll();
content.doLayout();
content.validate();
plotBox.validate();
}
/**
* Custom JPanel to implement the desired scrolling policy. Basically
* there won't be a scrollbar if all of the components have a greater height than
* {@link #PLOT_HEIGHT_MIN}. However, if you add so many components that they can't
* fit in while maintaining that minimum height, a scrollbar will be added.
*/
private class PlotBox extends JPanel implements Scrollable
{
private boolean enableScroll = false;
private Dimension preferredSize = new Dimension(VIEW_WIDTH, 800);
@Override
public Component add(Component comp)
{
Component retComp = super.add(comp);
preferredSize.height = getComponentCount() * PLOT_HEIGHT_MIN;
checkScrollNeeded();
return retComp;
}
/**
* Check if scrolling should be enabled and to so if it should.
*/
private void checkScrollNeeded()
{
enableScroll = getComponentCount() * PLOT_HEIGHT_MIN > getParent().getHeight();
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 50;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return PLOT_HEIGHT_MIN;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return true;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return !enableScroll;
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment