Go to the documentation of this file.00001 package instruction.gui.tab;
00002
00003 import instruction.syntaxparser.SyntaxElement;
00004 import instruction.syntaxparser.SyntaxTree;
00005 import java.util.Iterator;
00006 import java.util.List;
00007 import processing.core.PApplet;
00008 import processing.core.PFont;
00009
00010 public class SyntaxTreePanel extends PApplet {
00011
00012 private static final long serialVersionUID = 8312785333436185487L;
00013 private static int NODE_SIZE = 40;
00014 private static int COLUMN_WIDTH = 50;
00015 private static int ROW_HEIGHT = 60;
00016 private static int FONT_SIZE = 12;
00017
00018 PFont font = null;
00019
00020 List<SyntaxTree> tree = null;
00021 int leafCount = 0;
00022 int offset = 0;
00023 int maxdepth = 0;
00024
00025 int activeTreeIndex = 0;
00026
00027 int height_total = 100;
00028
00029 public void setSyntaxTree( List<SyntaxTree> tree ) {
00030
00031 this.tree = tree;
00032 }
00033
00034 @Override
00035 public void setup() {
00036
00037 size( 2500, height_total, JAVA2D );
00038 background( 255 );
00039 noLoop();
00040 font = createFont( "Arial", FONT_SIZE, true );
00041 smooth();
00042
00043 }
00044
00045 @Override
00046 public void draw() {
00047
00048 if ( tree == null )
00049 return;
00050
00051
00052 if ( tree != null ) {
00053 height_total = 0;
00054 maxdepth = 0;
00055 offset = 0;
00056 leafCount = 0;
00057
00058
00059
00060
00061 height_total += tree.get( activeTreeIndex ).getdepth();
00062 height_total *= ROW_HEIGHT;
00063 height_total += ROW_HEIGHT * tree.size();
00064 }
00065 size( 2500, height_total, JAVA2D );
00066 background( 255 );
00067
00068
00069
00070
00071
00072 drawTree(tree.get( activeTreeIndex ));
00073
00074 }
00075
00076 private void drawTree( SyntaxTree root ) {
00077
00078 drawTreeRecursive( root, 0 );
00079 offset += ( maxdepth + 1 ) * ROW_HEIGHT + ROW_HEIGHT;
00080 maxdepth = 0;
00081 leafCount = 0;
00082 }
00083
00084 private int drawTreeRecursive( SyntaxTree node, int depth ) {
00085
00086 if ( node.getChildren().isEmpty() ) {
00087 if ( depth > maxdepth )
00088 maxdepth = depth;
00089 return ( ++leafCount * COLUMN_WIDTH );
00090 }
00091 else {
00092 int pos = 0;
00093 int children = node.getChildren().size();
00094 int[] childPositions = new int[children];
00095 int counter = 0;
00096 for ( Iterator<SyntaxTree> i = node.getChildren().iterator(); i.hasNext(); ) {
00097 childPositions[counter] = drawTreeRecursive( i.next(), depth + 1 );
00098 pos += childPositions[counter];
00099 counter++;
00100 }
00101 pos = (int) Math.round( (double) pos / (double) children );
00102 for ( int i = 0; i < childPositions.length; i++ ) {
00103 line( pos, ( depth + 1 ) * ROW_HEIGHT + offset, childPositions[i], ( depth + 2 ) * ROW_HEIGHT
00104 + offset );
00105 drawNode( node.getChildren().get( i ), childPositions[i], ( depth + 2 ) * ROW_HEIGHT + offset );
00106 }
00107 drawNode( node, pos, ( depth + 1 ) * ROW_HEIGHT + offset );
00108 return pos;
00109 }
00110 }
00111
00112 private void drawNode( SyntaxTree node, int x, int y ) {
00113
00114 stroke( 0 );
00115 strokeWeight( 2.0f );
00116 fill( 232, 238, 247 );
00117 textAlign( CENTER, CENTER );
00118
00119 rectMode( CENTER );
00120 ellipse( x, y, NODE_SIZE, NODE_SIZE );
00121 fill( 0 );
00122 textFont( font );
00123
00124 SyntaxElement el = node.getElement();
00125 text( el.getType(), x, y, NODE_SIZE, NODE_SIZE );
00126 if ( el.getName() != null && ! el.getName().isEmpty() )
00127 text( el.getName(), x, y + NODE_SIZE );
00128
00129 }
00130
00131 public void setActiveTree(int index) {
00132 activeTreeIndex = index;
00133
00134 }
00135
00136 public int getActiveTree() {
00137 return activeTreeIndex;
00138 }
00139
00140 public int getTreeCount() {
00141 if (tree == null)
00142 return 0;
00143 else
00144 return tree.size();
00145 }
00146 }