55
55
* @author Udo Borkowski (ub@abego.org)
56
56
*/
57
57
public class TreeNodePane extends JComponent implements ResizableComponent {
58
- private final TreeLayout <TreeNode > treeLayout ;
59
-
60
- private TreeForTreeLayout <TreeNode > getTree () {
61
- return treeLayout .getTree ();
58
+ private final TreeLayout <TreeNode > treeLayout ;
59
+
60
+ private TreeForTreeLayout <TreeNode > getTree () {
61
+ return treeLayout .getTree ();
62
+ }
63
+
64
+ private Iterable <TreeNode > getChildren (TreeNode parent ) {
65
+ return getTree ().getChildren (parent );
66
+ }
67
+
68
+ private Rectangle2D .Double getBoundsOfNode (TreeNode node ) {
69
+ return treeLayout .getNodeBounds ().get (node );
70
+ }
71
+
72
+ /** Specifies the tree to be displayed by passing in a {@link TreeLayout} for that tree. */
73
+ public TreeNodePane (TreeNode treeNode ) {
74
+ TreeForTreeLayout <TreeNode > layout = TreeForTreeLayoutFactory .create (treeNode );
75
+ // create the layout
76
+ double gapBetweenLevels = 50 ;
77
+ double gapBetweenNodes = 15 ;
78
+ DefaultConfiguration <TreeNode > configuration =
79
+ new DefaultConfiguration <>(gapBetweenLevels , gapBetweenNodes );
80
+
81
+ TreeLayout <TreeNode > treeLayout =
82
+ new TreeLayout <>(layout , new TreeNodeExtentProvider (), configuration );
83
+ this .treeLayout = treeLayout ;
84
+ Dimension size = treeLayout .getBounds ().getBounds ().getSize ();
85
+ setPreferredSize (size );
86
+ }
87
+
88
+ // -------------------------------------------------------------------
89
+ // painting
90
+
91
+ private static final int ARC_SIZE = 20 ;
92
+ private static final Color BOX_COLOR = Color .white ;
93
+ private static final Color BORDER_COLOR = Color .black ;
94
+
95
+ private void paintEdges (Graphics g , TreeNode parent ) {
96
+ if (!getTree ().isLeaf (parent )) {
97
+ Rectangle2D .Double b1 = getBoundsOfNode (parent );
98
+ double x1 = b1 .getCenterX ();
99
+ double y1 = b1 .getCenterY ();
100
+ for (TreeNode child : getChildren (parent )) {
101
+ Rectangle2D .Double b2 = getBoundsOfNode (child );
102
+ g .drawLine ((int ) x1 , (int ) y1 , (int ) b2 .getCenterX (), (int ) b2 .getCenterY ());
103
+
104
+ paintEdges (g , child );
105
+ }
62
106
}
63
-
64
- private Iterable <TreeNode > getChildren (TreeNode parent ) {
65
- return getTree ().getChildren (parent );
107
+ }
108
+
109
+ private void paintBox (Graphics g , TreeNode treeNode ) {
110
+ // draw the box in the background
111
+ g .setColor (BOX_COLOR );
112
+ Rectangle2D .Double box = getBoundsOfNode (treeNode );
113
+ g .fillRoundRect (
114
+ (int ) box .x , (int ) box .y , (int ) box .width - 1 , (int ) box .height - 1 , ARC_SIZE , ARC_SIZE );
115
+ g .setColor (BORDER_COLOR );
116
+ g .drawRoundRect (
117
+ (int ) box .x , (int ) box .y , (int ) box .width - 1 , (int ) box .height - 1 , ARC_SIZE , ARC_SIZE );
118
+
119
+ // Draw the box content
120
+ int x = (int ) box .x + ARC_SIZE / 2 ;
121
+ int y = (int ) box .y + ARC_SIZE / 2 ;
122
+
123
+ String text = treeNode .getValue ().toString ();
124
+ String aboveNode = null ;
125
+ if (text .contains ("%begin-above-node" )) {
126
+ String [] textParts = text .split ("%begin-above-node" );
127
+ text = textParts [0 ];
128
+ aboveNode = textParts [1 ];
66
129
}
67
-
68
- private Rectangle2D .Double getBoundsOfNode (TreeNode node ) {
69
- return treeLayout .getNodeBounds ().get (node );
130
+ TeXIcon aboveNodeIcon ;
131
+ if (aboveNode != null ) {
132
+ TeXFormula aboveNodeFormula = new TeXFormula (aboveNode );
133
+ aboveNodeIcon =
134
+ aboveNodeFormula .createTeXIcon (
135
+ TeXConstants .STYLE_DISPLAY ,
136
+ 20 ,
137
+ TeXConstants .UNIT_PIXEL ,
138
+ 80 ,
139
+ TeXConstants .ALIGN_CENTER );
140
+ aboveNodeIcon .paintIcon (this , g , x , y - ARC_SIZE / 2 - 20 );
70
141
}
71
142
72
- /**
73
- * Specifies the tree to be displayed by passing in a {@link TreeLayout} for
74
- * that tree.
75
- */
76
- public TreeNodePane (TreeNode treeNode ) {
77
-
78
- TreeForTreeLayout <TreeNode > layout = TreeForTreeLayoutFactory .create (treeNode );
79
- // create the layout
80
- double gapBetweenLevels = 50 ;
81
- double gapBetweenNodes = 15 ;
82
- DefaultConfiguration <TreeNode > configuration = new DefaultConfiguration <>(
83
- gapBetweenLevels , gapBetweenNodes );
84
-
85
-
86
- TreeLayout <TreeNode > treeLayout = new TreeLayout <>(layout ,
87
- new TreeNodeExtentProvider (), configuration );
88
- this .treeLayout = treeLayout ;
89
- Dimension size = treeLayout .getBounds ().getBounds ().getSize ();
90
- setPreferredSize (size );
91
- }
143
+ TeXFormula nodeFormula = new TeXFormula (text );
144
+ TeXIcon nodeIcon =
145
+ nodeFormula .createTeXIcon (
146
+ TeXConstants .STYLE_DISPLAY , 20 , TeXConstants .UNIT_PIXEL , 80 , TeXConstants .ALIGN_CENTER );
147
+ nodeIcon .paintIcon (this , g , x , y );
148
+ }
92
149
93
- // -------------------------------------------------------------------
94
- // painting
95
-
96
- private final static int ARC_SIZE = 20 ;
97
- private final static Color BOX_COLOR = Color .white ;
98
- private final static Color BORDER_COLOR = Color .black ;
99
-
100
- private void paintEdges (Graphics g , TreeNode parent ) {
101
- if (!getTree ().isLeaf (parent )) {
102
- Rectangle2D .Double b1 = getBoundsOfNode (parent );
103
- double x1 = b1 .getCenterX ();
104
- double y1 = b1 .getCenterY ();
105
- for (TreeNode child : getChildren (parent )) {
106
- Rectangle2D .Double b2 = getBoundsOfNode (child );
107
- g .drawLine ((int ) x1 , (int ) y1 , (int ) b2 .getCenterX (),
108
- (int ) b2 .getCenterY ());
109
-
110
- paintEdges (g , child );
111
- }
112
- }
113
- }
150
+ @ Override
151
+ public void paint (Graphics g ) {
152
+ super .paint (g );
153
+ paintEdges (g , getTree ().getRoot ());
114
154
115
- private void paintBox (Graphics g , TreeNode treeNode ) {
116
- // draw the box in the background
117
- g .setColor (BOX_COLOR );
118
- Rectangle2D .Double box = getBoundsOfNode (treeNode );
119
- g .fillRoundRect ((int ) box .x , (int ) box .y , (int ) box .width - 1 ,
120
- (int ) box .height - 1 , ARC_SIZE , ARC_SIZE );
121
- g .setColor (BORDER_COLOR );
122
- g .drawRoundRect ((int ) box .x , (int ) box .y , (int ) box .width - 1 ,
123
- (int ) box .height - 1 , ARC_SIZE , ARC_SIZE );
124
-
125
- //Draw the box content
126
- int x = (int ) box .x + ARC_SIZE / 2 ;
127
- int y = (int ) box .y + ARC_SIZE / 2 ;
128
-
129
- String text = treeNode .getValue ().toString ();
130
- String aboveNode = null ;
131
- if (text .contains ("%begin-above-node" )) {
132
- String [] textParts = text .split ("%begin-above-node" );
133
- text = textParts [0 ];
134
- aboveNode = textParts [1 ];
135
- }
136
- TeXIcon aboveNodeIcon ;
137
- if (aboveNode != null ) {
138
- TeXFormula aboveNodeFormula = new TeXFormula (aboveNode );
139
- aboveNodeIcon = aboveNodeFormula .createTeXIcon (TeXConstants .STYLE_DISPLAY , 20 , TeXConstants .UNIT_PIXEL , 80 ,
140
- TeXConstants .ALIGN_CENTER );
141
- aboveNodeIcon .paintIcon (this , g , x , y - ARC_SIZE /2 - 20 );
142
- }
143
-
144
- TeXFormula nodeFormula = new TeXFormula (text );
145
- TeXIcon nodeIcon = nodeFormula .createTeXIcon (TeXConstants .STYLE_DISPLAY , 20 , TeXConstants .UNIT_PIXEL , 80 ,
146
- TeXConstants .ALIGN_CENTER );
147
- nodeIcon .paintIcon (this , g , x , y );
155
+ for (TreeNode textInBox : treeLayout .getNodeBounds ().keySet ()) {
156
+ paintBox (g , textInBox );
148
157
}
158
+ }
149
159
150
- @ Override
151
- public void paint (Graphics g ) {
152
- super .paint (g );
153
- paintEdges (g , getTree ().getRoot ());
160
+ @ Override
161
+ public void resetScale () {}
154
162
155
- for (TreeNode textInBox : treeLayout .getNodeBounds ().keySet ()) {
156
- paintBox (g , textInBox );
157
- }
158
- }
163
+ @ Override
164
+ public void increaseScale () {}
159
165
160
- @ Override
161
- public void resetScale () {
162
-
163
- }
166
+ @ Override
167
+ public void decreaseScale () {}
164
168
165
- @ Override
166
- public void increaseScale () {
169
+ private static class TreeForTreeLayoutFactory {
167
170
171
+ public static TreeForTreeLayout <TreeNode > create (TreeNode rootNode ) {
172
+ DefaultTreeForTreeLayout <TreeNode > tree = new DefaultTreeForTreeLayout <>(rootNode );
173
+ appendChildren (tree , rootNode );
174
+ return tree ;
168
175
}
169
176
170
- @ Override
171
- public void decreaseScale () {
172
-
177
+ private static void appendChildren (DefaultTreeForTreeLayout <TreeNode > tree , TreeNode parent ) {
178
+ for (TreeNode child : (List <TreeNode >) parent .getChildren ()) {
179
+ tree .addChild (parent , child );
180
+ appendChildren (tree , child );
181
+ }
173
182
}
183
+ }
174
184
175
- private static class TreeForTreeLayoutFactory {
185
+ private class TreeNodeExtentProvider implements NodeExtentProvider < TreeNode > {
176
186
177
-
178
- public static TreeForTreeLayout <TreeNode > create (TreeNode rootNode ) {
179
- DefaultTreeForTreeLayout <TreeNode > tree = new DefaultTreeForTreeLayout <>(rootNode );
180
- appendChildren (tree , rootNode );
181
- return tree ;
182
- }
183
-
184
- private static void appendChildren (DefaultTreeForTreeLayout <TreeNode > tree , TreeNode parent ) {
185
- for (TreeNode child : (List <TreeNode >) parent .getChildren ()) {
186
- tree .addChild (parent , child );
187
- appendChildren (tree , child );
188
- }
189
- }
187
+ @ Override
188
+ public double getWidth (TreeNode treeNode ) {
189
+ String text = treeNode .getValue ().toString ();
190
+ if (text .contains ("%begin-above-node" )) {
191
+ String [] textParts = text .split ("%begin-above-node" );
192
+ text = textParts [0 ];
193
+ }
194
+
195
+ TeXFormula formula = new TeXFormula (LatexUtil .normalizeTexExpression (text ));
196
+ TeXIcon icon =
197
+ formula .createTeXIcon (
198
+ TeXConstants .STYLE_DISPLAY ,
199
+ 20 ,
200
+ TeXConstants .UNIT_PIXEL ,
201
+ 80 ,
202
+ TeXConstants .ALIGN_CENTER );
203
+
204
+ return icon .getTrueIconWidth () + ARC_SIZE ;
190
205
}
191
206
192
-
193
- private class TreeNodeExtentProvider implements NodeExtentProvider <TreeNode > {
194
-
195
- @ Override
196
- public double getWidth (TreeNode treeNode ) {
197
- String text = treeNode .getValue ().toString ();
198
- if (text .contains ("%begin-above-node" )) {
199
- String [] textParts = text .split ("%begin-above-node" );
200
- text = textParts [0 ];
201
- }
202
-
203
- TeXFormula formula = new TeXFormula (LatexUtil .normalizeTexExpression (text ));
204
- TeXIcon icon = formula .createTeXIcon (TeXConstants .STYLE_DISPLAY , 20 , TeXConstants .UNIT_PIXEL , 80 ,
205
- TeXConstants .ALIGN_CENTER );
206
-
207
- return icon .getTrueIconWidth () + ARC_SIZE ;
208
- }
209
-
210
- @ Override
211
- public double getHeight (TreeNode treeNode ) {
212
-
213
- String text = treeNode .getValue ().toString ();
214
- if (text .contains ("%begin-above-node" )) {
215
- String [] textParts = text .split ("%begin-above-node" );
216
- text = textParts [0 ];
217
- }
218
-
219
- TeXFormula formula = new TeXFormula (LatexUtil .normalizeTexExpression (text ));
220
- TeXIcon icon = formula .createTeXIcon (TeXConstants .STYLE_DISPLAY , 20 , TeXConstants .UNIT_PIXEL , 80 ,
221
- TeXConstants .ALIGN_CENTER );
222
-
223
- return icon .getTrueIconHeight () + ARC_SIZE ;
224
- }
207
+ @ Override
208
+ public double getHeight (TreeNode treeNode ) {
209
+
210
+ String text = treeNode .getValue ().toString ();
211
+ if (text .contains ("%begin-above-node" )) {
212
+ String [] textParts = text .split ("%begin-above-node" );
213
+ text = textParts [0 ];
214
+ }
215
+
216
+ TeXFormula formula = new TeXFormula (LatexUtil .normalizeTexExpression (text ));
217
+ TeXIcon icon =
218
+ formula .createTeXIcon (
219
+ TeXConstants .STYLE_DISPLAY ,
220
+ 20 ,
221
+ TeXConstants .UNIT_PIXEL ,
222
+ 80 ,
223
+ TeXConstants .ALIGN_CENTER );
224
+
225
+ return icon .getTrueIconHeight () + ARC_SIZE ;
225
226
}
226
- }
227
+ }
228
+ }
0 commit comments