6748745: JConsole: plotters don't resize well when the window is resized

Part of the fix was contributed by jfdenise

Reviewed-by: jfdenise
This commit is contained in:
Daniel Fuchs 2008-09-17 13:40:40 +02:00
parent 3dd5cb3c26
commit b56f92d23b
4 changed files with 44 additions and 35 deletions

View File

@ -30,18 +30,15 @@ import java.awt.event.*;
import java.beans.*; import java.beans.*;
import java.io.*; import java.io.*;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.text.*;
import java.util.*; import java.util.*;
import javax.accessibility.*; import javax.accessibility.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.*; import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*; import javax.swing.filechooser.*;
import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileFilter;
import com.sun.tools.jconsole.JConsoleContext; import com.sun.tools.jconsole.JConsoleContext;
import com.sun.tools.jconsole.JConsoleContext.ConnectionState;
import static com.sun.tools.jconsole.JConsoleContext.ConnectionState.*; import static com.sun.tools.jconsole.JConsoleContext.ConnectionState.*;
@ -130,6 +127,7 @@ public class Plotter extends JComponent
private int bottomMargin = 45; private int bottomMargin = 45;
private int leftMargin = 65; private int leftMargin = 65;
private int rightMargin = 70; private int rightMargin = 70;
private final boolean displayLegend;
public Plotter() { public Plotter() {
this(Unit.NONE, 0); this(Unit.NONE, 0);
@ -139,15 +137,21 @@ public class Plotter extends JComponent
this(unit, 0); this(unit, 0);
} }
public Plotter(Unit unit, int decimals) {
this(unit,decimals,true);
}
// Note: If decimals > 0 then values must be decimally shifted left // Note: If decimals > 0 then values must be decimally shifted left
// that many places, i.e. multiplied by Math.pow(10.0, decimals). // that many places, i.e. multiplied by Math.pow(10.0, decimals).
public Plotter(Unit unit, int decimals) { public Plotter(Unit unit, int decimals, boolean displayLegend) {
this.displayLegend = displayLegend;
setUnit(unit); setUnit(unit);
setDecimals(decimals); setDecimals(decimals);
enableEvents(AWTEvent.MOUSE_EVENT_MASK); enableEvents(AWTEvent.MOUSE_EVENT_MASK);
addMouseListener(new MouseAdapter() { addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
if (getParent() instanceof PlotterPanel) { if (getParent() instanceof PlotterPanel) {
getParent().requestFocusInWindow(); getParent().requestFocusInWindow();
@ -240,6 +244,7 @@ public class Plotter extends JComponent
} }
} }
@Override
public JPopupMenu getComponentPopupMenu() { public JPopupMenu getComponentPopupMenu() {
if (popupMenu == null) { if (popupMenu == null) {
popupMenu = new JPopupMenu(Resources.getText("Chart:")); popupMenu = new JPopupMenu(Resources.getText("Chart:"));
@ -330,6 +335,7 @@ public class Plotter extends JComponent
} }
} }
@Override
public void paintComponent(Graphics g) { public void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
@ -670,7 +676,7 @@ public class Plotter extends JComponent
curValue += "%"; curValue += "%";
} }
int valWidth = fm.stringWidth(curValue); int valWidth = fm.stringWidth(curValue);
String legend = seq.name; String legend = (displayLegend?seq.name:"");
int legendWidth = fm.stringWidth(legend); int legendWidth = fm.stringWidth(legend);
if (checkRightMargin(valWidth) || checkRightMargin(legendWidth)) { if (checkRightMargin(valWidth) || checkRightMargin(legendWidth)) {
// Wait for next repaint // Wait for next repaint
@ -986,10 +992,12 @@ public class Plotter extends JComponent
} }
private static class SaveDataFileChooser extends JFileChooser { private static class SaveDataFileChooser extends JFileChooser {
private static final long serialVersionUID = -5182890922369369669L;
SaveDataFileChooser() { SaveDataFileChooser() {
setFileFilter(new FileNameExtensionFilter("CSV file", "csv")); setFileFilter(new FileNameExtensionFilter("CSV file", "csv"));
} }
@Override
public void approveSelection() { public void approveSelection() {
File file = getSelectedFile(); File file = getSelectedFile();
if (file != null) { if (file != null) {
@ -1034,6 +1042,7 @@ public class Plotter extends JComponent
} }
} }
@Override
public AccessibleContext getAccessibleContext() { public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) { if (accessibleContext == null) {
accessibleContext = new AccessiblePlotter(); accessibleContext = new AccessiblePlotter();
@ -1042,10 +1051,12 @@ public class Plotter extends JComponent
} }
protected class AccessiblePlotter extends AccessibleJComponent { protected class AccessiblePlotter extends AccessibleJComponent {
private static final long serialVersionUID = -3847205410473510922L;
protected AccessiblePlotter() { protected AccessiblePlotter() {
setAccessibleName(getText("Plotter.accessibleName")); setAccessibleName(getText("Plotter.accessibleName"));
} }
@Override
public String getAccessibleName() { public String getAccessibleName() {
String name = super.getAccessibleName(); String name = super.getAccessibleName();
@ -1076,6 +1087,7 @@ public class Plotter extends JComponent
return name; return name;
} }
@Override
public AccessibleRole getAccessibleRole() { public AccessibleRole getAccessibleRole() {
return AccessibleRole.CANVAS; return AccessibleRole.CANVAS;
} }

View File

@ -872,8 +872,8 @@ public class XMBeanAttributes extends XTable {
MaximizedCellRenderer(Component comp) { MaximizedCellRenderer(Component comp) {
this.comp = comp; this.comp = comp;
Dimension d = comp.getPreferredSize(); Dimension d = comp.getPreferredSize();
if (d.getHeight() > 200) { if (d.getHeight() > 220) {
comp.setPreferredSize(new Dimension((int) d.getWidth(), 200)); comp.setPreferredSize(new Dimension((int) d.getWidth(), 220));
} }
} }
@Override @Override

View File

@ -34,7 +34,7 @@ public class XPlotter extends Plotter {
JTable table; JTable table;
public XPlotter(JTable table, public XPlotter(JTable table,
Plotter.Unit unit) { Plotter.Unit unit) {
super(unit); super(unit,0,false);
this.table = table; this.table = table;
} }
@Override @Override

View File

@ -27,14 +27,10 @@ package sun.tools.jconsole.inspector;
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.*;
import java.util.*; import java.util.*;
import java.util.Timer; import java.util.Timer;
import javax.management.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import sun.tools.jconsole.*; import sun.tools.jconsole.*;
@ -127,6 +123,7 @@ public class XPlottingViewer extends PlotterPanel implements ActionListener {
setBackground(g.getColor()); setBackground(g.getColor());
plotter.paintComponent(g); plotter.paintComponent(g);
}*/ }*/
@Override
public void actionPerformed(ActionEvent evt) { public void actionPerformed(ActionEvent evt) {
plotterCache.remove(key); plotterCache.remove(key);
Timer t = timerCache.remove(key); Timer t = timerCache.remove(key);
@ -141,9 +138,11 @@ public class XPlottingViewer extends PlotterPanel implements ActionListener {
JTable table) { JTable table) {
final Plotter plotter = new XPlotter(table, Plotter.Unit.NONE) { final Plotter plotter = new XPlotter(table, Plotter.Unit.NONE) {
Dimension prefSize = new Dimension(400, 170); Dimension prefSize = new Dimension(400, 170);
@Override
public Dimension getPreferredSize() { public Dimension getPreferredSize() {
return prefSize; return prefSize;
} }
@Override
public Dimension getMinimumSize() { public Dimension getMinimumSize() {
return prefSize; return prefSize;
} }
@ -183,42 +182,40 @@ public class XPlottingViewer extends PlotterPanel implements ActionListener {
return plotter; return plotter;
} }
//Create Plotter display
private void setupDisplay(Plotter plotter) { private void setupDisplay(Plotter plotter) {
//setLayout(new GridLayout(2,0)); final JPanel buttonPanel = new JPanel();
GridBagLayout gbl = new GridBagLayout(); final GridBagLayout gbl = new GridBagLayout();
setLayout(gbl); buttonPanel.setLayout(gbl);
setLayout(new BorderLayout());
plotButton = new JButton(Resources.getText("Discard chart")); plotButton = new JButton(Resources.getText("Discard chart"));
plotButton.addActionListener(this); plotButton.addActionListener(this);
plotButton.setEnabled(true); plotButton.setEnabled(true);
// Add the display to the top four cells
GridBagConstraints buttonConstraints = new GridBagConstraints(); GridBagConstraints buttonConstraints = new GridBagConstraints();
buttonConstraints.gridx = 0; buttonConstraints.gridx = 0;
buttonConstraints.gridy = 0; buttonConstraints.gridy = 0;
buttonConstraints.fill = GridBagConstraints.VERTICAL; buttonConstraints.fill = GridBagConstraints.VERTICAL;
buttonConstraints.anchor = GridBagConstraints.CENTER; buttonConstraints.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(plotButton, buttonConstraints); gbl.setConstraints(plotButton, buttonConstraints);
add(plotButton); buttonPanel.add(plotButton);
GridBagConstraints plotterConstraints = new GridBagConstraints();
plotterConstraints.gridx = 0;
plotterConstraints.gridy = 1;
plotterConstraints.weightx = 1;
//plotterConstraints.gridwidth = (int) plotter.getPreferredSize().getWidth();
//plotterConstraints.gridheight = (int) plotter.getPreferredSize().getHeight();
plotterConstraints.fill = GridBagConstraints.VERTICAL;
gbl.setConstraints(plotter, plotterConstraints);
//bordered = new JPanel();
//bordered.setPreferredSize(new Dimension(400, 250));
//bordered.add(plotButton);
//bordered.add(plotter);
//add(bordered);
if (attributeName != null && attributeName.length()!=0) {
final JPanel plotterLabelPanel = new JPanel();
final JLabel label = new JLabel(attributeName);
final GridBagLayout gbl2 = new GridBagLayout();
plotterLabelPanel.setLayout(gbl2);
final GridBagConstraints labelConstraints = new GridBagConstraints();
labelConstraints.gridx = 0;
labelConstraints.gridy = 0;
labelConstraints.fill = GridBagConstraints.VERTICAL;
labelConstraints.anchor = GridBagConstraints.CENTER;
labelConstraints.ipady = 10;
gbl2.setConstraints(label, labelConstraints);
plotterLabelPanel.add(label);
add(plotterLabelPanel, BorderLayout.NORTH);
}
setPlotter(plotter); setPlotter(plotter);
add(buttonPanel, BorderLayout.SOUTH);
repaint(); repaint();
} }