Java Code Examples for javax.swing.tree.DefaultMutableTreeNode#isLeaf()

The following examples show how to use javax.swing.tree.DefaultMutableTreeNode#isLeaf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectTree.java    From KodeBeagle with Apache License 2.0 8 votes vote down vote up
public final KeyListener getKeyListener() {
    return new KeyAdapter() {
        @Override
        public void keyTyped(final KeyEvent keyEvent) {
            super.keyTyped(keyEvent);
            if (keyEvent.getKeyChar() == KeyEvent.VK_ENTER) {
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
                        windowObjects.getjTree().getLastSelectedPathComponent();
                if (selectedNode.isLeaf()) {
                    final CodeInfo codeInfo = (CodeInfo) selectedNode.getUserObject();
                    try {
                        showEditor(codeInfo);
                    } catch (Exception e) {
                        KBNotification.getInstance().error(e);
                        e.printStackTrace();
                    }
                }
            }
        }
    };
}
 
Example 2
Source File: InheritanceTree.java    From JDeodorant with MIT License 6 votes vote down vote up
public TreeMap<Integer, Set<String>> getLeavesByLevel() {
	TreeMap<Integer, Set<String>> levelMap = new TreeMap<Integer, Set<String>>();
	Enumeration<DefaultMutableTreeNode> e = rootNode.breadthFirstEnumeration();
	while(e.hasMoreElements()) {
        DefaultMutableTreeNode node = e.nextElement();
        if(node.isLeaf()) {
        	int level = node.getLevel();
        	if(levelMap.containsKey(level)) {
        		levelMap.get(level).add((String) node.getUserObject());
        	}
        	else {
        		Set<String> leaves = new LinkedHashSet<String>();
        		leaves.add((String) node.getUserObject());
        		levelMap.put(level, leaves);
        	}
        }
	}
	return levelMap;
}
 
Example 3
Source File: OptionGroupUI.java    From freecol with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This function analyses a tree selection event and calls the
 * right methods to take care of building the requested unit's
 * details.
 *
 * @param event The incoming TreeSelectionEvent.
 */
@Override
public void valueChanged(TreeSelectionEvent event) {
    detailPanel.removeAll();
    DefaultMutableTreeNode node
        = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    if (node != null) {
        if (node.isLeaf()) {
            OptionGroup group = (OptionGroup) node.getUserObject();
            for (Option option : group.getOptions()) {
                addOptionUI(option, editable && group.isEditable());
            }
        } else {
            tree.expandPath(event.getPath());
        }
    }
    detailPanel.revalidate();
    detailPanel.repaint();
}
 
Example 4
Source File: TreeIconDemo2.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if (node == null)
        return;

    Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        BookInfo book = (BookInfo) nodeInfo;
        displayURL(book.bookURL);
        if (DEBUG) {
            System.out.print(book.bookURL + ":  \n    ");
        }
    } else {
        displayURL(helpURL);
    }
    if (DEBUG) {
        System.out.println(nodeInfo.toString());
    }
}
 
Example 5
Source File: TreeIconDemo.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
/** Required by TreeSelectionListener interface. */
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if (node == null)
        return;

    Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        BookInfo book = (BookInfo) nodeInfo;
        displayURL(book.bookURL);
        if (DEBUG) {
            System.out.print(book.bookURL + ":  \n    ");
        }
    } else {
        displayURL(helpURL);
    }
    if (DEBUG) {
        System.out.println(nodeInfo.toString());
    }
}
 
Example 6
Source File: TabPanelOrganization.java    From mars-sim with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Track tree changes
 * 
 * @param e TreeSelectionEvent
 */
public void valueChanged(TreeSelectionEvent e) {
	//Returns the last path element of the selection.
	// This method is useful only when the selection model allows a single selection.
	DefaultMutableTreeNode node = (DefaultMutableTreeNode)
	                       tree.getLastSelectedPathComponent();

    if (node == null)
    //Nothing is selected.     
    return;

    Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        ;
    } else {
       ; 
    }
}
 
Example 7
Source File: ImageTreePanel.java    From moa with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if (node == null) {
        return;
    }

    Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        ImageChart chart = (ImageChart) nodeInfo;
        ImagePanel chPanel = new ImagePanel(chart.getChart());
        chPanel.setMouseWheelEnabled(true);
        chPanel.setMouseZoomable(true);
        chPanel.repaint();
        this.imgPanel.removeAll();
        this.imgPanel.add(chPanel);
        this.imgPanel.updateUI();
    }
}
 
Example 8
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public static void collapseFirstHierarchy(JTree tree) {
  TreeModel model = tree.getModel();
  DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();

  // // Java 9:
  // Collections.list(root.breadthFirstEnumeration()).stream()
  //   .filter(DefaultMutableTreeNode.class::isInstance)
  //   .map(DefaultMutableTreeNode.class::cast)
  //   .takeWhile(node -> node.getLevel() <= 1)
  //   .dropWhile(DefaultMutableTreeNode::isRoot)
  //   .dropWhile(DefaultMutableTreeNode::isLeaf)
  //   .map(DefaultMutableTreeNode::getPath)
  //   .map(TreePath::new)
  //   .forEach(tree::collapsePath);

  // Java 9: Enumeration<TreeNode> e = root.breadthFirstEnumeration();
  Enumeration<?> e = root.breadthFirstEnumeration();
  while (e.hasMoreElements()) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
    boolean isOverFirstLevel = node.getLevel() > 1;
    if (isOverFirstLevel) { // Collapse only nodes in the first hierarchy
      return;
    } else if (node.isLeaf() || node.isRoot()) {
      continue;
    }
    collapseNode(tree, node);
  }
}
 
Example 9
Source File: EditorRootPane.java    From libGDX-Path-Editor with Apache License 2.0 5 votes vote down vote up
private DefaultMutableTreeNode getSelectedNode() {
	DefaultMutableTreeNode node = (DefaultMutableTreeNode) projectTree.getSelectionPath().getLastPathComponent();
	if (node.isLeaf() && !(((NodeData) node.getUserObject()).getData() instanceof ScreenData)) {
		return (DefaultMutableTreeNode) node.getParent();
	}
	return node;
}
 
Example 10
Source File: FilterOpUI.java    From snap-desktop with GNU General Public License v3.0 5 votes vote down vote up
private static void expandAll(JTree tree) {
    DefaultMutableTreeNode actNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
    while (actNode != null) {
        if (!actNode.isLeaf()) {
            final TreePath actPath = new TreePath(actNode.getPath());
            tree.expandRow(tree.getRowForPath(actPath));
        }
        actNode = actNode.getNextNode();
    }
}
 
Example 11
Source File: ProjectTree.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public final MouseListener getMouseListener(final TreeNode root) {
    return new MouseAdapter() {
        @Override
        public void mouseClicked(final MouseEvent mouseEvent) {
            DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)
                    windowObjects.getjTree().getLastSelectedPathComponent();
            String url = "";
            if (mouseEvent.isMetaDown() && selectedNode != null
                    && selectedNode.getParent() != null) {
                final String gitUrl = getGitUrl(selectedNode, url, root);
                JPopupMenu menu = new JPopupMenu();

                if (selectedNode.isLeaf()) {
                    final CodeInfo codeInfo = (CodeInfo) selectedNode.getUserObject();

                    menu.add(new JMenuItem(addOpenInNewTabMenuItem(codeInfo))).
                            setText(OPEN_IN_NEW_TAB);
                }

                menu.add(new JMenuItem(new AbstractAction() {
                    @Override
                    public void actionPerformed(final ActionEvent actionEvent) {
                        if (!gitUrl.isEmpty()) {
                            BrowserUtil.browse(GITHUB_LINK + gitUrl);
                        }
                    }
                })).setText(RIGHT_CLICK_MENU_ITEM_TEXT);

                menu.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
            }
            doubleClickListener(mouseEvent);
        }
    };
}
 
Example 12
Source File: ExcitationEditorJPanel.java    From opensim-gui with Apache License 2.0 5 votes vote down vote up
private void removeDisplayedItems(DefaultMutableTreeNode treeNode, ArrayStr names) {
    int count = treeNode.getChildCount();
    for( int i=0;  i < count;  i++ ) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) treeNode.getChildAt(i);
        if ( child.isLeaf())
            names.remove(names.findIndex(child.toString()));
        else
            removeDisplayedItems( child, names );
    }
}
 
Example 13
Source File: CVariablesPanel.java    From binnavi with Apache License 2.0 5 votes vote down vote up
/**
 * Given a node in the {@link JTree tree} finds all instructions below the given node.
 *
 * @param node The {@link DefaultMutableTreeNode node} which is the root of the sub tree where
 *        to collect all {@link INaviInstruction instructions}.
 * @return The {@link INaviInstruction instructions} below the given
 *         {@link DefaultMutableTreeNode node}.
 */
private Set<INaviInstruction> findInstructions(final DefaultMutableTreeNode node) {
  final Set<INaviInstruction> instructions = Sets.newHashSet();
  Enumeration<?> enumeration = node.preorderEnumeration();
  while (enumeration.hasMoreElements()) {
    final DefaultMutableTreeNode currentNode =
        (DefaultMutableTreeNode) enumeration.nextElement();
    if (currentNode.isLeaf() && currentNode instanceof InstructionNode) {
      instructions.add(((InstructionNode) currentNode).getInstruction());
    }
  }
  return instructions;
}
 
Example 14
Source File: TextureBrowser.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();

    if (node == null) //Nothing is selected.	
    {
        return;
    }

    //Object nodeInfo = node.getUserObject();
    if (node.isLeaf()) {
        String selected = TreeUtil.getPath(node.getUserObjectPath());
        selected = selected.substring(0, selected.lastIndexOf("/"));
        Icon newicon = null;
        if (selected.toLowerCase().endsWith(".dds")) {
            if (ddsPreview == null) {
                ddsPreview = new DDSPreview(assetManager);
            }
            ddsPreview.requestPreview(selected, (String) node.getUserObject(), 450, 450, imagePreviewLabel, infoLabel);

        } else {
            Texture tex = assetManager.loadTexture(selected);
            newicon = ImageUtilities.image2Icon(ImageToAwt.convert(tex.getImage(), false, true, 0));
            imagePreviewLabel.setIcon(newicon);
            infoLabel.setText(" " + node.getUserObject() + "    w : " + newicon.getIconWidth() + "    h : " + newicon.getIconHeight());
        }

    } else {
        imagePreviewLabel.setIcon(null);
        infoLabel.setText("");

    }

}
 
Example 15
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public void valueChanged(TreeSelectionEvent e) {
  DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
  if (!node.isLeaf()) {
    return;
  }
  File parent = (File) node.getUserObject();
  if (!parent.isDirectory()) {
    return;
  }

  JTree tree = (JTree) e.getSource();
  DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
  new BackgroundTask(fileSystemView, parent) {
    @Override protected void process(List<File> chunks) {
      if (isCancelled()) {
        return;
      }
      if (!tree.isDisplayable()) {
        cancel(true);
        return;
      }
      chunks.stream().map(DefaultMutableTreeNode::new)
          .forEach(child -> model.insertNodeInto(child, node, node.getChildCount()));
      // model.reload(node);
    }
  }.execute();
}
 
Example 16
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public @NotNull
TreePath getLastExpandedDescendant(TreePath path) {
  while (tree.isExpanded(path)) {
    final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
    if (node.isLeaf()) {
      break;
    }
    path = path.pathByAddingChild(node.getLastChild());
  }
  return path;
}
 
Example 17
Source File: InspectorTreeUI.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public @NotNull
TreePath getLastExpandedDescendant(TreePath path) {
  while (tree.isExpanded(path)) {
    final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
    if (node.isLeaf()) {
      break;
    }
    path = path.pathByAddingChild(node.getLastChild());
  }
  return path;
}
 
Example 18
Source File: MaterialBrowser.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void valueChanged(TreeSelectionEvent e) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();

    if (node == null) {
        return;
    }

    if (node.isLeaf()) {
        String selected = TreeUtil.getPath(node.getUserObjectPath());
        selected = selected.substring(0, selected.lastIndexOf("/"));

        materialPreviewWidget1.showMaterial(assetManager, selected);

        try {
            FileReader fr = new FileReader(assetManager.getAbsoluteAssetPath(selected));

            if (fr != null) {
                char[] b = new char[5000];//preview 5000 char
                fr.read(b);
                materialTextPreview.setText(new String(b).trim());
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }

    } else {
        materialPreviewWidget1.clear();
        materialTextPreview.setText("");
    }

}
 
Example 19
Source File: ServicePanel.java    From megan-ce with GNU General Public License v3.0 5 votes vote down vote up
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
    setToolTipText(null);
    try {
        final DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getPathForRow(row).getLastPathComponent();
        if (node.isLeaf()) {
            final String fileName = leaf2file.get(node);
            if (fileName != null) {
                setToolTipText(service.getDescription(fileName));
            }
        }
    } catch (Exception ignored) {
    }
    return super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
}
 
Example 20
Source File: PlatformTestUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void printImpl(JTree tree,
                              Object root,
                              Collection<String> strings,
                              int level,
                              boolean withSelection,
                              @javax.annotation.Nullable Condition<String> nodePrintCondition) {
  DefaultMutableTreeNode defaultMutableTreeNode = (DefaultMutableTreeNode)root;

  final Object userObject = defaultMutableTreeNode.getUserObject();
  String nodeText;
  if (userObject != null) {
    nodeText = toString(userObject, null);
  }
  else {
    nodeText = "null";
  }

  if (nodePrintCondition != null && !nodePrintCondition.value(nodeText)) return;

  final StringBuilder buff = new StringBuilder();
  StringUtil.repeatSymbol(buff, ' ', level);

  final boolean expanded = tree.isExpanded(new TreePath(defaultMutableTreeNode.getPath()));
  if (!defaultMutableTreeNode.isLeaf()) {
    buff.append(expanded ? "-" : "+");
  }

  final boolean selected = tree.getSelectionModel().isPathSelected(new TreePath(defaultMutableTreeNode.getPath()));
  if (withSelection && selected) {
    buff.append("[");
  }

  buff.append(nodeText);

  if (withSelection && selected) {
    buff.append("]");
  }

  strings.add(buff.toString());

  int childCount = tree.getModel().getChildCount(root);
  if (expanded) {
    for (int i = 0; i < childCount; i++) {
      printImpl(tree, tree.getModel().getChild(root, i), strings, level + 1, withSelection, nodePrintCondition);
    }
  }
}