001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2001, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.gui.swing.tree;
018:
019: // J2SE dependencies
020: import java.util.List;
021: import java.util.ArrayList;
022: import javax.swing.tree.TreeNode;
023: import javax.swing.tree.TreePath;
024: import javax.swing.tree.TreeModel;
025: import org.w3c.dom.Node;
026:
027: // Geotools dependencies
028: import org.geotools.resources.XArray;
029: import org.geotools.resources.Arguments;
030: import org.geotools.resources.OptionalDependencies;
031:
032: /**
033: * Convenience static methods for trees operations.
034: *
035: * @since 2.0
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/widgets-swing/src/main/java/org/geotools/gui/swing/tree/Trees.java $
037: * @version $Id: Trees.java 26890 2007-09-07 11:05:40Z desruisseaux $
038: * @author Martin Desruisseaux
039: */
040: public final class Trees {
041: /**
042: * Interdit la création d'objets de cette classe.
043: */
044: private Trees() {
045: }
046:
047: /**
048: * Returns the path to the specified
049: * {@linkplain org.geotools.gui.swing.tree.TreeNode#getUserObject user object}. For each tree
050: * node which are actually instance of Geotools {@link org.geotools.gui.swing.tree.TreeNode},
051: * this method compares the specified {@code value} against the user object returned by the
052: * {@link org.geotools.gui.swing.tree.TreeNode#getUserObject} method.
053: *
054: * @param model The tree model to inspect.
055: * @param value User object to compare to
056: * {@link org.geotools.gui.swing.tree.TreeNode#getUserObject}.
057: * @return The paths to the specified value, or an empty array if none.
058: */
059: public static TreePath[] getPathsToUserObject(
060: final TreeModel model, final Object value) {
061: final List paths = new ArrayList(8);
062: final Object[] path = new Object[8];
063: path[0] = model.getRoot();
064: getPathsToUserObject(model, value, path, 1, paths);
065: return (TreePath[]) paths.toArray(new TreePath[paths.size()]);
066: }
067:
068: /**
069: * Implémentation de la recherche des chemins. Cette
070: * méthode s'appele elle-même d'une façon récursive.
071: *
072: * @param model Modèle dans lequel rechercher le chemin.
073: * @param value Objet à rechercher dans
074: * {@link org.geotools.gui.swing.tree.TreeNode#getUserObject}.
075: * @param path Chemin parcouru jusqu'à maintenant.
076: * @param length Longueur valide de {@code path}.
077: * @param list Liste dans laquelle ajouter les {@link TreePath} trouvés.
078: * @return {@code path}, ou un nouveau tableau s'il a fallu l'agrandir.
079: */
080: private static Object[] getPathsToUserObject(final TreeModel model,
081: final Object value, Object[] path, final int length,
082: final List list) {
083: final Object parent = path[length - 1];
084: if (parent instanceof org.geotools.gui.swing.tree.TreeNode) {
085: final Object nodeValue = ((org.geotools.gui.swing.tree.TreeNode) parent)
086: .getUserObject();
087: if (nodeValue == value
088: || (value != null && value.equals(nodeValue))) {
089: list.add(new TreePath(XArray.resize(path, length)));
090: }
091: }
092: final int count = model.getChildCount(parent);
093: for (int i = 0; i < count; i++) {
094: if (length >= path.length) {
095: path = XArray.resize(path, length << 1);
096: }
097: path[length] = model.getChild(parent, i);
098: path = getPathsToUserObject(model, value, path, length + 1,
099: list);
100: }
101: return path;
102: }
103:
104: /**
105: * Creates a Swing root tree node from a XML root tree node. Together with
106: * {@link #toString(TreeNode)}, this method provides a convenient way to print
107: * the content of a XML document for debugging purpose.
108: */
109: public static TreeNode xmlToSwing(final Node node) {
110: return OptionalDependencies.xmlToSwing(node);
111: }
112:
113: /**
114: * Returns a graphical representation of the specified tree model. This representation can
115: * be printed to the {@linkplain System#out standard output stream} (for example) if it uses
116: * a monospaced font and supports unicode.
117: *
118: * @param tree The tree to format.
119: * @return A string representation of the tree, or {@code null} if it doesn't contain any node.
120: */
121: public static String toString(final TreeModel tree) {
122: return OptionalDependencies.toString(tree);
123: }
124:
125: /**
126: * Returns a graphical representation of the specified tree. This representation can be
127: * printed to the {@linkplain System#out standard output stream} (for example) if it uses
128: * a monospaced font and supports unicode.
129: *
130: * @param node The root node of the tree to format.
131: * @return A string representation of the tree, or {@code null} if it doesn't contain any node.
132: */
133: public static String toString(final TreeNode node) {
134: return OptionalDependencies.toString(node);
135: }
136:
137: /**
138: * Prints the specified tree model to the {@linkplain System#out standard output stream}.
139: * This method is mostly a convenience for debugging purpose.
140: *
141: * @since 2.4
142: */
143: public static void print(final TreeModel tree) {
144: Arguments.getPrintWriter(System.out).println(toString(tree));
145: }
146:
147: /**
148: * Prints the specified tree to the {@linkplain System#out standard output stream}.
149: * This method is mostly a convenience for debugging purpose.
150: *
151: * @since 2.4
152: */
153: public static void print(final TreeNode node) {
154: Arguments.getPrintWriter(System.out).println(toString(node));
155: }
156: }
|