PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/trees/TreeFormat.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 153 lines | 76 code | 31 blank | 46 comment | 20 complexity | ffa77e4b125e28668d9dfb8690389536 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. /*
  2. * This file is part of YaBS.
  3. *
  4. * YaBS is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * YaBS is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with YaBS. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package mpv5.utils.trees;
  18. //~--- JDK imports ------------------------------------------------------------
  19. import java.awt.Color;
  20. import java.awt.Font;
  21. import java.awt.print.PrinterException;
  22. import javax.swing.JTable;
  23. import javax.swing.JTree;
  24. import javax.swing.table.DefaultTableModel;
  25. import javax.swing.tree.DefaultMutableTreeNode;
  26. import javax.swing.tree.TreePath;
  27. /**
  28. *
  29. *
  30. */
  31. public class TreeFormat {
  32. /**
  33. * Expands a given node in a JTree.
  34. *
  35. * @param tree The JTree to expand.
  36. * @param model The TreeModel for tree.
  37. * @param node The node within tree to expand.
  38. * @param row The displayed row in tree that represents
  39. * node.
  40. * @param depth The depth to which the tree should be expanded.
  41. * Zero will just expand node, a negative
  42. * value will fully expand the tree, and a positive
  43. * value will recursively expand the tree to that
  44. * depth relative to node.
  45. * @return
  46. */
  47. public static int expandJTreeNode(javax.swing.JTree tree, javax.swing.tree.TreeModel model, Object node, int row,
  48. int depth) {
  49. if ((node != null) &&!model.isLeaf(node)) {
  50. tree.expandRow(row);
  51. if (depth != 0) {
  52. for (int index = 0; (row + 1 < tree.getRowCount()) && (index < model.getChildCount(node)); index++) {
  53. row++;
  54. Object chil1 = model.getChild(node, index);
  55. if (chil1 == null) {
  56. break;
  57. }
  58. javax.swing.tree.TreePath path;
  59. while ((path = tree.getPathForRow(row)) != null && (path.getLastPathComponent() != chil1)) {
  60. row++;
  61. }
  62. if (path == null) {
  63. break;
  64. }
  65. row = expandJTreeNode(tree, model, chil1, row, depth - 1);
  66. }
  67. }
  68. }
  69. return row;
  70. } // expandJTreeNode()
  71. /**
  72. * Expand the tree
  73. * @param tree
  74. */
  75. public static void expandTree(JTree tree) {
  76. int rc = 0;
  77. do {
  78. rc = tree.getRowCount();
  79. for (int x = rc; x >= 0; x--) {
  80. tree.expandRow(x);
  81. }
  82. } while (rc != tree.getRowCount());
  83. }
  84. /**
  85. * Print the tree
  86. * @param treeWidth
  87. * @param tree
  88. */
  89. public static void print(final int treeWidth, final JTree tree) {
  90. final int MAX_HEIGHT = 20000;
  91. JTable printTree = new JTable(0, 1);
  92. printTree.setFont(Font.decode(Font.MONOSPACED));
  93. printTree.setSize(treeWidth, MAX_HEIGHT);
  94. printTree.getColumnModel().getColumn(0).setWidth(treeWidth);
  95. printTree.setGridColor(Color.WHITE);
  96. DefaultTableModel printModel = (DefaultTableModel) printTree.getModel();
  97. StringBuffer rowElement = new StringBuffer();
  98. for (int i = 0; i < tree.getRowCount(); i++) {
  99. TreePath path = tree.getPathForRow(i);
  100. int level = path.getPathCount();
  101. rowElement.delete(0, rowElement.length());
  102. for (int j = 0; j < level; j++) {
  103. rowElement.append(" ");
  104. }
  105. DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
  106. if (!node.isLeaf()) {
  107. rowElement.append(tree.isCollapsed(i)
  108. ? "+ "
  109. : "-> ");
  110. }
  111. rowElement.append(node.toString());
  112. Object[] rowData = new Object[] { rowElement.toString() };
  113. printModel.addRow(rowData);
  114. }
  115. try {
  116. printTree.print(); // since 1.5
  117. } catch (PrinterException ex) {
  118. ex.printStackTrace();
  119. }
  120. }
  121. }
  122. //~ Formatted by Jindent --- http://www.jindent.com