factor out setting of root so that external tools can reset the tree to get new display. Also allow setting of ruleNames list externally.

This commit is contained in:
Terence Parr 2014-01-10 18:34:10 -08:00 committed by Sam Harwell
parent 40f0540d8a
commit 1246b58b0e
1 changed files with 31 additions and 17 deletions

View File

@ -159,15 +159,10 @@ public class TreeViewer extends JComponent {
protected Color textColor = Color.black;
public TreeViewer(@Nullable List<String> ruleNames, Tree tree) {
setTreeTextProvider(new DefaultTreeTextProvider(ruleNames));
boolean useIdentity = true; // compare node identity
this.treeLayout =
new TreeLayout<Tree>(new TreeLayoutAdaptor(tree),
new TreeViewer.VariableExtentProvide(this),
new DefaultConfiguration<Tree>(gapBetweenLevels,
gapBetweenNodes),
useIdentity);
updatePreferredSize();
setRuleNames(ruleNames);
if ( tree!=null ) {
setTree(tree);
}
setFont(font);
}
@ -263,6 +258,10 @@ public class TreeViewer extends JComponent {
public void paint(Graphics g) {
super.paint(g);
if ( treeLayout==null ) {
return;
}
Graphics2D g2 = (Graphics2D)g;
// anti-alias the lines
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
@ -383,14 +382,7 @@ public class TreeViewer extends JComponent {
TreeNodeWrapper treeNode = (TreeNodeWrapper) path.getLastPathComponent();
// Set the clicked AST.
viewer.treeLayout = new TreeLayout<Tree>(
new TreeLayoutAdaptor((Tree) treeNode.getUserObject()),
new TreeViewer.VariableExtentProvide(viewer),
new DefaultConfiguration<Tree>(
viewer.gapBetweenLevels, viewer.gapBetweenNodes), true);
// Let the UI display this new AST.
viewer.updatePreferredSize();
viewer.setTree((Tree) treeNode.getUserObject());
}
});
@ -677,6 +669,24 @@ public class TreeViewer extends JComponent {
return treeLayout.getTree();
}
public void setTree(Tree root) {
if ( root!=null ) {
boolean useIdentity = true; // compare node identity
this.treeLayout =
new TreeLayout<Tree>(new TreeLayoutAdaptor(root),
new TreeViewer.VariableExtentProvide(this),
new DefaultConfiguration<Tree>(gapBetweenLevels,
gapBetweenNodes),
useIdentity);
// Let the UI display this new AST.
updatePreferredSize();
}
else {
this.treeLayout = null;
repaint();
}
}
public double getScale() {
return scale;
}
@ -689,6 +699,10 @@ public class TreeViewer extends JComponent {
updatePreferredSize();
}
public void setRuleNames(List<String> ruleNames) {
setTreeTextProvider(new DefaultTreeTextProvider(ruleNames));
}
private static class TreeNodeWrapper extends DefaultMutableTreeNode {
final TreeViewer viewer;