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