forked from jasder/antlr
fix various issues in TreeViewer
1) Issue: The tree "jumps" from a larger scale to a much smaller scale when starting dragging the scale slider. Cause: Initially the slider was not correctly positioned. It always assumed a scale of 1. Solution: I now calculate the initial slider position using the current scale (was 1.5). --- 2) Issue: NullPointerException when the scale slider is dragged to the very left. Cause: The formula "v / 1000.0 + 1.0" results in a scale of 0.0 when using -1000 (left position). A scale of 0 is not supported. Solution: in setScale I now check for "<= 0.0". In the case I use scale 1. In addition I changed the lower bound of the slider to -999, i.e. the left position represents the scale "0.001". --- 3) Issue: when scaling the scrollbars are not added/updated/removed Cause: when scaling the preferred size of the TreeViewer must be updated as the ScrollPane refers to the preferredSize for scrolling. Solution: now calling "updatePreferredSize" in setScale. This will set the preferred size to the "scaled tree size". It also ensures parents will be notified and the view is repainted. --- 4) Issue: only an upper left part of the tree is painted. Cause: the TreeViewer overwrote the getWidth/Height and returned unchanged values (800x600). Only this area in painted. If the preferred size exceeded this size the remaining area was not painted. Solution: Removed the overwrite of getWidth/Height. As one consequence the dialog will start at maximum size for large tree layouts. [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9481]
This commit is contained in:
parent
9a1a8428b5
commit
4e30119ab6
|
@ -102,9 +102,6 @@ public class TreeViewer extends JComponent {
|
||||||
protected int nodeHeightPadding = 0; // added above/below
|
protected int nodeHeightPadding = 0; // added above/below
|
||||||
protected int arcSize = 0; // make an arc in node outline?
|
protected int arcSize = 0; // make an arc in node outline?
|
||||||
|
|
||||||
protected int width = 800;
|
|
||||||
protected int height = 600;
|
|
||||||
|
|
||||||
protected double scale = 1.0;
|
protected double scale = 1.0;
|
||||||
|
|
||||||
protected Color boxColor = null; // set to a color to make it draw background
|
protected Color boxColor = null; // set to a color to make it draw background
|
||||||
|
@ -125,11 +122,19 @@ public class TreeViewer extends JComponent {
|
||||||
new DefaultConfiguration<Tree>(gapBetweenLevels,
|
new DefaultConfiguration<Tree>(gapBetweenLevels,
|
||||||
gapBetweenNodes),
|
gapBetweenNodes),
|
||||||
useIdentity);
|
useIdentity);
|
||||||
Dimension size = treeLayout.getBounds().getBounds().getSize();
|
updatePreferredSize();
|
||||||
setPreferredSize(size);
|
|
||||||
setFont(font);
|
setFont(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updatePreferredSize() {
|
||||||
|
setPreferredSize(getScaledTreeSize());
|
||||||
|
invalidate();
|
||||||
|
if (getParent() != null) {
|
||||||
|
getParent().validate();
|
||||||
|
}
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
// ---------------- PAINT -----------------------------------------------
|
// ---------------- PAINT -----------------------------------------------
|
||||||
|
|
||||||
protected void paintEdges(Graphics g, Tree parent) {
|
protected void paintEdges(Graphics g, Tree parent) {
|
||||||
|
@ -237,8 +242,7 @@ public class TreeViewer extends JComponent {
|
||||||
|
|
||||||
// Wrap viewer in scroll pane
|
// Wrap viewer in scroll pane
|
||||||
JScrollPane scrollPane = new JScrollPane(viewer);
|
JScrollPane scrollPane = new JScrollPane(viewer);
|
||||||
// Make it tree size up to width/height of viewer
|
// Make the scrollpane (containing the viewer) the center component
|
||||||
viewer.setPreferredSizeToScaledTree();
|
|
||||||
contentPane.add(scrollPane, BorderLayout.CENTER);
|
contentPane.add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
// Add button to bottom
|
// Add button to bottom
|
||||||
|
@ -259,8 +263,9 @@ public class TreeViewer extends JComponent {
|
||||||
bottomPanel.add(wrapper, BorderLayout.SOUTH);
|
bottomPanel.add(wrapper, BorderLayout.SOUTH);
|
||||||
|
|
||||||
// Add scale slider
|
// Add scale slider
|
||||||
|
int sliderValue = (int) ((viewer.getScale()-1.0) * 1000);
|
||||||
final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL,
|
final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL,
|
||||||
-1000,1000,0);
|
-999,1000,sliderValue);
|
||||||
scaleSlider.addChangeListener(
|
scaleSlider.addChangeListener(
|
||||||
new ChangeListener() {
|
new ChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -278,18 +283,12 @@ public class TreeViewer extends JComponent {
|
||||||
dialog.setVisible(true);
|
dialog.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setPreferredSizeToScaledTree() {
|
private Dimension getScaledTreeSize() {
|
||||||
Dimension scaledTreeSize =
|
Dimension scaledTreeSize =
|
||||||
treeLayout.getBounds().getBounds().getSize();
|
treeLayout.getBounds().getBounds().getSize();
|
||||||
scaledTreeSize = new Dimension((int)(scaledTreeSize.width*scale),
|
scaledTreeSize = new Dimension((int)(scaledTreeSize.width*scale),
|
||||||
(int)(scaledTreeSize.height*scale));
|
(int)(scaledTreeSize.height*scale));
|
||||||
if ( scaledTreeSize.width < width ) {
|
return scaledTreeSize;
|
||||||
setWidth(scaledTreeSize.width);
|
|
||||||
}
|
|
||||||
if ( scaledTreeSize.height < height ) {
|
|
||||||
setHeight(scaledTreeSize.height);
|
|
||||||
}
|
|
||||||
setPreferredSize(new Dimension(width, height));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void open() {
|
public void open() {
|
||||||
|
@ -435,25 +434,10 @@ public class TreeViewer extends JComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setScale(double scale) {
|
public void setScale(double scale) {
|
||||||
|
if(scale <= 0) {
|
||||||
|
scale = 1;
|
||||||
|
}
|
||||||
this.scale = scale;
|
this.scale = scale;
|
||||||
repaint();
|
updatePreferredSize();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getHeight() {
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setHeight(int height) {
|
|
||||||
this.height = height;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getWidth() {
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setWidth(int width) {
|
|
||||||
this.width = width;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue