Example usage for javax.swing.tree DefaultTreeModel getChildCount

List of usage examples for javax.swing.tree DefaultTreeModel getChildCount

Introduction

In this page you can find the example usage for javax.swing.tree DefaultTreeModel getChildCount.

Prototype

public int getChildCount(Object parent) 

Source Link

Document

Returns the number of children of parent.

Usage

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);/*w  w w . j a  v a  2s .c o m*/
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.USE_SELECTION);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);/*w  w w . j a  va 2 s  .c om*/
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.INSERT);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:DndTree.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel top = new JPanel(new BorderLayout());
    JLabel dragLabel = new JLabel("Drag me:");
    JTextField text = new JTextField();
    text.setDragEnabled(true);/*from w  w  w .  j ava2s  . c  om*/
    top.add(dragLabel, BorderLayout.WEST);
    top.add(text, BorderLayout.CENTER);
    f.add(top, BorderLayout.NORTH);

    final JTree tree = new JTree();
    final DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    tree.setTransferHandler(new TransferHandler() {
        public boolean canImport(TransferHandler.TransferSupport support) {
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor) || !support.isDrop()) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            return dropLocation.getPath() != null;
        }

        public boolean importData(TransferHandler.TransferSupport support) {
            if (!canImport(support)) {
                return false;
            }
            JTree.DropLocation dropLocation = (JTree.DropLocation) support.getDropLocation();
            TreePath path = dropLocation.getPath();
            Transferable transferable = support.getTransferable();

            String transferData;
            try {
                transferData = (String) transferable.getTransferData(DataFlavor.stringFlavor);
            } catch (IOException e) {
                return false;
            } catch (UnsupportedFlavorException e) {
                return false;
            }

            int childIndex = dropLocation.getChildIndex();
            if (childIndex == -1) {
                childIndex = model.getChildCount(path.getLastPathComponent());
            }

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(transferData);
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            model.insertNodeInto(newNode, parentNode, childIndex);

            TreePath newPath = path.pathByAddingChild(newNode);
            tree.makeVisible(newPath);
            tree.scrollRectToVisible(tree.getPathBounds(newPath));
            return true;
        }
    });

    JScrollPane pane = new JScrollPane(tree);
    f.add(pane, BorderLayout.CENTER);

    tree.setDropMode(DropMode.ON_OR_INSERT);
    f.setSize(300, 400);
    f.setVisible(true);
}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

private boolean analisiRicorsivaCancellaScenario(Object root, Scenario scenario, DefaultTreeModel model) {
    int indice = model.getChildCount(root);
    //        if (indice == 1 || indice == 2) {
    TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) root)
            .getUserObject();//w  ww.j  ava  2 s.  c o m
    TopComponent tc = componentAdapter.getTopComponent();
    if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
        TreeNode treeNode = (TreeNode) root;
        model.removeNodeFromParent((MutableTreeNode) treeNode.getParent());
        return false;
    }
    //        }

    for (int i = 0; i < indice; i++) {
        if (!analisiRicorsivaCancellaScenario(model.getChild(root, i), scenario, model)) {
            return false;
        }
    }
    return true;

    //        for (int i = 0; i < root.getChildCount(); i++) {
    //            TreePath treePath = jTree.getPathForRow(i);
    //            TreeNode treeNode = (TreeNode) treePath.getLastPathComponent();
    //            TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) treeNode).getUserObject();
    //            TopComponent tc = componentAdapter.getTopComponent();
    //            if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
    //                int place = model.getChildCount(treeNode.getParent());
    //                DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeTopComponentAdapter(topComponentAdded, false, false));
    //                model.insertNodeInto(newNode, (DefaultMutableTreeNode) treeNode.getParent(), place);
    //                model.reload();
    //                break;
    //            }
    //        }
}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

private void analisiRicorsivaAggiuntaTC(Object root, Scenario scenario, DefaultTreeModel model,
        TopComponent topComponentAdded) {
    int indice = model.getChildCount(root);
    for (int i = 0; i < indice; i++) {
        analisiRicorsivaAggiuntaTC(model.getChild(root, i), scenario, model, topComponentAdded);
    }//  ww  w  .  jav a2  s.c o  m
    //        if (indice == 2) {
    TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) root)
            .getUserObject();
    TopComponent tc = componentAdapter.getTopComponent();
    if (tc != null && tc == scenario.getMappingTaskTopComponent()) {
        TreeNode treeNode = (TreeNode) root;
        int place = model.getChildCount(treeNode.getParent());
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
                new TreeTopComponentAdapter(topComponentAdded, false, false, false));
        model.insertNodeInto(newNode, (DefaultMutableTreeNode) treeNode.getParent(), place);
    }
    //            return;
    //        }

}

From source file:it.unibas.spicygui.controllo.window.operator.ProjectTreeGenerator.java

private void analisiRicorsivaAggiuntaIstanza(Object root, Scenario scenario, DefaultTreeModel model,
        String stringa) {//from   ww w .  j ava2  s  .c o  m
    int indice = model.getChildCount(root);
    for (int i = 0; i < indice; i++) {
        analisiRicorsivaAggiuntaIstanza(model.getChild(root, i), scenario, model, stringa);
    }
    //        if (indice == 2) {
    TreeTopComponentAdapter componentAdapter = (TreeTopComponentAdapter) ((DefaultMutableTreeNode) root)
            .getUserObject();
    TopComponent tc = componentAdapter.getTopComponent();
    if (tc != null && tc == scenario.getMappingTaskTopComponent()) {

        TreeTopComponentAdapter ttca = new TreeTopComponentAdapter(null, false, false, true);
        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(ttca);
        TreeNode treeNode = ((TreeNode) root).getParent();
        TreeNode nodeInstancesFather = treeNode.getChildAt(2);
        //            if (stringa.equals(Costanti.SOURCE)) {
        TreeNode nodeInstacesSource = nodeInstancesFather.getChildAt(0);
        TreeNode nodeInstacesTarget = nodeInstancesFather.getChildAt(1);
        List<String> sourceInstancesName = (List<String>) scenario.getMappingTask().getSourceProxy()
                .getAnnotation(SpicyEngineConstants.XML_INSTANCE_FILE_LIST);
        List<String> targetInstancesName = (List<String>) scenario.getMappingTask().getTargetProxy()
                .getAnnotation(SpicyEngineConstants.XML_INSTANCE_FILE_LIST);
        int place = 0;
        String name = "xxx";
        if (sourceInstancesName.size() != nodeInstacesSource.getChildCount()) {
            place = nodeInstacesSource.getChildCount();
            name = findTitle(sourceInstancesName.get(sourceInstancesName.size() - 1));
            model.insertNodeInto(newNode, (DefaultMutableTreeNode) nodeInstacesSource, place);
        } else if (targetInstancesName.size() != nodeInstacesTarget.getChildCount()) {
            place = nodeInstacesTarget.getChildCount();
            name = findTitle(targetInstancesName.get(targetInstancesName.size() - 1));
            model.insertNodeInto(newNode, (DefaultMutableTreeNode) nodeInstacesTarget, place);
        }
        ttca.setName(name);
        scenario.getXQueryTopComponent().update();

        //                return;
        //            }

    }
    //            return;
    //        }

}

From source file:com.SE.myPlayer.MusicPlayerGUI.java

private void createPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createPlaylistActionPerformed
        String input = JOptionPane.showInputDialog("Enter playlist Name: ");
        if (input == null) {
        } else if (!input.equals("")) {
            int validInput = sd.newTreeNode(input);
            if (validInput == 1) {
                treeReferesh();/*from w  w w .ja  v a 2s. com*/
                DefaultTreeModel model = (DefaultTreeModel) folder_Playlist_Tree.getModel();
                DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
                DefaultMutableTreeNode playlist = (DefaultMutableTreeNode) model.getChild(root, 1);
                DefaultMutableTreeNode newPlaylist = (DefaultMutableTreeNode) model.getChild(playlist,
                        model.getChildCount(playlist) - 1);

                getSongTable(newPlaylist.toString());

                TreeNode[] nodes = model.getPathToRoot(newPlaylist);
                TreePath treepath = new TreePath(nodes);

                folder_Playlist_Tree.setExpandsSelectedPaths(true);
                folder_Playlist_Tree.setSelectionPath(treepath);
                folder_Playlist_Tree.scrollPathToVisible(treepath);

                addJMenuItemsToPopUP();

                lastOpen = input;
                for (ObjectBean list1 : list) {
                    if (list1.getTitle().equals("library")) {
                        list1.setLastOpen(lastOpen);
                    }
                }
            } else {
                createPlaylistActionPerformed(evt);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Please Enter Valid Playlist Name", "Error in Name",
                    JOptionPane.ERROR_MESSAGE);
            createPlaylistActionPerformed(evt);
        }
    }

From source file:org.codinjutsu.tools.jenkins.view.BrowserPanel.java

private void updateJobNode(Job job) {
    final DefaultTreeModel model = (DefaultTreeModel) jobTree.getModel();
    final Object modelRoot = model.getRoot();
    final int childCount = model.getChildCount(modelRoot);
    for (int i = 0; i < childCount; ++i) {
        Object child = model.getChild(modelRoot, i);
        if (child instanceof DefaultMutableTreeNode) {
            DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) child;
            if (childNode.getUserObject() == job) {
                model.nodeChanged(childNode);
                break;
            }//from  w  w w .  ja  va  2  s. c  o m
        }
    }
}

From source file:org.openengsb.ui.admin.testClient.TestClientTest.java

@Test
public void testForEachDomainVisibleInCreatePartIsAnEntryInTree_shouldWork() throws Exception {
    setupAndStartTestClientPage();/*  www.j av  a2 s  . c o m*/
    tester.assertRenderedPage(TestClient.class);
    List<String> domains = new ArrayList<String>();
    List<String> availableInTree = new ArrayList<String>();
    List<DefaultMutableTreeNode> availableInTreeAsTreeNode = new ArrayList<DefaultMutableTreeNode>();

    Component domainsComponent = tester.getComponentFromLastRenderedPage("serviceManagementContainer:domains");
    int count = ((ArrayList<?>) domainsComponent.getDefaultModelObject()).size();

    // get all domains
    for (int i = 0; i < count; i++) {
        Component label = tester
                .getComponentFromLastRenderedPage("serviceManagementContainer:domains:" + i + ":domain.name");
        domains.add(label.getDefaultModelObjectAsString());
    }

    // get all services from the tree
    DefaultTreeModel serviceListTree = (DefaultTreeModel) tester
            .getComponentFromLastRenderedPage("methodCallForm:serviceList").getDefaultModelObject();
    count = serviceListTree.getChildCount(serviceListTree.getRoot());
    for (int i = 0; i < count; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) serviceListTree
                .getChild(serviceListTree.getRoot(), i);
        String userObject = (String) child.getUserObject();
        availableInTreeAsTreeNode.add(child);
        availableInTree.add(userObject);
    }

    for (int i = 0; i < domains.size(); i++) {
        String domain = domains.get(i);
        assertThat(availableInTree.contains(domain), is(true));
        assertThat(serviceListTree.getChildCount(availableInTreeAsTreeNode.get(i)), greaterThan(0));
    }
}

From source file:ser321.media.MediaJavaClient.java

public void rebuildTree() {
    //String[] musicList = {"Come Monday","Fins","Crazy"};
    //String[] musicAlbum = {"Greatest Hits","Greatest Hits","Single"};
    //String[] videoList = {"Minions Banana Song","Minions Banana"};
    //String[] videoGenre = {"Animation","Animation"};

    cc.clear();/*from  w  w  w .  j ava  2 s . com*/

    cc.getTitles();
    cc.getVideoTitles();
    cc.getMusicTitles();

    tree.removeTreeSelectionListener(this);
    DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
    DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
    clearTree(root, model);

    DefaultMutableTreeNode musicNode = new DefaultMutableTreeNode("Music");
    model.insertNodeInto(musicNode, root, model.getChildCount(root));

    DefaultMutableTreeNode videoNode = new DefaultMutableTreeNode("Video");
    model.insertNodeInto(videoNode, root, model.getChildCount(root));

    // put nodes in the tree for all  registered with the library
    for (int i = 0; i < cc.musicTitles.size(); i++) {
        String aMTitle = cc.musicTitles.get(i);
        DefaultMutableTreeNode toAdd = new DefaultMutableTreeNode(aMTitle);
        model.insertNodeInto(toAdd, musicNode, i);
    }
    // put nodes in the tree for all video registered with the library
    for (int i = 0; i < cc.videoTitles.size(); i++) {
        String aTitle = cc.videoTitles.get(i);
        DefaultMutableTreeNode toAdd = new DefaultMutableTreeNode(aTitle);
        model.insertNodeInto(toAdd, videoNode, i);
    }
    // expand all the nodes in the JTree
    for (int r = 0; r < tree.getRowCount(); r++) {
        tree.expandRow(r);
    }
    tree.addTreeSelectionListener(this);
}