Java Code Examples for javax.swing.tree.TreePath#getLastPathComponent()

The following examples show how to use javax.swing.tree.TreePath#getLastPathComponent() . 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: TreeExpandCollapse.java    From consulo with Apache License 2.0 6 votes vote down vote up
public int expand(JTree tree, TreePath path) {
  if (myLevelsLeft == 0) return myExpansionLimit;
  TreeModel model = tree.getModel();
  Object node = path.getLastPathComponent();
  int levelDecrement = 0;
  if (!tree.isExpanded(path) && !model.isLeaf(node)) {
    tree.expandPath(path);
    levelDecrement = 1;
    myExpansionLimit--;
  }
  for (int i = 0; i < model.getChildCount(node); i++) {
    Object child = model.getChild(node, i);
    if (model.isLeaf(child)) continue;
    ExpandContext childContext = new ExpandContext(myExpansionLimit, myLevelsLeft - levelDecrement);
    myExpansionLimit = childContext.expand(tree, path.pathByAddingChild(child));
    if (myExpansionLimit <= 0) return 0;
  }
  return myExpansionLimit;
}
 
Example 2
Source File: ElementTreePanel.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Called whenever the value of the selection changes.
 * @param e the event that characterizes the change.
 */
public void valueChanged(TreeSelectionEvent e) {

    if (!updatingSelection && tree.getSelectionCount() == 1) {
        TreePath selPath = tree.getSelectionPath();
        Object lastPathComponent = selPath.getLastPathComponent();

        if (!(lastPathComponent instanceof DefaultMutableTreeNode)) {
            Element selElement = (Element) lastPathComponent;

            updatingSelection = true;
            try {
                getEditor().select(selElement.getStartOffset(),
                        selElement.getEndOffset());
            } finally {
                updatingSelection = false;
            }
        }
    }
}
 
Example 3
Source File: ScopeTreeViewPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nullable
private Module[] getSelectedModules() {
  final TreePath[] treePaths = myTree.getSelectionPaths();
  if (treePaths != null) {
    Set<Module> result = new HashSet<Module>();
    for (TreePath path : treePaths) {
      PackageDependenciesNode node = (PackageDependenciesNode)path.getLastPathComponent();
      if (node instanceof ModuleNode) {
        result.add(((ModuleNode)node).getModule());
      }
      else if (node instanceof ModuleGroupNode) {
        final ModuleGroupNode groupNode = (ModuleGroupNode)node;
        final ModuleGroup moduleGroup = groupNode.getModuleGroup();
        result.addAll(moduleGroup.modulesInGroup(myProject, true));
      }
    }
    return result.isEmpty() ? null : result.toArray(new Module[result.size()]);
  }
  return null;
}
 
Example 4
Source File: NewOperatorGroupTree.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void valueChanged(final String value) {
	TreePath[] selectionPaths = operatorGroupTree.getSelectionPaths();

	List<TreePath> expandedPaths = model.applyFilter(value);

	for (TreePath expandedPath : expandedPaths) {
		int row = this.operatorGroupTree.getRowForPath(expandedPath);
		this.operatorGroupTree.expandRow(row);
	}

	GroupTree root = (GroupTree) this.operatorGroupTree.getModel().getRoot();
	TreePath path = new TreePath(root);
	showNodes(root, path, expandedPaths);
	if (selectionPaths != null) {
		for (TreePath selectionPath : selectionPaths) {
			Object lastPathComponent = selectionPath.getLastPathComponent();
			if (model.contains(lastPathComponent)) {
				operatorGroupTree.addSelectionPath(selectionPath);
			}
		}
	}
}
 
Example 5
Source File: ProjectTreeMouseHandler.java    From mzmine2 with GNU General Public License v2.0 6 votes vote down vote up
private void handlePopupTriggerEvent(MouseEvent e) {
  TreePath clickedPath = tree.getPathForLocation(e.getX(), e.getY());
  if (clickedPath == null)
    return;
  DefaultMutableTreeNode node = (DefaultMutableTreeNode) clickedPath.getLastPathComponent();
  Object clickedObject = node.getUserObject();

  if (clickedObject instanceof RawDataFile)
    dataFilePopupMenu.show(e.getComponent(), e.getX(), e.getY());
  if (clickedObject instanceof Scan)
    scanPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  if (clickedObject instanceof MassList)
    massListPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  if (clickedObject instanceof PeakList)
    peakListPopupMenu.show(e.getComponent(), e.getX(), e.getY());
  if (clickedObject instanceof PeakListRow)
    peakListRowPopupMenu.show(e.getComponent(), e.getX(), e.getY());
}
 
Example 6
Source File: ProfilerTreeTable.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void collapseChildren(TreePath tpath) {
        if (tree != null) try {
            markExpansionTransaction();
            
            if (tpath == null || tree.isCollapsed(tpath)) return;
            
            TreeModel tmodel = tree.getModel();
            Object selected = tpath.getLastPathComponent();
            
            int nchildren = tmodel.getChildCount(selected);
            for (int i = 0; i < nchildren; i++) {
                TreePath tp = tpath.pathByAddingChild(tmodel.getChild(selected, i));
                tree.collapsePath(tp);
//                tree.resetExpandedState(tp);
            }
        
        } finally {
            clearExpansionTransaction();
        }
    }
 
Example 7
Source File: CollapseAllArchivesAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionContext context) {
	// This actions does double duty.  When invoked from the icon, it closes all nodes.
	// When invoked from the popup, it only closes selected nodes.

	if (!(context instanceof DataTypesActionContext)) {
		collapseAll(plugin.getProvider().getGTree()); // on the toolbar or filter field--collapse all
	}

	DataTypesActionContext dataTypeContext = (DataTypesActionContext) context;
	if (dataTypeContext.isToolbarAction()) {
		collapseAll(plugin.getProvider().getGTree()); // on the toolbar or filter field--collapse all
	}
	else {
		DataTypeArchiveGTree gtree = (DataTypeArchiveGTree) context.getContextObject();
		TreePath[] selectionPaths = gtree.getSelectionPaths();
		if (selectionPaths == null || selectionPaths.length != 1) {
			// no paths selected; close all paths
			collapseAll(plugin.getProvider().getGTree());
		}

		if (selectionPaths != null) {
			for (TreePath path : selectionPaths) {
				GTreeNode node = (GTreeNode) path.getLastPathComponent();
				if (node instanceof ArchiveRootNode) { // if the root is selected collapseAll
					collapseAll(gtree);
					return;
				}
				gtree.collapseAll(node);
			}
		}
	}
}
 
Example 8
Source File: ExtractMethodPanel.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
/**
 * Opens the definition of appropriate method for the selected suggestion by double-clicking or Enter key pressing.
 */
private void openMethodDefinition(InputEvent e) {
    TreeTableTree treeTableTree = treeTable.getTree();
    TreePath selectedPath = treeTableTree.getSelectionModel().getSelectionPath();
    if (selectedPath != null) {
        Object o = selectedPath.getLastPathComponent();
        if (o instanceof ASTSlice) {
            openDefinition(((ASTSlice) o).getSourceMethodDeclaration(), scope, (ASTSlice) o);
        } else if (o instanceof ExtractMethodCandidateGroup) {
            expandOrCollapsePath(e, treeTableTree, selectedPath);
        }
    }
}
 
Example 9
Source File: DefaultTreeCheckingModel.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
/**
        * Unchecks the subtree with root path.
        * 
        * @param path root of the tree to be unchecked
        */
   public void uncheckSubTree(TreePath path) {
removeFromCheckedPathsSet(path);
removeFromGreyedPathsSet(path);
Object node = path.getLastPathComponent();
int childrenNumber = this.model.getChildCount(node);
for (int childIndex = 0; childIndex < childrenNumber; childIndex++) {
    TreePath childPath = path.pathByAddingChild(this.model.getChild(node, childIndex));
    uncheckSubTree(childPath);
}
   }
 
Example 10
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
@Override public boolean isCellEditable(EventObject e) {
  // return e instanceof MouseEvent;
  Object source = e.getSource();
  if (!(source instanceof JTree) || !(e instanceof MouseEvent)) {
    return false;
  }
  JTree tree = (JTree) source;
  Point p = ((MouseEvent) e).getPoint();
  TreePath path = tree.getPathForLocation(p.x, p.y);
  if (Objects.isNull(path)) {
    return false;
  }
  Rectangle r = tree.getPathBounds(path);
  if (Objects.isNull(r)) {
    return false;
  }
  // r.width = panel.getButtonAreaWidth();
  // return r.contains(p);
  if (r.contains(p)) {
    TreeNode node = (TreeNode) path.getLastPathComponent();
    int row = tree.getRowForLocation(p.x, p.y);
    TreeCellRenderer renderer = tree.getCellRenderer();
    Component c = renderer.getTreeCellRendererComponent(tree, " ", true, true, node.isLeaf(), row, true);
    c.setBounds(r);
    c.setLocation(0, 0);
    // tree.doLayout();
    tree.revalidate();
    p.translate(-r.x, -r.y);
    return SwingUtilities.getDeepestComponentAt(c, p.x, p.y) instanceof JButton;
  }
  return false;
}
 
Example 11
Source File: MainFrameTree.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SwingThread
boolean leavesShown() {
    JTree jTree = getTree();

    int rows = jTree.getRowCount();
    for (int i = 0; i < rows; i++) {
        TreePath treePath = jTree.getPathForRow(i);
        Object lastPathComponent = treePath.getLastPathComponent();
        if (lastPathComponent instanceof BugLeafNode) {
            return true;
        }
    }
    return false;
}
 
Example 12
Source File: BaseStructureConfigurable.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean isEnabled() {
  final TreePath selectionPath = myTree.getSelectionPath();
  if (selectionPath != null) {
    final MyNode node = (MyNode)selectionPath.getLastPathComponent();
    return !node.isDisplayInBold();
  }
  else {
    return false;
  }
}
 
Example 13
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public static void searchTree(JTree tree, TreePath path, String q, List<TreePath> rollOverPathLists) {
  Object o = path.getLastPathComponent();
  if (o instanceof TreeNode) {
    TreeNode node = (TreeNode) o;
    if (node.toString().startsWith(q)) {
      rollOverPathLists.add(path);
      tree.expandPath(path.getParentPath());
    }
    if (!node.isLeaf()) {
      // Java 9: Collections.list(node.children())
      Collections.list((Enumeration<?>) node.children())
          .forEach(n -> searchTree(tree, path.pathByAddingChild(n), q, rollOverPathLists));
    }
  }
}
 
Example 14
Source File: JavaMixer.java    From Spark with Apache License 2.0 5 votes vote down vote up
public Component getPrefferedInputVolume() {
    TreePath path = findByName(new TreePath(root), new String[]{"MICROPHONE", "Volume"});

    if (path == null) {
        path = findByName(new TreePath(root), new String[]{"Capture source", "Capture", "Volume"});
    }

    if (path != null) {
        if (path.getLastPathComponent() instanceof JavaMixer.ControlNode)
            return ((JavaMixer.ControlNode) path.getLastPathComponent()).getComponent();
    }
    return null;
}
 
Example 15
Source File: AbstractRefactoringPanel.java    From IntelliJDeodorant with MIT License 5 votes vote down vote up
/**
 * Preforms selected refactoring.
 */
private void refactorSelected() {
    TreePath selectedPath = treeTable.getTree().getSelectionPath();
    if (selectedPath != null && selectedPath.getPathCount() == refactorDepth) {
        AbstractCandidateRefactoring computationSlice = (AbstractCandidateRefactoring) selectedPath.getLastPathComponent();
        removeSelection();
        doRefactor(computationSlice);
    }
}
 
Example 16
Source File: XTree.java    From scelight with Apache License 2.0 5 votes vote down vote up
@Override
public void collapsePathRecursive( final TreePath path ) {
	final TreeNode node = (TreeNode) path.getLastPathComponent();
	
	// Going downward is faster because collapsing needs to update row indices of rows that follow
	// and this way there are less rows to follow when collapsing a node
	for ( int i = node.getChildCount() - 1; i >= 0; i-- )
		collapsePathRecursive( path.pathByAddingChild( node.getChildAt( i ) ) );
	
	collapsePath( path );
}
 
Example 17
Source File: VisibleTreeState.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void setSelectionPaths(final TreePath[] selectionPaths) {
  mySelectedNodes.clear();
  if (selectionPaths != null) {
    for (TreePath selectionPath : selectionPaths) {
      final InspectionConfigTreeNode node = (InspectionConfigTreeNode)selectionPath.getLastPathComponent();
      mySelectedNodes.add(getState(node));
    }
  }
}
 
Example 18
Source File: CheckBoxTreeNodeSelectionListener.java    From SmartIM with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseClicked(MouseEvent event) {
    JTree tree = (JTree)event.getSource();
    int x = event.getX();
    int y = event.getY();
    int row = tree.getRowForLocation(x, y);
    TreePath path = tree.getPathForRow(row);
    if (path != null) {
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode)path.getLastPathComponent();
        if (selectedNode != null && selectedNode.getChildCount() > 0 && event.getClickCount() > 1) {
            // if (tree.isExpanded(path)) {
            // tree.collapsePath(path);
            // }
            // else {
            // tree.expandPath(path);
            // }
            return;
        } else if (selectedNode instanceof CheckBoxTreeNode && event.getClickCount() == 1) {
            CheckBoxTreeNode node = (CheckBoxTreeNode)selectedNode;
            if (node != null) {
                boolean isSelected = !node.isSelected();
                node.setSelected(isSelected);
                ((DefaultTreeModel)tree.getModel()).nodeStructureChanged(node);
            }
        }
    }
}
 
Example 19
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
public static void visitAll(JTree tree, TreePath parent, boolean expand) {
  TreeNode node = (TreeNode) parent.getLastPathComponent();
  if (!node.isLeaf()) {
    // Java 9: Collections.list(node.children())
    Collections.list((Enumeration<?>) node.children())
        .forEach(n -> visitAll(tree, parent.pathByAddingChild(n), expand));
  }
  if (expand) {
    tree.expandPath(parent);
  } else {
    tree.collapsePath(parent);
  }
}
 
Example 20
Source File: LaunchSaver.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Gets the selected node.
 *
 * @return the selected node
 */
public Node getSelectedNode() {
  TreePath path = tree.getSelectionPath();
  if(path==null) {
    return null;
  }
  return(Node) path.getLastPathComponent();
}