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:
Udo Borkowski 2011-11-29 07:40:09 -08:00
parent 9a1a8428b5
commit 4e30119ab6
1 changed files with 19 additions and 35 deletions

View File

@ -102,9 +102,6 @@ public class TreeViewer extends JComponent {
protected int nodeHeightPadding = 0; // added above/below
protected int arcSize = 0; // make an arc in node outline?
protected int width = 800;
protected int height = 600;
protected double scale = 1.0;
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,
gapBetweenNodes),
useIdentity);
Dimension size = treeLayout.getBounds().getBounds().getSize();
setPreferredSize(size);
updatePreferredSize();
setFont(font);
}
private void updatePreferredSize() {
setPreferredSize(getScaledTreeSize());
invalidate();
if (getParent() != null) {
getParent().validate();
}
repaint();
}
// ---------------- PAINT -----------------------------------------------
protected void paintEdges(Graphics g, Tree parent) {
@ -237,8 +242,7 @@ public class TreeViewer extends JComponent {
// Wrap viewer in scroll pane
JScrollPane scrollPane = new JScrollPane(viewer);
// Make it tree size up to width/height of viewer
viewer.setPreferredSizeToScaledTree();
// Make the scrollpane (containing the viewer) the center component
contentPane.add(scrollPane, BorderLayout.CENTER);
// Add button to bottom
@ -259,8 +263,9 @@ public class TreeViewer extends JComponent {
bottomPanel.add(wrapper, BorderLayout.SOUTH);
// Add scale slider
int sliderValue = (int) ((viewer.getScale()-1.0) * 1000);
final JSlider scaleSlider = new JSlider(JSlider.HORIZONTAL,
-1000,1000,0);
-999,1000,sliderValue);
scaleSlider.addChangeListener(
new ChangeListener() {
@Override
@ -278,18 +283,12 @@ public class TreeViewer extends JComponent {
dialog.setVisible(true);
}
protected void setPreferredSizeToScaledTree() {
private Dimension getScaledTreeSize() {
Dimension scaledTreeSize =
treeLayout.getBounds().getBounds().getSize();
scaledTreeSize = new Dimension((int)(scaledTreeSize.width*scale),
(int)(scaledTreeSize.height*scale));
if ( scaledTreeSize.width < width ) {
setWidth(scaledTreeSize.width);
}
if ( scaledTreeSize.height < height ) {
setHeight(scaledTreeSize.height);
}
setPreferredSize(new Dimension(width, height));
return scaledTreeSize;
}
public void open() {
@ -435,25 +434,10 @@ public class TreeViewer extends JComponent {
}
public void setScale(double scale) {
if(scale <= 0) {
scale = 1;
}
this.scale = scale;
repaint();
}
@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;
updatePreferredSize();
}
}