1187 |
isLeaf = treeModel.isLeaf(path[i]); |
isLeaf = treeModel.isLeaf(path[i]); |
1188 |
if (!isLeaf && hasControlIcons()) |
if (!isLeaf && hasControlIcons()) |
1189 |
bounds.width += getCurrentControlIcon(curr).getIconWidth(); |
bounds.width += getCurrentControlIcon(curr).getIconWidth(); |
|
|
|
1190 |
maxWidth = Math.max(maxWidth, bounds.x + bounds.width); |
maxWidth = Math.max(maxWidth, bounds.x + bounds.width); |
1191 |
} |
} |
1192 |
preferredSize = new Dimension(maxWidth, (getRowHeight() * path.length)); |
preferredSize = new Dimension(maxWidth, (getRowHeight() * path.length)); |
1346 |
installDefaults(); |
installDefaults(); |
1347 |
|
|
1348 |
installComponents(); |
installComponents(); |
1349 |
|
installKeyboardActions(); |
1350 |
|
installListeners(); |
1351 |
|
|
1352 |
setCellEditor(createDefaultCellEditor()); |
setCellEditor(createDefaultCellEditor()); |
1353 |
createdCellEditor = true; |
createdCellEditor = true; |
1354 |
isEditing = false; |
isEditing = false; |
1363 |
} |
} |
1364 |
treeSelectionModel = tree.getSelectionModel(); |
treeSelectionModel = tree.getSelectionModel(); |
1365 |
|
|
|
installKeyboardActions(); |
|
|
installListeners(); |
|
1366 |
completeUIInstall(); |
completeUIInstall(); |
1367 |
} |
} |
1368 |
|
|
1409 |
public void paint(Graphics g, JComponent c) |
public void paint(Graphics g, JComponent c) |
1410 |
{ |
{ |
1411 |
JTree tree = (JTree) c; |
JTree tree = (JTree) c; |
1412 |
|
if (currentVisiblePath == null) |
1413 |
|
updateCurrentVisiblePath(); |
1414 |
|
|
1415 |
if (treeModel != null) |
if (treeModel != null) |
1416 |
{ |
{ |
1417 |
Object root = treeModel.getRoot(); |
Object root = treeModel.getRoot(); |
1418 |
paintRecursive(g, 0, 0, 0, tree, treeModel, root); |
paintRecursive(g, 0, 0, 0, tree, treeModel, root); |
1419 |
|
|
1420 |
if (hasControlIcons()) |
if (hasControlIcons()) |
1421 |
paintControlIcons(g, 0, 0, 0, 0, tree, treeModel, root); |
paintControlIcons(g, 0, 0, 0, tree, treeModel, root); |
1422 |
} |
} |
1423 |
} |
} |
1424 |
|
|
2556 |
public void treeExpanded(TreeExpansionEvent event) |
public void treeExpanded(TreeExpansionEvent event) |
2557 |
{ |
{ |
2558 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2559 |
|
updateCurrentVisiblePath(); |
2560 |
tree.revalidate(); |
tree.revalidate(); |
2561 |
tree.repaint(); |
tree.repaint(); |
2562 |
} |
} |
2570 |
public void treeCollapsed(TreeExpansionEvent event) |
public void treeCollapsed(TreeExpansionEvent event) |
2571 |
{ |
{ |
2572 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2573 |
|
updateCurrentVisiblePath(); |
2574 |
tree.revalidate(); |
tree.revalidate(); |
2575 |
tree.repaint(); |
tree.repaint(); |
2576 |
} |
} |
2754 |
public void treeNodesChanged(TreeModelEvent e) |
public void treeNodesChanged(TreeModelEvent e) |
2755 |
{ |
{ |
2756 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2757 |
|
updateCurrentVisiblePath(); |
2758 |
tree.revalidate(); |
tree.revalidate(); |
2759 |
tree.repaint(); |
tree.repaint(); |
2760 |
} |
} |
2770 |
public void treeNodesInserted(TreeModelEvent e) |
public void treeNodesInserted(TreeModelEvent e) |
2771 |
{ |
{ |
2772 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2773 |
|
updateCurrentVisiblePath(); |
2774 |
tree.revalidate(); |
tree.revalidate(); |
2775 |
tree.repaint(); |
tree.repaint(); |
2776 |
} |
} |
2789 |
public void treeNodesRemoved(TreeModelEvent e) |
public void treeNodesRemoved(TreeModelEvent e) |
2790 |
{ |
{ |
2791 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2792 |
|
updateCurrentVisiblePath(); |
2793 |
tree.revalidate(); |
tree.revalidate(); |
2794 |
tree.repaint(); |
tree.repaint(); |
2795 |
} |
} |
2807 |
public void treeStructureChanged(TreeModelEvent e) |
public void treeStructureChanged(TreeModelEvent e) |
2808 |
{ |
{ |
2809 |
validCachedPreferredSize = false; |
validCachedPreferredSize = false; |
2810 |
|
updateCurrentVisiblePath(); |
2811 |
tree.revalidate(); |
tree.revalidate(); |
2812 |
tree.repaint(); |
tree.repaint(); |
2813 |
} |
} |
3010 |
String s = cell.toString(); |
String s = cell.toString(); |
3011 |
Font f = tree.getFont(); |
Font f = tree.getFont(); |
3012 |
FontMetrics fm = tree.getToolkit().getFontMetrics(f); |
FontMetrics fm = tree.getToolkit().getFontMetrics(f); |
3013 |
|
|
3014 |
if (s != null) |
if (s != null) |
3015 |
return new Rectangle(x, y, SwingUtilities.computeStringWidth(fm, s) + gap, |
return new Rectangle(x, y, SwingUtilities.computeStringWidth(fm, s) + gap, |
3016 |
fm.getHeight()); |
fm.getHeight()); |
3017 |
} |
} |
3018 |
return new Rectangle(x, y, 0, 0); |
return new Rectangle(x, y, 0, 0); |
3093 |
boolean isLeaf = mod.isLeaf(curr); |
boolean isLeaf = mod.isLeaf(curr); |
3094 |
Rectangle bounds = getPathBounds(tree, path); |
Rectangle bounds = getPathBounds(tree, path); |
3095 |
Object root = mod.getRoot(); |
Object root = mod.getRoot(); |
3096 |
int iconWidth = 0; |
|
|
if (!isLeaf && hasControlIcons()) |
|
|
iconWidth += getCurrentControlIcon(path).getIconWidth(); |
|
|
bounds.width += bounds.x + iconWidth + gap; |
|
|
|
|
3097 |
if (isLeaf) |
if (isLeaf) |
3098 |
{ |
{ |
3099 |
paintRow(g, clip, null, bounds, path, row, true, false, true); |
paintRow(g, clip, null, bounds, path, row, true, false, true); |
3166 |
* @return int current descent of the tree |
* @return int current descent of the tree |
3167 |
*/ |
*/ |
3168 |
int paintControlIcons(Graphics g, int indentation, int descent, |
int paintControlIcons(Graphics g, int indentation, int descent, |
3169 |
int childNumber, int depth, JTree tree, TreeModel mod, |
int depth, JTree tree, TreeModel mod, |
3170 |
Object node) |
Object node) |
3171 |
{ |
{ |
|
int h = descent; |
|
3172 |
int rowHeight = getRowHeight(); |
int rowHeight = getRowHeight(); |
3173 |
TreePath path = new TreePath(getPathToRoot(node, 0)); |
TreePath path = new TreePath(getPathToRoot(node, 0)); |
3174 |
Icon icon = getCurrentControlIcon(path); |
Icon icon = getCurrentControlIcon(path); |
3181 |
if (mod.isLeaf(node)) |
if (mod.isLeaf(node)) |
3182 |
descent += rowHeight; |
descent += rowHeight; |
3183 |
else |
else |
3184 |
{ |
{ |
|
if (depth > 0 || tree.isRootVisible()) |
|
|
descent += rowHeight; |
|
|
|
|
|
int max = 0; |
|
|
if (!mod.isLeaf(node)) |
|
|
max = mod.getChildCount(node); |
|
|
|
|
3185 |
if (!node.equals(mod.getRoot()) && |
if (!node.equals(mod.getRoot()) && |
3186 |
(tree.isRootVisible() || getLevel(node) != 1)) |
(tree.isRootVisible() || getLevel(node) != 1)) |
3187 |
icon.paintIcon(tree, g, indentation - rightChildIndent - 3, h); |
{ |
3188 |
|
int width = icon.getIconWidth(); |
3189 |
|
int height = icon.getIconHeight() + 2; |
3190 |
|
int posX = indentation - rightChildIndent; |
3191 |
|
int posY = descent; |
3192 |
|
if (width > rightChildIndent) |
3193 |
|
posX -= gap; |
3194 |
|
else posX += width/2; |
3195 |
|
|
3196 |
|
if (height < rowHeight) |
3197 |
|
posY += height/2; |
3198 |
|
|
3199 |
|
icon.paintIcon(tree, g, posX, posY); |
3200 |
|
} |
3201 |
|
|
3202 |
|
if (depth > 0 || tree.isRootVisible()) |
3203 |
|
descent += rowHeight; |
3204 |
|
|
3205 |
if (tree.isExpanded(path)) |
if (tree.isExpanded(path)) |
3206 |
{ |
{ |
3207 |
|
int max = 0; |
3208 |
|
if (!mod.isLeaf(node)) |
3209 |
|
max = mod.getChildCount(node); |
3210 |
|
|
3211 |
for (int i = 0; i < max; i++) |
for (int i = 0; i < max; i++) |
3212 |
{ |
{ |
3213 |
int indent = indentation + rightChildIndent; |
int indent = indentation + rightChildIndent; |
3214 |
if (depth == 0 && !tree.isRootVisible()) |
if (depth == 0 && !tree.isRootVisible()) |
3215 |
indent = 1; |
indent = 1; |
3216 |
|
|
3217 |
descent = paintControlIcons(g, indent, descent, i, depth + 1, |
descent = paintControlIcons(g, indent, descent, depth + 1, |
3218 |
tree, mod, mod.getChild(node, i)); |
tree, mod, mod.getChild(node, i)); |
3219 |
} |
} |
3220 |
} |
} |
3620 |
boolean isLeaf) |
boolean isLeaf) |
3621 |
{ |
{ |
3622 |
if (treeModel != null && hasControlIcons()) |
if (treeModel != null && hasControlIcons()) |
3623 |
paintControlIcons(g, 0, 0, 0, 0, tree, treeModel, path.getLastPathComponent()); |
paintControlIcons(g, 0, 0, 0, tree, treeModel, path.getLastPathComponent()); |
3624 |
} |
} |
3625 |
|
|
3626 |
/** |
/** |
3688 |
|
|
3689 |
if (tree.isVisible(path)) |
if (tree.isVisible(path)) |
3690 |
{ |
{ |
3691 |
|
// need to set exact width of entire row |
3692 |
|
int iconWidth = 0; |
3693 |
|
if (!isLeaf && hasControlIcons()) |
3694 |
|
iconWidth = getCurrentControlIcon(path).getIconWidth(); |
3695 |
|
if (isLeaf && leafIcon != null) |
3696 |
|
iconWidth += leafIcon.getIconWidth(); |
3697 |
|
else if (isExpanded && expandedIcon != null) |
3698 |
|
iconWidth += expandedIcon.getIconWidth(); |
3699 |
|
else if (collapsedIcon != null) |
3700 |
|
iconWidth += collapsedIcon.getIconWidth(); |
3701 |
|
bounds.width += bounds.x + iconWidth + gap; |
3702 |
|
|
3703 |
if (editingComponent != null && editingPath != null && isEditing(tree) |
if (editingComponent != null && editingPath != null && isEditing(tree) |
3704 |
&& node.equals(editingPath.getLastPathComponent())) |
&& node.equals(editingPath.getLastPathComponent())) |
3705 |
{ |
{ |
3711 |
TreeCellRenderer dtcr = tree.getCellRenderer(); |
TreeCellRenderer dtcr = tree.getCellRenderer(); |
3712 |
if (dtcr == null) |
if (dtcr == null) |
3713 |
dtcr = createDefaultCellRenderer(); |
dtcr = createDefaultCellRenderer(); |
3714 |
|
|
3715 |
Component c = dtcr.getTreeCellRendererComponent(tree, node, |
Component c = dtcr.getTreeCellRendererComponent(tree, node, |
3716 |
selected, isExpanded, isLeaf, row, false); |
selected, isExpanded, isLeaf, row, false); |
3717 |
bounds.x += gap; |
bounds.x += gap; |
3768 |
|
|
3769 |
while (next != null) |
while (next != null) |
3770 |
{ |
{ |
3771 |
if (current != null) |
if (current == null) |
|
current = current.pathByAddingChild(next); |
|
|
else |
|
3772 |
current = new TreePath(next); |
current = new TreePath(next); |
3773 |
|
else |
3774 |
// FIXME: Inefficent to have 2 loops when the |
current = current.pathByAddingChild(next); |
|
// tree is very large. Find a better way. |
|
3775 |
do |
do |
3776 |
next = getNextNode(next); |
next = getNextNode(next); |
3777 |
while (next != null |
while (next != null && |
3778 |
&& !tree.isVisible(new TreePath(getPathToRoot(next, 0)))); |
!tree.isVisible(new TreePath(getPathToRoot(next, 0)))); |
3779 |
} |
} |
3780 |
currentVisiblePath = current; |
currentVisiblePath = current; |
3781 |
} |
} |