Example usage for javax.swing.tree TreeModel getRoot

List of usage examples for javax.swing.tree TreeModel getRoot

Introduction

In this page you can find the example usage for javax.swing.tree TreeModel getRoot.

Prototype

public Object getRoot();

Source Link

Document

Returns the root of the tree.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Swing Package Hierarchy");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Vector rootVector = new TreeVector("A", new String[] { "a" });
    JTree tree = new JTree(rootVector);
    tree.setRootVisible(true);/*from   w ww  .  j  a  v a  2  s .c  om*/
    TreeModel model = tree.getModel();
    model.valueForPathChanged(new TreePath(model.getRoot()), "javax.swing");
    ((DefaultTreeModel) model).reload();

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 300);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Traverse Tree");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTree tree = new JTree();
    tree.setRootVisible(true);//from www. java 2s.c om
    TreeModel model = tree.getModel();
    Object rootObject = model.getRoot();
    if ((rootObject != null) && (rootObject instanceof DefaultMutableTreeNode)) {
        DefaultMutableTreeNode r = (DefaultMutableTreeNode) rootObject;
        printDescendents(r);
        Enumeration breadth = r.breadthFirstEnumeration();
        Enumeration depth = r.depthFirstEnumeration();
        Enumeration preOrder = r.preorderEnumeration();
        printEnumeration(breadth, "Breadth");
        printEnumeration(depth, "Depth");
        printEnumeration(preOrder, "Pre");
    }

    TreeSelectionListener treeSelectionListener = new TreeSelectionListener() {
        public void valueChanged(TreeSelectionEvent treeSelectionEvent) {
            JTree treeSource = (JTree) treeSelectionEvent.getSource();
            TreePath path = treeSource.getSelectionPath();
            System.out.println(path);
            System.out.println(path.getPath());
            System.out.println(path.getParentPath());
            System.out.println(((DefaultMutableTreeNode) path.getLastPathComponent()).getUserObject());
            System.out.println(path.getPathCount());
        }
    };
    tree.addTreeSelectionListener(treeSelectionListener);

    JScrollPane scrollPane = new JScrollPane(tree);
    frame.add(scrollPane, BorderLayout.CENTER);
    frame.setSize(300, 400);
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Determines whether a path exists in a given tree model. For this to work,
 * the TreeModel.getIndexOfChild() method has to be correctly implemented
 *///from   w  w w  .jav  a 2s  .  c  o  m
public static boolean existsInModel(TreeModel model, TreePath path) {
    Object[] objects = path.getPath();
    if (!objects[0].equals(model.getRoot()))
        return false;
    Object prev = objects[0];
    for (int i = 1; i < objects.length; i++) {
        if (model.getIndexOfChild(prev, objects[i]) < 0)
            return false;
        prev = objects[i];
    }
    return true;
}

From source file:Main.java

public static JComponent makeUI() {
    JTree tree = new JTree();
    TreeModel model = tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    Enumeration e = root.breadthFirstEnumeration();
    while (e.hasMoreElements()) {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        Object o = node.getUserObject();
        if (o instanceof String) {
            node.setUserObject(new CheckBoxNode((String) o, false));
        }/*from  ww w  . j ava2s. co  m*/
    }
    tree.setEditable(true);
    tree.setCellRenderer(new CheckBoxNodeRenderer());
    tree.setCellEditor(new CheckBoxNodeEditor());
    tree.expandRow(0);
    return new JScrollPane(tree);
}

From source file:net.sourceforge.doddle_owl.utils.Utils.java

public static Set getAllConcept(TreeModel treeModel) {
    Set<Concept> conceptSet = new HashSet<Concept>();
    if (!(treeModel.getRoot() instanceof ConceptTreeNode)) {
        return conceptSet;
    }/*from   ww  w  .j  av a  2  s.  c o  m*/
    ConceptTreeNode rootNode = (ConceptTreeNode) treeModel.getRoot();
    conceptSet.add(rootNode.getConcept());
    getAllConcept(rootNode, conceptSet);
    return conceptSet;
}

From source file:net.sourceforge.doddle_owl.utils.Utils.java

public static double getChildCntAverage(TreeModel treeModel) {
    List<Integer> childNodeCntList = new ArrayList<Integer>();
    if (!(treeModel.getRoot() instanceof ConceptTreeNode)) {
        return 0;
    }//www  .  j a v a 2  s .c om
    ConceptTreeNode rootNode = (ConceptTreeNode) treeModel.getRoot();
    getChildCntAverage(rootNode, childNodeCntList);
    double totalChildNum = 0;
    for (int childNum : childNodeCntList) {
        totalChildNum += childNum;
    }
    if (childNodeCntList.size() == 0) {
        return 0;
    }
    return totalChildNum / childNodeCntList.size();
}

From source file:eu.apenet.dpt.standalone.gui.hgcreation.LevelTreeActions.java

public File createXML(TreeModel model, HashMap<String, String> paramMap, String countryCode,
        String globalIdentifier) {
    CLevelTreeObject obj = (CLevelTreeObject) ((DefaultMutableTreeNode) model.getRoot()).getUserObject();
    try {//from w  ww.ja  va2 s . c o m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        DOMImplementation domImplementation = builder.getDOMImplementation();
        Document doc = domImplementation.createDocument(null, null, null);

        Element root = createArchdesc(doc, model, model.getRoot(), paramMap, obj.getId(), obj.getName());

        doc.appendChild(root);

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer output = tf.newTransformer();
        output.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
        output.setOutputProperty(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
        output.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        output.transform(new DOMSource(doc.getFirstChild()),
                new StreamResult(new File(Utilities.TEMP_DIR + ".hg_creation.xml")));

        File outputFile = new File(Utilities.TEMP_DIR + "temp_HG.xml");
        File finalFile = new File(
                Utilities.TEMP_DIR + "Holdings_Guide_" + globalIdentifier + "_" + obj.getId() + ".xml");
        finalFile.deleteOnExit();

        FileUtils.writeStringToFile(outputFile, HoldingsGuideCreationUtils.eadDeclaration(obj.getName(),
                obj.getId(), countryCode, globalIdentifier, DataPreparationToolGUI.VERSION_NB), "UTF-8");
        fileUtil.writeToFile(fileUtil.readFileAsString_linebreak(Utilities.TEMP_DIR + ".hg_creation.xml"),
                Utilities.TEMP_DIR + outputFile.getName(), true);
        fileUtil.writeToFile(HoldingsGuideCreationUtils.endDeclaration(),
                Utilities.TEMP_DIR + outputFile.getName(), true);

        TransformationTool.createTransformation(fileUtil.readFileAsInputStream(outputFile), finalFile,
                Utilities.BEFORE_XSL_FILE, null, true, true, null, true, null);
        outputFile.delete();

        return finalFile;
    } catch (Exception e) {
        LOG.error("Error", e);
    }
    return null;
}

From source file:eu.apenet.dpt.standalone.gui.APETabbedPane.java

private void expandFirstLevel() {
    TreeModel model = tree.getTreeTableModel();
    TreePath root = new TreePath(model.getRoot());
    tree.expandPath(root);//from   w w  w .  jav a2 s. com

    for (int i = 0; i < tree.getRowCount(); i++) {
        if (i > 20)
            break;
        tree.expandRow(i);
    }
}

From source file:dotaSoundEditor.Controls.EditorPanel.java

protected TreeModel buildSoundListTree(TreeModel scriptTree) {
    TreeNode rootNode = (TreeNode) scriptTree.getRoot();
    int childCount = rootNode.getChildCount();
    TreeModel soundListTreeModel = new DefaultTreeModel(new DefaultMutableTreeNode("root"));
    ArrayList<String> wavePathsList = new ArrayList<>();
    for (int i = 0; i < childCount; i++) {
        String nodeValue = scriptTree.getChild(rootNode, i).toString();
        if (nodeValue.trim().startsWith("//")) {
            continue;
        }//from   w ww.  ja  va2  s. co m
        wavePathsList = getWavePathsAsList((TreeNode) scriptTree.getChild(rootNode, i));
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(nodeValue);
        for (String s : wavePathsList) {
            DefaultMutableTreeNode tempNode = new DefaultMutableTreeNode(s);
            newNode.add(tempNode);
        }
        ((DefaultMutableTreeNode) soundListTreeModel.getRoot()).add(newNode);
    }
    return soundListTreeModel;
}

From source file:dotaSoundEditor.Controls.EditorPanel.java

protected TreeNode getTreeNodeFromWavePath(String wavePath) {
    TreeModel model = this.currentTreeModel;

    TreeNode root = (TreeNode) model.getRoot();
    for (Enumeration e = ((DefaultMutableTreeNode) root).breadthFirstEnumeration(); e.hasMoreElements()
            && root != null;) {//w w w. j ava 2  s. c o m
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
        if (node.toString().contains(wavePath)) {
            return node;
        }
    }
    return null;
}